PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.7
Post Views Counter v1.3.7
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 4 years ago counter.php 4 years ago crawler-detect.php 6 years ago cron.php 6 years ago dashboard.php 4 years ago frontend.php 6 years ago functions.php 4 years ago query.php 6 years ago settings-api.php 4 years ago settings.php 4 years ago update.php 6 years ago widgets.php 4 years ago
dashboard.php
618 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' ), 1 );
16 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_styles' ) );
17 add_action( 'wp_ajax_pvc_dashboard_most_viewed', array( $this, 'dashboard_get_most_viewed' ) );
18 add_action( 'wp_ajax_pvc_dashboard_chart', array( $this, 'dashboard_get_chart' ) );
19 add_action( 'wp_ajax_pvc_dashboard_user_options', array( $this, 'update_dashboard_user_options' ) );
20 }
21
22 /**
23 * Initialize widget.
24 */
25 public function wp_dashboard_setup() {
26 // filter user_can_see_stats
27 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) {
28 return;
29 }
30
31 // add dashboard post views chart widget
32 wp_add_dashboard_widget( 'pvc_dashboard', __( 'Post Views Counter', 'post-views-counter' ), array( $this, 'dashboard_widget' ) );
33 }
34
35 /**
36 * Enqueue admin scripts and styles.
37 *
38 * @param string $pagenow
39 */
40 public function admin_scripts_styles( $pagenow ) {
41 if ( $pagenow != 'index.php' )
42 return;
43
44 // filter user_can_see_stats
45 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
46 return;
47
48 wp_register_style( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css', array(), Post_Views_Counter()->defaults['version'] );
49 wp_enqueue_style( 'pvc-admin-dashboard' );
50 wp_enqueue_style( 'pvc-chartjs', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.css', array(), Post_Views_Counter()->defaults['version'] );
51 wp_enqueue_style( 'pvc-microtip', POST_VIEWS_COUNTER_URL . '/assets/microtip/microtip.min.css', array(), Post_Views_Counter()->defaults['version'] );
52
53 wp_register_script( 'pvc-chartjs', POST_VIEWS_COUNTER_URL . '/assets/chartjs/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true );
54 wp_register_script( 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chartjs' ), Post_Views_Counter()->defaults['version'], true );
55
56 wp_enqueue_script( 'pvc-admin-dashboard' );
57
58 wp_localize_script(
59 'pvc-admin-dashboard',
60 'pvcArgs',
61 array(
62 'ajaxURL' => admin_url( 'admin-ajax.php' ),
63 'nonce' => wp_create_nonce( 'pvc-dashboard-chart' ),
64 'nonceUser' => wp_create_nonce( 'pvc-dashboard-user-options' )
65 )
66 );
67 }
68
69 /**
70 * Render dashboard widget.
71 *
72 * @return mixed
73 */
74 public function dashboard_widget() {
75 // get user ID
76 $user_id = get_current_user_id();
77
78 // get user options
79 $user_options = get_user_meta( $user_id, 'pvc_dashboard', true );
80
81 // empty options?
82 if ( ! is_array( $user_options ) || empty( $user_options ) )
83 $user_options = array();
84
85 // sanitize options
86 $user_options = map_deep( $user_options, 'sanitize_text_field' );
87 $menu_items = ! empty( $user_options['menu_items'] ) ? $user_options['menu_items'] : array();
88
89 $months_html = $this->generate_months( current_time( 'timestamp', false ) );
90 ?>
91 <div id="pvc-dashboard-accordion" class="pvc-accordion">
92 <div id="pvc-post-views" class="pvc-accordion-item<?php echo in_array( 'post-views', $menu_items ) ? ' pvc-collapsed' : ''; ?>">
93 <div class="pvc-accordion-header">
94 <div class="pvc-accordion-toggle"><span class="pvc-accordion-title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span><span class="pvc-tooltip" aria-label="<?php _e( 'Displays the chart of most viewed post types for a selected time period.', 'post-views-counter' ); ?>" data-microtip-position="top" data-microtip-size="large" role="tooltip"><span class="pvc-tooltip-icon"></span></span></div>
95 <?php /*
96 <div class="pvc-accordion-actions">
97 <a href="javascript:void(0);" class="pvc-accordion-action dashicons dashicons-admin-generic"></a>
98 </div>
99 */ ?>
100 </div>
101 <div class="pvc-accordion-content">
102 <div class="pvc-dashboard-container loading">
103
104 <div id="pvc-chart-container" class="pvc-data-container">
105 <canvas id="pvc-chart" height="175"></canvas>
106 <span class="spinner"></span>
107 </div>
108
109 <div class="pvc-months">
110 <?php echo $months_html; ?>
111 </div>
112
113 </div>
114 </div>
115 </div>
116 <div id="pvc-most-viewed" class="pvc-accordion-item<?php echo in_array( 'most-viewed', $menu_items ) ? ' pvc-collapsed' : ''; ?>">
117 <div class="pvc-accordion-header">
118 <div class="pvc-accordion-toggle"><span class="pvc-accordion-title"><?php _e( 'Top Posts', 'post-views-counter' ); ?></span><span class="pvc-tooltip" aria-label="<?php _e( 'Displays the list of most viewed posts and pages on your website.', 'post-views-counter' ); ?>" data-microtip-position="top" data-microtip-size="large" role="tooltip"><span class="pvc-tooltip-icon"></span></span></div>
119 </div>
120 <div class="pvc-accordion-content">
121 <div class="pvc-dashboard-container loading">
122
123 <div class="pvc-data-container">
124 <div id="pvc-viewed" class="pvc-table-responsive"></div>
125 <span class="spinner"></span>
126 </div>
127
128 <div class="pvc-months">
129 <?php echo $months_html; ?>
130 </div>
131
132 </div>
133 </div>
134 </div>
135 </div>
136 <?php
137 }
138
139 /**
140 * Dashboard widget chart data function.
141 *
142 * @global $_wp_admin_css_colors
143 * @return void
144 */
145 public function dashboard_get_chart() {
146 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
147 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
148
149 if ( ! check_ajax_referer( 'pvc-dashboard-chart', 'nonce' ) )
150 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
151
152 // get period
153 $period = isset( $_POST['period'] ) ? esc_attr( $_POST['period'] ) : 'this_month';
154
155 // get post types
156 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
157
158 // get stats
159 $query_args = array(
160 'post_type' => $post_types,
161 'posts_per_page' => -1,
162 'paged' => false,
163 'orderby' => 'post_views',
164 'suppress_filters' => false,
165 'no_found_rows' => true
166 );
167
168 // $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) );
169 $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) - 2592000 );
170
171 // get color scheme global
172 global $_wp_admin_css_colors;
173
174 // set default color;
175 $color = array(
176 'r' => 105,
177 'g' => 168,
178 'b' => 187
179 );
180
181 if ( ! empty( $_wp_admin_css_colors ) ) {
182 // get current admin color scheme name
183 $current_color_scheme = get_user_option( 'admin_color' );
184
185 if ( empty( $current_color_scheme ) )
186 $current_color_scheme = 'fresh';
187
188 if ( isset( $_wp_admin_css_colors[$current_color_scheme] ) )
189 $color = $this->hex2rgb( $_wp_admin_css_colors[$current_color_scheme]->colors[2] );
190 }
191
192 // set chart labels
193 switch ( $period ) {
194 case 'this_week':
195 $data = array(
196 'text' => array(
197 'xAxes' => date_i18n( 'F Y' ),
198 'yAxes' => __( 'Post Views', 'post-views-counter' )
199 )
200 );
201
202 for ( $day = 0; $day <= 6; $day ++ ) {
203 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
204 $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 ) ) ) ) );
205
206 $data['data']['labels'][] = date_i18n( 'j', $date );
207 $data['data']['datasets'][$type_name]['label'] = __( 'Post Views', 'post-views-counter' );
208 $data['data']['datasets'][0]['data'][] = $query->total_views;
209 }
210 break;
211
212 case 'this_year':
213 $data = array(
214 'text' => array(
215 'xAxes' => __( 'Year', 'post-views-counter' ) . date( ' Y' ),
216 'yAxes' => __( 'Post Views', 'post-views-counter' ),
217 ),
218 'design' => array(
219 'fill' => true,
220 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
221 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
222 'borderWidth' => 1.2,
223 'borderDash' => array(),
224 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
225 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
226 'pointBorderWidth' => 1.2
227 )
228 );
229
230 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
231 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
232
233 // reindex post types
234 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
235
236 $post_type_data = array();
237
238 foreach ( $post_types as $id => $post_type ) {
239 $post_type_obj = get_post_type_object( $post_type );
240
241 // unrecognized post type? (mainly from deactivated plugins)
242 if ( empty( $post_type_obj ) )
243 $label = __( 'Unknown', 'post-views-counter' ) . ' #' . $id;
244 else
245 $label = $post_type_obj->labels->name;
246
247 $data['data']['datasets'][$id]['label'] = $label;
248 $data['data']['datasets'][$id]['post_type'] = $post_type;
249 $data['data']['datasets'][$id]['data'] = array();
250
251 // get month views
252 $post_type_data[$id] = array_values(
253 pvc_get_views(
254 array(
255 'fields' => 'date=>views',
256 'post_type' => $post_type,
257 'views_query' => array(
258 'year' => date( 'Y' ),
259 'month' => '',
260 'week' => '',
261 'day' => ''
262 )
263 )
264 )
265 );
266 }
267
268 $sum = array();
269
270 foreach ( $post_type_data as $post_type_id => $post_views ) {
271 foreach ( $post_views as $id => $views ) {
272 // generate chart data for specific post types
273 $data['data']['datasets'][$post_type_id]['data'][] = $views;
274
275 if ( ! array_key_exists( $id, $sum ) )
276 $sum[$id] = 0;
277
278 $sum[$id] += $views;
279 }
280 }
281
282 // this month all days
283 for ( $i = 1; $i <= 12; $i ++ ) {
284 // generate chart data
285 $data['data']['labels'][] = $i;
286 $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) );
287 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
288 }
289 break;
290
291 case 'this_month':
292 default:
293 $user_options = $this->get_dashboard_user_options( get_current_user_id(), 'post_types' );
294
295 if ( $period !== 'this_month' ) {
296 $date = explode( '|', $period, 2 );
297 $months = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' );
298 } else
299 $months = current_time( 'timestamp', false );
300
301 // get date chunks
302 $date = explode( ' ', date( "m Y t F", $months ) );
303
304 $data = array(
305 'months' => $this->generate_months( $months ),
306 'text' => array(
307 'xAxes' => $date[3] . ' ' . $date[1],
308 'yAxes' => __( 'Post Views', 'post-views-counter' ),
309 ),
310 'design' => array(
311 'fill' => true,
312 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
313 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
314 'borderWidth' => 1.2,
315 'borderDash' => array(),
316 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
317 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
318 'pointBorderWidth' => 1.2
319 )
320 );
321
322 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
323 $data['data']['datasets'][0]['post_type'] = '_pvc_total_views';
324 $data['data']['datasets'][0]['hidden'] = in_array( '_pvc_total_views', $user_options, true );
325
326 // reindex post types
327 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
328
329 $post_type_data = array();
330
331 foreach ( $post_types as $id => $post_type ) {
332 $post_type_obj = get_post_type_object( $post_type );
333
334 // unrecognized post type? (mainly from deactivated plugins)
335 if ( empty( $post_type_obj ) )
336 $label = __( 'Unknown', 'post-views-counter' ) . ' #' . $id;
337 else
338 $label = $post_type_obj->labels->name;
339
340 $data['data']['datasets'][$id]['label'] = $label;
341 $data['data']['datasets'][$id]['post_type'] = $post_type;
342 $data['data']['datasets'][$id]['hidden'] = in_array( $post_type, $user_options, true );
343 $data['data']['datasets'][$id]['data'] = array();
344
345 // get month views
346 $post_type_data[$id] = array_values(
347 pvc_get_views(
348 array(
349 'fields' => 'date=>views',
350 'post_type' => $post_type,
351 'views_query' => array(
352 'year' => $date[1],
353 'month' => $date[0],
354 'week' => '',
355 'day' => ''
356 )
357 )
358 )
359 );
360 }
361
362 $sum = array();
363
364 foreach ( $post_type_data as $post_type_id => $post_views ) {
365 foreach ( $post_views as $id => $views ) {
366 // generate chart data for specific post types
367 $data['data']['datasets'][$post_type_id]['data'][] = $views;
368
369 if ( ! array_key_exists( $id, $sum ) )
370 $sum[$id] = 0;
371
372 $sum[$id] += $views;
373 }
374 }
375
376 // this month all days
377 for ( $i = 1; $i <= $date[2]; $i ++ ) {
378 // generate chart data
379 $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i );
380 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( $date[1] . '-' . $date[0] . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
381 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
382 }
383 break;
384 }
385
386 echo json_encode( $data );
387
388 exit;
389 }
390
391 /**
392 * Dashboard widget chart data function.
393 *
394 * @global $_wp_admin_css_colors
395 * @return void
396 */
397 public function dashboard_get_most_viewed() {
398 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
399 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
400
401 if ( ! check_ajax_referer( 'pvc-dashboard-chart', 'nonce' ) )
402 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
403
404 // get period
405 $period = isset( $_POST['period'] ) ? esc_attr( $_POST['period'] ) : 'this_month';
406
407 // get post types
408 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
409
410 if ( $period !== 'this_month' ) {
411 $date = explode( '|', $period, 2 );
412 $months = strtotime( (string) $date[1] . '-' . (string) $date[0] . '-13' );
413 } else
414 $months = current_time( 'timestamp', false );
415
416 // get date chunks
417 $date = explode( ' ', date( "m Y t F", $months ) );
418
419 // get stats
420 $query_args = array(
421 'post_type' => $post_types,
422 'posts_per_page' => 10,
423 'paged' => false,
424 'suppress_filters' => false,
425 'no_found_rows' => true,
426 'views_query' => array(
427 'year' => $date[1],
428 'month' => $date[0],
429 )
430 );
431
432 $posts = pvc_get_most_viewed_posts( $query_args );
433
434 $data = array(
435 'months' => $this->generate_months( $months ),
436 'html' => '',
437 );
438
439 $html = '<table id="pvc-most-viewed-table" class="pvc-table pvc-table-hover">';
440 $html .= '<thead>';
441 $html .= '<tr>';
442 $html .= '<th scope="col">#</th>';
443 $html .= '<th scope="col">' . __( 'Post', 'post-views-counter' ) . '</th>';
444 $html .= '<th scope="col">' . __( 'Post Views', 'post-views-counter' ) . '</th>';
445 $html .= '</tr>';
446 $html .= '</thead>';
447 $html .= '<tbody>';
448
449 if ( $posts ) {
450 foreach ( $posts as $index => $post ) {
451 setup_postdata( $post );
452
453 $html .= '<tr>';
454 $html .= '<th scope="col">' . ( $index + 1 ) . '</th>';
455
456 if ( current_user_can( 'edit_post', $post->ID ) ) {
457 $html .= '<td><a href="' . get_edit_post_link( $post->ID ) . '">' . get_the_title( $post ) . '</a></td>';
458 } else {
459 $html .= '<td>' . get_the_title( $post ). '</td>';
460 }
461
462 $html .= '<td>' . number_format_i18n( $post->post_views ) . '</td>';
463 $html .= '</tr>';
464 }
465 } else {
466 $html .= '<tr class="no-posts">';
467 $html .= '<td colspan="3">' . __( 'No most viewed posts found', 'post-views-counter' ) . '</td>';
468 $html .= '</tr>';
469 }
470
471 $html .= '</tbody>';
472 $html .='</table>';
473
474 $data['html'] = $html;
475
476 echo json_encode( $data );
477 exit;
478 }
479
480 /**
481 * Generate months.
482 *
483 * @param string $timestamp
484 * @return string
485 */
486 public function generate_months( $timestamp ) {
487 $dates = array(
488 explode( ' ', date( "m F Y", strtotime( "-1 months", $timestamp ) ) ),
489 explode( ' ', date( "m F Y", $timestamp ) ),
490 explode( ' ', date( "m F Y", strtotime( "+1 months", $timestamp ) ) )
491 );
492
493 $current = date( "Ym", current_time( 'timestamp', false ) );
494
495 if ( (int) $current <= (int) ( $dates[1][2] . $dates[1][0] ) )
496 $next = '<span class="next">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</span>';
497 else
498 $next = '<a class="next" href="#" data-date="' . ( $dates[2][0] . '|' . $dates[2][2] ) . '">' . $dates[2][1] . ' ' . $dates[2][2] . ' ›</a>';
499
500 $dates = array(
501 'prev' => '<a class="prev" href="#" data-date="' . ( $dates[0][0] . '|' . $dates[0][2] ) . '">‹ ' . $dates[0][1] . ' ' . $dates[0][2] . '</a>',
502 'current' => '<span class="current">' . $dates[1][1] . ' ' . $dates[1][2] . '</span>',
503 'next' => $next
504 );
505
506 return $dates['prev'] . $dates['current'] . $dates['next'];
507 }
508
509 /**
510 * Dashboard widget chart user post types.
511 *
512 * @return void
513 */
514 public function update_dashboard_user_options() {
515 if ( ! check_ajax_referer( 'pvc-dashboard-user-options', 'nonce' ) )
516 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
517
518 // get allowed post types
519 $allowed_post_types = Post_Views_Counter()->options['general']['post_types_count'];
520
521 // simulate total views as post type
522 $allowed_post_types[] = '_pvc_total_views';
523
524 // get allowed menu items
525 $allowed_menu_items = array( 'post-views', 'most-viewed' );
526
527 // valid data?
528 if ( isset( $_POST['nonce'], $_POST['options'] ) && ! empty( $_POST['options'] ) ) {
529 // get options
530 $update = map_deep( $_POST['options'], 'sanitize_text_field' );
531
532 // get user ID
533 $user_id = get_current_user_id();
534
535 // get user dashboard data
536 $user_options = get_user_meta( $user_id, 'pvc_dashboard', true );
537
538 // empty userdata?
539 if ( ! is_array( $user_options ) || empty( $user_options ) )
540 $user_options = array();
541
542 // empty post types?
543 if ( ! array_key_exists( 'post_types', $user_options ) || ! is_array( $user_options['post_types'] ) )
544 $user_options['post_types'] = array();
545
546 // hide post type?
547 if ( ! empty( $update['post_type'] ) && in_array( $update['post_type'], $allowed_post_types, true ) ) {
548 if ( isset( $update['hidden'] ) && $update['hidden'] === 'true' ) {
549 if ( ! in_array( $update['post_type'], $user_options['post_types'], true ) )
550 $user_options['post_types'][] = $update['post_type'];
551 } else {
552 if ( ( $key = array_search( $update['post_type'], $user_options['post_types'] ) ) !== false )
553 unset( $user_options['post_types'][$key] );
554 }
555 }
556
557 // hide menu item?
558 $user_options['menu_items'] = array();
559
560 if ( ! empty( $update['menu_items'] ) && is_array( $update['menu_items'] ) ) {
561 $update['menu_items'] = map_deep( $update['menu_items'], 'sanitize_text_field' );
562
563 foreach ( $update['menu_items'] as $menu_item => $hidden ) {
564 if ( in_array( $menu_item, $allowed_menu_items ) && $hidden === 'true' )
565 $user_options['menu_items'][] = $menu_item;
566 }
567 }
568
569 // update userdata
570 update_user_meta( $user_id, 'pvc_dashboard', $user_options );
571 }
572
573 exit;
574 }
575
576 /**
577 * Get user dashboard data.
578 *
579 * @param string $data_type
580 * @return array
581 */
582 public function get_dashboard_user_options( $user_id, $data_type ) {
583 $user_options = get_user_meta( $user_id, 'pvc_dashboard', true );
584
585 if ( ! is_array( $user_options ) || empty( $user_options ) )
586 $user_options = array();
587
588 if ( ! array_key_exists( $data_type, $user_options ) || ! is_array( $user_options[$data_type] ) )
589 $user_options[$data_type] = array();
590
591 return $user_options[$data_type];
592 }
593
594 /**
595 * Convert hex to rgb color.
596 *
597 * @param type $color
598 * @return boolean
599 */
600 public function hex2rgb( $color ) {
601 if ( $color[0] == '#' ) {
602 $color = substr( $color, 1 );
603 }
604 if ( strlen( $color ) == 6 ) {
605 list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
606 } elseif ( strlen( $color ) == 3 ) {
607 list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
608 } else {
609 return false;
610 }
611 $r = hexdec( $r );
612 $g = hexdec( $g );
613 $b = hexdec( $b );
614 return array( 'r' => $r, 'g' => $g, 'b' => $b );
615 }
616
617 }
618