PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.2
Post Views Counter v1.3.2
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / dashboard.php
post-views-counter / includes Last commit date
ajax.php 6 years ago columns.php 6 years ago counter.php 6 years ago crawler-detect.php 6 years ago cron.php 6 years ago dashboard.php 6 years ago frontend.php 6 years ago functions.php 6 years ago query.php 6 years ago settings.php 6 years ago update.php 6 years ago widgets.php 6 years ago
dashboard.php
429 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Dashboard class.
8 *
9 * @class Post_Views_Counter_Dashboard
10 */
11 class Post_Views_Counter_Dashboard {
12
13 public function __construct() {
14 // actions
15 add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) );
16 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_styles' ) );
17 add_action( 'wp_ajax_pvc_dashboard_chart', array( $this, 'dashboard_widget_chart' ) );
18 add_action( 'wp_ajax_pvc_dashboard_chart_user_post_types', array( $this, 'dashboard_widget_chart_user_post_types' ) );
19 }
20
21 /**
22 * Initialize widget.
23 */
24 public function wp_dashboard_setup() {
25 // filter user_can_see_stats
26 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) {
27 return;
28 }
29
30 // add dashboard widget
31 wp_add_dashboard_widget( 'pvc_dashboard', __( 'Post Views', 'post-views-counter' ), array( $this, 'dashboard_widget' ) );
32 }
33
34 /**
35 * Render dashboard widget.
36 *
37 * @return mixed
38 */
39 public function dashboard_widget() {
40 ?>
41 <div id="pvc_dashboard_container">
42 <canvas id="pvc_chart" height="175"></canvas>
43 <div class="pvc_months">
44
45 <?php echo $this->generate_months( current_time( 'timestamp', false ) ); ?>
46
47 </div>
48 </div>
49 <?php
50 }
51
52 /**
53 * Generate months.
54 *
55 * @param string $timestamp
56 * @return string
57 */
58 public function generate_months( $timestamp ) {
59 $dates = array(
60 explode( ' ', date( "m F Y", strtotime( "-1 months", $timestamp ) ) ),
61 explode( ' ', date( "m F Y", $timestamp ) ),
62 explode( ' ', date( "m F Y", strtotime( "+1 months", $timestamp ) ) )
63 );
64
65 $current = date( "Ym", current_time( 'timestamp', false ) );
66
67 if ( (int) $current <= (int) ( $dates[1][2] . $dates[1][0] ) )
68 $next = '<span class="next">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</span>';
69 else
70 $next = '<a class="next" href="#" data-date="' . ( $dates[2][0] . '|' . $dates[2][2] ) . '">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</a>';
71
72 $dates = array(
73 'prev' => '<a class="prev" href="#" data-date="' . ( $dates[0][0] . '|' . $dates[0][2] ) . '">‹ ' . $dates[0][1] . ' ' . $dates[0][2] . '</a>',
74 'current' => '<span class="current">' . $dates[1][1] . ' ' . $dates[1][2] . '</span>',
75 'next' => $next
76 );
77
78 return $dates['prev'] . $dates['current'] . $dates['next'];
79 }
80
81 /**
82 * Dashboard widget chart user post types.
83 *
84 * @return void
85 */
86 public function dashboard_widget_chart_user_post_types() {
87 if ( ! check_ajax_referer( 'dashboard-chart-user-post-types', 'nonce' ) )
88 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
89
90 // get post types
91 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
92
93 // simulate total views as post type
94 $post_types[] = '_pvc_total_views';
95
96 // valid data?
97 if ( isset( $_POST['nonce'], $_POST['hidden'], $_POST['post_type'] ) && ( in_array( $_POST['post_type'], $post_types, true ) ) ) {
98 // get user ID
99 $user_id = get_current_user_id();
100
101 // get user dashboard data
102 $userdata = get_user_meta( $user_id, 'pvc_dashboard', true );
103
104 // empty userdata?
105 if ( ! is_array( $userdata ) || empty( $userdata ) )
106 $userdata = array();
107
108 // empty post types?
109 if ( ! array_key_exists( 'post_types', $userdata ) || ! is_array( $userdata['post_types'] ) )
110 $userdata['post_types'] = array();
111
112 // hide post type?
113 if ( $_POST['hidden'] === 'true' ) {
114 if ( ! in_array( $_POST['post_type'], $userdata['post_types'], true ) )
115 $userdata['post_types'][] = $_POST['post_type'];
116 } else {
117 if ( ( $key = array_search( $_POST['post_type'], $userdata['post_types'] ) ) !== false )
118 unset( $userdata['post_types'][$key] );
119 }
120
121 // update userdata
122 update_user_meta( $user_id, 'pvc_dashboard', $userdata );
123 }
124
125 exit;
126 }
127
128 /**
129 * Dashboard widget chart data function.
130 *
131 * @global $_wp_admin_css_colors
132 * @return void
133 */
134 public function dashboard_widget_chart() {
135 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
136 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
137
138 if ( ! check_ajax_referer( 'dashboard-chart', 'nonce' ) )
139 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
140
141 // get period
142 $period = isset( $_POST['period'] ) ? esc_attr( $_POST['period'] ) : 'this_month';
143
144 // get post types
145 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
146
147 // get stats
148 $query_args = array(
149 'post_type' => $post_types,
150 'posts_per_page' => -1,
151 'paged' => false,
152 'orderby' => 'post_views',
153 'suppress_filters' => false,
154 'no_found_rows' => true
155 );
156
157 // $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) );
158 $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) - 2592000 );
159
160 // get admin color scheme
161 global $_wp_admin_css_colors;
162
163 $admin_color = get_user_option( 'admin_color' );
164 $colors = $_wp_admin_css_colors[$admin_color]->colors;
165 $color = $this->hex2rgb( $colors[2] );
166
167 // set chart labels
168 switch ( $period ) {
169 case 'this_week':
170 $data = array(
171 'text' => array(
172 'xAxes' => date_i18n( 'F Y' ),
173 'yAxes' => __( 'Post Views', 'post-views-counter' )
174 )
175 );
176
177 for ( $day = 0; $day <= 6; $day ++ ) {
178 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
179 $query = new WP_Query( wp_parse_args( $query_args, array( 'views_query' => array( 'year' => date( 'Y', $date ), 'month' => date( 'n', $date ), 'day' => date( 'd', $date ) ) ) ) );
180
181 $data['data']['labels'][] = date_i18n( 'j', $date );
182 $data['data']['datasets'][$type_name]['label'] = __( 'Post Views', 'post-views-counter' );
183 $data['data']['datasets'][0]['data'][] = $query->total_views;
184 }
185 break;
186
187 case 'this_year':
188 $data = array(
189 'text' => array(
190 'xAxes' => __( 'Year', 'post-views-counter' ) . date( ' Y' ),
191 'yAxes' => __( 'Post Views', 'post-views-counter' ),
192 ),
193 'design' => array(
194 'fill' => true,
195 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
196 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
197 'borderWidth' => 1.2,
198 'borderDash' => array(),
199 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
200 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
201 'pointBorderWidth' => 1.2
202 )
203 );
204
205 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
206 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
207
208 // reindex post types
209 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
210
211 $post_type_data = array();
212
213 foreach ( $post_types as $id => $post_type ) {
214 $post_type_obj = get_post_type_object( $post_type );
215
216 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
217 $data['data']['datasets'][$id]['post_type'] = $post_type_obj->name;
218 $data['data']['datasets'][$id]['data'] = array();
219
220 // get month views
221 $post_type_data[$id] = array_values(
222 pvc_get_views(
223 array(
224 'fields' => 'date=>views',
225 'post_type' => $post_type,
226 'views_query' => array(
227 'year' => date( 'Y' ),
228 'month' => '',
229 'week' => '',
230 'day' => ''
231 )
232 )
233 )
234 );
235 }
236
237 $sum = array();
238
239 foreach ( $post_type_data as $post_type_id => $post_views ) {
240 foreach ( $post_views as $id => $views ) {
241 // generate chart data for specific post types
242 $data['data']['datasets'][$post_type_id]['data'][] = $views;
243
244 if ( ! array_key_exists( $id, $sum ) )
245 $sum[$id] = 0;
246
247 $sum[$id] += $views;
248 }
249 }
250
251 // this month all days
252 for ( $i = 1; $i <= 12; $i ++ ) {
253 // generate chart data
254 $data['data']['labels'][] = $i;
255 $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) );
256 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
257 }
258 break;
259
260 case 'this_month':
261 default:
262 $userdata = $this->get_dashboard_user_data( get_current_user_id(), 'post_types' );
263
264 if ( $period !== 'this_month' ) {
265 $date = explode( '|', $period, 2 );
266 $months = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' );
267 } else
268 $months = current_time( 'timestamp', false );
269
270 // get date chunks
271 $date = explode( ' ', date( "m Y t F", $months ) );
272
273 $data = array(
274 'months' => $this->generate_months( $months ),
275 'text' => array(
276 'xAxes' => $date[3] . ' ' . $date[1],
277 'yAxes' => __( 'Post Views', 'post-views-counter' ),
278 ),
279 'design' => array(
280 'fill' => true,
281 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
282 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
283 'borderWidth' => 1.2,
284 'borderDash' => array(),
285 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
286 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
287 'pointBorderWidth' => 1.2
288 )
289 );
290
291 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
292 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
293 $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $userdata, true );
294
295 // reindex post types
296 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
297
298 $post_type_data = array();
299
300 foreach ( $post_types as $id => $post_type ) {
301 $post_type_obj = get_post_type_object( $post_type );
302
303 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
304 $data['data']['datasets'][$id]['post_type'] = $post_type_obj->name;
305 $data['data']['datasets'][$id]['hidden'] = in_array( $post_type_obj->name, $userdata, true );
306 $data['data']['datasets'][$id]['data'] = array();
307
308 // get month views
309 $post_type_data[$id] = array_values(
310 pvc_get_views(
311 array(
312 'fields' => 'date=>views',
313 'post_type' => $post_type,
314 'views_query' => array(
315 'year' => $date[1],
316 'month' => $date[0],
317 'week' => '',
318 'day' => ''
319 )
320 )
321 )
322 );
323 }
324
325 $sum = array();
326
327 foreach ( $post_type_data as $post_type_id => $post_views ) {
328 foreach ( $post_views as $id => $views ) {
329 // generate chart data for specific post types
330 $data['data']['datasets'][$post_type_id]['data'][] = $views;
331
332 if ( ! array_key_exists( $id, $sum ) )
333 $sum[$id] = 0;
334
335 $sum[$id] += $views;
336 }
337 }
338
339 // this month all days
340 for ( $i = 1; $i <= $date[2]; $i ++ ) {
341 // generate chart data
342 $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i );
343 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( $date[1] . '-' . $date[0] . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
344 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
345 }
346 break;
347 }
348
349 echo json_encode( $data );
350
351 exit;
352 }
353
354 /**
355 * Get user dashboard data.
356 *
357 * @param string $data_type
358 * @return array
359 */
360 public function get_dashboard_user_data( $user_id, $data_type ) {
361 $userdata = get_user_meta( $user_id, 'pvc_dashboard', true );
362
363 if ( ! is_array( $userdata ) || empty( $userdata ) )
364 $userdata = array();
365
366 if ( ! array_key_exists( $data_type, $userdata ) || ! is_array( $userdata[$data_type] ) )
367 $userdata[$data_type] = array();
368
369 return $userdata[$data_type];
370 }
371
372 /**
373 * Enqueue admin scripts and styles.
374 *
375 * @param string $pagenow
376 */
377 public function admin_scripts_styles( $pagenow ) {
378 if ( $pagenow != 'index.php' )
379 return;
380
381 // filter user_can_see_stats
382 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
383 return;
384
385 wp_register_style( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css' );
386 wp_enqueue_style( 'pvc-admin-dashboard' );
387 wp_enqueue_style( 'pvc-chart-css', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.css' );
388
389 wp_register_script( 'pvc-chart', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true );
390 wp_register_script( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chart' ), Post_Views_Counter()->defaults['version'], true );
391
392 wp_enqueue_script( 'pvc-admin-dashboard' );
393
394 wp_localize_script(
395 'pvc-admin-dashboard',
396 'pvcArgs',
397 array(
398 'ajaxURL' => admin_url( 'admin-ajax.php' ),
399 'nonce' => wp_create_nonce( 'dashboard-chart' ),
400 'nonceUser' => wp_create_nonce( 'dashboard-chart-user-post-types' )
401 )
402 );
403 }
404
405 /**
406 * Convert hex to rgb color.
407 *
408 * @param type $color
409 * @return boolean
410 */
411 public function hex2rgb( $color ) {
412 if ( $color[0] == '#' ) {
413 $color = substr( $color, 1 );
414 }
415 if ( strlen( $color ) == 6 ) {
416 list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
417 } elseif ( strlen( $color ) == 3 ) {
418 list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
419 } else {
420 return false;
421 }
422 $r = hexdec( $r );
423 $g = hexdec( $g );
424 $b = hexdec( $b );
425 return array( 'r' => $r, 'g' => $g, 'b' => $b );
426 }
427
428 }
429