PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 6.0.1
WP Popular Posts v6.0.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 / src / Widget / Widget.php
wordpress-popular-posts / src / Widget Last commit date
Widget.php 3 years ago form.php 3 years ago
Widget.php
440 lines
1 <?php
2
3 namespace WordPressPopularPosts\Widget;
4
5 use WordPressPopularPosts\{ Helper, Image, Output, Themer, Translate };
6 use WordPressPopularPosts\Traits\QueriesPosts;
7
8 class Widget extends \WP_Widget {
9
10 use QueriesPosts;
11
12 /**
13 * Default options.
14 *
15 * @since 5.0.0
16 * @var array
17 */
18 private $defaults = [];
19
20 /**
21 * Administrative settings.
22 *
23 * @since 2.3.3
24 * @var array
25 */
26 private $config = [];
27
28 /**
29 * Image object.
30 *
31 * @since 5.0.0
32 * @var WordPressPopularPosts\Image
33 */
34 private $thumbnail;
35
36 /**
37 * Output object.
38 *
39 * @var \WordPressPopularPosts\Output
40 * @access private
41 */
42 private $output;
43
44 /**
45 * Translate object.
46 *
47 * @var \WordPressPopularPosts\Translate $translate
48 * @access private
49 */
50 private $translate;
51
52 /**
53 * Themer object.
54 *
55 * @var \WordPressPopularPosts\Themer $themer
56 * @access private
57 */
58 private $themer;
59
60 /**
61 * Construct.
62 *
63 * @since 1.0.0
64 * @param array $options
65 * @param array $config
66 * @param \WordPressPopularPosts\Output $output
67 * @param \WordPressPopularPosts\Image $image
68 * @param \WordPressPopularPosts\Translate $translate
69 * @param \WordPressPopularPosts\Themer $themer
70 */
71 public function __construct(array $options, array $config, Output $output, Image $thumbnail, Translate $translate, Themer $themer)
72 {
73 // Create the widget
74 parent::__construct(
75 'wpp',
76 'WordPress Popular Posts',
77 [
78 'classname' => 'popular-posts',
79 'description' => __('The most Popular Posts on your blog.', 'wordpress-popular-posts')
80 ]
81 );
82
83 $this->defaults = $options;
84 $this->config = $config;
85 $this->output = $output;
86 $this->thumbnail = $thumbnail;
87 $this->translate = $translate;
88 $this->themer = $themer;
89 }
90
91 /**
92 * Widget hooks.
93 *
94 * @since 5.0.0
95 */
96 public function hooks()
97 {
98 // Register the widget
99 add_action('widgets_init', [$this, 'register']);
100 // Remove widget from Legacy Widget block
101 add_filter('widget_types_to_hide_from_legacy_widget_block', [$this, 'remove_from_legacy_widget_block']);
102 }
103
104 /**
105 * Registers the widget.
106 *
107 * @since 5.0.0
108 */
109 public function register()
110 {
111 register_widget($this);
112 }
113
114 /**
115 * Outputs the content of the widget.
116 *
117 * @since 1.0.0
118 * @param array $args The array of form elements.
119 * @param array $instance The current instance of the widget.
120 */
121 public function widget($args, $instance)
122 {
123 /**
124 * @var string $name
125 * @var string $id
126 * @var string $description
127 * @var string $class
128 * @var string $before_widget
129 * @var string $after_widget
130 * @var string $before_title
131 * @var string $after_title
132 * @var string $widget_id
133 * @var string $widget_name
134 */
135 extract($args, EXTR_SKIP);
136
137 $instance = Helper::merge_array_r(
138 $this->defaults,
139 (array) $instance
140 );
141
142 echo "\n" . $before_widget . "\n";
143
144 // Has user set a title?
145 if ( '' != $instance['title'] ) {
146 $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
147
148 if (
149 $instance['markup']['custom_html']
150 && $instance['markup']['title-start'] != ""
151 && $instance['markup']['title-end'] != ""
152 ) {
153 echo htmlspecialchars_decode($instance['markup']['title-start'], ENT_QUOTES) . $title . htmlspecialchars_decode($instance['markup']['title-end'], ENT_QUOTES);
154 } else {
155 echo $before_title . $title . $after_title;
156 }
157 }
158
159 // Expose Widget ID & base for customization
160 $instance['widget_id'] = $widget_id;
161 $instance['id_base'] = $this->id_base;
162
163 // Get posts
164 if ( $this->config['tools']['ajax'] && ! is_customize_preview() ) {
165 ?>
166 <div class="wpp-widget-placeholder" data-widget-id="<?php echo esc_attr($widget_id); ?>"></div>
167 <?php
168 } else {
169 $this->get_popular($instance);
170 }
171
172 echo "\n" . $after_widget . "\n";
173 }
174
175 /**
176 * Generates the administration form for the widget.
177 *
178 * @since 1.0.0
179 * @param array $instance The array of keys and values for the widget.
180 */
181 public function form($instance)
182 {
183 $instance = Helper::merge_array_r(
184 $this->defaults,
185 (array) $instance
186 );
187 require plugin_dir_path(__FILE__) . '/form.php';
188 }
189
190 /**
191 * Processes the widget's options to be saved.
192 *
193 * @since 1.0.0
194 * @param array $new_instance The previous instance of values before the update.
195 * @param array $old_instance The new instance of values to be generated via the update.
196 * @return array $instance Updated instance.
197 */
198 public function update($new_instance, $old_instance)
199 {
200 if ( empty($old_instance) ) {
201 $old_instance = $this->defaults;
202 } else {
203 $old_instance = Helper::merge_array_r(
204 $this->defaults,
205 (array) $old_instance
206 );
207 }
208
209 $instance = $old_instance;
210
211 $instance['title'] = htmlspecialchars(stripslashes_deep(strip_tags($new_instance['title'])), ENT_QUOTES);
212 $instance['limit'] = ( Helper::is_number($new_instance['limit']) && $new_instance['limit'] > 0 )
213 ? $new_instance['limit']
214 : 10;
215 $instance['range'] = $new_instance['range'];
216 $instance['time_quantity'] = ( Helper::is_number($new_instance['time_quantity']) && $new_instance['time_quantity'] > 0 )
217 ? $new_instance['time_quantity']
218 : 24;
219 $instance['time_unit'] = $new_instance['time_unit'];
220 $instance['order_by'] = $new_instance['order_by'];
221
222 // FILTERS
223 // user did not set a post type name, so we fall back to default
224 $instance['post_type'] = ( '' == $new_instance['post_type'] )
225 ? 'post'
226 : $new_instance['post_type'];
227
228 $instance['freshness'] = isset($new_instance['freshness']);
229
230 // Post / Page / CTP filter
231 $ids = array_filter(explode(",", rtrim(preg_replace('|[^0-9,]|', '', $new_instance['pid']), ",")), 'is_numeric');
232 // Got no valid IDs, clear
233 if ( empty($ids) ) {
234 $instance['pid'] = '';
235 }
236 else {
237 $instance['pid'] = implode(",", $ids);
238 }
239
240 // Taxonomy filter
241 $taxonomies = $new_instance['taxonomy'];
242
243 if ( isset($taxonomies['names']) ) {
244 // Remove taxonomies that don't have any valid term IDs
245 foreach( $taxonomies['terms'] as $taxonomy => $terms ) {
246 $taxonomies['terms'][$taxonomy] = array_filter(
247 explode(",", trim(preg_replace('|[^0-9,-]|', '', $taxonomies['terms'][$taxonomy]), ", ")),
248 'is_numeric'
249 );
250
251 if (
252 empty($taxonomies['terms'][$taxonomy])
253 || ! in_array($taxonomy, $taxonomies['names'])
254 ) {
255 unset($taxonomies['terms'][$taxonomy]);
256 } else {
257 $taxonomies['terms'][$taxonomy] = implode(',', $taxonomies['terms'][$taxonomy]);
258 }
259 }
260
261 if ( ! empty($taxonomies['terms']) ) {
262 $instance['taxonomy'] = implode(';', array_keys($taxonomies['terms']));
263 $instance['term_id'] = implode(';', array_values($taxonomies['terms']));
264 }
265 } // Discard everything
266 else {
267 $instance['taxonomy'] = '';
268 $instance['term_id'] = '';
269 }
270
271 // Author filter
272 $ids = array_filter(explode(",", rtrim(preg_replace('|[^0-9,]|', '', $new_instance['uid']), ",")), 'is_numeric');
273 // Got no valid IDs, clear
274 if ( empty($ids) ) {
275 $instance['author'] = '';
276 }
277 else {
278 $instance['author'] = implode( ",", $ids );
279 }
280
281 $instance['shorten_title']['words'] = $new_instance['shorten_title-words'];
282 $instance['shorten_title']['active'] = isset($new_instance['shorten_title-active']);
283 $instance['shorten_title']['length'] = ( Helper::is_number($new_instance['shorten_title-length']) && $new_instance['shorten_title-length'] > 0 )
284 ? $new_instance['shorten_title-length']
285 : 25;
286
287 $instance['post-excerpt']['keep_format'] = isset($new_instance['post-excerpt-format']);
288 $instance['post-excerpt']['words'] = $new_instance['post-excerpt-words'];
289 $instance['post-excerpt']['active'] = isset($new_instance['post-excerpt-active']);
290 $instance['post-excerpt']['length'] = ( Helper::is_number($new_instance['post-excerpt-length']) && $new_instance['post-excerpt-length'] > 0 )
291 ? $new_instance['post-excerpt-length']
292 : 55;
293
294 $instance['thumbnail']['active'] = isset($new_instance['thumbnail-active']);
295 $instance['thumbnail']['build'] = $new_instance['thumbnail-size-source'];
296 $instance['thumbnail']['width'] = 75;
297 $instance['thumbnail']['height'] = 75;
298
299 // Use predefined thumbnail sizes
300 if ( 'predefined' == $new_instance['thumbnail-size-source'] ) {
301 $default_thumbnail_sizes = $this->thumbnail->get_sizes(null);
302 $size = $default_thumbnail_sizes[$new_instance['thumbnail-size']];
303
304 $instance['thumbnail']['width'] = $size['width'];
305 $instance['thumbnail']['height'] = $size['height'];
306 $instance['thumbnail']['crop'] = $size['crop'];
307 } // Set thumbnail size manually
308 else {
309 if ( Helper::is_number($new_instance['thumbnail-width']) && Helper::is_number($new_instance['thumbnail-height']) ) {
310 $instance['thumbnail']['width'] = $new_instance['thumbnail-width'];
311 $instance['thumbnail']['height'] = $new_instance['thumbnail-height'];
312 $instance['thumbnail']['crop'] = true;
313 }
314 }
315
316 $instance['rating'] = isset($new_instance['rating']);
317 $instance['stats_tag']['comment_count'] = isset($new_instance['comment_count']);
318 $instance['stats_tag']['views'] = isset($new_instance['views']);
319 $instance['stats_tag']['author'] = isset($new_instance['author']);
320 $instance['stats_tag']['date']['active'] = isset($new_instance['date']);
321 $instance['stats_tag']['date']['format'] = empty($new_instance['date_format'])
322 ? 'F j, Y'
323 : $new_instance['date_format'];
324
325 $instance['stats_tag']['taxonomy']['active'] = isset($new_instance['stats_taxonomy']);
326 $instance['stats_tag']['taxonomy']['name'] = isset($new_instance['stats_taxonomy_name']) ? $new_instance['stats_taxonomy_name'] : 'category';
327 $instance['stats_tag']['category'] = isset($new_instance['stats_taxonomy'] ); // Deprecated in 4.0.0!
328
329 $instance['markup']['custom_html'] = isset($new_instance['custom_html']);
330 $instance['markup']['wpp-start'] = empty($new_instance['wpp-start'])
331 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? htmlspecialchars('<ul class="wpp-list">', ENT_QUOTES) : ''
332 : htmlspecialchars($new_instance['wpp-start'], ENT_QUOTES);
333
334 $instance['markup']['wpp-end'] = empty($new_instance['wpp-end'])
335 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? htmlspecialchars('</ul>', ENT_QUOTES) : ''
336 : htmlspecialchars($new_instance['wpp-end'], ENT_QUOTES);
337
338 $instance['markup']['post-html'] = empty($new_instance['post-html'])
339 ? htmlspecialchars('<li>{thumb} {title} {stats}</li>', ENT_QUOTES)
340 : htmlspecialchars($new_instance['post-html'], ENT_QUOTES);
341
342 $instance['markup']['title-start'] = empty($new_instance['title-start'])
343 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? '<h2>' : ''
344 : htmlspecialchars($new_instance['title-start'], ENT_QUOTES);
345
346 $instance['markup']['title-end'] = empty($new_instance['title-end'])
347 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? '</h2>' : '' :
348 htmlspecialchars($new_instance['title-end'], ENT_QUOTES);
349
350 $instance['theme'] = [
351 'name' => isset($new_instance['theme']) ? $new_instance['theme'] : '',
352 'applied' => isset($new_instance['theme']) ? (bool) $new_instance['theme-applied'] : false
353 ];
354
355 if ( ! isset($new_instance['theme']) || $old_instance['theme']['name'] != $new_instance['theme'] ) {
356 $instance['theme']['applied'] = false;
357 }
358
359 // On the new Widgets screen $new_instance['theme'] is
360 // an array for some reason, let's grab the theme name
361 // from the array and move on
362 if ( is_array($instance['theme']['name']) ) {
363 $instance['theme']['name'] = $instance['theme']['name']['name'];
364 }
365
366 $theme = $instance['theme']['name'] ? $this->themer->get_theme($instance['theme']['name']) : null;
367
368 if (
369 is_array($theme)
370 && isset($theme['json'])
371 && isset($theme['json']['config'])
372 && is_array($theme['json']['config'])
373 && ! $instance['theme']['applied']
374 ) {
375 $instance = Helper::merge_array_r(
376 $instance,
377 $theme['json']['config']
378 );
379 $instance['markup']['custom_html'] = true;
380 $instance['theme']['applied'] = true;
381
382 $current_sidebar_data = $this->get_sidebar_data();
383
384 if ( $current_sidebar_data ) {
385 $instance['markup']['title-start'] = htmlspecialchars($current_sidebar_data['before_title'], ENT_QUOTES);
386 $instance['markup']['title-end'] = htmlspecialchars($current_sidebar_data['after_title'], ENT_QUOTES);
387 }
388 }
389
390 return $instance;
391 }
392
393 /**
394 * Returns HTML list.
395 *
396 * @since 2.3.3
397 */
398 public function get_popular(array $instance)
399 {
400 $popular_posts = $this->maybe_query($instance);
401
402 $this->output->set_data($popular_posts->get_posts());
403 $this->output->set_public_options($instance);
404 $this->output->build_output();
405 $this->output->output();
406 }
407
408 /**
409 * Returns data on the current sidebar.
410 *
411 * @since 5.0.0
412 * @access private
413 * @return array|null
414 */
415 private function get_sidebar_data()
416 {
417 global $wp_registered_sidebars;
418 $sidebars = wp_get_sidebars_widgets();
419
420 foreach ( (array) $sidebars as $sidebar_id => $sidebar ) {
421 if ( in_array($this->id, (array) $sidebar, true ) )
422 return $wp_registered_sidebars[$sidebar_id];
423 }
424
425 return null;
426 }
427
428 /**
429 * Removes the standard widget from the Legacy Widget block.
430 *
431 * @param array
432 * @return array
433 */
434 public function remove_from_legacy_widget_block(array $widget_types)
435 {
436 $widget_types[] = 'wpp';
437 return $widget_types;
438 }
439 }
440