PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 4.1.1
WP Popular Posts v4.1.1
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-rest-controller.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
389 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" . $before_widget . "\n";
67
68 // Has user set a title?
69 if ( '' != $instance['title'] ) {
70
71 $title = apply_filters( 'widget_title', $instance['title'] );
72
73 if (
74 $instance['markup']['custom_html']
75 && $instance['markup']['title-start'] != ""
76 && $instance['markup']['title-end'] != ""
77 ) {
78 echo htmlspecialchars_decode( $instance['markup']['title-start'], ENT_QUOTES) . $title . htmlspecialchars_decode($instance['markup']['title-end'], ENT_QUOTES );
79 } else {
80 echo $before_title . $title . $after_title;
81 }
82 }
83
84 // Expose Widget ID for customization
85 $instance['widget_id'] = $widget_id;
86
87 // Get posts
88 if ( $this->admin_options['tools']['ajax'] && !is_customize_preview() ) {
89
90 if ( empty( $before_widget ) || !preg_match( '/id="[^"]*"/', $before_widget ) ) {
91 ?>
92 <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>
93 <?php
94 } else {
95 ?>
96 <script type="text/javascript">
97 document.addEventListener('DOMContentLoaded', function() {
98 var wpp_widget_container = document.getElementById('<?php echo $widget_id; ?>');
99
100 if ( 'undefined' != typeof WordPressPopularPosts ) {
101 WordPressPopularPosts.get(
102 wpp_params.ajax_url + 'widget',
103 'action=wpp_get_popular&id=<?php echo $this->number; ?>',
104 function( response ){
105 wpp_widget_container.innerHTML += JSON.parse( response ).widget;
106
107 var event = null;
108
109 if ( 'function' === typeof(Event) ) {
110 event = new Event( "wpp-onload", {"bubbles": true, "cancelable": false} );
111 } /* Fallback for older browsers */
112 else {
113 if ( document.createEvent ) {
114 event = document.createEvent('Event');
115 event.initEvent( "wpp-onload", true, false );
116 }
117 }
118
119 if ( event ) {
120 wpp_widget_container.dispatchEvent( event );
121 }
122 }
123 );
124 }
125 });
126 </script>
127 <?php
128 }
129 } else {
130 $this->get_popular( $instance );
131 }
132
133 echo "\n" . $after_widget . "\n";
134
135 }
136
137 /**
138 * Generates the administration form for the widget.
139 *
140 * @since 1.0.0
141 * @param array instance The array of keys and values for the widget.
142 */
143 public function form( $instance ){
144
145 $instance = WPP_Helper::merge_array_r(
146 WPP_Settings::$defaults[ 'widget_options' ],
147 (array) $instance
148 );
149 $wpp_image = WPP_Image::get_instance();
150
151 include( plugin_dir_path( __FILE__ ) . '/widget-form.php' );
152
153 }
154
155 /**
156 * Processes the widget's options to be saved.
157 *
158 * @since 1.0.0
159 * @param array new_instance The previous instance of values before the update.
160 * @param array old_instance The new instance of values to be generated via the update.
161 * @return array instance Updated instance.
162 */
163 public function update( $new_instance, $old_instance ){
164
165 $wpp_image = WPP_Image::get_instance();
166
167 $instance = $old_instance;
168
169 $instance['title'] = htmlspecialchars( stripslashes_deep(strip_tags( $new_instance['title'] )), ENT_QUOTES );
170 $instance['limit'] = ( WPP_Helper::is_number($new_instance['limit']) && $new_instance['limit'] > 0 )
171 ? $new_instance['limit']
172 : 10;
173 $instance['range'] = $new_instance['range'];
174 $instance['time_quantity'] = ( WPP_Helper::is_number($new_instance['time_quantity']) && $new_instance['time_quantity'] > 0 )
175 ? $new_instance['time_quantity']
176 : 24;
177 $instance['time_unit'] = $new_instance['time_unit'];
178 $instance['order_by'] = $new_instance['order_by'];
179
180 // FILTERS
181 // user did not set a post type name, so we fall back to default
182 $instance['post_type'] = ( '' == $new_instance['post_type'] )
183 ? 'post,page'
184 : $new_instance['post_type'];
185
186 $instance['freshness'] = isset( $new_instance['freshness'] );
187
188 // Post / Page / CTP filter
189 $ids = array_filter( explode( ",", rtrim(preg_replace( '|[^0-9,]|', '', $new_instance['pid'] ), ",") ), 'is_numeric' );
190 // Got no valid IDs, clear
191 if ( empty( $ids ) ) {
192 $instance['pid'] = '';
193 }
194 else {
195 $instance['pid'] = implode( ",", $ids );
196 }
197
198 // Taxonomy filter
199 $instance['taxonomy'] = $new_instance['taxonomy'];
200 $instance['cat'] = ''; // Deprecated in 4.0.0!
201
202 $ids = array_filter( explode( ",", rtrim(preg_replace( '|[^0-9,-]|', '', $new_instance['term_id'] ), ",") ), 'is_numeric' );
203 // Got no valid IDs, clear
204 if ( empty( $ids ) ) {
205 $instance['term_id'] = '';
206 }
207 else {
208 $instance['term_id'] = implode( ",", $ids );
209 }
210
211 // Author filter
212 $ids = array_filter( explode( ",", rtrim(preg_replace( '|[^0-9,]|', '', $new_instance['uid'] ), ",") ), 'is_numeric' );
213 // Got no valid IDs, clear
214 if ( empty( $ids ) ) {
215 $instance['author'] = '';
216 }
217 else {
218 $instance['author'] = implode( ",", $ids );
219 }
220
221 $instance['shorten_title']['words'] = $new_instance['shorten_title-words'];
222 $instance['shorten_title']['active'] = isset( $new_instance['shorten_title-active'] );
223 $instance['shorten_title']['length'] = ( WPP_Helper::is_number($new_instance['shorten_title-length']) && $new_instance['shorten_title-length'] > 0 )
224 ? $new_instance['shorten_title-length']
225 : 25;
226
227 $instance['post-excerpt']['keep_format'] = isset( $new_instance['post-excerpt-format'] );
228 $instance['post-excerpt']['words'] = $new_instance['post-excerpt-words'];
229 $instance['post-excerpt']['active'] = isset( $new_instance['post-excerpt-active'] );
230 $instance['post-excerpt']['length'] = ( WPP_Helper::is_number($new_instance['post-excerpt-length']) && $new_instance['post-excerpt-length'] > 0 )
231 ? $new_instance['post-excerpt-length']
232 : 55;
233
234 $instance['thumbnail']['active'] = false;
235 $instance['thumbnail']['width'] = 15;
236 $instance['thumbnail']['height'] = 15;
237
238 // can create thumbnails
239 if ( $wpp_image->can_create_thumbnails() ) {
240
241 $instance['thumbnail']['active'] = isset( $new_instance['thumbnail-active'] );
242 $instance['thumbnail']['build'] = $new_instance['thumbnail-size-source'];
243
244 // Use predefined thumbnail sizes
245 if ( 'predefined' == $new_instance['thumbnail-size-source'] ) {
246
247 $default_thumbnail_sizes = $wpp_image->get_image_sizes();
248 $size = $default_thumbnail_sizes[ $new_instance['thumbnail-size'] ];
249
250 $instance['thumbnail']['width'] = $size['width'];
251 $instance['thumbnail']['height'] = $size['height'];
252 $instance['thumbnail']['crop'] = $size['crop'];
253
254 } // Set thumbnail size manually
255 else {
256 if ( WPP_Helper::is_number($new_instance['thumbnail-width']) && WPP_Helper::is_number($new_instance['thumbnail-height']) ) {
257 $instance['thumbnail']['width'] = $new_instance['thumbnail-width'];
258 $instance['thumbnail']['height'] = $new_instance['thumbnail-height'];
259 $instance['thumbnail']['crop'] = true;
260 }
261 }
262
263 }
264
265 $instance['rating'] = isset( $new_instance['rating'] );
266 $instance['stats_tag']['comment_count'] = isset( $new_instance['comment_count'] );
267 $instance['stats_tag']['views'] = isset( $new_instance['views'] );
268 $instance['stats_tag']['author'] = isset( $new_instance['author'] );
269 $instance['stats_tag']['date']['active'] = isset( $new_instance['date'] );
270 $instance['stats_tag']['date']['format'] = empty($new_instance['date_format'])
271 ? 'F j, Y'
272 : $new_instance['date_format'];
273
274 $instance['stats_tag']['taxonomy']['active'] = isset( $new_instance['stats_taxonomy'] );
275 $instance['stats_tag']['taxonomy']['name'] = isset( $new_instance['stats_taxonomy_name'] ) ? $new_instance['stats_taxonomy_name'] : 'category';
276 $instance['stats_tag']['category'] = isset( $new_instance['stats_taxonomy'] ); // Deprecated in 4.0.0!
277
278 $instance['markup']['custom_html'] = isset( $new_instance['custom_html'] );
279 $instance['markup']['wpp-start'] = empty($new_instance['wpp-start'])
280 ? htmlspecialchars( '<ul class="wpp-list">', ENT_QUOTES )
281 : htmlspecialchars( $new_instance['wpp-start'], ENT_QUOTES );
282
283 $instance['markup']['wpp-end'] = empty($new_instance['wpp-end'])
284 ? htmlspecialchars( '</ul>', ENT_QUOTES )
285 : htmlspecialchars( $new_instance['wpp-end'], ENT_QUOTES );
286
287 $instance['markup']['post-html'] = empty($new_instance['post-html'])
288 ? htmlspecialchars( '<li>{thumb} {title} {stats}</li>', ENT_QUOTES )
289 : htmlspecialchars( $new_instance['post-html'], ENT_QUOTES );
290
291 $instance['markup']['title-start'] = empty($new_instance['title-start'])
292 ? ''
293 : htmlspecialchars( $new_instance['title-start'], ENT_QUOTES );
294
295 $instance['markup']['title-end'] = empty($new_instance['title-end'])
296 ? '' :
297 htmlspecialchars( $new_instance['title-end'], ENT_QUOTES );
298
299 return $instance;
300
301 }
302
303 /**
304 * Returns HTML list.
305 *
306 * @since 2.3.3
307 */
308 public function get_popular( $instance = null ) {
309
310 if ( is_array( $instance ) && !empty( $instance ) ) {
311
312 // Return cached results
313 if ( $this->admin_options['tools']['cache']['active'] ) {
314
315 $transient_name = md5( json_encode($instance) );
316 $popular_posts = get_transient( $transient_name );
317
318 if ( false === $popular_posts ) {
319
320 $popular_posts = new WPP_Query( $instance );
321
322 switch( $this->admin_options['tools']['cache']['interval']['time'] ){
323
324 case 'minute':
325 $time = 60;
326 break;
327
328 case 'hour':
329 $time = 60 * 60;
330 break;
331
332 case 'day':
333 $time = 60 * 60 * 24;
334 break;
335
336 case 'week':
337 $time = 60 * 60 * 24 * 7;
338 break;
339
340 case 'month':
341 $time = 60 * 60 * 24 * 30;
342 break;
343
344 case 'year':
345 $time = 60 * 60 * 24 * 365;
346 break;
347
348 default:
349 $time = 60 * 60;
350 break;
351
352 }
353
354 $expiration = $time * $this->admin_options['tools']['cache']['interval']['value'];
355
356 // Store transient
357 set_transient( $transient_name, $popular_posts, $expiration );
358
359 // Store transient in WPP transients array for garbage collection
360 $wpp_transients = get_option('wpp_transients');
361
362 if ( !$wpp_transients ) {
363 $wpp_transients = array( $transient_name );
364 add_option( 'wpp_transients', $wpp_transients );
365 } else {
366 if ( !in_array($transient_name, $wpp_transients) ) {
367 $wpp_transients[] = $transient_name;
368 update_option( 'wpp_transients', $wpp_transients );
369 }
370 }
371
372 }
373
374 } // Get popular posts
375 else {
376 $popular_posts = new WPP_Query( $instance );
377 }
378
379 $output = new WPP_Output( $popular_posts->get_posts(), $instance );
380
381 echo ( $this->admin_options['tools']['cache']['active'] ? '<!-- cached -->' : '' );
382 $output->output();
383
384 }
385
386 }
387
388 } // end class WPP_Widget
389