Jetpack_Top_Posts_Widget

The Jetpack by WordPress.com Jetpack Top Posts Widget class.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/top-posts.php  
  1. class Jetpack_Top_Posts_Widget extends WP_Widget { 
  2. public $alt_option_name = 'widget_stats_topposts'; 
  3. public $default_title = ''; 
  4.  
  5. function __construct() { 
  6. parent::__construct( 
  7. 'top-posts',  
  8. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  9. apply_filters( 'jetpack_widget_name', __( 'Top Posts & Pages', 'jetpack' ) ),  
  10. array( 
  11. 'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ),  
  12. ); 
  13.  
  14. $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); 
  15.  
  16. if ( is_active_widget( false, false, $this->id_base ) ) { 
  17. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); 
  18.  
  19. function enqueue_style() { 
  20. wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' ); 
  21. wp_enqueue_style( 'jetpack-top-posts-widget' ); 
  22.  
  23. function form( $instance ) { 
  24. $title = isset( $instance['title' ] ) ? $instance['title'] : false; 
  25. if ( false === $title ) { 
  26. $title = $this->default_title; 
  27.  
  28. $count = isset( $instance['count'] ) ? (int) $instance['count'] : 10; 
  29. if ( $count < 1 || 10 < $count ) { 
  30. $count = 10; 
  31.  
  32. $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) ); 
  33. $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); 
  34.  
  35. if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { 
  36. $display = $instance['display']; 
  37. } else { 
  38. $display = 'text'; 
  39.  
  40. ?> 
  41.  
  42. <p> 
  43. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> 
  44. <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 ); ?>" /> 
  45. </p> 
  46.  
  47. <p> 
  48. <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> 
  49. <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" /> 
  50. </p> 
  51.  
  52. <p> 
  53. <label for="<?php echo $this->get_field_id( 'types' ); ?>"><?php esc_html_e( 'Types of pages to display:', 'jetpack' ); ?></label> 
  54. <ul> 
  55. <?php foreach( $allowed_post_types as $type ) { 
  56. // Get the Post Type name to display next to the checkbox 
  57. $post_type_object = get_post_type_object( $type ); 
  58. $label = $post_type_object->labels->name; 
  59.  
  60. $checked = ''; 
  61. if ( in_array( $type, $types ) ) { 
  62. $checked = 'checked="checked" '; 
  63. } ?> 
  64.  
  65. <li><label> 
  66. <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; ?>> 
  67. <?php echo esc_html( $label ); ?> 
  68. </label></li> 
  69.  
  70. <?php } // End foreach ?> 
  71. </ul> 
  72. </p> 
  73.  
  74. <p> 
  75. <label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label> 
  76. <ul> 
  77. <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> 
  78. <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> 
  79. <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> 
  80. </ul> 
  81. </p> 
  82.  
  83. <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> 
  84.  
  85. <?php 
  86.  
  87. function update( $new_instance, $old_instance ) { 
  88. $instance = array(); 
  89. $instance['title'] = wp_kses( $new_instance['title'], array() ); 
  90. if ( $instance['title'] === $this->default_title ) { 
  91. $instance['title'] = false; // Store as false in case of language change 
  92.  
  93. $instance['count'] = (int) $new_instance['count']; 
  94. if ( $instance['count'] < 1 || 10 < $instance['count'] ) { 
  95. $instance['count'] = 10; 
  96.  
  97. $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) ); 
  98. $instance['types'] = $new_instance['types']; 
  99. foreach( $new_instance['types'] as $key => $type ) { 
  100. if ( ! in_array( $type, $allowed_post_types ) ) { 
  101. unset( $new_instance['types'][ $key ] ); 
  102.  
  103. if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text' ) ) ) { 
  104. $instance['display'] = $new_instance['display']; 
  105. } else { 
  106. $instance['display'] = 'text'; 
  107.  
  108. return $instance; 
  109.  
  110. function widget( $args, $instance ) { 
  111. $title = isset( $instance['title' ] ) ? $instance['title'] : false; 
  112. if ( false === $title ) { 
  113. $title = $this->default_title; 
  114. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  115. $title = apply_filters( 'widget_title', $title ); 
  116.  
  117. $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; 
  118. if ( $count < 1 || 10 < $count ) { 
  119. $count = 10; 
  120. /** 
  121. * Control the number of displayed posts. 
  122. * @module widgets 
  123. * @since 3.3.0 
  124. * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. 
  125. */ 
  126. $count = apply_filters( 'jetpack_top_posts_widget_count', $count ); 
  127.  
  128. $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); 
  129.  
  130. if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { 
  131. $display = $instance['display']; 
  132. } else { 
  133. $display = 'text'; 
  134.  
  135. if ( 'text' != $display ) { 
  136. $get_image_options = array( 
  137. 'fallback_to_avatars' => true,  
  138. /** This filter is documented in modules/shortcodes/audio.php */ 
  139. 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'http://en.wordpress.com/i/logo/white-gray-80.png' ) ),  
  140. ); 
  141. if ( 'grid' == $display ) { 
  142. $get_image_options['avatar_size'] = 200; 
  143. } else { 
  144. $get_image_options['avatar_size'] = 40; 
  145. /** 
  146. * Top Posts Widget Image options. 
  147. * @module widgets 
  148. * @since 1.8.0 
  149. * @param array $get_image_options { 
  150. * Array of Image options. 
  151. * @type bool true Should we default to Gravatars when no image is found? Default is true. 
  152. * @type string $gravatar_default Default Image URL if no Gravatar is found. 
  153. * @type int $avatar_size Default Image size. 
  154. * } 
  155. */ 
  156. $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); 
  157.  
  158. $posts = $this->get_by_views( $count ); 
  159.  
  160. // Filter the returned posts. Remove all posts that do not match the chosen Post Types. 
  161. if ( isset( $types ) ) { 
  162. foreach ( $posts as $k => $post ) { 
  163. if ( ! in_array( $post['post_type'], $types ) ) { 
  164. unset( $posts[$k] ); 
  165.  
  166. if ( ! $posts ) { 
  167. $posts = $this->get_fallback_posts(); 
  168.  
  169. echo $args['before_widget']; 
  170. if ( ! empty( $title ) ) 
  171. echo $args['before_title'] . $title . $args['after_title']; 
  172.  
  173. if ( ! $posts ) { 
  174. if ( current_user_can( 'edit_theme_options' ) ) { 
  175. echo '<p>' . sprintf( 
  176. __( 'There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack' ),  
  177. 'http://en.support.wordpress.com/getting-more-site-traffic/' 
  178. ) . '</p>'; 
  179.  
  180. echo $args['after_widget']; 
  181. return; 
  182.  
  183. switch ( $display ) { 
  184. case 'list' : 
  185. case 'grid' : 
  186. wp_enqueue_style( 'widget-grid-and-list' ); 
  187. foreach ( $posts as &$post ) { 
  188. $image = Jetpack_PostImages::get_image( $post['post_id'], array( 'fallback_to_avatars' => true ) ); 
  189. $post['image'] = $image['src']; 
  190. if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { 
  191. $size = (int) $get_image_options['avatar_size']; 
  192. $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$size, $size" ) ); 
  193.  
  194. unset( $post ); 
  195.  
  196. if ( 'grid' == $display ) { 
  197. echo "<div class='widgets-grid-layout no-grav'>\n"; 
  198. foreach ( $posts as $post ) : 
  199. ?> 
  200. <div class="widget-grid-view-image"> 
  201. <?php 
  202. /** 
  203. * Fires before each Top Post result, inside <li>. 
  204. * @module widgets 
  205. * @since 3.2.0 
  206. * @param string $post['post_id'] Post ID. 
  207. */ 
  208. do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); 
  209. ?> 
  210. <a href="<?php echo esc_url( $post['permalink'] ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp"> 
  211. <img src="<?php echo esc_url( $post['image'] ); ?>" alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" /> 
  212. </a> 
  213. <?php 
  214. /** 
  215. * Fires after each Top Post result, inside <li>. 
  216. * @module widgets 
  217. * @since 3.2.0 
  218. * @param string $post['post_id'] Post ID. 
  219. */ 
  220. do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); 
  221. ?> 
  222. </div> 
  223. <?php 
  224. endforeach; 
  225. echo "</div>\n"; 
  226. } else { 
  227. echo "<ul class='widgets-list-layout no-grav'>\n"; 
  228. foreach ( $posts as $post ) : 
  229. ?> 
  230. <li> 
  231. <?php 
  232. /** This action is documented in modules/widgets/top-posts.php */ 
  233. do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); 
  234. ?> 
  235. <a href="<?php echo esc_url( $post['permalink'] ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp"> 
  236. <img src="<?php echo esc_url( $post['image'] ); ?>" class='widgets-list-layout-blavatar' alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" /> 
  237. </a> 
  238. <div class="widgets-list-layout-links"> 
  239. <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> 
  240. <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> 
  241. </a> 
  242. </div> 
  243. <?php 
  244. /** This action is documented in modules/widgets/top-posts.php */ 
  245. do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); 
  246. ?> 
  247. </li> 
  248. <?php 
  249. endforeach; 
  250. echo "</ul>\n"; 
  251. break; 
  252. default : 
  253. echo '<ul>'; 
  254. foreach ( $posts as $post ) : 
  255. ?> 
  256. <li> 
  257. <?php 
  258. /** This action is documented in modules/widgets/top-posts.php */ 
  259. do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); 
  260. ?> 
  261. <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> 
  262. <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> 
  263. </a> 
  264. <?php 
  265. /** This action is documented in modules/widgets/top-posts.php */ 
  266. do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); 
  267. ?> 
  268. </li> 
  269. <?php 
  270. endforeach; 
  271. echo '</ul>'; 
  272.  
  273. echo $args['after_widget']; 
  274.  
  275. function get_by_views( $count ) { 
  276. /** 
  277. * Filter the number of days used to calculate Top Posts for the Top Posts widget. 
  278. * @module widgets 
  279. * @since 2.8.0 
  280. * @param int 2 Number of days. Default is 2. 
  281. */ 
  282. $days = (int) apply_filters( 'jetpack_top_posts_days', 2 ); 
  283.  
  284. if ( $days < 1 ) { 
  285. $days = 2; 
  286.  
  287. if ( $days > 10 ) { 
  288. $days = 10; 
  289.  
  290. $post_view_posts = stats_get_csv( 'postviews', array( 'days' => absint( $days ), 'limit' => 11 ) ); 
  291. if ( ! $post_view_posts ) { 
  292. return array(); 
  293.  
  294. $post_view_ids = array_filter( wp_list_pluck( $post_view_posts, 'post_id' ) ); 
  295. if ( ! $post_view_ids ) { 
  296. return array(); 
  297.  
  298. return $this->get_posts( $post_view_ids, $count ); 
  299.  
  300. function get_fallback_posts() { 
  301. if ( current_user_can( 'edit_theme_options' ) ) { 
  302. return array(); 
  303.  
  304. $post_query = new WP_Query; 
  305.  
  306. $posts = $post_query->query( array( 
  307. 'posts_per_page' => 1,  
  308. 'post_status' => 'publish',  
  309. 'post_type' => array( 'post', 'page' ),  
  310. 'no_found_rows' => true,  
  311. ) ); 
  312.  
  313. if ( ! $posts ) { 
  314. return array(); 
  315.  
  316. $post = array_pop( $posts ); 
  317.  
  318. return $this->get_posts( $post->ID, 1 ); 
  319.  
  320. function get_posts( $post_ids, $count ) { 
  321. $counter = 0; 
  322.  
  323. $posts = array(); 
  324. foreach ( (array) $post_ids as $post_id ) { 
  325. $post = get_post( $post_id ); 
  326.  
  327. if ( ! $post ) 
  328. continue; 
  329.  
  330. // hide private and password protected posts 
  331. if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $post->ID ) ) 
  332. continue; 
  333.  
  334. // Both get HTML stripped etc on display 
  335. if ( empty( $post->post_title ) ) { 
  336. $title_source = $post->post_content; 
  337. $title = wp_html_excerpt( $title_source, 50 ); 
  338. $title .= '…'; 
  339. } else { 
  340. $title = $post->post_title; 
  341.  
  342. $permalink = get_permalink( $post->ID ); 
  343.  
  344. $post_type = $post->post_type; 
  345.  
  346. $posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' ); 
  347. $counter++; 
  348.  
  349. if ( $counter == $count ) { 
  350. break; // only need to load and show x number of likes 
  351.  
  352. /** 
  353. * Filter the Top Posts and Pages. 
  354. * @module widgets 
  355. * @since 3.0.0 
  356. * @param array $posts Array of the most popular posts. 
  357. * @param array $post_ids Array of Post IDs. 
  358. * @param string $count Number of Top Posts we want to display. 
  359. */ 
  360. return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count );