PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 4.0.12
WP Popular Posts v4.0.12
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / includes / class-wordpress-popular-posts-widget.php
wordpress-popular-posts / includes Last commit date
class-wordpress-popular-posts-activator.php 8 years ago class-wordpress-popular-posts-deactivator.php 8 years ago class-wordpress-popular-posts-helper.php 8 years ago class-wordpress-popular-posts-i18n.php 8 years ago class-wordpress-popular-posts-image.php 8 years ago class-wordpress-popular-posts-loader.php 8 years ago class-wordpress-popular-posts-output.php 8 years ago class-wordpress-popular-posts-query.php 8 years ago class-wordpress-popular-posts-settings.php 8 years ago class-wordpress-popular-posts-template.php 8 years ago class-wordpress-popular-posts-translate.php 8 years ago class-wordpress-popular-posts-widget.php 8 years ago class-wordpress-popular-posts.php 8 years ago widget-form.php 8 years ago
class-wordpress-popular-posts-widget.php
417 lines
1 <?php
2
3 class WPP_Widget extends WP_Widget {
4
5 /**
6 * Administrative settings.
7 *
8 * @since 2.3.3
9 * @var array
10 */
11 private $admin_options = array();
12
13 public function __construct(){
14
15 // Create the widget
16 parent::__construct(
17 'wpp',
18 'WordPress Popular Posts',
19 array(
20 'classname' => 'popular-posts',
21 'description' => __( 'The most Popular Posts on your blog.', 'wordpress-popular-posts' )
22 )
23 );
24
25 $this->admin_options = WPP_Settings::get( 'admin_options' );
26
27 // Widget's AJAX hook
28 if ( $this->admin_options['tools']['ajax'] ) {
29 add_action( 'wp_ajax_wpp_get_popular', array( $this, 'get_popular') );
30 add_action( 'wp_ajax_nopriv_wpp_get_popular', array( $this, 'get_popular') );
31 }
32
33 }
34
35 /**
36 * Outputs the content of the widget.
37 *
38 * @since 1.0.0
39 * @param array args The array of form elements
40 * @param array instance The current instance of the widget
41 */
42 public function widget( $args, $instance ){
43 /**
44 * @var string $name
45 * @var string $id
46 * @var string $description
47 * @var string $class
48 * @var string $before_widget
49 * @var string $after_widget
50 * @var string $before_title
51 * @var string $after_title
52 * @var string $widget_id
53 * @var string $widget_name
54 */
55 extract( $args, EXTR_SKIP );
56
57 $instance = WPP_Helper::merge_array_r(
58 WPP_Settings::$defaults[ 'widget_options' ],
59 (array) $instance
60 );
61
62 $markup = ( $instance['markup']['custom_html'] || has_filter('wpp_custom_html') || has_filter('wpp_post') )
63 ? 'custom'
64 : 'regular';
65
66 echo "\n". "<!-- WordPress Popular Posts Plugin [W] [{$instance['range']}] [{$instance['order_by']}] [{$markup}]" . ( !empty($instance['pid']) ? " [PID]" : "" ) . ( !empty($instance['cat']) ? " [CAT]" : "" ) . ( !empty($instance['author']) ? " [UID]" : "" ) . " -->" . "\n";
67
68 echo "\n" . $before_widget . "\n";
69
70 // Has user set a title?
71 if ( '' != $instance['title'] ) {
72
73 $title = apply_filters( 'widget_title', $instance['title'] );
74
75 if (
76 $instance['markup']['custom_html']
77 && $instance['markup']['title-start'] != ""
78 && $instance['markup']['title-end'] != ""
79 ) {
80 echo htmlspecialchars_decode( $instance['markup']['title-start'], ENT_QUOTES) . $title . htmlspecialchars_decode($instance['markup']['title-end'], ENT_QUOTES );
81 } else {
82 echo $before_title . $title . $after_title;
83 }
84 }
85
86 // Expose Widget ID for customization
87 $instance['widget_id'] = $widget_id;
88
89 // Get posts
90 if ( $this->admin_options['tools']['ajax'] ) {
91
92 if ( empty( $before_widget ) || !preg_match( '/id="[^"]*"/', $before_widget ) ) {
93 ?>
94 <p><?php printf( __('Error: cannot ajaxify WordPress Popular Posts on this theme. It\'s missing the <em>id</em> attribute on before_widget (see <a href="%s" target="_blank" rel="nofollow">register_sidebar</a> for more)', 'wordpress-popular-posts'), 'https://codex.wordpress.org/Function_Reference/register_sidebar' ); ?>.</p>
95 <?php
96 } else {
97 ?>
98 <script type="text/javascript">
99 /* jQuery is available, so proceed */
100 if ( window.jQuery ) {
101
102 jQuery(document).ready(function($){
103
104 var widget_container = $('#<?php echo $widget_id; ?>');
105 widget_container.append('<p class="wpp-loader"><span><?php _e( "Loading...", "wordpress-popular-posts" ); ?></span></p>');
106
107 $.get(
108 '<?php echo admin_url('admin-ajax.php'); ?>',
109 {
110 action: 'wpp_get_popular',
111 id: '<?php echo $this->number; ?>'
112 }, function( response ){
113 widget_container.children("p.wpp-loader").remove();
114 widget_container.append(response);
115 widget_container.trigger('wpp-onload');
116 }
117 );
118
119 });
120
121 } /* jQuery is not defined */
122 else {
123 if ( window.console && window.console.log ) {
124 window.console.log( 'WordPress Popular Posts: jQuery is not defined!' );
125 }
126 }
127 </script>
128 <?php
129 }
130 } else {
131 $this->get_popular( $instance );
132 }
133
134 echo "\n" . $after_widget . "\n";
135
136 }
137
138 /**
139 * Generates the administration form for the widget.
140 *
141 * @since 1.0.0
142 * @param array instance The array of keys and values for the widget.
143 */
144 public function form( $instance ){
145
146 $instance = WPP_Helper::merge_array_r(
147 WPP_Settings::$defaults[ 'widget_options' ],
148 (array) $instance
149 );
150 $wpp_image = WPP_Image::get_instance();
151
152 include( plugin_dir_path( __FILE__ ) . '/widget-form.php' );
153
154 }
155
156 /**
157 * Processes the widget's options to be saved.
158 *
159 * @since 1.0.0
160 * @param array new_instance The previous instance of values before the update.
161 * @param array old_instance The new instance of values to be generated via the update.
162 * @return array instance Updated instance.
163 */
164 public function update( $new_instance, $old_instance ){
165
166 $wpp_image = WPP_Image::get_instance();
167
168 $instance = $old_instance;
169
170 $instance['title'] = htmlspecialchars( stripslashes_deep(strip_tags( $new_instance['title'] )), ENT_QUOTES );
171 $instance['limit'] = ( WPP_Helper::is_number($new_instance['limit']) && $new_instance['limit'] > 0 )
172 ? $new_instance['limit']
173 : 10;
174 $instance['range'] = $new_instance['range'];
175 $instance['time_quantity'] = ( WPP_Helper::is_number($new_instance['time_quantity']) && $new_instance['time_quantity'] > 0 )
176 ? $new_instance['time_quantity']
177 : 24;
178 $instance['time_unit'] = $new_instance['time_unit'];
179 $instance['order_by'] = $new_instance['order_by'];
180
181 // FILTERS
182 // user did not set a post type name, so we fall back to default
183 $instance['post_type'] = ( '' == $new_instance['post_type'] )
184 ? 'post,page'
185 : $new_instance['post_type'];
186
187 $instance['freshness'] = isset( $new_instance['freshness'] );
188
189 // Post / Page / CTP filter
190 $ids = array_filter( explode( ",", rtrim(preg_replace( '|[^0-9,]|', '', $new_instance['pid'] ), ",") ), 'is_numeric' );
191 // Got no valid IDs, clear
192 if ( empty( $ids ) ) {
193 $instance['pid'] = '';
194 }
195 else {
196 $instance['pid'] = implode( ",", $ids );
197 }
198
199 // Taxonomy filter
200 $instance['taxonomy'] = $new_instance['taxonomy'];
201 $instance['cat'] = ''; // Deprecated in 4.0.0!
202
203 $ids = array_filter( explode( ",", rtrim(preg_replace( '|[^0-9,-]|', '', $new_instance['term_id'] ), ",") ), 'is_numeric' );
204 // Got no valid IDs, clear
205 if ( empty( $ids ) ) {
206 $instance['term_id'] = '';
207 }
208 else {
209 $instance['term_id'] = implode( ",", $ids );
210 }
211
212 // Author filter
213 $ids = array_filter( explode( ",", rtrim(preg_replace( '|[^0-9,]|', '', $new_instance['uid'] ), ",") ), 'is_numeric' );
214 // Got no valid IDs, clear
215 if ( empty( $ids ) ) {
216 $instance['author'] = '';
217 }
218 else {
219 $instance['author'] = implode( ",", $ids );
220 }
221
222 $instance['shorten_title']['words'] = $new_instance['shorten_title-words'];
223 $instance['shorten_title']['active'] = isset( $new_instance['shorten_title-active'] );
224 $instance['shorten_title']['length'] = ( WPP_Helper::is_number($new_instance['shorten_title-length']) && $new_instance['shorten_title-length'] > 0 )
225 ? $new_instance['shorten_title-length']
226 : 25;
227
228 $instance['post-excerpt']['keep_format'] = isset( $new_instance['post-excerpt-format'] );
229 $instance['post-excerpt']['words'] = $new_instance['post-excerpt-words'];
230 $instance['post-excerpt']['active'] = isset( $new_instance['post-excerpt-active'] );
231 $instance['post-excerpt']['length'] = ( WPP_Helper::is_number($new_instance['post-excerpt-length']) && $new_instance['post-excerpt-length'] > 0 )
232 ? $new_instance['post-excerpt-length']
233 : 55;
234
235 $instance['thumbnail']['active'] = false;
236 $instance['thumbnail']['width'] = 15;
237 $instance['thumbnail']['height'] = 15;
238
239 // can create thumbnails
240 if ( $wpp_image->can_create_thumbnails() ) {
241
242 $instance['thumbnail']['active'] = isset( $new_instance['thumbnail-active'] );
243 $instance['thumbnail']['build'] = $new_instance['thumbnail-size-source'];
244
245 // Use predefined thumbnail sizes
246 if ( 'predefined' == $new_instance['thumbnail-size-source'] ) {
247
248 $default_thumbnail_sizes = $wpp_image->get_image_sizes();
249 $size = $default_thumbnail_sizes[ $new_instance['thumbnail-size'] ];
250
251 $instance['thumbnail']['width'] = $size['width'];
252 $instance['thumbnail']['height'] = $size['height'];
253 $instance['thumbnail']['crop'] = $size['crop'];
254
255 } // Set thumbnail size manually
256 else {
257 if ( WPP_Helper::is_number($new_instance['thumbnail-width']) && WPP_Helper::is_number($new_instance['thumbnail-height']) ) {
258 $instance['thumbnail']['width'] = $new_instance['thumbnail-width'];
259 $instance['thumbnail']['height'] = $new_instance['thumbnail-height'];
260 $instance['thumbnail']['crop'] = true;
261 }
262 }
263
264 }
265
266 $instance['rating'] = isset( $new_instance['rating'] );
267 $instance['stats_tag']['comment_count'] = isset( $new_instance['comment_count'] );
268 $instance['stats_tag']['views'] = isset( $new_instance['views'] );
269 $instance['stats_tag']['author'] = isset( $new_instance['author'] );
270 $instance['stats_tag']['date']['active'] = isset( $new_instance['date'] );
271 $instance['stats_tag']['date']['format'] = empty($new_instance['date_format'])
272 ? 'F j, Y'
273 : $new_instance['date_format'];
274
275 $instance['stats_tag']['taxonomy']['active'] = isset( $new_instance['stats_taxonomy'] );
276 $instance['stats_tag']['taxonomy']['name'] = isset( $new_instance['stats_taxonomy_name'] ) ? $new_instance['stats_taxonomy_name'] : 'category';
277 $instance['stats_tag']['category'] = isset( $new_instance['stats_taxonomy'] ); // Deprecated in 4.0.0!
278
279 $instance['markup']['custom_html'] = isset( $new_instance['custom_html'] );
280 $instance['markup']['wpp-start'] = empty($new_instance['wpp-start'])
281 ? htmlspecialchars( '<ul class="wpp-list">', ENT_QUOTES )
282 : htmlspecialchars( $new_instance['wpp-start'], ENT_QUOTES );
283
284 $instance['markup']['wpp-end'] = empty($new_instance['wpp-end'])
285 ? htmlspecialchars( '</ul>', ENT_QUOTES )
286 : htmlspecialchars( $new_instance['wpp-end'], ENT_QUOTES );
287
288 $instance['markup']['post-html'] = empty($new_instance['post-html'])
289 ? htmlspecialchars( '<li>{thumb} {title} {stats}</li>', ENT_QUOTES )
290 : htmlspecialchars( $new_instance['post-html'], ENT_QUOTES );
291
292 $instance['markup']['title-start'] = empty($new_instance['title-start'])
293 ? ''
294 : htmlspecialchars( $new_instance['title-start'], ENT_QUOTES );
295
296 $instance['markup']['title-end'] = empty($new_instance['title-end'])
297 ? '' :
298 htmlspecialchars( $new_instance['title-end'], ENT_QUOTES );
299
300 return $instance;
301
302 }
303
304 /**
305 * Returns HTML list via AJAX
306 *
307 * @since 2.3.3
308 */
309 public function get_popular( $instance = null ) {
310
311 if ( defined('DOING_AJAX') && DOING_AJAX ) {
312
313 if ( isset( $_GET['id'] ) && WPP_helper::is_number( $_GET['id'] ) ) {
314
315 $id = $_GET['id'];
316 $widget_instances = $this->get_settings();
317
318 if ( isset( $widget_instances[$id] ) ) {
319 $instance = $widget_instances[$id];
320
321 if ( !isset( $instance['widget_id'] ) ) {
322 $instance['widget_id'] = $this->id;
323 }
324 }
325
326 }
327
328 }
329
330 if ( is_array( $instance ) && !empty( $instance ) ) {
331
332 // Return cached results
333 if ( $this->admin_options['tools']['cache']['active'] ) {
334
335 $transient_name = md5( json_encode($instance) );
336 $popular_posts = get_transient( $transient_name );
337
338 if ( false === $popular_posts ) {
339
340 $popular_posts = new WPP_Query( $instance );
341
342 switch( $this->admin_options['tools']['cache']['interval']['time'] ){
343
344 case 'minute':
345 $time = 60;
346 break;
347
348 case 'hour':
349 $time = 60 * 60;
350 break;
351
352 case 'day':
353 $time = 60 * 60 * 24;
354 break;
355
356 case 'week':
357 $time = 60 * 60 * 24 * 7;
358 break;
359
360 case 'month':
361 $time = 60 * 60 * 24 * 30;
362 break;
363
364 case 'year':
365 $time = 60 * 60 * 24 * 365;
366 break;
367
368 default:
369 $time = 60 * 60;
370 break;
371
372 }
373
374 $expiration = $time * $this->admin_options['tools']['cache']['interval']['value'];
375
376 // Store transient
377 set_transient( $transient_name, $popular_posts, $expiration );
378
379 // Store transient in WPP transients array for garbage collection
380 $wpp_transients = get_option('wpp_transients');
381
382 if ( !$wpp_transients ) {
383 $wpp_transients = array( $transient_name );
384 add_option( 'wpp_transients', $wpp_transients );
385 } else {
386 if ( !in_array($transient_name, $wpp_transients) ) {
387 $wpp_transients[] = $transient_name;
388 update_option( 'wpp_transients', $wpp_transients );
389 }
390 }
391
392 }
393
394 } // Get popular posts
395 else {
396 $popular_posts = new WPP_Query( $instance );
397 }
398
399 $output = new WPP_Output( $popular_posts->get_posts(), $instance );
400
401 echo ( $this->admin_options['tools']['cache']['active'] ? '<!-- cached -->' : '' );
402 $output->output();
403
404 }
405
406 if (
407 defined('DOING_AJAX')
408 && DOING_AJAX && !is_preview()
409 && !( is_singular() && isset( $_GET['fl_builder'] ) )
410 ) {
411 wp_die();
412 }
413
414 }
415
416 } // end class WPP_Widget
417