columns.php
8 years ago
counter.php
8 years ago
crawler-detect.php
8 years ago
cron.php
8 years ago
dashboard.php
8 years ago
frontend.php
8 years ago
functions.php
8 years ago
query.php
8 years ago
settings.php
8 years ago
update.php
8 years ago
widgets.php
8 years ago
dashboard.php
339 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 | $range = 'this_month'; |
| 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 | |
| 176 | if ( ! array_key_exists( $id, $sum ) ) |
| 177 | $sum[$id] = 0; |
| 178 | |
| 179 | $sum[$id] += $views; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // this month all days |
| 184 | for ( $i = 1; $i <= 12; $i ++ ) { |
| 185 | // generate chart data |
| 186 | $data['data']['labels'][] = $i; |
| 187 | $data['data']['dates'][] = date_i18n( 'F Y', strtotime( date( 'Y' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) . '-01' ) ); |
| 188 | $data['data']['datasets'][0]['data'][] = $sum[$i - 1]; |
| 189 | } |
| 190 | break; |
| 191 | |
| 192 | case 'this_month': |
| 193 | default : |
| 194 | $data = array( |
| 195 | 'text' => array( |
| 196 | 'xAxes' => date_i18n( 'F Y' ), |
| 197 | 'yAxes' => __( 'Post Views', 'post-views-counter' ), |
| 198 | ), |
| 199 | 'design' => array( |
| 200 | 'fill' => true, |
| 201 | 'backgroundColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 0.2)', |
| 202 | 'borderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)', |
| 203 | 'borderWidth' => 1.2, |
| 204 | 'borderDash' => array(), |
| 205 | 'pointBorderColor' => 'rgba(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ', 1)', |
| 206 | 'pointBackgroundColor' => 'rgba(255, 255, 255, 1)', |
| 207 | 'pointBorderWidth' => 1.2 |
| 208 | ) |
| 209 | ); |
| 210 | |
| 211 | $data['data']['datasets'][0]['label'] = __( 'Total Views', 'post-views-counter' ); |
| 212 | |
| 213 | // get data for specific post types |
| 214 | $empty_post_type_views = array(); |
| 215 | |
| 216 | // reindex post types |
| 217 | $post_types = array_combine( range( 1, count( $post_types ) ), array_values( $post_types ) ); |
| 218 | |
| 219 | $post_type_data = array(); |
| 220 | |
| 221 | foreach ( $post_types as $id => $post_type ) { |
| 222 | $empty_post_type_views[$post_type] = 0; |
| 223 | $post_type_obj = get_post_type_object( $post_type ); |
| 224 | |
| 225 | $data['data']['datasets'][$id]['label'] = $post_type_obj->labels->name; |
| 226 | $data['data']['datasets'][$id]['data'] = array(); |
| 227 | |
| 228 | // get month views |
| 229 | $post_type_data[$id] = array_values( |
| 230 | pvc_get_views( |
| 231 | array( |
| 232 | 'fields' => 'date=>views', |
| 233 | 'post_type' => $post_type, |
| 234 | 'views_query' => array( |
| 235 | 'year' => date( 'Y' ), |
| 236 | 'month' => date( 'm' ), |
| 237 | 'week' => '', |
| 238 | 'day' => '' |
| 239 | ) |
| 240 | ) |
| 241 | ) |
| 242 | ); |
| 243 | } |
| 244 | |
| 245 | $sum = array(); |
| 246 | |
| 247 | foreach ( $post_type_data as $post_type_id => $post_views ) { |
| 248 | foreach ( $post_views as $id => $views ) { |
| 249 | // generate chart data for specific post types |
| 250 | $data['data']['datasets'][$post_type_id]['data'][] = $views; |
| 251 | |
| 252 | if ( ! array_key_exists( $id, $sum ) ) |
| 253 | $sum[$id] = 0; |
| 254 | |
| 255 | $sum[$id] += $views; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // this month all days |
| 260 | for ( $i = 1; $i <= date( 't' ); $i ++ ) { |
| 261 | // generate chart data |
| 262 | $data['data']['labels'][] = ( $i % 2 === 0 ? '' : $i ); |
| 263 | $data['data']['dates'][] = date_i18n( get_option( 'date_format' ), strtotime( date( 'Y' ) . '-' . date( 'm' ) . '-' . str_pad( $i, 2, '0', STR_PAD_LEFT ) ) ); |
| 264 | $data['data']['datasets'][0]['data'][] = $sum[$i - 1]; |
| 265 | } |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | echo json_encode( $data ); |
| 270 | |
| 271 | exit; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Enqueue admin scripts and styles. |
| 276 | * |
| 277 | * @param string $pagenow |
| 278 | */ |
| 279 | public function admin_scripts_styles( $pagenow ) { |
| 280 | if ( $pagenow != 'index.php' ) |
| 281 | return; |
| 282 | |
| 283 | // filter user_can_see_stats |
| 284 | if ( ! apply_filters( 'pvc_user_can_see_stats', current_user_can( 'publish_posts' ) ) ) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | wp_register_style( |
| 289 | 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/css/admin-dashboard.css' |
| 290 | ); |
| 291 | wp_enqueue_style( 'pvc-admin-dashboard' ); |
| 292 | |
| 293 | wp_register_script( |
| 294 | 'pvc-admin-dashboard', POST_VIEWS_COUNTER_URL . '/js/admin-dashboard.js', array( 'jquery', 'pvc-chart' ), Post_Views_Counter()->defaults['version'], true |
| 295 | ); |
| 296 | |
| 297 | wp_register_script( |
| 298 | 'pvc-chart', POST_VIEWS_COUNTER_URL . '/js/chart.min.js', array( 'jquery' ), Post_Views_Counter()->defaults['version'], true |
| 299 | ); |
| 300 | |
| 301 | // set ajax args |
| 302 | $ajax_args = array( |
| 303 | 'ajaxURL' => admin_url( 'admin-ajax.php' ), |
| 304 | 'nonce' => wp_create_nonce( 'dashboard-chart' ) |
| 305 | ); |
| 306 | |
| 307 | wp_enqueue_script( 'pvc-admin-dashboard' ); |
| 308 | // wp_enqueue_script( 'pvc-chart' ); |
| 309 | |
| 310 | wp_localize_script( |
| 311 | 'pvc-admin-dashboard', 'pvcArgs', $ajax_args |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Convert hex to rgb color. |
| 317 | * |
| 318 | * @param type $color |
| 319 | * @return boolean |
| 320 | */ |
| 321 | public function hex2rgb( $color ) { |
| 322 | if ( $color[0] == '#' ) { |
| 323 | $color = substr( $color, 1 ); |
| 324 | } |
| 325 | if ( strlen( $color ) == 6 ) { |
| 326 | list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); |
| 327 | } elseif ( strlen( $color ) == 3 ) { |
| 328 | list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); |
| 329 | } else { |
| 330 | return false; |
| 331 | } |
| 332 | $r = hexdec( $r ); |
| 333 | $g = hexdec( $g ); |
| 334 | $b = hexdec( $b ); |
| 335 | return array( 'r' => $r, 'g' => $g, 'b' => $b ); |
| 336 | } |
| 337 | |
| 338 | } |
| 339 |