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