| 1 |
<?php |
| 2 |
/** |
| 3 |
* Statify: Statify_Dashboard class |
| 4 |
* |
| 5 |
* This file contains the derived class for the plugin's dashboard features. |
| 6 |
* |
| 7 |
* @package Statify |
| 8 |
* @since 1.1 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Quit if accessed outside WP context. |
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Statify_Dashboard |
| 16 |
* |
| 17 |
* @since 1.1 |
| 18 |
*/ |
| 19 |
class Statify_Dashboard extends Statify { |
| 20 |
|
| 21 |
/** |
| 22 |
* Plugin version. |
| 23 |
* |
| 24 |
* @since 1.4.0 |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected static $_plugin_version; |
| 28 |
|
| 29 |
/** |
| 30 |
* Dashboard widget initialize |
| 31 |
* |
| 32 |
* @since 0.1.0 |
| 33 |
* @version 2016-12-21 |
| 34 |
* |
| 35 |
* @wp-hook boolean statify__user_can_see_stats |
| 36 |
* @see https://wordpress.org/plugins/statify/ |
| 37 |
*/ |
| 38 |
public static function init() { |
| 39 |
|
| 40 |
// Filter user_can_see_stats. |
| 41 |
if ( ! apply_filters( 'statify__user_can_see_stats', current_user_can( 'edit_dashboard' ) ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
// Load textdomain. |
| 46 |
load_plugin_textdomain( |
| 47 |
'statify', |
| 48 |
false, |
| 49 |
wp_normalize_path( sprintf( '%s/lang', STATIFY_DIR ) ) |
| 50 |
); |
| 51 |
|
| 52 |
// Plugin version. |
| 53 |
self::_get_version(); |
| 54 |
|
| 55 |
// Add dashboard widget. |
| 56 |
wp_add_dashboard_widget( |
| 57 |
'statify_dashboard', |
| 58 |
'Statify', |
| 59 |
array( __CLASS__, 'print_frontview' ), |
| 60 |
array( __CLASS__, 'print_backview' ) |
| 61 |
); |
| 62 |
|
| 63 |
// Init CSS. |
| 64 |
add_action( 'admin_print_styles', array( __CLASS__, 'add_style' ) ); |
| 65 |
|
| 66 |
// Init JS. |
| 67 |
add_action( 'admin_print_scripts', array( __CLASS__, 'add_js' ) ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Print CSS |
| 72 |
* |
| 73 |
* @since 0.1.0 |
| 74 |
* @version 1.4.0 |
| 75 |
*/ |
| 76 |
public static function add_style() { |
| 77 |
|
| 78 |
// Register CSS. |
| 79 |
wp_register_style( |
| 80 |
'chartist_css', |
| 81 |
plugins_url( '/css/chartist.min.css', STATIFY_FILE ), |
| 82 |
array(), |
| 83 |
self::$_plugin_version |
| 84 |
); |
| 85 |
wp_register_style( |
| 86 |
'chartist_tooltip_css', |
| 87 |
plugins_url( '/css/chartist-plugin-tooltip.min.css', STATIFY_FILE ), |
| 88 |
array(), |
| 89 |
self::$_plugin_version |
| 90 |
); |
| 91 |
wp_register_style( |
| 92 |
'statify', |
| 93 |
plugins_url( '/css/dashboard.min.css', STATIFY_FILE ), |
| 94 |
array(), |
| 95 |
self::$_plugin_version |
| 96 |
); |
| 97 |
|
| 98 |
// Load CSS. |
| 99 |
wp_enqueue_style( 'chartist_css' ); |
| 100 |
wp_enqueue_style( 'chartist_tooltip_css' ); |
| 101 |
wp_enqueue_style( 'statify' ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Print JavaScript |
| 106 |
* |
| 107 |
* @since 0.1.0 |
| 108 |
* @version 1.4.0 |
| 109 |
*/ |
| 110 |
public static function add_js() { |
| 111 |
|
| 112 |
// Register JS. |
| 113 |
wp_register_script( |
| 114 |
'chartist_js', |
| 115 |
plugins_url( 'js/chartist.min.js', STATIFY_FILE ), |
| 116 |
array(), |
| 117 |
self::$_plugin_version, |
| 118 |
true |
| 119 |
); |
| 120 |
wp_register_script( |
| 121 |
'chartist_tooltip_js', |
| 122 |
plugins_url( 'js/chartist-plugin-tooltip.min.js', STATIFY_FILE ), |
| 123 |
array( 'chartist_js' ), |
| 124 |
self::$_plugin_version, |
| 125 |
true |
| 126 |
); |
| 127 |
wp_register_script( |
| 128 |
'statify_chart_js', |
| 129 |
plugins_url( |
| 130 |
'js/dashboard.min.js', |
| 131 |
STATIFY_FILE |
| 132 |
), |
| 133 |
array( 'jquery', 'chartist_tooltip_js' ), |
| 134 |
self::$_plugin_version, |
| 135 |
true |
| 136 |
); |
| 137 |
|
| 138 |
// Localize strings. |
| 139 |
wp_localize_script( |
| 140 |
'statify_chart_js', |
| 141 |
'statify_translations', |
| 142 |
array( |
| 143 |
'pageview' => strip_tags( esc_html__( 'Pageview', 'statify' ) ), |
| 144 |
'pageviews' => strip_tags( esc_html__( 'Pageviews', 'statify' ) ), |
| 145 |
) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Print widget frontview. |
| 152 |
* |
| 153 |
* @since 0.1.0 |
| 154 |
* @version 1.4.0 |
| 155 |
*/ |
| 156 |
public static function print_frontview() { |
| 157 |
|
| 158 |
// Load JS. |
| 159 |
wp_enqueue_script( 'chartist_js' ); |
| 160 |
wp_enqueue_script( 'statify_chart_js' ); |
| 161 |
|
| 162 |
// Load template. |
| 163 |
load_template( |
| 164 |
wp_normalize_path( sprintf( '%s/views/widget-front.php', STATIFY_DIR ) ) |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Print widget backview |
| 171 |
* |
| 172 |
* @since 0.4.0 |
| 173 |
* @version 1.4.0 |
| 174 |
*/ |
| 175 |
public static function print_backview() { |
| 176 |
|
| 177 |
// Capability check. |
| 178 |
if ( ! current_user_can( 'edit_dashboard' ) ) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
// Update plugin options. |
| 183 |
if ( ! empty( $_POST['statify'] ) ) { |
| 184 |
check_admin_referer( 'statify-dashboard' ); |
| 185 |
|
| 186 |
self::_save_options(); |
| 187 |
} |
| 188 |
|
| 189 |
// Load view. |
| 190 |
load_template( |
| 191 |
wp_normalize_path( sprintf( '%s/views/widget-back.php', STATIFY_DIR ) ) |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Save plugin options |
| 198 |
* |
| 199 |
* @since 1.4.0 |
| 200 |
* @version 2017-01-10 |
| 201 |
*/ |
| 202 |
private static function _save_options() { |
| 203 |
// Check the nonce field from the dashboard form. |
| 204 |
if ( ! check_admin_referer( 'statify-dashboard' ) ) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
// Get numeric values from POST variables. |
| 209 |
$options = array(); |
| 210 |
foreach ( array( 'days', 'limit' ) as $option_name ) { |
| 211 |
$options[ $option_name ] = Statify::$_options[ $option_name ]; |
| 212 |
if ( isset( $_POST['statify'][ $option_name ] ) && (int) $_POST['statify'][ $option_name ] > 0 ) { |
| 213 |
$options[ $option_name ] = (int) $_POST['statify'][ $option_name ]; |
| 214 |
} |
| 215 |
} |
| 216 |
if ( (int) $options['limit'] > 100 ) { |
| 217 |
$options['limit'] = 100; |
| 218 |
} |
| 219 |
|
| 220 |
// Get checkbox values from POST variables. |
| 221 |
foreach ( array( 'today', 'snippet', 'blacklist' ) as $option_name ) { |
| 222 |
if ( isset( $_POST['statify'][ $option_name ] ) && 1 === (int) $_POST['statify'][ $option_name ] ) { |
| 223 |
$options[ $option_name ] = 1; |
| 224 |
} else { |
| 225 |
$options[ $option_name ] = 0; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// Update values. |
| 230 |
update_option( 'statify', $options ); |
| 231 |
|
| 232 |
// Delete transient. |
| 233 |
delete_transient( 'statify_data' ); |
| 234 |
|
| 235 |
// Clear Cachify cache. |
| 236 |
if ( has_action( 'cachify_flush_cache' ) ) { |
| 237 |
do_action( 'cachify_flush_cache' ); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
/** |
| 243 |
* Set plugin version from plugin meta data |
| 244 |
* |
| 245 |
* @since 1.4.0 |
| 246 |
* @version 1.4.0 |
| 247 |
*/ |
| 248 |
private static function _get_version() { |
| 249 |
|
| 250 |
// Get plugin meta. |
| 251 |
$meta = get_plugin_data( STATIFY_FILE ); |
| 252 |
|
| 253 |
self::$_plugin_version = $meta['Version']; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* Get stats from cache |
| 259 |
* |
| 260 |
* @since 0.1.0 |
| 261 |
* @version 1.4.0 |
| 262 |
* |
| 263 |
* @return array $data Data from cache or DB |
| 264 |
*/ |
| 265 |
public static function get_stats() { |
| 266 |
|
| 267 |
// Get from cache. |
| 268 |
$data = get_transient( 'statify_data' ); |
| 269 |
if ( $data ) { |
| 270 |
return $data; |
| 271 |
} |
| 272 |
|
| 273 |
// Get from DB. |
| 274 |
$data = self::_select_data(); |
| 275 |
|
| 276 |
// Prepare data. |
| 277 |
if ( ! empty( $data['visits'] ) ) { |
| 278 |
$data['visits'] = array_reverse( $data['visits'] ); |
| 279 |
} else { |
| 280 |
$data = null; |
| 281 |
} |
| 282 |
|
| 283 |
// Make cache. |
| 284 |
set_transient( |
| 285 |
'statify_data', |
| 286 |
$data, |
| 287 |
MINUTE_IN_SECONDS * 4 |
| 288 |
); |
| 289 |
|
| 290 |
return $data; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
/** |
| 295 |
* Get stats from DB |
| 296 |
* |
| 297 |
* @since 0.1.0 |
| 298 |
* @version 1.4.0 |
| 299 |
* |
| 300 |
* @return array DB results |
| 301 |
*/ |
| 302 |
private static function _select_data() { |
| 303 |
|
| 304 |
// Global. |
| 305 |
global $wpdb; |
| 306 |
|
| 307 |
// Init values. |
| 308 |
$days = (int) self::$_options['days']; |
| 309 |
$limit = (int) self::$_options['limit']; |
| 310 |
$today = (int) self::$_options['today']; |
| 311 |
|
| 312 |
return array( |
| 313 |
'visits' => $wpdb->get_results( |
| 314 |
$wpdb->prepare( |
| 315 |
"SELECT `created` as `date`, COUNT(`created`) as `count` FROM `$wpdb->statify` GROUP BY `created` ORDER BY `created` DESC LIMIT %d", |
| 316 |
$days |
| 317 |
), |
| 318 |
ARRAY_A |
| 319 |
), |
| 320 |
'target' => $wpdb->get_results( |
| 321 |
$wpdb->prepare( |
| 322 |
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` " . ( $today ? 'WHERE created = DATE(NOW())' : '' ) . ' GROUP BY `target` ORDER BY `count` DESC LIMIT %d', |
| 323 |
$limit |
| 324 |
), |
| 325 |
ARRAY_A |
| 326 |
), |
| 327 |
'referrer' => $wpdb->get_results( |
| 328 |
$wpdb->prepare( |
| 329 |
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' " . ( $today ? 'AND created = DATE(NOW())' : '' ) . ' GROUP BY `host` ORDER BY `count` DESC LIMIT %d', |
| 330 |
$limit |
| 331 |
), |
| 332 |
ARRAY_A |
| 333 |
), |
| 334 |
); |
| 335 |
} |
| 336 |
} |
| 337 |
|