PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.5
Post Views Counter v1.2.5
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 9 years ago counter.php 9 years ago crawler-detect.php 9 years ago cron.php 9 years ago dashboard.php 9 years ago frontend.php 9 years ago functions.php 9 years ago query.php 9 years ago settings.php 9 years ago update.php 9 years ago widgets.php 9 years ago
dashboard.php
331 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 }
19
20 /**
21 * Initialize widget.
22 */
23 public function wp_dashboard_setup() {
24 // filter user_can_see_stats
25 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) {
26 return;
27 }
28
29 // add dashboard widget
30 wp_add_dashboard_widget( 'pvc_dashboard', __( 'Post Views', 'post-views-counter' ), array( $this, 'dashboard_widget' ) /* , array( $this, 'dashboard_widget_control' ) */ );
31 }
32
33 /**
34 * Render dashboard widget.
35 *
36 * @return mixed
37 */
38 public function dashboard_widget() {
39 ?>
40 <div id="pvc_dashboard_container">
41 <canvas id="pvc_chart" height="175"></canvas>
42 </div>
43 <?php
44 }
45
46 /**
47 * Dashboard widget settings.
48 *
49 * @return mixed
50 */
51 public function dashboard_widget_control() {
52
53 }
54
55 /**
56 * Dashboard widget chart data function.
57 *
58 * @global $_wp_admin_css_colors
59 * @return mixed
60 */
61 public function dashboard_widget_chart() {
62
63 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
64 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
65
66 if ( ! check_ajax_referer( 'dashboard-chart', 'nonce' ) )
67 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
68
69 // $period = isset( $_REQUEST['period'] ) ? esc_attr( $_REQUEST['period'] ) : 'this_month';
70
71 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
72
73 // get stats
74 $query_args = array(
75 'post_type' => $post_types,
76 'posts_per_page' => -1,
77 'paged' => false,
78 'orderby' => 'post_views',
79 'suppress_filters' => false,
80 'no_found_rows' => true
81 );
82
83 // set range
84 $this_month = 'this_year';
85
86 // $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) );
87 $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) - 2592000 );
88
89 // get admin color scheme
90 global $_wp_admin_css_colors;
91
92 $admin_color = get_user_option( 'admin_color' );
93 $colors = $_wp_admin_css_colors[$admin_color]->colors;
94 $color = $this->hex2rgb( $colors[2] );
95
96 // set chart labels
97 switch ( $range ) {
98 case 'this_week':
99 $data = array(
100 'text' => array(
101 'xAxes' => date_i18n( 'F Y' ),
102 'yAxes' => __( 'Post Views', 'post-views-counter' )
103 )
104 );
105
106 for ( $day = 0; $day <= 6; $day ++ ) {
107
108 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
109 $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 ) ) ) ) );
110
111 $data['data']['labels'][] = date_i18n( 'j', $date );
112 $data['data']['datasets'][$type_name]['label'] = __( 'Post Views', 'post-views-counter' );
113 $data['data']['datasets'][0]['data'][] = $query->total_views;
114 }
115 break;
116
117 case 'this_year':
118 $data = array(
119 'text' => array(
120 'xAxes' => __( 'Year', 'post-views-counter' ) . date( ' Y' ),
121 'yAxes' => __( 'Post Views', 'post-views-counter' ),
122 ),
123 'design' => array(
124 'fill' => true,
125 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
126 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
127 'borderWidth' => 1.2,
128 'borderDash' => array(),
129 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
130 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
131 'pointBorderWidth' => 1.2
132 )
133 );
134
135 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
136
137 // get data for specific post types
138 $empty_post_type_views = array();
139
140 // reindex post types
141 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
142
143 $post_type_data = array();
144
145 foreach ( $post_types as $id => $post_type ) {
146 $empty_post_type_views[$post_type] = 0;
147 $post_type_obj = get_post_type_object( $post_type );
148
149 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
150 $data['data']['datasets'][$id]['data'] = array();
151
152 // get month views
153 $post_type_data[$id] = array_values(
154 pvc_get_views(
155 array(
156 'fields' => 'date=>views',
157 'post_type' => $post_type,
158 'views_query' => array(
159 'year' => date( 'Y' ),
160 'month' => '',
161 'week' => '',
162 'day' => ''
163 )
164 )
165 )
166 );
167 }
168
169 $sum = array();
170
171 foreach ( $post_type_data as $post_type_id => $post_views ) {
172 foreach ( $post_views as $id => $views ) {
173 // generate chart data for specific post types
174 $data['data']['datasets'][$post_type_id]['data'][] = $views;
175 $sum[$id] += $views;
176 }
177 }
178
179 // this month all days
180 for ( $i = 1; $i <= 12; $i ++ ) {
181 // generate chart data
182 $data['data']['labels'][] = $i;
183 $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) );
184 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
185 }
186 break;
187
188 case 'this_month':
189 default :
190 $data = array(
191 'text' => array(
192 'xAxes' => date_i18n( 'F Y' ),
193 'yAxes' => __( 'Post Views', 'post-views-counter' ),
194 ),
195 'design' => array(
196 'fill' => true,
197 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)',
198 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
199 'borderWidth' => 1.2,
200 'borderDash' => array(),
201 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)',
202 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
203 'pointBorderWidth' => 1.2
204 )
205 );
206
207 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
208
209 // get data for specific post types
210 $empty_post_type_views = array();
211
212 // reindex post types
213 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
214
215 $post_type_data = array();
216
217 foreach ( $post_types as $id => $post_type ) {
218 $empty_post_type_views[$post_type] = 0;
219 $post_type_obj = get_post_type_object( $post_type );
220
221 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
222 $data['data']['datasets'][$id]['data'] = array();
223
224 // get month views
225 $post_type_data[$id] = array_values(
226 pvc_get_views(
227 array(
228 'fields' => 'date=>views',
229 'post_type' => $post_type,
230 'views_query' => array(
231 'year' => date( 'Y' ),
232 'month' => date( 'm' ),
233 'week' => '',
234 'day' => ''
235 )
236 )
237 )
238 );
239 }
240
241 $sum = array();
242
243 foreach ( $post_type_data as $post_type_id => $post_views ) {
244 foreach ( $post_views as $id => $views ) {
245 // generate chart data for specific post types
246 $data['data']['datasets'][$post_type_id]['data'][] = $views;
247 $sum[$id] += $views;
248 }
249 }
250
251 // this month all days
252 for ( $i = 1; $i <= date( 't' ); $i ++ ) {
253 // generate chart data
254 $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i );
255 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( date( 'Y' ) . '-' . date( 'm' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
256 $data['data']['datasets'][0]['data'][] = $sum[$i - 1];
257 }
258 break;
259 }
260
261 echo json_encode( $data );
262
263 exit;
264 }
265
266 /**
267 * Enqueue admin scripts and styles.
268 *
269 * @param string $pagenow
270 */
271 public function admin_scripts_styles( $pagenow ) {
272 if ( $pagenow != 'index.php' )
273 return;
274
275 // filter user_can_see_stats
276 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) {
277 return;
278 }
279
280 wp_register_style(
281 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css'
282 );
283 wp_enqueue_style( 'pvc-admin-dashboard' );
284
285 wp_register_script(
286 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chart' ), Post_Views_Counter()->defaults['version'], true
287 );
288
289 wp_register_script(
290 'pvc-chart', POST_VIEWS_COUNTER_URL . '/js/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true
291 );
292
293 // set ajax args
294 $ajax_args = array(
295 'ajaxURL' => admin_url( 'admin-ajax.php' ),
296 'nonce' => wp_create_nonce( 'dashboard-chart' )
297 );
298
299 wp_enqueue_script( 'pvc-admin-dashboard' );
300 // wp_enqueue_script( 'pvc-chart' );
301
302 wp_localize_script(
303 'pvc-admin-dashboard', 'pvcArgs', $ajax_args
304 );
305 }
306
307 /**
308 * Convert hex to rgb color.
309 *
310 * @param type $color
311 * @return boolean
312 */
313 public function hex2rgb( $color ) {
314 if ( $color[0] == '#' ) {
315 $color = substr( $color, 1 );
316 }
317 if ( strlen( $color ) == 6 ) {
318 list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
319 } elseif ( strlen( $color ) == 3 ) {
320 list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
321 } else {
322 return false;
323 }
324 $r = hexdec( $r );
325 $g = hexdec( $g );
326 $b = hexdec( $b );
327 return array( 'r' => $r, 'g' => $g, 'b' => $b );
328 }
329
330 }
331