-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast-commented-posts.php
172 lines (125 loc) · 4.22 KB
/
last-commented-posts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Plugin Name: Last Commented Posts Block
* Plugin URI: https://marc.tv/
* Description: Adds a block that lists the recent commented posts.
* Version: 2.7
* Author: Marc Tönsing
* Author URI: https://toensing.com
* Text Domain: lastcommentedposts
* Domain Path: /languages
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace RCPB;
defined('ABSPATH') || exit;
/**
* Initalise frontend and backend and register block
**/
add_action('init', __NAMESPACE__ . '\\init');
add_action('init', __NAMESPACE__ . '\\register_block');
/* Init RCPB */
function init() {
add_filter('plugin_row_meta', __NAMESPACE__ . '\\rcpb_plugin_meta', 10, 2);
}
/**
* Registers all block assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*
*/
function register_block() {
register_block_type(__DIR__ . '/build', [
'render_callback' => __NAMESPACE__ . '\\render_callback'
]);
}
/**
* Render block output
*
*/
function render_callback( $attributes, $content ) {
//add only if block is used in this post.
add_filter('render_block', __NAMESPACE__ . '\\filter_block', 10, 2);
$query_result = query_posts_with_recent_comments($attributes['max_level']);
$html = format_last_commented_list( $query_result, $attributes );
return $html;
}
function rcpb_plugin_meta( $links, $file ) {
if ( false !== strpos( $file, 'recent-comments-block' ) ) {
$links = array_merge( $links, array( '<a href="https://marc.tv/out/donate">' . __( 'Donate', 'lastcommentedposts' ) . '</a>' ) );
}
return $links;
}
function filter_block($block_content, $block) {
$className = '';
if ($block['blockName'] !== 'core/heading') {
return $block_content;
}
return $block_content;
}
function query_posts_with_recent_comments($limit)
{
global $wpdb;
$query = "select
wp_posts.*,
coalesce((
select
max(comment_date)
from
$wpdb->comments wpc
where
wpc.comment_post_id = wp_posts.id
AND comment_approved = 1
AND post_password = ''
AND comment_type NOT IN ( 'pingback' )
AND comment_type NOT IN ( 'trackback' )
),
wp_posts.post_date ) as mcomment_date
from
$wpdb->posts wp_posts
where
post_type = 'post'
and post_status = 'publish'
and comment_count > '0'
order by
mcomment_date desc limit $limit";
$query_result = $wpdb->get_results($query);
return $query_result;
}
function format_last_commented_list( $results , $attributes)
{
$alignclass = '';
if ( isset ($attributes['align']) ) {
$align = $attributes['align'];
$alignclass = 'align' . $align;
}
$icon = '– ';
$is_backend = defined('REST_REQUEST') && true === REST_REQUEST && 'edit' === filter_input(INPUT_GET, 'context');
$html = '<ol class="wp-block-last-commented-posts ' . $alignclass . ' ">';
foreach ($results as $result) {
$html .= '<li>';
$comment = get_first_approved_comment($result->ID);
//$startTime = microtime(true);
$comment_url = get_comment_link($comment);
//echo "<!-- Elapsed time is: ". (microtime(true) - $startTime) ." seconds -->";
$authorname = $comment->comment_author;
$tag = 'a';
if ($is_backend) {
$tag = 'span';
}
$comment_user = '<'.$tag.' href="' . $comment_url . '">' . get_the_title($result->ID) . '</'.$tag.'><br><span class="comment-author-link">' . $icon . $authorname . '</span>';
$html .= $comment_user;
$html .= '</li>';
}
$html .= '</ol>';
return $html;
}
function get_first_approved_comment($post_id)
{
$comments = get_comments(
array('status' => 'approve',
'post_id' => $post_id,
'number' => 1)
);
$comment = $comments[0];
return $comment;
}