PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4.3
Post Views Counter v1.4.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 / class-dashboard.php
post-views-counter / includes Last commit date
class-admin.php 2 years ago class-columns.php 2 years ago class-counter.php 2 years ago class-crawler-detect.php 2 years ago class-cron.php 2 years ago class-dashboard.php 2 years ago class-frontend.php 2 years ago class-functions.php 2 years ago class-query.php 2 years ago class-settings-api.php 2 years ago class-settings.php 2 years ago class-update.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-dashboard.php
729 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 private $widget_items = [];
14
15 /**
16 * Class constructor.
17 *
18 * @return void
19 */
20 public function __construct() {
21 // actions
22 add_action( 'admin_init', [ $this, 'init_admin_dashboard' ] );
23 }
24
25 /**
26 * Dashboard initialization.
27 *
28 * @global string $pagenow
29 *
30 * @return void
31 */
32 public function init_admin_dashboard() {
33 global $pagenow;
34
35 // setup widget items
36 $this->setup_widget_items();
37
38 // do it only on dashboard page
39 if ( $pagenow === 'index.php' ) {
40 // filter user_can_see_stats
41 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
42 return;
43
44 add_action( 'wp_dashboard_setup', [ $this, 'wp_dashboard_setup' ], 1 );
45 add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts_styles' ] );
46 // ajax endpoints
47 } elseif ( $pagenow === 'admin-ajax.php' ) {
48 add_action( 'wp_ajax_pvc_dashboard_post_most_viewed', [ $this, 'dashboard_post_most_viewed' ] );
49 add_action( 'wp_ajax_pvc_dashboard_post_views_chart', [ $this, 'dashboard_post_views_chart' ] );
50 add_action( 'wp_ajax_pvc_dashboard_user_options', [ $this, 'update_dashboard_user_options' ] );
51 }
52 }
53
54 /**
55 * Add dashboard widget.
56 *
57 * @return void
58 */
59 public function wp_dashboard_setup() {
60 // add dashboard widget
61 wp_add_dashboard_widget( 'pvc_dashboard', __( 'Post Views Counter', 'post-views-counter' ), [ $this, 'dashboard_widget' ] );
62 }
63
64 /**
65 * Enqueue admin scripts and styles.
66 *
67 * @return void
68 */
69 public function admin_scripts_styles() {
70 // get main instance
71 $pvc = Post_Views_Counter();
72
73 // styles
74 wp_enqueue_style( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.min.css', [], $pvc->defaults['version'] );
75 wp_enqueue_style( 'pvc-microtip', POST_VIEWS_COUNTER_URL . '/assets/microtip/microtip.min.css', [], '1.0.0' );
76
77 // scripts
78 wp_enqueue_script( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', [ 'jquery', 'pvc-chartjs' ], $pvc->defaults['version'], true );
79
80 // prepare script data
81 $script_data = [
82 'ajaxURL' => admin_url( 'admin-ajax.php' ),
83 'nonce' => wp_create_nonce( 'pvc-dashboard-widget' ),
84 'nonceUser' => wp_create_nonce( 'pvc-dashboard-user-options' )
85 ];
86
87 wp_add_inline_script( 'pvc-admin-dashboard', 'var pvcArgs = ' . wp_json_encode( $script_data ) . ";\n", 'before' );
88 }
89
90 /**
91 * Setup dashboard widget items.
92 *
93 * @return void
94 */
95 private function setup_widget_items() {
96 // standard items
97 $items = [
98 [
99 'id' => 'post-views',
100 'title' => __( 'Post Views', 'post-views-counter' ),
101 'description' => __( 'Displays the chart of most viewed post types for a selected time period.', 'post-views-counter' ),
102 'content' => '<canvas id="pvc-post-views-chart" height="' . (int) $this->calculate_canvas_size( Post_Views_Counter()->options['general']['post_types_count'] ) . '"></canvas>',
103 'position' => 2
104 ],
105 [
106 'id' => 'post-most-viewed',
107 'title' => __( 'Top Posts', 'post-views-counter' ),
108 'description' => __( 'Displays the list of most viewed posts and pages on your website.', 'post-views-counter' ),
109 'content' => '<div id="pvc-post-most-viewed-content" class="pvc-table-responsive"></div>',
110 'position' => 3
111 ]
112 ];
113
114 // filter items, do not allow to remove main items
115 $new_items = apply_filters( 'pvc_dashboard_widget_items', [] );
116
117 // any new items?
118 if ( is_array( $new_items ) && ! empty( $new_items ) ) {
119 foreach ( $new_items as $item ) {
120 // add new item
121 array_push( $items, $item );
122 }
123 }
124
125 // sort dashboard items by position
126 array_multisort( array_column( $items, 'position' ), SORT_ASC, SORT_NUMERIC, $items );
127
128 // set widget items
129 $this->widget_items = $items;
130 }
131
132 /**
133 * Calculate canvas height based on number of legend items.
134 *
135 * @param array $data
136 * @param bool $expression
137 * @return int
138 */
139 public function calculate_canvas_size( $data, $expression = true ) {
140 if ( $expression && ! empty( $data ) ) {
141 // treat every 4 legend items as 1 line - 23 pixels
142 $height = 23 * ( (int) ceil( count( $data ) / 4 ) - 1 );
143 } else
144 $height = 0;
145
146 return (int) ( 170 + $height );
147 }
148
149 /**
150 * Render dashboard widget.
151 *
152 * @return void
153 */
154 public function dashboard_widget() {
155 // get user options
156 $user_options = get_user_meta( get_current_user_id(), 'pvc_dashboard', true );
157
158 // empty options?
159 if ( empty( $user_options ) || ! is_array( $user_options ) )
160 $user_options = [];
161
162 // sanitize options
163 $user_options = map_deep( $user_options, 'sanitize_text_field' );
164
165 // get menu items
166 $menu_items = ! empty( $user_options['menu_items'] ) ? $user_options['menu_items'] : [];
167
168 // generate months
169 $months_html = wp_kses_post( $this->generate_months( current_time( 'timestamp', false ) ) );
170
171 // get widget items
172 $items = $this->widget_items;
173
174 $html = '
175 <div id="pvc-dashboard-accordion" class="pvc-accordion">';
176
177 foreach ( $items as $item ) {
178 $html .= $this->generate_dashboard_widget_item( $item, $menu_items, $months_html );
179 }
180
181 $html .= '
182 </div>';
183
184 echo $html;
185 }
186
187 /**
188 * Generate dashboard widget item HTML.
189 *
190 * @param array $item
191 * @param array $menu_items
192 * @param string $esc_months_html
193 * @return string
194 */
195 public function generate_dashboard_widget_item( $item, $menu_items, $esc_months_html ) {
196 // get allowed html tags
197 $allowed_html = wp_kses_allowed_html( 'post' );
198 $allowed_html['canvas'] = [
199 'id' => [],
200 'height' => []
201 ];
202
203 return '
204 <div id="pvc-' . esc_attr( $item['id'] ) . '" class="pvc-accordion-item' . ( in_array( $item['id'], $menu_items, true ) ? ' pvc-collapsed' : '' ) . '">
205 <div class="pvc-accordion-header">
206 <div class="pvc-accordion-toggle"><span class="pvc-accordion-title">' . esc_html( $item['title'] ) . '</span><span class="pvc-tooltip" aria-label="' . esc_html( $item['description'] ) . '" data-microtip-position="top" data-microtip-size="large" role="tooltip"><span class="pvc-tooltip-icon"></span></span></div>
207 <!--
208 <div class="pvc-accordion-actions">
209 <a href="javascript:void(0);" class="pvc-accordion-action dashicons dashicons-admin-generic"></a>
210 </div>
211 -->
212 </div>
213 <div class="pvc-accordion-content">
214 <div class="pvc-dashboard-container loading">
215 <div class="pvc-data-container">
216 ' . wp_kses( $item['content'], $allowed_html ) . '
217 <span class="spinner"></span>
218 </div>
219 <div class="pvc-months">' . $esc_months_html . '</div>
220 </div>
221 </div>
222 </div>';
223 }
224
225 /**
226 * Render dashboard widget with post views.
227 *
228 * @return void
229 */
230 public function dashboard_post_views_chart() {
231 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) || ! check_ajax_referer( 'pvc-dashboard-widget', 'nonce' ) )
232 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
233
234 // get period
235 $period = isset( $_POST['period'] ) ? preg_replace( '/[^a-z0-9_|]/', '', $_POST['period'] ) : 'this_month';
236
237 // get post types
238 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
239
240 // get dashboard user options
241 $user_options = $this->get_dashboard_user_options( get_current_user_id(), 'post_types' );
242
243 // get current date
244 $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) );
245
246 // get colors
247 $colors = Post_Views_Counter()->functions->get_colors();
248
249 // set chart labels
250 switch ( $period ) {
251 case 'this_week':
252 //TODO
253 $data = [
254 'design' => [
255 'fill' => true,
256 'backgroundColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 0.2)',
257 'borderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)',
258 'borderWidth' => 1.2,
259 'borderDash' => [],
260 'pointBorderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)',
261 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
262 'pointBorderWidth' => 1.2
263 ]
264 ];
265
266 $data['data']['datasets'][0]['label'] = __( 'Post Views', 'post-views-counter' );
267 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
268
269 for ( $day = 0; $day <= 6; $day++ ) {
270 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
271 $query = new WP_Query(
272 wp_parse_args(
273 [
274 'post_type' => $post_types,
275 'posts_per_page' => -1,
276 'paged' => false,
277 'orderby' => 'post_views',
278 'suppress_filters' => false,
279 'no_found_rows' => true
280 ],
281 [
282 'views_query' => [
283 'year' => date( 'Y', $date ),
284 'month' => date( 'n', $date ),
285 'day' => date( 'd', $date )
286 ]
287 ]
288 )
289 );
290
291 $data['data']['labels'][] = date_i18n( 'j', $date );
292 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), $date );
293 $data['data']['datasets'][0]['data'][] = $query->total_views;
294 $data['data']['datasets'][$day]['data'][] = $query->total_views;
295 }
296 break;
297
298 case 'this_year':
299 $data = [
300 'design' => [
301 'fill' => true,
302 'backgroundColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 0.2)',
303 'borderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)',
304 'borderWidth' => 1.2,
305 'borderDash' => [],
306 'pointBorderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)',
307 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
308 'pointBorderWidth' => 1.2
309 ]
310 ];
311
312 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
313 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
314 $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $user_options, true );
315 $data['data']['datasets'][0]['data'] = [];
316
317 $sum = [];
318
319 // any post types?
320 if ( ! empty( $post_types ) ) {
321 // reindex post types
322 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
323
324 $post_type_data = [];
325
326 foreach ( $post_types as $id => $post_type ) {
327 $post_type_obj = get_post_type_object( $post_type );
328
329 // unrecognized post type? (mainly from deactivated plugins)
330 if ( empty( $post_type_obj ) )
331 $label = $post_type;
332 else
333 $label = $post_type_obj->labels->name;
334
335 $data['data']['datasets'][$id]['label'] = $label;
336 $data['data']['datasets'][$id]['post_type'] = $post_type;
337 $data['data']['datasets'][$id]['hidden'] = in_array( $post_type, $user_options, true );
338 $data['data']['datasets'][$id]['data'] = [];
339
340 // get month views
341 $post_type_data[$id] = array_values(
342 pvc_get_views(
343 [
344 'fields' => 'date=>views',
345 'post_type' => $post_type,
346 'views_query' => [
347 'year' => date( 'Y' ),
348 'month' => '',
349 'week' => '',
350 'day' => ''
351 ]
352 ]
353 )
354 );
355 }
356
357 foreach ( $post_type_data as $post_type_id => $post_views ) {
358 foreach ( $post_views as $id => $views ) {
359 // generate chart data for specific post types
360 $data['data']['datasets'][$post_type_id]['data'][] = $views;
361
362 if ( ! array_key_exists( $id, $sum ) )
363 $sum[$id] = 0;
364
365 $sum[$id] += $views;
366 }
367 }
368 }
369
370 // this month all days
371 for ( $i = 1; $i <= 12; $i++ ) {
372 // generate chart data
373 $data['data']['labels'][] = $i;
374 $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) );
375 $data['data']['datasets'][0]['data'][] = ( array_key_exists( $i - 1, $sum ) ? $sum[$i - 1] : 0 );
376 }
377 break;
378
379 case 'this_month':
380 default:
381 // convert period
382 $time = $this->period2timestamp( $period );
383
384 // get date chunks
385 $date = explode( ' ', date( "m Y t F", $time ) );
386
387 $data = [
388 'months' => $this->generate_months( $time ),
389 'design' => [
390 'fill' => true,
391 'backgroundColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 0.2)',
392 'borderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)',
393 'borderWidth' => 1.2,
394 'borderDash' => [],
395 'pointBorderColor' => 'rgba(' . $colors['r'] . ',' . $colors['g'] . ',' . $colors['b'] . ', 1)',
396 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
397 'pointBorderWidth' => 1.2
398 ]
399 ];
400
401 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
402 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
403 $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $user_options, true );
404 $data['data']['datasets'][0]['data'] = [];
405
406 $sum = [];
407
408 // any post types?
409 if ( ! empty( $post_types ) ) {
410 // reindex post types
411 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
412
413 $post_type_data = [];
414
415 foreach ( $post_types as $id => $post_type ) {
416 $post_type_obj = get_post_type_object( $post_type );
417
418 // unrecognized post type? (mainly from deactivated plugins)
419 if ( empty( $post_type_obj ) )
420 $label = $post_type;
421 else
422 $label = $post_type_obj->labels->name;
423
424 $data['data']['datasets'][$id]['label'] = $label;
425 $data['data']['datasets'][$id]['post_type'] = $post_type;
426 $data['data']['datasets'][$id]['hidden'] = in_array( $post_type, $user_options, true );
427 $data['data']['datasets'][$id]['data'] = [];
428
429 // get month views
430 $post_type_data[$id] = array_values(
431 pvc_get_views(
432 [
433 'fields' => 'date=>views',
434 'post_type' => $post_type,
435 'views_query' => [
436 'year' => $date[1],
437 'month' => $date[0],
438 'week' => '',
439 'day' => '',
440 'hide_empty' => true
441 ]
442 ]
443 )
444 );
445 }
446
447 foreach ( $post_type_data as $post_type_id => $post_views ) {
448 foreach ( $post_views as $id => $views ) {
449 // generate chart data for specific post types
450 $data['data']['datasets'][$post_type_id]['data'][] = $views;
451
452 if ( ! array_key_exists( $id, $sum ) )
453 $sum[$id] = 0;
454
455 $sum[$id] += $views;
456 }
457 }
458 }
459
460 // this month all days
461 for ( $i = 1; $i <= $date[2]; $i++ ) {
462 // generate chart data
463 $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i );
464 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( $date[1] . '-' . $date[0] . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
465 $data['data']['datasets'][0]['data'][] = ( array_key_exists( $i - 1, $sum ) ? $sum[$i - 1] : 0 );
466 }
467 break;
468 }
469
470 echo wp_json_encode( $data );
471
472 exit;
473 }
474
475 /**
476 * Render dashboard widget with most viewed posts.
477 *
478 * @return void
479 */
480 public function dashboard_post_most_viewed() {
481 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) || ! check_ajax_referer( 'pvc-dashboard-widget', 'nonce' ) )
482 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
483
484 // get main instance
485 $pvc = Post_Views_Counter();
486
487 // get post types
488 $post_types = $pvc->options['general']['post_types_count'];
489
490 // get period
491 $period = isset( $_POST['period'] ) ? preg_replace( '/[^a-z0-9_|]/', '', $_POST['period'] ) : 'this_month';
492
493 // convert period
494 $time = $this->period2timestamp( $period );
495
496 // get date chunks
497 $date = explode( ' ', date( "m Y t F", $time ) );
498
499 // get stats
500 $query_args = [
501 'post_type' => $post_types,
502 'posts_per_page' => 10,
503 'paged' => false,
504 'suppress_filters' => false,
505 'no_found_rows' => true,
506 'views_query' => [
507 'year' => $date[1],
508 'month' => $date[0],
509 'hide_empty' => true
510 ]
511 ];
512
513 $posts = pvc_get_most_viewed_posts( $query_args );
514
515 $data = [
516 'months' => $this->generate_months( $time ),
517 'html' => ''
518 ];
519
520 $html = '
521 <table id="pvc-post-most-viewed-table" class="pvc-table pvc-table-hover">
522 <thead>
523 <tr>
524 <th scope="col">#</th>
525 <th scope="col">' . esc_html__( 'Post', 'post-views-counter' ) . '</th>
526 <th scope="col">' . esc_html__( 'Views', 'post-views-counter' ) . '</th>
527 </tr>
528 </thead>
529 <tbody>';
530
531 if ( $posts ) {
532 // active post types
533 $active_post_types = [];
534
535 foreach ( $posts as $index => $post ) {
536 setup_postdata( $post );
537
538 $html .= '
539 <tr>
540 <th scope="col">' . ( $index + 1 ) . '</th>';
541
542 // check post type existence
543 if ( array_key_exists( $post->post_type, $active_post_types ) )
544 $post_type_exists = $active_post_types[$post->post_type];
545 else
546 $post_type_exists = $active_post_types[$post->post_type] = post_type_exists( $post->post_type );
547
548 $title = get_the_title( $post );
549
550 if ( $title === '' )
551 $title = __( '(no title)' );
552
553 // edit post link
554 if ( $post_type_exists && current_user_can( 'edit_post', $post->ID ) ) {
555 $html .= '
556 <td><a href="' . esc_url( get_edit_post_link( $post->ID ) ) . '">' . esc_html( $title ) . '</a></td>';
557 } else {
558 $html .= '
559 <td>' . esc_html( $title ). '</td>';
560 }
561
562 $html .= '
563 <td>' . number_format_i18n( $post->post_views ) . '</td>
564 </tr>';
565 }
566 } else {
567 $html .= '
568 <tr class="no-posts">
569 <td colspan="3">' . esc_html__( 'No most viewed posts found.', 'post-views-counter' ) . '</td>
570 </tr>';
571 }
572
573 $html .= '
574 </tbody>
575 </table>';
576
577 $data['html'] = $html;
578
579 echo wp_json_encode( $data );
580
581 exit;
582 }
583
584 /**
585 * Generate dashboard widget months HTML.
586 *
587 * @param int $timestamp
588 * @return string
589 */
590 public function generate_months( $timestamp ) {
591 $dates = [
592 explode( ' ', date( "m F Y", strtotime( "-1 months", $timestamp ) ) ),
593 explode( ' ', date( "m F Y", $timestamp ) ),
594 explode( ' ', date( "m F Y", strtotime( "+1 months", $timestamp ) ) )
595 ];
596
597 $current = date( "Ym", current_time( 'timestamp', false ) );
598
599 if ( (int) $current <= (int) ( $dates[1][2] . $dates[1][0] ) )
600 $next = '<span class="next">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</span>';
601 else
602 $next = '<a class="next" href="#" data-date="' . ( $dates[2][0] . '|' . $dates[2][2] ) . '">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</a>';
603
604 $dates = [
605 'prev' => '<a class="prev" href="#" data-date="' . ( $dates[0][0] . '|' . $dates[0][2] ) . '">‹ ' . $dates[0][1] . ' ' . $dates[0][2] . '</a>',
606 'current' => '<span class="current">' . $dates[1][1] . ' ' . $dates[1][2] . '</span>',
607 'next' => $next
608 ];
609
610 return $dates['prev'] . $dates['current'] . $dates['next'];
611 }
612
613 /**
614 * Update dashboard widget user options.
615 *
616 * @return void
617 */
618 public function update_dashboard_user_options() {
619 if ( ! check_ajax_referer( 'pvc-dashboard-user-options', 'nonce' ) )
620 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
621
622 // valid data?
623 if ( isset( $_POST['nonce'], $_POST['options'] ) && ! empty( $_POST['options'] ) ) {
624 // get sanitized options
625 $update = map_deep( $_POST['options'], 'sanitize_text_field' );
626
627 // get user ID
628 $user_id = get_current_user_id();
629
630 // get user dashboard data
631 $user_options = get_user_meta( $user_id, 'pvc_dashboard', true );
632
633 // empty userdata?
634 if ( ! is_array( $user_options ) || empty( $user_options ) )
635 $user_options = [];
636
637 // empty post types?
638 if ( ! array_key_exists( 'post_types', $user_options ) || ! is_array( $user_options['post_types'] ) )
639 $user_options['post_types'] = [];
640
641 // hide post type?
642 if ( ! empty( $update['post_type'] ) ) {
643 // get allowed post types
644 $allowed_post_types = Post_Views_Counter()->options['general']['post_types_count'];
645
646 // simulate total post views as post type
647 $allowed_post_types[] = '_pvc_total_views';
648
649 if ( in_array( $update['post_type'], $allowed_post_types, true ) ) {
650 if ( isset( $update['hidden'] ) && $update['hidden'] === 'true' ) {
651 if ( ! in_array( $update['post_type'], $user_options['post_types'], true ) )
652 $user_options['post_types'][] = $update['post_type'];
653 } else {
654 if ( ( $key = array_search( $update['post_type'], $user_options['post_types'] ) ) !== false )
655 unset( $user_options['post_types'][$key] );
656 }
657 }
658 }
659
660 // empty menu items?
661 if ( ! array_key_exists( 'menu_items', $user_options ) || ! is_array( $user_options['menu_items'] ) )
662 $user_options['menu_items'] = [];
663
664 if ( ! empty( $update['menu_items'] ) && is_array( $update['menu_items'] ) ) {
665 $user_options['menu_items'] = [];
666
667 // get allowed menu items
668 $allowed_menu_items = array_column( $this->widget_items, 'id' );
669
670 foreach ( $update['menu_items'] as $menu_item => $hidden ) {
671 if ( in_array( $menu_item, $allowed_menu_items, true ) && $hidden === 'true' )
672 $user_options['menu_items'][] = $menu_item;
673 }
674 }
675
676 // filter user options
677 $user_options = apply_filters( 'pvc_update_dashboard_user_options', $user_options, $update, $user_id );
678
679 // update userdata
680 update_user_meta( $user_id, 'pvc_dashboard', $user_options );
681 }
682
683 exit;
684 }
685
686 /**
687 * Get user dashboard data.
688 *
689 * @param int $user_id
690 * @param string $data_type
691 * @return array
692 */
693 public function get_dashboard_user_options( $user_id, $data_type ) {
694 $user_options = get_user_meta( $user_id, 'pvc_dashboard', true );
695
696 if ( ! is_array( $user_options ) || empty( $user_options ) )
697 $user_options = [];
698
699 if ( ! array_key_exists( $data_type, $user_options ) || ! is_array( $user_options[$data_type] ) )
700 $user_options[$data_type] = [];
701
702 return $user_options[$data_type];
703 }
704
705 /**
706 * Convert period to timestamp.
707 *
708 * @param string $period
709 * @return int
710 */
711 public function period2timestamp( $period ) {
712 // whitelisted period?
713 if ( in_array( $period, [ 'this_week', 'this_year', 'this_month' ], true ) ) {
714 $timestamp = current_time( 'timestamp', false );
715 } else {
716 if ( preg_match( '/^([0-9]{2}\|[0-9]{4})$/', $period ) === 1 ) {
717 // month|year
718 $date = explode( '|', $period, 2 );
719
720 // get timestamp
721 $timestamp = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' );
722 } else
723 $timestamp = current_time( 'timestamp', false );
724 }
725
726 return $timestamp;
727 }
728 }
729