blog-stats.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Blog Stats Widget.
  4. *
  5. * @since 4.5.0
  6. *
  7. * @package Jetpack
  8. */
  9. /**
  10. * Disable direct access/execution to/of the widget code.
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * Blog Stats Widget.
  17. *
  18. * Displays all time stats for that site.
  19. *
  20. * @since 4.5.0
  21. */
  22. class Jetpack_Blog_Stats_Widget extends WP_Widget {
  23. /**
  24. * Constructor
  25. */
  26. function __construct() {
  27. $widget_ops = array(
  28. 'classname' => 'blog-stats',
  29. 'description' => esc_html__( 'Show a hit counter for your blog.', 'jetpack' ),
  30. 'customize_selective_refresh' => true,
  31. );
  32. parent::__construct(
  33. 'blog-stats',
  34. /** This filter is documented in modules/widgets/facebook-likebox.php */
  35. apply_filters( 'jetpack_widget_name', esc_html__( 'Blog Stats', 'jetpack' ) ),
  36. $widget_ops
  37. );
  38. $this->alt_option_name = 'widget_statscounter';
  39. }
  40. /**
  41. * Return an associative array of default values
  42. *
  43. * These values are used in new widgets.
  44. *
  45. * @return array Array of default values for the Widget's options
  46. */
  47. public function defaults() {
  48. return array(
  49. 'title' => esc_html__( 'Blog Stats', 'jetpack' ),
  50. /* Translators: Number of views, plural */
  51. 'hits' => esc_html__( 'hits', 'jetpack' ),
  52. );
  53. }
  54. /**
  55. * Return All Time Stats for that blog.
  56. *
  57. * We query the WordPress.com Stats REST API endpoint.
  58. *
  59. * @uses stats_get_from_restapi(). That function caches data locally for 5 minutes.
  60. *
  61. * @return string|false $views All Time Stats for that blog.
  62. */
  63. public function get_stats() {
  64. // Get data from the WordPress.com Stats REST API endpoint.
  65. $stats = stats_get_from_restapi( array( 'fields' => 'stats' ) );
  66. if ( isset( $stats->stats->views ) ) {
  67. return $stats->stats->views;
  68. } else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * Back end widget form.
  74. *
  75. * @see WP_Widget::form()
  76. *
  77. * @param array $instance Previously saved values from database.
  78. *
  79. * @return void
  80. */
  81. function form( $instance ) {
  82. $instance = wp_parse_args( $instance, $this->defaults() );
  83. ?>
  84. <p>
  85. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
  86. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  87. </p>
  88. <p>
  89. <label for="<?php echo esc_attr( $this->get_field_id( 'hits' ) ); ?>"><?php echo number_format_i18n( '12345' ); ?></label>
  90. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'hits' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hits' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['hits'] ); ?>" />
  91. </p>
  92. <p><?php esc_html_e( 'Hit counter is delayed by up to 60 seconds.', 'jetpack' ); ?></p>
  93. <?php
  94. }
  95. /**
  96. * Sanitize widget form values as they are saved.
  97. *
  98. * @see WP_Widget::update()
  99. *
  100. * @param array $new_instance Values just sent to be saved.
  101. * @param array $old_instance Previously saved values from database.
  102. *
  103. * @return array Updated safe values to be saved.
  104. */
  105. function update( $new_instance, $old_instance ) {
  106. $instance = array();
  107. $instance['title'] = wp_kses( $new_instance['title'], array() );
  108. $instance['hits'] = wp_kses( $new_instance['hits'], array() );
  109. return $instance;
  110. }
  111. /**
  112. * Front-end display of widget.
  113. *
  114. * @see WP_Widget::widget()
  115. *
  116. * @param array $args Widget arguments.
  117. * @param array $instance Saved values from database.
  118. */
  119. function widget( $args, $instance ) {
  120. $instance = wp_parse_args( $instance, $this->defaults() );
  121. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  122. $title = apply_filters( 'widget_title', $instance['title'] );
  123. echo $args['before_widget'];
  124. if ( ! empty( $title ) ) {
  125. echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
  126. }
  127. // Get the Site Stats.
  128. $views = $this->get_stats();
  129. if ( ! empty( $views ) ) {
  130. printf(
  131. '<ul><li>%1$s %2$s</li></ul>',
  132. number_format_i18n( $views ),
  133. isset( $instance['hits'] ) ? esc_html( $instance['hits'] ) : ''
  134. );
  135. } else {
  136. esc_html_e( 'No hits.', 'jetpack' );
  137. }
  138. echo $args['after_widget'];
  139. /** This action is already documented in modules/widgets/gravatar-profile.php */
  140. do_action( 'jetpack_stats_extra', 'widget_view', 'blog_stats' );
  141. }
  142. }
  143. /**
  144. * If the Stats module is active in a recent version of Jetpack, register the widget.
  145. *
  146. * @since 4.5.0
  147. */
  148. function jetpack_blog_stats_widget_init() {
  149. if ( function_exists( 'stats_get_from_restapi' ) ) {
  150. register_widget( 'Jetpack_Blog_Stats_Widget' );
  151. }
  152. }
  153. add_action( 'widgets_init', 'jetpack_blog_stats_widget_init' );