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