PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.0
Post Views Counter v1.2.0
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
columns.php 10 years ago counter.php 10 years ago cron.php 10 years ago dashboard.php 10 years ago frontend.php 10 years ago functions.php 10 years ago query.php 10 years ago settings.php 10 years ago update.php 10 years ago widgets.php 10 years ago
dashboard.php
213 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 public function __construct() {
12 // actions
13 add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) );
14 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_styles' ) );
15 add_action( 'wp_ajax_pvc_dashboard_chart', array( $this, 'dashboard_widget_chart' ) );
16 }
17
18 /**
19 * Initialize widget.
20 */
21 public function wp_dashboard_setup() {
22 // filter user_can_see_stats
23 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) {
24 return;
25 }
26
27 // add dashboard widget
28 wp_add_dashboard_widget( 'pvc_dashboard', __( 'Post Views', 'post-views-counter' ), array( $this, 'dashboard_widget' ) /*, array( $this, 'dashboard_widget_control' ) */ );
29 }
30
31 /**
32 * Render dashboard widget.
33 *
34 * @return mixed
35 */
36 public function dashboard_widget() {
37 ?>
38 <div id="pvc_dashboard_container">
39 <canvas id="pvc_chart" height="175"></canvas>
40 </div>
41 <?php
42 }
43
44 /**
45 * Dashboard widget settings.
46 *
47 * @return mixed
48 */
49 public function dashboard_widget_control() {
50
51 }
52
53 /**
54 * Dashboard widget chart data function.
55 *
56 */
57 public function dashboard_widget_chart() {
58
59 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
60 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
61
62 if ( ! check_ajax_referer( 'dashboard-chart', 'nonce' ) )
63 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
64
65 $period = isset( $_REQUEST['period'] ) ? esc_attr( $_REQUEST['period'] ) : 'this_month';
66
67 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
68
69 // get stats
70 $query_args = array(
71 'post_type' => $post_types,
72 'posts_per_page' => -1,
73 'orderby' => 'post_views',
74 'suppress_filters' => false
75 );
76
77 // set range
78 $range = 'this_month';
79 $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) );
80
81 // set chart labels
82 switch ( $range ) {
83 case 'this_week' :
84 $data = array(
85 'text' => array(
86 'xAxes' => date_i18n( 'F Y' ),
87 'yAxes' => __( 'Post Views', 'post-views-counter' )
88 )
89 );
90
91 for ( $day = 0; $day <= 6; $day ++ ) {
92
93 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
94 $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 ) ) ) ) );
95
96 $data['data']['labels'][] = date_i18n( 'j', $date );
97 $data['data']['datasets'][$type_name]['label'] = __( 'Post Views', 'post-views-counter');
98 $data['data']['datasets'][0]['data'][] = $query->total_views;
99 }
100 break;
101
102 case 'this month' :
103 default :
104 $data = array(
105 'text' => array(
106 'xAxes' => date_i18n( 'F Y' ),
107 'yAxes' => __( 'Post Views', 'post-views-counter' ),
108 ),
109 'design' => array(
110 'fill' => true,
111 'backgroundColor' => 'rgba(50, 143, 186, 0.2)',
112 'borderColor' => 'rgba(50, 143, 186, 1)',
113 'borderWidth' => 2,
114 'borderDash' => array(),
115 'pointBorderColor' => 'rgba(50, 143, 186, 1)',
116 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
117 'pointBorderWidth' => 1.2
118 )
119 );
120
121 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter');
122
123 // get data for specific post types
124 $empty_post_type_views = array();
125
126 // reindex post types
127 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
128
129 foreach ( $post_types as $id => $post_type ) {
130 $empty_post_type_views[$post_type] = 0;
131 $post_type_obj = get_post_type_object( $post_type );
132
133 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
134 $data['data']['datasets'][$id]['data'] = array();
135 }
136
137 // reverse post types
138 $rev_post_types = array_flip( $post_types );
139
140 // this month day loop
141 for ( $i = 1; $i <= date( 't' ); $i++ ) {
142 $query = new WP_Query( wp_parse_args( $query_args, array( 'views_query' => array( 'year' => date( 'Y' ), 'month' => date( 'n' ), 'day' => str_pad( $i, 2, '0', STR_PAD_LEFT ) ) ) ) );
143
144 // get data for specific post types
145 $post_type_views = $empty_post_type_views;
146
147 if ( ! empty( $query->posts ) ) {
148 foreach ( $query->posts as $index => $post ) {
149 $post_type_views[$post->post_type] += $post->post_views;
150 }
151 }
152
153 // generate chart data
154 $data['data']['labels'][] = str_pad( $i, 2, '0', STR_PAD_LEFT );
155 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( date( 'Y' ) . '-' . date( 'n' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
156 $data['data']['datasets'][0]['data'][] = $query->total_views;
157
158 // generate chart data for specific post types
159 foreach ( $post_type_views as $type_name => $type_views ) {
160 $data['data']['datasets'][$rev_post_types[$type_name]]['data'][] = $type_views;
161 }
162 }
163
164 break;
165 }
166
167 echo json_encode( $data );
168
169 exit;
170 }
171
172 /**
173 * Enqueue admin scripts and styles.
174 *
175 * @param string $pagenow
176 */
177 public function admin_scripts_styles( $pagenow ) {
178 if ( $pagenow != 'index.php' )
179 return;
180
181 // filter user_can_see_stats
182 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) {
183 return;
184 }
185
186 wp_register_style(
187 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css'
188 );
189 wp_enqueue_style( 'pvc-admin-dashboard' );
190
191 wp_register_script(
192 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chart' ), Post_Views_Counter()->defaults['version'], true
193 );
194
195 wp_register_script(
196 'pvc-chart', POST_VIEWS_COUNTER_URL . '/js/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true
197 );
198
199 // set ajax args
200 $ajax_args = array(
201 'ajaxURL' => admin_url( 'admin-ajax.php' ),
202 'nonce' => wp_create_nonce( 'dashboard-chart' )
203 );
204
205 wp_enqueue_script( 'pvc-admin-dashboard' );
206 // wp_enqueue_script( 'pvc-chart' );
207
208 wp_localize_script(
209 'pvc-admin-dashboard', 'pvcArgs', $ajax_args
210 );
211
212 }
213 }