Jetpack_Blog_Stats_Widget

Blog Stats Widget.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/blog-stats.php  
  1. class Jetpack_Blog_Stats_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Constructor 
  5. */ 
  6. function __construct() { 
  7. $widget_ops = array( 
  8. 'classname' => 'blog-stats',  
  9. 'description' => esc_html__( 'Show a hit counter for your blog.', 'jetpack' ),  
  10. 'customize_selective_refresh' => true,  
  11. ); 
  12. parent::__construct( 
  13. 'blog-stats',  
  14. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  15. apply_filters( 'jetpack_widget_name', esc_html__( 'Blog Stats', 'jetpack' ) ),  
  16. $widget_ops 
  17. ); 
  18. $this->alt_option_name = 'widget_statscounter'; 
  19.  
  20. /** 
  21. * Return an associative array of default values 
  22. * These values are used in new widgets. 
  23. * @return array Array of default values for the Widget's options 
  24. */ 
  25. public function defaults() { 
  26. return array( 
  27. 'title' => esc_html__( 'Blog Stats', 'jetpack' ),  
  28. /** Translators: Number of views, plural */ 
  29. 'hits' => esc_html__( 'hits', 'jetpack' ),  
  30. ); 
  31.  
  32. /** 
  33. * Return All Time Stats for that blog. 
  34. * We query the WordPress.com Stats REST API endpoint. 
  35. * @uses stats_get_from_restapi(). That function caches data locally for 5 minutes. 
  36. * @return string|false $views All Time Stats for that blog. 
  37. */ 
  38. public function get_stats() { 
  39. // Get data from the WordPress.com Stats REST API endpoint. 
  40. $stats = stats_get_from_restapi( array( 'fields' => 'stats' ) ); 
  41.  
  42. if ( isset( $stats->stats->views ) ) { 
  43. return $stats->stats->views; 
  44. } else { 
  45. return false; 
  46.  
  47. /** 
  48. * Back end widget form. 
  49. * @see WP_Widget::form() 
  50. * @param array $instance Previously saved values from database. 
  51. * @return void 
  52. */ 
  53. function form( $instance ) { 
  54. $instance = wp_parse_args( $instance, $this->defaults() ); 
  55. ?> 
  56.  
  57. <p> 
  58. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> 
  59. <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'] ); ?>" /> 
  60. </p> 
  61. <p> 
  62. <label for="<?php echo esc_attr( $this->get_field_id( 'hits' ) ); ?>"><?php echo number_format_i18n( '12345' ); ?></label> 
  63. <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'] ); ?>" /> 
  64. </p> 
  65. <p><?php esc_html_e( 'Hit counter is delayed by up to 60 seconds.', 'jetpack' ); ?></p> 
  66.  
  67. <?php 
  68.  
  69. /** 
  70. * Sanitize widget form values as they are saved. 
  71. * @see WP_Widget::update() 
  72. * @param array $new_instance Values just sent to be saved. 
  73. * @param array $old_instance Previously saved values from database. 
  74. * @return array Updated safe values to be saved. 
  75. */ 
  76. function update( $new_instance, $old_instance ) { 
  77. $instance = array(); 
  78. $instance['title'] = wp_kses( $new_instance['title'], array() ); 
  79. $instance['hits'] = wp_kses( $new_instance['hits'], array() ); 
  80.  
  81. return $instance; 
  82.  
  83. /** 
  84. * Front-end display of widget. 
  85. * @see WP_Widget::widget() 
  86. * @param array $args Widget arguments. 
  87. * @param array $instance Saved values from database. 
  88. */ 
  89. function widget( $args, $instance ) { 
  90. $instance = wp_parse_args( $instance, $this->defaults() ); 
  91.  
  92. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  93. $title = apply_filters( 'widget_title', $instance['title'] ); 
  94.  
  95. echo $args['before_widget']; 
  96.  
  97. if ( ! empty( $title ) ) { 
  98. echo $args['before_title'] . esc_html( $title ) . $args['after_title']; 
  99.  
  100. // Get the Site Stats. 
  101. $views = $this->get_stats(); 
  102.  
  103. if ( ! empty( $views ) ) { 
  104. printf( 
  105. '<ul><li>%1$s %2$s</li></ul>',  
  106. number_format_i18n( $views ),  
  107. isset( $instance['hits'] ) ? esc_html( $instance['hits'] ) : '' 
  108. ); 
  109. } else { 
  110. esc_html_e( 'No hits.', 'jetpack' ); 
  111.  
  112. echo $args['after_widget']; 
  113.  
  114. /** This action is already documented in modules/widgets/gravatar-profile.php */ 
  115. do_action( 'jetpack_stats_extra', 'widget_view', 'blog_stats' );