PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.3
Post Views Counter v1.3.3
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 7 years ago columns.php 5 years ago counter.php 5 years ago crawler-detect.php 6 years ago cron.php 6 years ago dashboard.php 5 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
443 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 color scheme global
161 global $_wp_admin_css_colors;
162
163 // set default color;
164 $color = array(
165 'r' => 105,
166 'g' => 168,
167 'b' => 187
168 );
169
170 if ( ! empty( $_wp_admin_css_colors ) ) {
171 // get current admin color scheme name
172 $current_color_scheme = get_user_option( 'admin_color' );
173
174 if ( empty( $current_color_scheme ) )
175 $current_color_scheme = 'fresh';
176
177 if ( isset( $_wp_admin_css_colors[$current_color_scheme] ) )
178 $color = $this->hex2rgb( $_wp_admin_css_colors[$current_color_scheme]->colors[2] );
179 }
180
181 // set chart labels
182 switch ( $period ) {
183 case 'this_week':
184 $data = array(
185 'text' => array(
186 'xAxes' => date_i18n( 'F Y' ),
187 'yAxes' => __( 'Post Views', 'post-views-counter' )
188 )
189 );
190
191 for ( $day = 0; $day <= 6; $day ++ ) {
192 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
193 $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 ) ) ) ) );
194
195 $data['data']['labels'][] = date_i18n( 'j', $date );
196 $data['data']['datasets'][$type_name]['label'] = __( 'Post Views', 'post-views-counter' );
197 $data['data']['datasets'][0]['data'][] = $query->total_views;
198 }
199 break;
200
201 case 'this_year':
202 $data = array(
203 'text' => array(
204 'xAxes' => __( 'Year', 'post-views-counter' ) . date( ' Y' ),
205 'yAxes' => __( 'Post Views', 'post-views-counter' ),
206 ),
207 'design' => array(
208 'fill' => true,
209 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
210 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
211 'borderWidth' => 1.2,
212 'borderDash' => array(),
213 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
214 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
215 'pointBorderWidth' => 1.2
216 )
217 );
218
219 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
220 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
221
222 // reindex post types
223 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
224
225 $post_type_data = array();
226
227 foreach ( $post_types as $id => $post_type ) {
228 $post_type_obj = get_post_type_object( $post_type );
229
230 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
231 $data['data']['datasets'][$id]['post_type'] = $post_type_obj->name;
232 $data['data']['datasets'][$id]['data'] = array();
233
234 // get month views
235 $post_type_data[$id] = array_values(
236 pvc_get_views(
237 array(
238 'fields' => 'date=>views',
239 'post_type' => $post_type,
240 'views_query' => array(
241 'year' => date( 'Y' ),
242 'month' => '',
243 'week' => '',
244 'day' => ''
245 )
246 )
247 )
248 );
249 }
250
251 $sum = array();
252
253 foreach ( $post_type_data as $post_type_id => $post_views ) {
254 foreach ( $post_views as $id => $views ) {
255 // generate chart data for specific post types
256 $data['data']['datasets'][$post_type_id]['data'][] = $views;
257
258 if ( ! array_key_exists( $id, $sum ) )
259 $sum[$id] = 0;
260
261 $sum[$id] += $views;
262 }
263 }
264
265 // this month all days
266 for ( $i = 1; $i <= 12; $i ++ ) {
267 // generate chart data
268 $data['data']['labels'][] = $i;
269 $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) );
270 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
271 }
272 break;
273
274 case 'this_month':
275 default:
276 $userdata = $this->get_dashboard_user_data( get_current_user_id(), 'post_types' );
277
278 if ( $period !== 'this_month' ) {
279 $date = explode( '|', $period, 2 );
280 $months = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' );
281 } else
282 $months = current_time( 'timestamp', false );
283
284 // get date chunks
285 $date = explode( ' ', date( "m Y t F", $months ) );
286
287 $data = array(
288 'months' => $this->generate_months( $months ),
289 'text' => array(
290 'xAxes' => $date[3] . ' ' . $date[1],
291 'yAxes' => __( 'Post Views', 'post-views-counter' ),
292 ),
293 'design' => array(
294 'fill' => true,
295 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
296 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
297 'borderWidth' => 1.2,
298 'borderDash' => array(),
299 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
300 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
301 'pointBorderWidth' => 1.2
302 )
303 );
304
305 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
306 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
307 $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $userdata, true );
308
309 // reindex post types
310 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
311
312 $post_type_data = array();
313
314 foreach ( $post_types as $id => $post_type ) {
315 $post_type_obj = get_post_type_object( $post_type );
316
317 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
318 $data['data']['datasets'][$id]['post_type'] = $post_type_obj->name;
319 $data['data']['datasets'][$id]['hidden'] = in_array( $post_type_obj->name, $userdata, true );
320 $data['data']['datasets'][$id]['data'] = array();
321
322 // get month views
323 $post_type_data[$id] = array_values(
324 pvc_get_views(
325 array(
326 'fields' => 'date=>views',
327 'post_type' => $post_type,
328 'views_query' => array(
329 'year' => $date[1],
330 'month' => $date[0],
331 'week' => '',
332 'day' => ''
333 )
334 )
335 )
336 );
337 }
338
339 $sum = array();
340
341 foreach ( $post_type_data as $post_type_id => $post_views ) {
342 foreach ( $post_views as $id => $views ) {
343 // generate chart data for specific post types
344 $data['data']['datasets'][$post_type_id]['data'][] = $views;
345
346 if ( ! array_key_exists( $id, $sum ) )
347 $sum[$id] = 0;
348
349 $sum[$id] += $views;
350 }
351 }
352
353 // this month all days
354 for ( $i = 1; $i <= $date[2]; $i ++ ) {
355 // generate chart data
356 $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i );
357 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( $date[1] . '-' . $date[0] . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
358 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
359 }
360 break;
361 }
362
363 echo json_encode( $data );
364
365 exit;
366 }
367
368 /**
369 * Get user dashboard data.
370 *
371 * @param string $data_type
372 * @return array
373 */
374 public function get_dashboard_user_data( $user_id, $data_type ) {
375 $userdata = get_user_meta( $user_id, 'pvc_dashboard', true );
376
377 if ( ! is_array( $userdata ) || empty( $userdata ) )
378 $userdata = array();
379
380 if ( ! array_key_exists( $data_type, $userdata ) || ! is_array( $userdata[$data_type] ) )
381 $userdata[$data_type] = array();
382
383 return $userdata[$data_type];
384 }
385
386 /**
387 * Enqueue admin scripts and styles.
388 *
389 * @param string $pagenow
390 */
391 public function admin_scripts_styles( $pagenow ) {
392 if ( $pagenow != 'index.php' )
393 return;
394
395 // filter user_can_see_stats
396 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
397 return;
398
399 wp_register_style( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css' );
400 wp_enqueue_style( 'pvc-admin-dashboard' );
401 wp_enqueue_style( 'pvc-chart-css', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.css' );
402
403 wp_register_script( 'pvc-chart', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true );
404 wp_register_script( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chart' ), Post_Views_Counter()->defaults['version'], true );
405
406 wp_enqueue_script( 'pvc-admin-dashboard' );
407
408 wp_localize_script(
409 'pvc-admin-dashboard',
410 'pvcArgs',
411 array(
412 'ajaxURL' => admin_url( 'admin-ajax.php' ),
413 'nonce' => wp_create_nonce( 'dashboard-chart' ),
414 'nonceUser' => wp_create_nonce( 'dashboard-chart-user-post-types' )
415 )
416 );
417 }
418
419 /**
420 * Convert hex to rgb color.
421 *
422 * @param type $color
423 * @return boolean
424 */
425 public function hex2rgb( $color ) {
426 if ( $color[0] == '#' ) {
427 $color = substr( $color, 1 );
428 }
429 if ( strlen( $color ) == 6 ) {
430 list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
431 } elseif ( strlen( $color ) == 3 ) {
432 list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
433 } else {
434 return false;
435 }
436 $r = hexdec( $r );
437 $g = hexdec( $g );
438 $b = hexdec( $b );
439 return array( 'r' => $r, 'g' => $g, 'b' => $b );
440 }
441
442 }
443