widget.php
414 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | //use WordPressPopularPosts\{Image, Themer}; |
| 7 | |
| 8 | /** |
| 9 | * Elementor WPP Widget. |
| 10 | * |
| 11 | * Elementor widget that inserts a WP Popular Posts list on your site. |
| 12 | * |
| 13 | * @since 7.3.0 |
| 14 | */ |
| 15 | class Elementor_WPP_Widget extends \Elementor\Widget_Base { |
| 16 | |
| 17 | /** |
| 18 | * Image object. |
| 19 | * |
| 20 | * @since 7.3.0 |
| 21 | * @var \WordPressPopularPosts\Image |
| 22 | * @access private |
| 23 | */ |
| 24 | private $thumbnail; |
| 25 | |
| 26 | /** |
| 27 | * Available image sizes. |
| 28 | * |
| 29 | * @since 7.3.0 |
| 30 | * @var array |
| 31 | */ |
| 32 | private $available_sizes = []; |
| 33 | |
| 34 | /** |
| 35 | * Themer object. |
| 36 | * |
| 37 | * @since 7.3.0 |
| 38 | * @var \WordPressPopularPosts\Themer $themer |
| 39 | * @access private |
| 40 | */ |
| 41 | private $themer; |
| 42 | |
| 43 | /** |
| 44 | * Construct. |
| 45 | * |
| 46 | * @param array $data Widget data. Default is an empty array. |
| 47 | * @param array|null $args Optional. Widget default arguments. Default is null. |
| 48 | * @param WordPressPopularPosts\Themer $themer |
| 49 | */ |
| 50 | public function __construct($data = [], $args = null) { |
| 51 | parent::__construct($data, $args); |
| 52 | |
| 53 | $this->thumbnail = $args['image']; |
| 54 | $this->themer = $args['themer']; |
| 55 | $this->available_sizes = $this->thumbnail->get_sizes(null); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get widget name. |
| 60 | * |
| 61 | * Retrieve WPP widget name. |
| 62 | * |
| 63 | * @since 7.3.0 |
| 64 | * @access public |
| 65 | * @return string Widget name. |
| 66 | */ |
| 67 | public function get_name(): string { |
| 68 | return 'wordpress-popular-posts-ewidget'; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get widget title. |
| 73 | * |
| 74 | * Retrieve WPP widget title. |
| 75 | * |
| 76 | * @since 7.3.0 |
| 77 | * @access public |
| 78 | * @return string Widget title. |
| 79 | */ |
| 80 | public function get_title(): string { |
| 81 | return 'WP Popular Posts'; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get widget icon. |
| 86 | * |
| 87 | * Retrieve WPP widget icon. |
| 88 | * |
| 89 | * @since 7.3.0 |
| 90 | * @access public |
| 91 | * @return string Widget icon. |
| 92 | */ |
| 93 | public function get_icon(): string { |
| 94 | return 'wpp-eicon'; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get widget categories. |
| 99 | * |
| 100 | * Retrieve the list of categories the WPP widget belongs to. |
| 101 | * |
| 102 | * @since 7.3.0 |
| 103 | * @access public |
| 104 | * @return array Widget categories. |
| 105 | */ |
| 106 | public function get_categories(): array { |
| 107 | return ['general']; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get widget keywords. |
| 112 | * |
| 113 | * Retrieve the list of keywords the WPP widget belongs to. |
| 114 | * |
| 115 | * @since 7.3.0 |
| 116 | * @access public |
| 117 | * @return array Widget keywords. |
| 118 | */ |
| 119 | public function get_keywords(): array { |
| 120 | return ['popular', 'posts', 'popularity', 'top']; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get custom help URL. |
| 125 | * |
| 126 | * Retrieve a URL where the user can get more information about the widget. |
| 127 | * |
| 128 | * @since 7.3.0 |
| 129 | * @access public |
| 130 | * @return string Widget help URL. |
| 131 | */ |
| 132 | public function get_custom_help_url(): string { |
| 133 | return 'https://wordpress.org/support/plugin/wordpress-popular-posts/'; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Displays a custom CTA to offer upsales and whatnot. |
| 138 | * |
| 139 | * @since 7.3.0 |
| 140 | * @see https://developers.elementor.com/docs/widgets/widget-promotions/ |
| 141 | * @return array |
| 142 | */ |
| 143 | protected function get_upsale_data(): array { |
| 144 | return []; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get stack. |
| 149 | * |
| 150 | * Retrieve the widget stack of controls. |
| 151 | * |
| 152 | * @since 7.3.0 |
| 153 | * @param bool $with_common_controls Optional. Whether to include the common controls. Default is true. |
| 154 | * @return array Widget stack of controls. |
| 155 | */ |
| 156 | public function get_stack( $with_common_controls = true ) { |
| 157 | return parent::get_stack(false); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Whether the widget requires inner wrapper. |
| 162 | * |
| 163 | * Determine whether to optimize the DOM size. |
| 164 | * |
| 165 | * @since 7.3.0 |
| 166 | * @access protected |
| 167 | * @return bool Whether to optimize the DOM size. |
| 168 | */ |
| 169 | public function has_widget_inner_wrapper(): bool { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Whether the element returns dynamic content. |
| 175 | * |
| 176 | * Determine whether to cache the element output or not. |
| 177 | * |
| 178 | * @since 7.3.0 |
| 179 | * @access protected |
| 180 | * @return bool Whether to cache the element output. |
| 181 | */ |
| 182 | protected function is_dynamic_content(): bool { |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Register WPP widget controls. |
| 188 | * |
| 189 | * Add input fields to allow the user to customize the widget settings. |
| 190 | * |
| 191 | * @since 7.3.0 |
| 192 | * @access protected |
| 193 | */ |
| 194 | protected function register_controls(): void { |
| 195 | require 'widget-controls.php'; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Render WPP widget output on the frontend. |
| 200 | * |
| 201 | * Written in PHP and used to generate the final HTML. |
| 202 | * |
| 203 | * @since 7.3.0 |
| 204 | * @access protected |
| 205 | */ |
| 206 | protected function render(): void { |
| 207 | $is_edit_mode = \Elementor\Plugin::$instance->editor->is_edit_mode(); |
| 208 | $widget_id = $this->get_id(); |
| 209 | $settings = $this->parse_settings(); |
| 210 | |
| 211 | /** |
| 212 | * Allows to modify settings passed to wpp_get_mostpopular() |
| 213 | * |
| 214 | * @param array $settings WPP settings |
| 215 | * @param string $widget_id Elementor Widget ID |
| 216 | */ |
| 217 | $settings = apply_filters('wpp_elementor_widget_settings', $settings, $widget_id); |
| 218 | |
| 219 | /** While on edit mode... */ |
| 220 | if ( $is_edit_mode ) { |
| 221 | /** ... disable AJAX loading */ |
| 222 | $settings['ajaxify'] = 0; |
| 223 | |
| 224 | /** ... display widget ID above the list when debug mode is on */ |
| 225 | if ( defined('WP_DEBUG') && WP_DEBUG ) { |
| 226 | echo '<p style="margin: 0 0 1em; font-size: 10px; font-weight: 600;">[Widget ID:' . esc_html($widget_id) . ']</p>'; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | wpp_get_mostpopular($settings); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Parses and cleans up settings data from Elementor |
| 235 | * before using it. |
| 236 | * |
| 237 | * @since 7.3.0 |
| 238 | * @access private |
| 239 | */ |
| 240 | private function parse_settings() { |
| 241 | $settings = $this->get_settings_for_display(); |
| 242 | |
| 243 | $allowed_keys = [ |
| 244 | 'header', 'limit', 'offset', 'range', 'time_unit', 'time_quantity', 'freshness', 'order_by', 'post_type', 'exclude', 'cat', 'taxonomy', 'term_id', 'author', 'title_length', 'title_by_words', 'excerpt_length', 'excerpt_format', 'excerpt_by_words', |
| 245 | 'thumbnail_width', 'thumbnail_height', 'thumbnail_build', 'rating', 'stats_comments', 'stats_views', 'stats_author', 'stats_date', 'stats_date_format', 'stats_category', 'stats_taxonomy', 'wpp_start', 'wpp_end', 'header_start', 'header_end', 'post_html', |
| 246 | 'theme', 'ajaxify' |
| 247 | ]; |
| 248 | |
| 249 | /** Handle taxonomies */ |
| 250 | $wpp_taxonomy_fields = array_filter($settings, function($key) { |
| 251 | return str_starts_with($key, 'wpp_taxonomy_'); // str_starts_with() requires either WP 5.9+ or PHP 8+ |
| 252 | }, ARRAY_FILTER_USE_KEY); |
| 253 | $wpp_taxonomies = []; |
| 254 | |
| 255 | if ( $wpp_taxonomy_fields ) { |
| 256 | $slugs_arr = array_filter($settings, function($key) { |
| 257 | return str_starts_with($key, 'wpp_taxonomy_slug_'); // str_starts_with() requires either WP 5.9+ or PHP 8+ |
| 258 | }, ARRAY_FILTER_USE_KEY); |
| 259 | |
| 260 | if ( $slugs_arr ) { |
| 261 | $slugs = array_filter($slugs_arr, function($slug) { |
| 262 | return 0 !== $slug; |
| 263 | }); |
| 264 | |
| 265 | if ( $slugs ) { |
| 266 | $slugs = array_values($slugs); |
| 267 | |
| 268 | foreach( $slugs as $slug ) { |
| 269 | if ( ! isset($wpp_taxonomy_fields['wpp_taxonomy_' . $slug . '_terms']) ) { |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | $terms = array_filter( |
| 274 | explode( |
| 275 | ',', |
| 276 | trim(preg_replace('|[^0-9,-]|', '', $wpp_taxonomy_fields['wpp_taxonomy_' . $slug . '_terms']), ', ') |
| 277 | ), |
| 278 | 'is_numeric' |
| 279 | ); |
| 280 | |
| 281 | if ( ! empty($terms) ) { |
| 282 | $wpp_taxonomies[$slug] = implode(',', $terms); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if ( $wpp_taxonomies ) { |
| 290 | $settings['taxonomy'] = implode(';', array_keys($wpp_taxonomies)); |
| 291 | $settings['term_id'] = implode(';', array_values($wpp_taxonomies)); |
| 292 | } |
| 293 | |
| 294 | /** Handle thumbnail selection */ |
| 295 | if ( isset($settings['thumbnail_size']) && isset($this->available_sizes[$settings['thumbnail_size']]) ) { |
| 296 | $settings['thumbnail_width'] = $this->available_sizes[$settings['thumbnail_size']]['width']; |
| 297 | $settings['thumbnail_height'] = $this->available_sizes[$settings['thumbnail_size']]['height']; |
| 298 | } |
| 299 | |
| 300 | /** Let's remove all Elementor related settings */ |
| 301 | $settings = array_filter($settings, function($key) use ($allowed_keys) { |
| 302 | return in_array($key, $allowed_keys); |
| 303 | }, ARRAY_FILTER_USE_KEY); |
| 304 | |
| 305 | /** Handle toggles that are enabled by default */ |
| 306 | if ( ! $settings['stats_views'] ) { |
| 307 | $settings['stats_views'] = '0'; |
| 308 | } |
| 309 | |
| 310 | /** Handle themes */ |
| 311 | $registered_themes = $this->themer->get_themes(); |
| 312 | |
| 313 | if ( $settings['theme'] && isset($registered_themes[$settings['theme']]) ) { |
| 314 | $theme_config = $registered_themes[$settings['theme']]['json']['config']; |
| 315 | |
| 316 | if ( |
| 317 | isset($theme_config['shorten_title']) |
| 318 | && $theme_config['shorten_title']['active'] |
| 319 | && is_numeric($theme_config['shorten_title']['length']) |
| 320 | && $theme_config['shorten_title']['length'] > 0 |
| 321 | ) { |
| 322 | $settings['title_length'] = $theme_config['shorten_title']['length']; |
| 323 | |
| 324 | if ( $theme_config['shorten_title']['length'] ) { |
| 325 | $settings['title_by_words'] = 1; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if ( |
| 330 | isset($theme_config['post-excerpt']) |
| 331 | && $theme_config['post-excerpt']['active'] |
| 332 | && is_numeric($theme_config['post-excerpt']['length']) |
| 333 | && $theme_config['post-excerpt']['length'] > 0 |
| 334 | ) { |
| 335 | $settings['excerpt_length'] = $theme_config['post-excerpt']['length']; |
| 336 | |
| 337 | if ( $theme_config['post-excerpt']['keep_format'] ) { |
| 338 | $settings['excerpt_format'] = 1; |
| 339 | } |
| 340 | |
| 341 | if ( $theme_config['post-excerpt']['words'] ) { |
| 342 | $settings['excerpt_by_words'] = 1; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if ( |
| 347 | isset($theme_config['thumbnail']) |
| 348 | && $theme_config['thumbnail']['active'] |
| 349 | && is_numeric($theme_config['thumbnail']['width']) |
| 350 | && is_numeric($theme_config['thumbnail']['height']) |
| 351 | && $theme_config['thumbnail']['width'] > 0 |
| 352 | && $theme_config['thumbnail']['height'] > 0 |
| 353 | ) { |
| 354 | $settings['thumbnail_width'] = $theme_config['thumbnail']['width']; |
| 355 | $settings['thumbnail_height'] = $theme_config['thumbnail']['height']; |
| 356 | $settings['thumbnail_build'] = $theme_config['thumbnail']['build'] ?? 'manual'; |
| 357 | |
| 358 | if ( $theme_config['post-excerpt']['keep_format'] ) { |
| 359 | $settings['excerpt_format'] = 1; |
| 360 | } |
| 361 | |
| 362 | if ( $theme_config['post-excerpt']['words'] ) { |
| 363 | $settings['excerpt_by_words'] = 1; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if ( isset($theme_config['rating']) && $theme_config['rating'] ) { |
| 368 | $settings['rating'] = 1; |
| 369 | } |
| 370 | |
| 371 | if ( isset($theme_config['stats_tag']['comment_count']) && $theme_config['stats_tag']['comment_count'] ) { |
| 372 | $settings['stats_comments'] = 1; |
| 373 | } |
| 374 | |
| 375 | if ( isset($theme_config['stats_tag']['views']) && $theme_config['stats_tag']['views'] ) { |
| 376 | $settings['stats_views'] = 1; |
| 377 | } |
| 378 | |
| 379 | if ( isset($theme_config['stats_tag']['author']) && $theme_config['stats_tag']['author'] ) { |
| 380 | $settings['stats_author'] = 1; |
| 381 | } |
| 382 | |
| 383 | if ( isset($theme_config['stats_tag']['date']) && $theme_config['stats_tag']['date']['active'] ) { |
| 384 | $settings['stats_date'] = 1; |
| 385 | |
| 386 | if ( isset($theme_config['stats_tag']['date']['format']) && $theme_config['stats_tag']['date']['format'] ) { |
| 387 | $settings['stats_date_format'] = $theme_config['stats_tag']['date']['format']; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if ( isset($theme_config['stats_tag']['category']) && $theme_config['stats_tag']['category'] ) { |
| 392 | $settings['stats_category'] = 1; |
| 393 | } else { |
| 394 | if ( isset($theme_config['stats_tag']['taxonomy']) && $theme_config['stats_tag']['taxonomy']['active'] ) { |
| 395 | $settings['stats_taxonomy'] = 1; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @TODO |
| 401 | * Allow displaying multiple taxonomies |
| 402 | */ |
| 403 | |
| 404 | $settings['wpp_start'] = $theme_config['markup']['wpp-start']; |
| 405 | $settings['post_html'] = $theme_config['markup']['post-html']; |
| 406 | $settings['wpp_end'] = $theme_config['markup']['wpp-end']; |
| 407 | } else { |
| 408 | $settings['theme'] = ''; |
| 409 | } |
| 410 | |
| 411 | return $settings; |
| 412 | } |
| 413 | } |
| 414 |