| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Currently, this widget depends on the Stats Module. To not load this file |
| 5 |
* when the Stats Module is not active would potentially bypass Jetpack's |
| 6 |
* fatal error detection on module activation, so we always load this file. |
| 7 |
* Instead, we don't register the widget if the Stats Module isn't active. |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Register the widget for use in Appearance -> Widgets |
| 12 |
*/ |
| 13 |
add_action( 'widgets_init', 'jetpack_top_posts_widget_init' ); |
| 14 |
|
| 15 |
function jetpack_top_posts_widget_init() { |
| 16 |
// Currently, this widget depends on the Stats Module |
| 17 |
if ( |
| 18 |
( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) |
| 19 |
&& |
| 20 |
! function_exists( 'stats_get_from_restapi' ) |
| 21 |
) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
register_widget( 'Jetpack_Top_Posts_Widget' ); |
| 26 |
} |
| 27 |
|
| 28 |
class Jetpack_Top_Posts_Widget extends WP_Widget { |
| 29 |
public $alt_option_name = 'widget_stats_topposts'; |
| 30 |
public $default_title = ''; |
| 31 |
|
| 32 |
function __construct() { |
| 33 |
parent::__construct( |
| 34 |
'top-posts', |
| 35 |
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 36 |
apply_filters( 'jetpack_widget_name', __( 'Top Posts & Pages', 'jetpack' ) ), |
| 37 |
array( |
| 38 |
'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ), |
| 39 |
'customize_selective_refresh' => true, |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
$this->default_title = __( 'Top Posts & Pages', 'jetpack' ); |
| 44 |
|
| 45 |
if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { |
| 46 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Add explanation about how the statistics are calculated. |
| 51 |
* |
| 52 |
* @module widgets |
| 53 |
* |
| 54 |
* @since 3.9.3 |
| 55 |
*/ |
| 56 |
add_action( 'jetpack_widget_top_posts_after_fields', array( $this, 'stats_explanation' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
function enqueue_style() { |
| 60 |
wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' ); |
| 61 |
wp_enqueue_style( 'jetpack-top-posts-widget' ); |
| 62 |
} |
| 63 |
|
| 64 |
function form( $instance ) { |
| 65 |
$instance = wp_parse_args( (array) $instance, $this->defaults() ); |
| 66 |
|
| 67 |
if ( false === $instance['title'] ) { |
| 68 |
$instance['title'] = $this->default_title; |
| 69 |
} |
| 70 |
$title = stripslashes( $instance['title'] ); |
| 71 |
|
| 72 |
$count = isset( $instance['count'] ) ? (int) $instance['count'] : 10; |
| 73 |
if ( $count < 1 || 10 < $count ) { |
| 74 |
$count = 10; |
| 75 |
} |
| 76 |
|
| 77 |
$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) ); |
| 78 |
$types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
| 79 |
|
| 80 |
// 'likes' are not available in Jetpack |
| 81 |
$ordering = isset( $instance['ordering'] ) && 'likes' === $instance['ordering'] ? 'likes' : 'views'; |
| 82 |
|
| 83 |
if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
| 84 |
$display = $instance['display']; |
| 85 |
} else { |
| 86 |
$display = 'text'; |
| 87 |
} |
| 88 |
|
| 89 |
?> |
| 90 |
|
| 91 |
<p> |
| 92 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
| 93 |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 94 |
</p> |
| 95 |
|
| 96 |
<p> |
| 97 |
<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php esc_html_e( 'Maximum number of posts to show (no more than 10):', 'jetpack' ); ?></label> |
| 98 |
<input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" type="number" value="<?php echo (int) $count; ?>" min="1" max="10" /> |
| 99 |
</p> |
| 100 |
|
| 101 |
<?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?> |
| 102 |
<p> |
| 103 |
<label><?php esc_html_e( 'Order Top Posts & Pages By:', 'jetpack' ); ?></label> |
| 104 |
<ul> |
| 105 |
<li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-likes" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="likes" <?php checked( 'likes', $ordering ); ?> /> <?php esc_html_e( 'Likes', 'jetpack' ); ?></label></li> |
| 106 |
<li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-views" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="views" <?php checked( 'views', $ordering ); ?> /> <?php esc_html_e( 'Views', 'jetpack' ); ?></label></li> |
| 107 |
</ul> |
| 108 |
</p> |
| 109 |
<?php endif; ?> |
| 110 |
|
| 111 |
<p> |
| 112 |
<label for="<?php echo $this->get_field_id( 'types' ); ?>"><?php esc_html_e( 'Types of pages to display:', 'jetpack' ); ?></label> |
| 113 |
<ul> |
| 114 |
<?php |
| 115 |
foreach ( $allowed_post_types as $type ) { |
| 116 |
// Get the Post Type name to display next to the checkbox |
| 117 |
$post_type_object = get_post_type_object( $type ); |
| 118 |
$label = $post_type_object->labels->name; |
| 119 |
|
| 120 |
$checked = ''; |
| 121 |
if ( in_array( $type, $types ) ) { |
| 122 |
$checked = 'checked="checked" '; |
| 123 |
} |
| 124 |
?> |
| 125 |
|
| 126 |
<li><label> |
| 127 |
<input value="<?php echo esc_attr( $type ); ?>" name="<?php echo $this->get_field_name( 'types' ); ?>[]" id="<?php echo $this->get_field_id( 'types' ); ?>-<?php echo $type; ?>" type="checkbox" <?php echo $checked; ?>> |
| 128 |
<?php echo esc_html( $label ); ?> |
| 129 |
</label></li> |
| 130 |
|
| 131 |
<?php } // End foreach ?> |
| 132 |
</ul> |
| 133 |
</p> |
| 134 |
|
| 135 |
<p> |
| 136 |
<label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label> |
| 137 |
<ul> |
| 138 |
<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-text" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="text" <?php checked( 'text', $display ); ?> /> <?php esc_html_e( 'Text List', 'jetpack' ); ?></label></li> |
| 139 |
<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-list" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="list" <?php checked( 'list', $display ); ?> /> <?php esc_html_e( 'Image List', 'jetpack' ); ?></label></li> |
| 140 |
<li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-grid" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="grid" <?php checked( 'grid', $display ); ?> /> <?php esc_html_e( 'Image Grid', 'jetpack' ); ?></label></li> |
| 141 |
</ul> |
| 142 |
</p> |
| 143 |
<?php |
| 144 |
|
| 145 |
/** |
| 146 |
* Fires after the fields are displayed in the Top Posts Widget settings in wp-admin. |
| 147 |
* |
| 148 |
* Allow adding extra content after the fields are displayed. |
| 149 |
* |
| 150 |
* @module widgets |
| 151 |
* |
| 152 |
* @since 3.9.3 |
| 153 |
* |
| 154 |
* @param array $args { |
| 155 |
* @param array $instance The widget instance. |
| 156 |
* @param object $this The class object. |
| 157 |
* } |
| 158 |
*/ |
| 159 |
do_action( 'jetpack_widget_top_posts_after_fields', array( $instance, $this ) ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Explains how the statics are calculated. |
| 164 |
*/ |
| 165 |
function stats_explanation() { |
| 166 |
?> |
| 167 |
|
| 168 |
<p><?php esc_html_e( 'Top Posts & Pages by views are calculated from 24-48 hours of stats. They take a while to change.', 'jetpack' ); ?></p> |
| 169 |
<?php |
| 170 |
} |
| 171 |
|
| 172 |
function update( $new_instance, $old_instance ) { |
| 173 |
$instance = array(); |
| 174 |
$instance['title'] = wp_kses( $new_instance['title'], array() ); |
| 175 |
if ( $instance['title'] === $this->default_title ) { |
| 176 |
$instance['title'] = false; // Store as false in case of language change |
| 177 |
} |
| 178 |
|
| 179 |
$instance['count'] = (int) $new_instance['count']; |
| 180 |
if ( $instance['count'] < 1 || 10 < $instance['count'] ) { |
| 181 |
$instance['count'] = 10; |
| 182 |
} |
| 183 |
|
| 184 |
// 'likes' are not available in Jetpack |
| 185 |
$instance['ordering'] = isset( $new_instance['ordering'] ) && 'likes' == $new_instance['ordering'] ? 'likes' : 'views'; |
| 186 |
|
| 187 |
$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) ); |
| 188 |
$instance['types'] = $new_instance['types']; |
| 189 |
foreach ( $new_instance['types'] as $key => $type ) { |
| 190 |
if ( ! in_array( $type, $allowed_post_types ) ) { |
| 191 |
unset( $new_instance['types'][ $key ] ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
| 196 |
$instance['display'] = $new_instance['display']; |
| 197 |
} else { |
| 198 |
$instance['display'] = 'text'; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Filters Top Posts Widget settings before they're saved. |
| 203 |
* |
| 204 |
* @module widgets |
| 205 |
* |
| 206 |
* @since 3.9.3 |
| 207 |
* |
| 208 |
* @param array $instance The santized widget instance. Only contains data processed by the current widget. |
| 209 |
* @param array $new_instance The new widget instance before sanitization. |
| 210 |
*/ |
| 211 |
$instance = apply_filters( 'jetpack_top_posts_saving', $instance, $new_instance ); |
| 212 |
|
| 213 |
return $instance; |
| 214 |
} |
| 215 |
|
| 216 |
function widget( $args, $instance ) { |
| 217 |
/** This action is documented in modules/widgets/gravatar-profile.php */ |
| 218 |
do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' ); |
| 219 |
|
| 220 |
$instance = wp_parse_args( (array) $instance, $this->defaults() ); |
| 221 |
|
| 222 |
$title = isset( $instance['title'] ) ? $instance['title'] : false; |
| 223 |
if ( false === $title ) { |
| 224 |
$title = $this->default_title; |
| 225 |
} |
| 226 |
/** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 227 |
$title = apply_filters( 'widget_title', $title ); |
| 228 |
|
| 229 |
$count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
| 230 |
if ( $count < 1 || 10 < $count ) { |
| 231 |
$count = 10; |
| 232 |
} |
| 233 |
/** |
| 234 |
* Control the number of displayed posts. |
| 235 |
* |
| 236 |
* @module widgets |
| 237 |
* |
| 238 |
* @since 3.3.0 |
| 239 |
* |
| 240 |
* @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. |
| 241 |
*/ |
| 242 |
$count = apply_filters( 'jetpack_top_posts_widget_count', $count ); |
| 243 |
|
| 244 |
$types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
| 245 |
|
| 246 |
// 'likes' are not available in Jetpack |
| 247 |
$ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views'; |
| 248 |
|
| 249 |
if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
| 250 |
$display = $instance['display']; |
| 251 |
} else { |
| 252 |
$display = 'text'; |
| 253 |
} |
| 254 |
|
| 255 |
if ( 'text' != $display ) { |
| 256 |
$get_image_options = array( |
| 257 |
'fallback_to_avatars' => true, |
| 258 |
/** This filter is documented in modules/stats.php */ |
| 259 |
'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'https://en.wordpress.com/i/logo/white-gray-80.png' ) ), |
| 260 |
'avatar_size' => 40, |
| 261 |
'width' => null, |
| 262 |
'height' => null, |
| 263 |
); |
| 264 |
if ( 'grid' == $display ) { |
| 265 |
$get_image_options['avatar_size'] = 200; |
| 266 |
} |
| 267 |
/** |
| 268 |
* Top Posts Widget Image options. |
| 269 |
* |
| 270 |
* @module widgets |
| 271 |
* |
| 272 |
* @since 1.8.0 |
| 273 |
* |
| 274 |
* @param array $get_image_options { |
| 275 |
* Array of Image options. |
| 276 |
* @type bool true Should we default to Gravatars when no image is found? Default is true. |
| 277 |
* @type string $gravatar_default Default Image URL if no Gravatar is found. |
| 278 |
* @type int $avatar_size Default Image size. |
| 279 |
* @type mixed $width Image width, not set by default and $avatar_size is used instead. |
| 280 |
* @type mixed $height Image height, not set by default and $avatar_size is used instead. |
| 281 |
* } |
| 282 |
*/ |
| 283 |
$get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); |
| 284 |
} |
| 285 |
|
| 286 |
if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) { |
| 287 |
$posts = $this->get_by_likes( $count ); |
| 288 |
} else { |
| 289 |
$posts = $this->get_by_views( $count, $args ); |
| 290 |
} |
| 291 |
|
| 292 |
// Filter the returned posts. Remove all posts that do not match the chosen Post Types. |
| 293 |
if ( isset( $types ) ) { |
| 294 |
foreach ( $posts as $k => $post ) { |
| 295 |
if ( ! in_array( $post['post_type'], $types ) ) { |
| 296 |
unset( $posts[ $k ] ); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if ( ! $posts ) { |
| 302 |
$posts = $this->get_fallback_posts(); |
| 303 |
} |
| 304 |
|
| 305 |
echo $args['before_widget']; |
| 306 |
if ( ! empty( $title ) ) { |
| 307 |
echo $args['before_title'] . $title . $args['after_title']; |
| 308 |
} |
| 309 |
|
| 310 |
if ( ! $posts ) { |
| 311 |
$link = 'https://jetpack.com/support/getting-more-views-and-traffic/'; |
| 312 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 313 |
$link = 'https://en.support.wordpress.com/getting-more-site-traffic/'; |
| 314 |
} |
| 315 |
|
| 316 |
if ( current_user_can( 'edit_theme_options' ) ) { |
| 317 |
echo '<p>' . sprintf( |
| 318 |
__( 'There are no posts to display. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ), |
| 319 |
esc_url( $link ) |
| 320 |
) . '</p>'; |
| 321 |
} |
| 322 |
|
| 323 |
echo $args['after_widget']; |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Filter the layout of the Top Posts Widget |
| 329 |
* |
| 330 |
* @module widgets |
| 331 |
* |
| 332 |
* @since 6.4.0 |
| 333 |
* |
| 334 |
* @param string $layout layout of the Top Posts Widget (empty string) |
| 335 |
* @param array $posts IDs of the posts to be displayed |
| 336 |
* @param array $display Display option from widget form |
| 337 |
*/ |
| 338 |
$layout = apply_filters( 'jetpack_top_posts_widget_layout', '', $posts, $display ); |
| 339 |
if ( ! empty( $layout ) ) { |
| 340 |
echo $layout; |
| 341 |
echo $args['after_widget']; |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
switch ( $display ) { |
| 346 |
case 'list': |
| 347 |
case 'grid': |
| 348 |
// Keep the avatar_size as default dimensions for backward compatibility. |
| 349 |
$width = (int) $get_image_options['avatar_size']; |
| 350 |
$height = (int) $get_image_options['avatar_size']; |
| 351 |
|
| 352 |
// Check if the user has changed the width. |
| 353 |
if ( ! empty( $get_image_options['width'] ) ) { |
| 354 |
$width = (int) $get_image_options['width']; |
| 355 |
} |
| 356 |
|
| 357 |
// Check if the user has changed the height. |
| 358 |
if ( ! empty( $get_image_options['height'] ) ) { |
| 359 |
$height = (int) $get_image_options['height']; |
| 360 |
} |
| 361 |
|
| 362 |
foreach ( $posts as &$post ) { |
| 363 |
$image = Jetpack_PostImages::get_image( |
| 364 |
$post['post_id'], |
| 365 |
array( |
| 366 |
'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'], |
| 367 |
'width' => (int) $width, |
| 368 |
'height' => (int) $height, |
| 369 |
'avatar_size' => (int) $get_image_options['avatar_size'], |
| 370 |
) |
| 371 |
); |
| 372 |
$post['image'] = $image['src']; |
| 373 |
if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { |
| 374 |
$post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) ); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
unset( $post ); |
| 379 |
|
| 380 |
if ( 'grid' == $display ) { |
| 381 |
echo "<div class='widgets-grid-layout no-grav'>\n"; |
| 382 |
foreach ( $posts as $post ) : |
| 383 |
?> |
| 384 |
<div class="widget-grid-view-image"> |
| 385 |
<?php |
| 386 |
/** |
| 387 |
* Fires before each Top Post result, inside <li>. |
| 388 |
* |
| 389 |
* @module widgets |
| 390 |
* |
| 391 |
* @since 3.2.0 |
| 392 |
* |
| 393 |
* @param string $post['post_id'] Post ID. |
| 394 |
*/ |
| 395 |
do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
| 396 |
|
| 397 |
/** |
| 398 |
* Filter the permalink of items in the Top Posts widget. |
| 399 |
* |
| 400 |
* @module widgets |
| 401 |
* |
| 402 |
* @since 4.4.0 |
| 403 |
* |
| 404 |
* @param string $post['permalink'] Post permalink. |
| 405 |
* @param array $post Post array. |
| 406 |
*/ |
| 407 |
$filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
| 408 |
|
| 409 |
printf( |
| 410 |
'<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true"/></a>', |
| 411 |
esc_url( $filtered_permalink ), |
| 412 |
esc_attr( wp_kses( $post['title'], array() ) ), |
| 413 |
( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
| 414 |
absint( $width ), |
| 415 |
absint( $height ), |
| 416 |
esc_url( $post['image'] ) |
| 417 |
); |
| 418 |
|
| 419 |
/** |
| 420 |
* Fires after each Top Post result, inside <li>. |
| 421 |
* |
| 422 |
* @module widgets |
| 423 |
* |
| 424 |
* @since 3.2.0 |
| 425 |
* |
| 426 |
* @param string $post['post_id'] Post ID. |
| 427 |
*/ |
| 428 |
do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
| 429 |
?> |
| 430 |
</div> |
| 431 |
<?php |
| 432 |
endforeach; |
| 433 |
echo "</div>\n"; |
| 434 |
} else { |
| 435 |
echo "<ul class='widgets-list-layout no-grav'>\n"; |
| 436 |
foreach ( $posts as $post ) : |
| 437 |
?> |
| 438 |
<li> |
| 439 |
<?php |
| 440 |
/** This action is documented in modules/widgets/top-posts.php */ |
| 441 |
do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
| 442 |
|
| 443 |
/** This filter is documented in modules/widgets/top-posts.php */ |
| 444 |
$filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
| 445 |
|
| 446 |
printf( |
| 447 |
'<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s> |
| 448 |
<img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true" class="widgets-list-layout-blavatar"/> |
| 449 |
</a> |
| 450 |
<div class="widgets-list-layout-links"> |
| 451 |
<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s>%7$s</a> |
| 452 |
</div> |
| 453 |
', |
| 454 |
esc_url( $filtered_permalink ), |
| 455 |
esc_attr( wp_kses( $post['title'], array() ) ), |
| 456 |
( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
| 457 |
absint( $width ), |
| 458 |
absint( $height ), |
| 459 |
esc_url( $post['image'] ), |
| 460 |
esc_html( wp_kses( $post['title'], array() ) ) |
| 461 |
); |
| 462 |
|
| 463 |
/** This action is documented in modules/widgets/top-posts.php */ |
| 464 |
do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
| 465 |
?> |
| 466 |
</li> |
| 467 |
<?php |
| 468 |
endforeach; |
| 469 |
echo "</ul>\n"; |
| 470 |
} |
| 471 |
break; |
| 472 |
default: |
| 473 |
echo '<ul>'; |
| 474 |
foreach ( $posts as $post ) : |
| 475 |
?> |
| 476 |
<li> |
| 477 |
<?php |
| 478 |
/** This action is documented in modules/widgets/top-posts.php */ |
| 479 |
do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
| 480 |
|
| 481 |
/** This filter is documented in modules/widgets/top-posts.php */ |
| 482 |
$filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
| 483 |
|
| 484 |
printf( |
| 485 |
'<a href="%1$s" class="bump-view" data-bump-view="tp"%2$s>%3$s</a>', |
| 486 |
esc_url( $filtered_permalink ), |
| 487 |
( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
| 488 |
esc_html( wp_kses( $post['title'], array() ) ) |
| 489 |
); |
| 490 |
|
| 491 |
/** This action is documented in modules/widgets/top-posts.php */ |
| 492 |
do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
| 493 |
?> |
| 494 |
</li> |
| 495 |
<?php |
| 496 |
endforeach; |
| 497 |
echo '</ul>'; |
| 498 |
} |
| 499 |
|
| 500 |
echo $args['after_widget']; |
| 501 |
} |
| 502 |
|
| 503 |
public static function defaults() { |
| 504 |
return array( |
| 505 |
'title' => esc_html__( 'Top Posts & Pages', 'jetpack' ), |
| 506 |
'count' => absint( 10 ), |
| 507 |
'types' => array( 'post', 'page' ), |
| 508 |
'ordering' => 'views', |
| 509 |
'display' => 'text', |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
* Get most liked posts |
| 515 |
* |
| 516 |
* ONLY TO BE USED IN WPCOM |
| 517 |
*/ |
| 518 |
function get_by_likes( $count ) { |
| 519 |
$post_likes = wpl_get_blogs_most_liked_posts(); |
| 520 |
if ( ! $post_likes ) { |
| 521 |
return array(); |
| 522 |
} |
| 523 |
|
| 524 |
return $this->get_posts( array_keys( $post_likes ), $count ); |
| 525 |
} |
| 526 |
|
| 527 |
function get_by_views( $count, $args ) { |
| 528 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 529 |
global $wpdb; |
| 530 |
|
| 531 |
$post_views = wp_cache_get( "get_top_posts_$count", 'stats' ); |
| 532 |
if ( false === $post_views ) { |
| 533 |
$post_views = array_shift( stats_get_daily_history( false, get_current_blog_id(), 'postviews', 'post_id', false, 2, '', $count * 2 + 10, true ) ); |
| 534 |
unset( $post_views[0] ); |
| 535 |
wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200 ); |
| 536 |
} |
| 537 |
|
| 538 |
return $this->get_posts( array_keys( $post_views ), $count ); |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* Filter the number of days used to calculate Top Posts for the Top Posts widget. |
| 543 |
* We do not recommend accessing more than 10 days of results at one. |
| 544 |
* When more than 10 days of results are accessed at once, results should be cached via the WordPress transients API. |
| 545 |
* Querying for -1 days will give results for an infinite number of days. |
| 546 |
* |
| 547 |
* @module widgets |
| 548 |
* |
| 549 |
* @since 3.9.3 |
| 550 |
* |
| 551 |
* @param int 2 Number of days. Default is 2. |
| 552 |
* @param array $args The widget arguments. |
| 553 |
*/ |
| 554 |
$days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args ); |
| 555 |
|
| 556 |
/** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */ |
| 557 |
if ( 0 == $days || false == $days ) { |
| 558 |
$days = 2; |
| 559 |
} |
| 560 |
|
| 561 |
$post_view_posts = stats_get_from_restapi( array(), 'top-posts?max=11&summarize=1&num=' . intval( $days ) ); |
| 562 |
|
| 563 |
if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) { |
| 564 |
return array(); |
| 565 |
} |
| 566 |
|
| 567 |
$post_view_ids = array_filter( wp_list_pluck( $post_view_posts->summary->postviews, 'id' ) ); |
| 568 |
|
| 569 |
if ( ! $post_view_ids ) { |
| 570 |
return array(); |
| 571 |
} |
| 572 |
|
| 573 |
return $this->get_posts( $post_view_ids, $count ); |
| 574 |
} |
| 575 |
|
| 576 |
function get_fallback_posts() { |
| 577 |
if ( current_user_can( 'edit_theme_options' ) ) { |
| 578 |
return array(); |
| 579 |
} |
| 580 |
|
| 581 |
$post_query = new WP_Query; |
| 582 |
|
| 583 |
$posts = $post_query->query( |
| 584 |
array( |
| 585 |
'posts_per_page' => 1, |
| 586 |
'post_status' => 'publish', |
| 587 |
'post_type' => array( 'post', 'page' ), |
| 588 |
'no_found_rows' => true, |
| 589 |
) |
| 590 |
); |
| 591 |
|
| 592 |
if ( ! $posts ) { |
| 593 |
return array(); |
| 594 |
} |
| 595 |
|
| 596 |
$post = array_pop( $posts ); |
| 597 |
|
| 598 |
return $this->get_posts( $post->ID, 1 ); |
| 599 |
} |
| 600 |
|
| 601 |
function get_posts( $post_ids, $count ) { |
| 602 |
$counter = 0; |
| 603 |
|
| 604 |
$posts = array(); |
| 605 |
foreach ( (array) $post_ids as $post_id ) { |
| 606 |
$post = get_post( $post_id ); |
| 607 |
|
| 608 |
if ( ! $post ) { |
| 609 |
continue; |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Attachment pages use the 'inherit' post status by default. |
| 614 |
* To be able to remove attachment pages from private and password protect posts, |
| 615 |
* we need to replace their post status by the parent post' status. |
| 616 |
*/ |
| 617 |
if ( 'inherit' == $post->post_status && 'attachment' == $post->post_type ) { |
| 618 |
$post->post_status = get_post_status( $post_id ); |
| 619 |
} |
| 620 |
|
| 621 |
// hide private and password protected posts |
| 622 |
if ( 'publish' != $post->post_status || ! empty( $post->post_password ) ) { |
| 623 |
continue; |
| 624 |
} |
| 625 |
|
| 626 |
// Both get HTML stripped etc on display |
| 627 |
if ( empty( $post->post_title ) ) { |
| 628 |
$title_source = $post->post_content; |
| 629 |
$title = wp_html_excerpt( $title_source, 50 ); |
| 630 |
$title .= '…'; |
| 631 |
} else { |
| 632 |
$title = $post->post_title; |
| 633 |
} |
| 634 |
|
| 635 |
$permalink = get_permalink( $post->ID ); |
| 636 |
|
| 637 |
$post_type = $post->post_type; |
| 638 |
|
| 639 |
$posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' ); |
| 640 |
$counter++; |
| 641 |
|
| 642 |
if ( $counter == $count ) { |
| 643 |
break; // only need to load and show x number of likes |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* Filter the Top Posts and Pages. |
| 649 |
* |
| 650 |
* @module widgets |
| 651 |
* |
| 652 |
* @since 3.0.0 |
| 653 |
* |
| 654 |
* @param array $posts Array of the most popular posts. |
| 655 |
* @param array $post_ids Array of Post IDs. |
| 656 |
* @param string $count Number of Top Posts we want to display. |
| 657 |
*/ |
| 658 |
return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count ); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* Create a shortcode to display the widget anywhere. |
| 664 |
* |
| 665 |
* @since 3.9.2 |
| 666 |
*/ |
| 667 |
function jetpack_do_top_posts_widget( $instance ) { |
| 668 |
// Post Types can't be entered as an array in the shortcode parameters. |
| 669 |
if ( isset( $instance['types'] ) && is_array( $instance['types'] ) ) { |
| 670 |
$instance['types'] = implode( ',', $instance['types'] ); |
| 671 |
} |
| 672 |
|
| 673 |
$instance = shortcode_atts( |
| 674 |
Jetpack_Top_Posts_Widget::defaults(), |
| 675 |
$instance, |
| 676 |
'jetpack_top_posts_widget' |
| 677 |
); |
| 678 |
|
| 679 |
// Add a class to allow styling |
| 680 |
$args = array( |
| 681 |
'before_widget' => sprintf( '<div class="%s">', 'jetpack_top_posts_widget' ), |
| 682 |
); |
| 683 |
|
| 684 |
ob_start(); |
| 685 |
the_widget( 'Jetpack_Top_Posts_Widget', $instance, $args ); |
| 686 |
$output = ob_get_clean(); |
| 687 |
|
| 688 |
return $output; |
| 689 |
} |
| 690 |
add_shortcode( 'jetpack_top_posts_widget', 'jetpack_do_top_posts_widget' ); |
| 691 |
|