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