PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/top-posts.php

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