Nessuna descrizione

top-posts.php 24KB

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