top-posts.php 22 KB

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