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