-
Notifications
You must be signed in to change notification settings - Fork 6
/
widget-recent-global-posts.php
360 lines (319 loc) · 15.8 KB
/
widget-recent-global-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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/*
Plugin Name: Recent Global Posts Widget
Plugin URI: http://premium.wpmudev.org/project/recent-global-posts-widget/
Description: Show the most recent global posts in a widget
Author: WPMU DEV
Author URI: http://premium.wpmudev.org/
Version: 3.0.6
WDP ID: 66
*/
// +----------------------------------------------------------------------+
// | Copyright Incsub (http://incsub.com/) |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License, version 2, as |
// | published by the Free Software Foundation. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
// | MA 02110-1301 USA |
// +----------------------------------------------------------------------+
// define default constan value
if ( !defined( 'RECENT_GLOBAL_POSTS_WIDGET_MAIN_BLOG_ONLY' ) ) {
define( 'RECENT_GLOBAL_POSTS_WIDGET_MAIN_BLOG_ONLY', true );
}
add_action( 'widgets_init', 'rgpwidget_register_widget' );
if ( !function_exists( 'rgpwidget_register_widget' ) ) :
/**
* Registers widget in the system.
*
* @global wpdb $wpdb Current database connection instance.
*/
function rgpwidget_register_widget() {
global $wpdb;
// don't register the widget if Network_Query class is not loaded
if ( !class_exists( 'Network_Query', false ) ) {
return;
}
if ( filter_var( RECENT_GLOBAL_POSTS_WIDGET_MAIN_BLOG_ONLY, FILTER_VALIDATE_BOOLEAN ) ) {
if ( $wpdb->blogid == 1 ) {
register_widget( Recent_Global_Posts_Widget::NAME );
}
} else {
register_widget( Recent_Global_Posts_Widget::NAME );
}
}
endif;
add_action( 'plugins_loaded', 'rgpwidget_load_text_domain' );
if ( !function_exists( 'rgpwidget_load_text_domain' ) ) :
/**
* Loads text domain for Recent Global Posts Widget
*
* @since 3.0.2
*/
function rgpwidget_load_text_domain() {
load_plugin_textdomain( 'rgpwidget', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
endif;
add_filter( 'network_posts_where', 'rgpwidget_exclude_blogs', 10, 2 );
if ( !function_exists( 'rgpwidget_exclude_blogs' ) ) :
/**
* Excludes blogs from network query.
*
* @since 3.0.4
*
* @param string $where Initial WHERE clause of the network query.
* @param Network_Query $query The network query object.
* @return string Updated WHERE clause.
*/
function rgpwidget_exclude_blogs( $where, Network_Query $query ) {
return !empty( $query->query_vars['blogs_not_in'] )
? $where . sprintf( ' AND %s.BLOG_ID NOT IN (%s) ', $query->network_posts, implode( ', ', (array)$query->query_vars['blogs_not_in'] ) )
: $where;
}
endif;
/**
* Recent Global Posts Widget class.
*/
class Recent_Global_Posts_Widget extends WP_Widget {
const NAME = __CLASS__;
const DISPLAY_TITLE_CONTENT = 'title_content';
const DISPLAY_TITLE_BLOG_CONTENT = 'title_blog_content';
const DISPLAY_TITLE = 'title';
const DISPLAY_TITLE_BLOG = 'title_blog';
const DISPLAY_CONTENT = 'content';
const DISPLAY_BLOG_CONTENT = 'blog_content';
/**
* Constructor.
*
* @since 3.0.2
*
* @access public
* @param array $widget_options Array of widget options.
* @param array $control_options Array of control options.
*/
public function __construct( $widget_options = array(), $control_options = array() ) {
$widget_options = array_merge( array(
'classname' => 'rgpwidget',
'description' => __( 'Recent Global Posts', 'rgpwidget' ),
), $widget_options );
$control_options = array_merge( array(
'id_base' => 'rgpwidget',
), $control_options );
parent::__construct( 'rgpwidget', __( 'Recent Global Posts', 'rgpwidget' ), $widget_options, $control_options );
}
/**
* Renders widget content.
*
* @access public
* @global Network_Query $network_query
* @param array $args The array of widget arguments.
* @param array $instance The array of widget instance settings.
*/
public function widget( $args, $instance ) {
global $network_query;
$substr = function_exists( 'mb_substr' ) ? 'mb_substr' : 'substr';
extract( array_merge( array(
'recentglobalpostsdisplay' => '',
'recentglobalpostsnumber' => '',
'recentglobalpoststitlecharacters' => '',
'recentglobalpostscontentcharacters' => '',
'recentglobalpostsavatars' => '',
'recentglobalpostsavatarsize' => '',
'exclude_blogs' => '',
), $instance ) );
$title = !empty( $instance['recentglobalpoststitle'] ) ? $instance['recentglobalpoststitle'] : __( 'Recent Global Posts' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$recentglobalpoststype = !empty( $instance['recentglobalpoststype'] ) ? $instance['recentglobalpoststype'] : 'post';
$recentglobalpostsnumber = !empty( $instance['recentglobalpostsnumber'] ) ? absint( $instance['recentglobalpostsnumber'] ) : 10;
if ( !$recentglobalpostsnumber ) {
$recentglobalpostsnumber = 10;
}
$exclude_blogs = array_filter( array_map( 'intval', explode( ',', $exclude_blogs ) ) );
$network_query = network_query_posts( array(
'post_type' => $recentglobalpoststype,
'posts_per_page' => $recentglobalpostsnumber,
'blogs_not_in' => $exclude_blogs,
) );
echo $args['before_widget'];
echo $args['before_title'], $title, $args['after_title'];
if ( network_have_posts() ) :
echo '<ul>';
while ( network_have_posts() ) :
network_the_post();
echo '<li>';
$post = network_get_post();
$the_permalink = network_get_permalink();
$the_title = network_get_the_title();
$the_content = network_get_the_content();
if ( $recentglobalpostsavatars == 'show' ) :
echo '<a href="', $the_permalink, '">', get_avatar( network_get_the_author_id(), $recentglobalpostsavatarsize, '' ), '</a> ';
endif;
$blog = get_blog_details( $post->BLOG_ID );
$blog_title = $blog ? $blog->blogname : '';
$title = $substr( $the_title, 0, $recentglobalpoststitlecharacters );
$content = $substr( strip_tags( $the_content ), 0, $recentglobalpostscontentcharacters );
switch ( $recentglobalpostsdisplay ) {
case self::DISPLAY_BLOG_CONTENT:
echo '<a href="', $the_permalink, '">', '[', $blog_title, ']</a>';
echo '<br>';
echo $content, $recentglobalpostscontentcharacters < strlen( $the_content ) ? '…' : '';
echo '<br><a href="', $the_permalink, '">', __( 'Read More', 'rgpwidget' ), ' »</a>';
break;
case self::DISPLAY_CONTENT:
echo $content, $recentglobalpostscontentcharacters < strlen( $the_content ) ? '…' : '';
echo '<br><a href="', $the_permalink, '">', __( 'Read More', 'rgpwidget' ), ' »</a>';
break;
case self::DISPLAY_TITLE:
echo '<a href="', $the_permalink, '">', $title, '</a>';
break;
case self::DISPLAY_TITLE_BLOG:
echo '<a href="', $the_permalink, '">', $title, ' [', $blog_title, ']</a>';
break;
case self::DISPLAY_TITLE_BLOG_CONTENT:
echo '<a href="', $the_permalink, '">', $title, ' [', $blog_title, ']</a>';
echo '<br>';
echo $content;
break;
case self::DISPLAY_TITLE_CONTENT:
default:
echo '<a href="', $the_permalink, '">', $title, '</a>';
echo '<br>';
echo $content;
break;
}
echo '</li>';
endwhile;
echo '</ul>';
endif;
echo $args['after_widget'];
}
/**
* Renders widget settings form.
*
* @access public
* @param array $instance The array of current widget instance settings.
*/
public function form( $instance ) {
$post_types = $this->get_post_types();
$instance = wp_parse_args( $instance, array(
'recentglobalpoststitle' => '',
'recentglobalpostsdisplay' => '',
'recentglobalpostsnumber' => '',
'recentglobalpoststitlecharacters' => '',
'recentglobalpostscontentcharacters' => '',
'recentglobalpostsavatars' => '',
'recentglobalpostsavatarsize' => '',
'recentglobalpoststype' => 'post',
'post_type' => 'post',
'exclude_blogs' => '',
) );
$displays = array(
self::DISPLAY_TITLE_CONTENT => __( 'Title and content', 'rgpwidget' ),
self::DISPLAY_TITLE_BLOG_CONTENT => __( 'Title, blog name and content', 'rgpwidget' ),
self::DISPLAY_TITLE => __( 'Title only', 'rgpwidget' ),
self::DISPLAY_TITLE_BLOG => __( 'Title and blog name', 'rgpwidget' ),
self::DISPLAY_CONTENT => __( 'Content only', 'rgpwidget' ),
self::DISPLAY_BLOG_CONTENT => __( 'Blog name and content', 'rgpwidget' ),
);
if ( !absint( $instance['recentglobalpostsnumber'] ) ) {
$instance['recentglobalpostsnumber'] = 5;
}
if ( !absint( $instance['recentglobalpoststitlecharacters'] ) ) {
$instance['recentglobalpoststitlecharacters'] = 30;
}
if ( !absint( $instance['recentglobalpostscontentcharacters'] ) ) {
$instance['recentglobalpostscontentcharacters'] = 100;
}
?><p>
<label for="<?php echo $this->get_field_id( 'recentglobalpoststitle' ) ?>"><?php _e( 'Title', 'rgpwidget' ) ?>:</label>
<input type="text" id="<?php echo $this->get_field_id( 'recentglobalpoststitle' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpoststitle' ); ?>" value="<?php echo esc_attr( stripslashes( $instance['recentglobalpoststitle'] ) ) ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'recentglobalpostsdisplay' ) ?>"><?php _e( 'Display', 'rgpwidget' ) ?>:</label>
<select id="<?php echo $this->get_field_id( 'recentglobalpostsdisplay' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpostsdisplay' ) ?>">
<?php foreach ( $displays as $key => $label ) : ?>
<option value="<?php echo $key ?>"<?php selected( $key, $instance['recentglobalpostsdisplay'] ) ?>><?php echo $label ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'recentglobalpostsnumber' ) ?>"><?php _e( 'Number', 'rgpwidget' ) ?>:</label>
<select id="<?php echo $this->get_field_id( 'recentglobalpostsnumber' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpostsnumber' ) ?>">
<?php for ( $counter = 1; $counter <= 25; $counter++ ) : ?>
<option value="<?php echo $counter ?>"<?php selected( $counter, $instance['recentglobalpostsnumber'] ) ?>><?php echo $counter ?></option>
<?php endfor; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'recentglobalpoststitlecharacters' ) ?>"><?php _e( 'Title Characters', 'rgpwidget' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'recentglobalpoststitlecharacters' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpoststitlecharacters' ) ?>">
<?php for ( $counter = 1; $counter <= 200; $counter++ ) : ?>
<option value="<?php echo $counter ?>"<?php selected( $counter, $instance['recentglobalpoststitlecharacters'] ) ?>><?php echo $counter ?></option>
<?php endfor; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'recentglobalpostscontentcharacters' ) ?>"><?php _e( 'Content Characters', 'rgpwidget' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'recentglobalpostscontentcharacters' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpostscontentcharacters' ) ?>">
<?php for ( $counter = 1; $counter <= 500; $counter++ ) : ?>
<option value="<?php echo $counter ?>"<?php selected( $counter, $instance['recentglobalpostscontentcharacters'] ) ?>><?php echo $counter ?></option>
<?php endfor; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'recentglobalpostsavatars' ) ?>"><?php _e( 'Avatars', 'rgpwidget' ) ?>:</label>
<select id="<?php echo $this->get_field_id( 'recentglobalpostsavatars' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpostsavatars' ) ?>">
<option value="show"<?php selected( $instance['recentglobalpostsavatars'], 'show' ) ?> ><?php _e( 'Show', 'rgpwidget' ) ?></option>
<option value="hide"<?php selected( $instance['recentglobalpostsavatars'], 'hide' ) ?> ><?php _e( 'Hide', 'rgpwidget' ) ?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'recentglobalpostsavatarsize' ) ?>"><?php _e( 'Avatar Size', 'rgpwidget' ) ?>:</label>
<select id="<?php echo $this->get_field_id( 'recentglobalpostsavatarsize' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpostsavatarsize' ) ?>">
<option value="16"<?php selected( $instance['recentglobalpostsavatarsize'], '16' ) ?>>16px</option>
<option value="32"<?php selected( $instance['recentglobalpostsavatarsize'], '32' ) ?>>32px</option>
<option value="48"<?php selected( $instance['recentglobalpostsavatarsize'], '48' ) ?>>48px</option>
<option value="96"<?php selected( $instance['recentglobalpostsavatarsize'], '96' ) ?>>96px</option>
<option value="128"<?php selected( $instance['recentglobalpostsavatarsize'], '128' ) ?>>128px</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'recentglobalpoststype' ) ?>"><?php _e( 'Post Type', 'rgpwidget' ) ?>:</label>
<select id="<?php echo $this->get_field_id( 'recentglobalpoststype' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'recentglobalpoststype' ) ?>">
<?php if ( !empty( $post_types ) ) : ?>
<?php foreach ( $post_types as $r ) : ?>
<option value="<?php echo $r ?>"<?php selected( $instance['recentglobalpoststype'], $r ) ?>><?php echo esc_html( $r ) ?></option>
<?php endforeach; ?>
<?php else : ?>
<option value="post"<?php selected( $instance['recentglobalpoststype'], 'post' ) ?>><?php _e( 'post' ) ?></option>
<?php endif; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'exclude_blogs' ) ?>"><?php _e( 'Exclude Blogs', 'rgpwidget' ) ?>:</label>
<input type="text" id="<?php echo $this->get_field_id( 'exclude_blogs' ) ?>" class="widefat" name="<?php echo $this->get_field_name( 'exclude_blogs' ); ?>" value="<?php echo esc_attr( $instance['exclude_blogs'] ) ?>"><br>
<small><?php esc_html_e( 'Blog IDs, separated by commas.', 'rgpwidget' ) ?></small>
</p>
<input type="hidden" name="<?php echo $this->get_field_name( 'recentglobalpostssubmit' ) ?>" value="1"><?php
}
/**
* Returns array of available post types.
*
* @access public
* @global wpdb $wpdb The current database connection instance.
* @return array The array of available post types.
*/
public function get_post_types() {
global $wpdb;
$prefix = isset( $wpdb->base_prefix ) ? $wpdb->base_prefix : $wpdb->prefix;
return $wpdb->get_col( "SELECT DISTINCT post_type FROM {$prefix}network_posts" );
}
}