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