/modules/widgets/top-posts.php

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