PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.4
Post Views Counter v1.2.4
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
232 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 * @return mixed
59 */
60 public function dashboard_widget_chart() {
61
62 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) )
63 wp_die( _( 'You do not have permission to access this page.', 'post-views-counter' ) );
64
65 if ( ! check_ajax_referer( 'dashboard-chart', 'nonce' ) )
66 wp_die( __( 'You do not have permission to access this page.', 'post-views-counter' ) );
67
68 $period = isset( $_REQUEST['period'] ) ? esc_attr( $_REQUEST['period'] ) : 'this_month';
69
70 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
71
72 // get stats
73 $query_args = array(
74 'post_type' => $post_types,
75 'posts_per_page' => -1,
76 'paged' => false,
77 'orderby' => 'post_views',
78 'suppress_filters' => false,
79 'no_found_rows' => true
80 );
81
82 // set range
83 $range = 'this_month';
84 $now = getdate( current_time( 'timestamp', get_option( 'gmt_offset' ) ) );
85
86 // set chart labels
87 switch ( $range ) {
88 case 'this_week' :
89 $data = array(
90 'text' => array(
91 'xAxes' => date_i18n( 'F Y' ),
92 'yAxes' => __( 'Post Views', 'post-views-counter' )
93 )
94 );
95
96 for ( $day = 0; $day <= 6; $day ++ ) {
97
98 $date = strtotime( $now['mday'] . '-' . $now['mon'] . '-' . $now['year'] . ' + ' . $day . ' days - ' . $now['wday'] . ' days' );
99 $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 ) ) ) ) );
100
101 $data['data']['labels'][] = date_i18n( 'j', $date );
102 $data['data']['datasets'][$type_name]['label'] = __( 'Post Views', 'post-views-counter' );
103 $data['data']['datasets'][0]['data'][] = $query->total_views;
104 }
105 break;
106
107 case 'this month' :
108 default :
109 $data = array(
110 'text' => array(
111 'xAxes' => date_i18n( 'F Y' ),
112 'yAxes' => __( 'Post Views', 'post-views-counter' ),
113 ),
114 'design' => array(
115 'fill' => true,
116 'backgroundColor' => 'rgba(50, 143, 186, 0.2)',
117 'borderColor' => 'rgba(50, 143, 186, 1)',
118 'borderWidth' => 2,
119 'borderDash' => array(),
120 'pointBorderColor' => 'rgba(50, 143, 186, 1)',
121 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)',
122 'pointBorderWidth' => 1.2
123 )
124 );
125
126 $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' );
127
128 // get data for specific post types
129 $empty_post_type_views = array();
130
131 // reindex post types
132 $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) );
133
134 foreach ( $post_types as $id => $post_type ) {
135 $empty_post_type_views[$post_type] = 0;
136 $post_type_obj = get_post_type_object( $post_type );
137
138 $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name;
139 $data['data']['datasets'][$id]['data'] = array();
140 }
141
142 // reverse post types
143 $rev_post_types = array_flip( $post_types );
144
145 // this month day loop
146 for ( $i = 1; $i <= date( 't' ); $i ++ ) {
147
148 if ( $i <= $now['mday'] ) {
149
150 $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 ) ) ) ) );
151
152 // get data for specific post types
153 $post_type_views = $empty_post_type_views;
154
155 if ( ! empty( $query->posts ) ) {
156 foreach ( $query->posts as $index => $post ) {
157 $post_type_views[$post->post_type] += $post->post_views;
158 }
159 }
160 } else {
161
162 $post_type_views = $empty_post_type_views;
163
164 foreach ( $post_types as $id => $post_type ) {
165 $post_type_views[$post_type] = 0;
166 }
167
168 $query->total_views = 0;
169 }
170
171 // generate chart data
172 $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i );
173 $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( date( 'Y' ) . '-' . date( 'n' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) );
174 $data['data']['datasets'][0]['data'][] = $query->total_views;
175
176 // generate chart data for specific post types
177 foreach ( $post_type_views as $type_name => $type_views ) {
178 $data['data']['datasets'][$rev_post_types[$type_name]]['data'][] = $type_views;
179 }
180 }
181
182 break;
183 }
184
185 echo json_encode( $data );
186
187 exit;
188 }
189
190 /**
191 * Enqueue admin scripts and styles.
192 *
193 * @param string $pagenow
194 */
195 public function admin_scripts_styles( $pagenow ) {
196 if ( $pagenow != 'index.php' )
197 return;
198
199 // filter user_can_see_stats
200 if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) {
201 return;
202 }
203
204 wp_register_style(
205 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css'
206 );
207 wp_enqueue_style( 'pvc-admin-dashboard' );
208
209 wp_register_script(
210 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chart' ), Post_Views_Counter()->defaults['version'], true
211 );
212
213 wp_register_script(
214 'pvc-chart', POST_VIEWS_COUNTER_URL . '/js/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true
215 );
216
217 // set ajax args
218 $ajax_args = array(
219 'ajaxURL' => admin_url( 'admin-ajax.php' ),
220 'nonce' => wp_create_nonce( 'dashboard-chart' )
221 );
222
223 wp_enqueue_script( 'pvc-admin-dashboard' );
224 // wp_enqueue_script( 'pvc-chart' );
225
226 wp_localize_script(
227 'pvc-admin-dashboard', 'pvcArgs', $ajax_args
228 );
229 }
230
231 }
232