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. * @since 3.3.0 
  123. * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. 
  124. */ 
  125. $count = apply_filters( 'jetpack_top_posts_widget_count', $count ); 
  126.  
  127. $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); 
  128.  
  129. if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { 
  130. $display = $instance['display']; 
  131. } else { 
  132. $display = 'text'; 
  133.  
  134. if ( 'text' != $display ) { 
  135. $get_image_options = array( 
  136. 'fallback_to_avatars' => true,  
  137. /** This filter is documented in modules/shortcodes/audio.php */ 
  138. 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'http://en.wordpress.com/i/logo/white-gray-80.png' ) ),  
  139. ); 
  140. if ( 'grid' == $display ) { 
  141. $get_image_options['avatar_size'] = 200; 
  142. } else { 
  143. $get_image_options['avatar_size'] = 40; 
  144. /** 
  145. * Top Posts Widget Image options. 
  146. * @since 1.8.0 
  147. * @param array $get_image_options { 
  148. * Array of Image options. 
  149. * @type bool true Should we default to Gravatars when no image is found? Default is true. 
  150. * @type string $gravatar_default Default Image URL if no Gravatar is found. 
  151. * @type int $avatar_size Default Image size. 
  152. * } 
  153. */ 
  154. $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); 
  155.  
  156. $posts = $this->get_by_views( $count ); 
  157.  
  158. // Filter the returned posts. Remove all posts that do not match the chosen Post Types. 
  159. if ( isset( $types ) ) { 
  160. foreach ( $posts as $k => $post ) { 
  161. if ( ! in_array( $post['post_type'], $types ) ) { 
  162. unset( $posts[$k] ); 
  163.  
  164. if ( ! $posts ) { 
  165. $posts = $this->get_fallback_posts(); 
  166.  
  167. echo $args['before_widget']; 
  168. if ( ! empty( $title ) ) 
  169. echo $args['before_title'] . $title . $args['after_title']; 
  170.  
  171. if ( ! $posts ) { 
  172. if ( current_user_can( 'edit_theme_options' ) ) { 
  173. echo '<p>' . sprintf( 
  174. __( 'There are no posts to display. <a href="%s">Want more traffic?</a>', 'jetpack' ),  
  175. 'http://en.support.wordpress.com/getting-more-site-traffic/' 
  176. ) . '</p>'; 
  177.  
  178. echo $args['after_widget']; 
  179. return; 
  180.  
  181. switch ( $display ) { 
  182. case 'list' : 
  183. case 'grid' : 
  184. wp_enqueue_style( 'widget-grid-and-list' ); 
  185. foreach ( $posts as &$post ) { 
  186. $image = Jetpack_PostImages::get_image( $post['post_id'], array( 'fallback_to_avatars' => true ) ); 
  187. $post['image'] = $image['src']; 
  188. if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { 
  189. $size = (int) $get_image_options['avatar_size']; 
  190. $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$size, $size" ) ); 
  191.  
  192. unset( $post ); 
  193.  
  194. if ( 'grid' == $display ) { 
  195. echo "<div class='widgets-grid-layout no-grav'>\n"; 
  196. foreach ( $posts as $post ) : 
  197. ?> 
  198. <div class="widget-grid-view-image"> 
  199. <?php 
  200. /** 
  201. * Fires before each Top Post result, inside <li>. 
  202. * @since 3.2.0 
  203. * @param string $post['post_id'] Post ID. 
  204. */ 
  205. do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); 
  206. ?> 
  207. <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"> 
  208. <img src="<?php echo esc_url( $post['image'] ); ?>" alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" /> 
  209. </a> 
  210. <?php 
  211. /** 
  212. * Fires after each Top Post result, inside <li>. 
  213. * @since 3.2.0 
  214. * @param string $post['post_id'] Post ID. 
  215. */ 
  216. do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); 
  217. ?> 
  218. </div> 
  219. <?php 
  220. endforeach; 
  221. echo "</div>\n"; 
  222. } else { 
  223. echo "<ul class='widgets-list-layout no-grav'>\n"; 
  224. foreach ( $posts as $post ) : 
  225. ?> 
  226. <li> 
  227. <?php 
  228. /** This action is documented in modules/widgets/top-posts.php */ 
  229. do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); 
  230. ?> 
  231. <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"> 
  232. <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" /> 
  233. </a> 
  234. <div class="widgets-list-layout-links"> 
  235. <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> 
  236. <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> 
  237. </a> 
  238. </div> 
  239. <?php 
  240. /** This action is documented in modules/widgets/top-posts.php */ 
  241. do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); 
  242. ?> 
  243. </li> 
  244. <?php 
  245. endforeach; 
  246. echo "</ul>\n"; 
  247. break; 
  248. default : 
  249. echo '<ul>'; 
  250. foreach ( $posts as $post ) : 
  251. ?> 
  252. <li> 
  253. <?php 
  254. /** This action is documented in modules/widgets/top-posts.php */ 
  255. do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); 
  256. ?> 
  257. <a href="<?php echo esc_url( $post['permalink'] ); ?>" class="bump-view" data-bump-view="tp"> 
  258. <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> 
  259. </a> 
  260. <?php 
  261. /** This action is documented in modules/widgets/top-posts.php */ 
  262. do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); 
  263. ?> 
  264. </li> 
  265. <?php 
  266. endforeach; 
  267. echo '</ul>'; 
  268.  
  269. echo $args['after_widget']; 
  270.  
  271. function get_by_views( $count ) { 
  272. /** 
  273. * Filter the number of days used to calculate Top Posts for the Top Posts widget. 
  274. * @since 2.8.0 
  275. * @param int 2 Number of days. Default is 2. 
  276. */ 
  277. $days = (int) apply_filters( 'jetpack_top_posts_days', 2 ); 
  278.  
  279. if ( $days < 1 ) { 
  280. $days = 2; 
  281.  
  282. if ( $days > 10 ) { 
  283. $days = 10; 
  284.  
  285. $post_view_posts = stats_get_csv( 'postviews', array( 'days' => absint( $days ), 'limit' => 11 ) ); 
  286. if ( ! $post_view_posts ) { 
  287. return array(); 
  288.  
  289. $post_view_ids = array_filter( wp_list_pluck( $post_view_posts, 'post_id' ) ); 
  290. if ( ! $post_view_ids ) { 
  291. return array(); 
  292.  
  293. return $this->get_posts( $post_view_ids, $count ); 
  294.  
  295. function get_fallback_posts() { 
  296. if ( current_user_can( 'edit_theme_options' ) ) { 
  297. return array(); 
  298.  
  299. $post_query = new WP_Query; 
  300.  
  301. $posts = $post_query->query( array( 
  302. 'posts_per_page' => 1,  
  303. 'post_status' => 'publish',  
  304. 'post_type' => array( 'post', 'page' ),  
  305. 'no_found_rows' => true,  
  306. ) ); 
  307.  
  308. if ( ! $posts ) { 
  309. return array(); 
  310.  
  311. $post = array_pop( $posts ); 
  312.  
  313. return $this->get_posts( $post->ID, 1 ); 
  314.  
  315. function get_posts( $post_ids, $count ) { 
  316. $counter = 0; 
  317.  
  318. $posts = array(); 
  319. foreach ( (array) $post_ids as $post_id ) { 
  320. $post = get_post( $post_id ); 
  321.  
  322. if ( ! $post ) 
  323. continue; 
  324.  
  325. // hide private and password protected posts 
  326. if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $post->ID ) ) 
  327. continue; 
  328.  
  329. // Both get HTML stripped etc on display 
  330. if ( empty( $post->post_title ) ) { 
  331. $title_source = $post->post_content; 
  332. $title = wp_html_excerpt( $title_source, 50 ); 
  333. $title .= '…'; 
  334. } else { 
  335. $title = $post->post_title; 
  336.  
  337. $permalink = get_permalink( $post->ID ); 
  338.  
  339. $post_type = $post->post_type; 
  340.  
  341. $posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' ); 
  342. $counter++; 
  343.  
  344. if ( $counter == $count ) { 
  345. break; // only need to load and show x number of likes 
  346.  
  347. /** 
  348. * Filter the Top Posts and Pages. 
  349. * @since 3.0.0 
  350. * @param array $posts Array of the most popular posts. 
  351. * @param array $post_ids Array of Post IDs. 
  352. * @param string $count Number of Top Posts we want to display. 
  353. */ 
  354. return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count );