Completed
Push — add/blog-stats-widget ( 53b823 )
by Jeremy
118:31 queued 105:53
created

Jetpack_Blog_Stats_Widget::widget()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 3
Ratio 10 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 4
nop 2
dl 3
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Blog Stats Widget.
4
 *
5
 * @since 4.5.0
6
 *
7
 * @package 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 View Code Duplication
	function __construct() {
30
		$widget_ops = array(
31
			'classname' => 'blog-stats',
32
			'description' => __( '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', __( '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' => __( 'Blog Stats', 'jetpack' ),
54
			'hits'  => __( 'hits', 'jetpack' ),
55
		);
56
	}
57
58
	/**
59
	 * Return All Time Stats for that blog.
60
	 *
61
	 * We query the WordPress.com Stats REST API endpoint.
62
	 *
63
	 * @uses stats_get_from_restapi(). That function caches data locally for 5 minutes.
64
	 *
65
	 * @return string|false $views All Time Stats for that blog.
66
	 */
67
	public function get_stats() {
68
		// Get data from the WordPress.com Stats REST API endpoint.
69
		$stats = stats_get_from_restapi( array( 'fields' => 'stats' ) );
70
71
		if (
72
			isset( $stats )
73
			&& ! empty( $stats )
74
			&& isset( $stats->stats->views )
75
		) {
76
			return $stats->stats->views;
77
		} else {
78
			return false;
79
		}
80
	}
81
82
	/**
83
	 * Back-end widget form.
84
	 *
85
	 * @see WP_Widget::form()
86
	 *
87
	 * @param array $instance Previously saved values from database.
88
	 *
89
	 * @return void
90
	 */
91
	function form( $instance ) {
92
		$instance = wp_parse_args( $instance, $this->defaults() );
93
		?>
94
95
		<p>
96
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
97
			<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'] ); ?>" />
98
		</p>
99
		<p>
100
			<label for="<?php echo esc_attr( $this->get_field_id( 'hits' ) ); ?>"><?php echo number_format_i18n( '12,345' ); ?></label>
101
			<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'] ); ?>" />
102
		</p>
103
		<p><?php esc_html_e( 'Hit counter is delayed by up to 60 seconds.', 'jetpack' ); ?></p>
104
105
		<?php
106
	}
107
108
	/**
109
	 * Sanitize widget form values as they are saved.
110
	 *
111
	 * @see WP_Widget::update()
112
	 *
113
	 * @param array $new_instance Values just sent to be saved.
114
	 * @param array $old_instance Previously saved values from database.
115
	 *
116
	 * @return array Updated safe values to be saved.
117
	 */
118
	function update( $new_instance, $old_instance ) {
119
		$instance          = array();
120
		$instance['title'] = wp_kses( $new_instance['title'], array() );
121
		$instance['hits']  = wp_kses( $new_instance['hits'], array() );
122
123
		return $instance;
124
	}
125
126
	/**
127
	 * Front-end display of widget.
128
	 *
129
	 * @see WP_Widget::widget()
130
	 *
131
	 * @param array $args     Widget arguments.
132
	 * @param array $instance Saved values from database.
133
	 */
134
	function widget( $args, $instance ) {
135
		$instance = wp_parse_args( $instance, $this->defaults() );
136
137
		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
138
		$title = apply_filters( 'widget_title', $instance['title'] );
139
140
		echo $args['before_widget'];
141
142 View Code Duplication
		if ( ! empty( $title ) ) {
143
			echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
144
		}
145
146
		// Get the Site Stats.
147
		$views = $this->get_stats();
148
149
		if ( isset( $views ) && ! empty( $views ) ) {
150
			printf(
151
				'<ul><li>$1%s $2%s</li></ul>',
152
				number_format_i18n( $views ),
153
				isset( $instance['hits'] ) ? esc_html( $instance['hits'] ) : ''
154
			);
155
		} else {
156
			esc_html_e( 'No hits.', 'jetpack' );
157
		}
158
159
		echo $args['after_widget'];
160
161
		/** This action is already documented in modules/widgets/gravatar-profile.php */
162
		do_action( 'jetpack_stats_extra', 'widget_view', 'blog_stats' );
163
	}
164
}
165
166
/**
167
 * If the Stats module is active in a recent version of Jetpack, register the widget.
168
 *
169
 * @since 4.5.0
170
 */
171
function jetpack_blog_stats_widget_init() {
172
	if ( ! function_exists( 'stats_get_from_restapi' ) ) {
173
		return;
174
	}
175
	register_widget( 'Jetpack_Blog_Stats_Widget' );
176
}
177
add_action( 'widgets_init', 'jetpack_blog_stats_widget_init' );
178