| 1 |
<?php |
| 2 |
/* |
| 3 |
* Plugin Name: Analytify - Google Analytics Dashboard |
| 4 |
* Plugin URI: http://wp-analytify.com/ |
| 5 |
* Description: Analytify makes Google Analytics simple for everything in WordPress (posts,pages etc). It presents the statistics in a beautiful way under the WordPress Posts/Pages at front end, backend and in its own Dashboard. This provides Stats from Country, Referrers, Social media, General stats, New visitors, Returning visitors, Exit pages, Browser wise and Top keywords. This plugin provides the RealTime statistics in a new UI that is easy to understand and looks good. |
| 6 |
* Version: 1.0.0 |
| 7 |
* Author: WPBrigade |
| 8 |
* Author URI: http://wpbrigade.com/ |
| 9 |
* License: GPLv2+ |
| 10 |
* Text Domain: wp-analytify |
| 11 |
* Min WP Version: 3.0.1 |
| 12 |
* Max WP Version: 4.1 |
| 13 |
* Domain Path: /lang |
| 14 |
*/ |
| 15 |
|
| 16 |
// Exit if accessed directly |
| 17 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 18 |
|
| 19 |
ini_set( 'include_path', dirname(__FILE__) . '/lib/' ); |
| 20 |
|
| 21 |
if ( !class_exists( 'WP_Analytify' ) ) { |
| 22 |
|
| 23 |
if ( !class_exists( 'Analytify_General' ) ){ |
| 24 |
|
| 25 |
require_once WP_PLUGIN_DIR .'/wp-analytify/analytify-general.php'; |
| 26 |
} |
| 27 |
|
| 28 |
class WP_Analytify extends Analytify_General{ |
| 29 |
|
| 30 |
public $token = false; |
| 31 |
public $client = null; |
| 32 |
|
| 33 |
// Constructor |
| 34 |
function __construct() { |
| 35 |
|
| 36 |
parent::__construct(); |
| 37 |
|
| 38 |
if ( !class_exists( 'Analytify_Google_Client' ) ) { |
| 39 |
|
| 40 |
require_once ANALYTIFY_LIB_PATH . 'Google/Client.php'; |
| 41 |
require_once ANALYTIFY_LIB_PATH . 'Google/Service/Analytics.php'; |
| 42 |
} |
| 43 |
|
| 44 |
add_action( 'plugin_action_links', array( |
| 45 |
$this, |
| 46 |
'pa_plugin_links' |
| 47 |
),10,2); |
| 48 |
|
| 49 |
add_action( 'plugin_row_meta', array( |
| 50 |
$this, |
| 51 |
'analytify_plugin_row_meta' |
| 52 |
),10,2); |
| 53 |
|
| 54 |
add_action( 'admin_enqueue_scripts', array( |
| 55 |
$this, |
| 56 |
'pa_scripts' |
| 57 |
)); |
| 58 |
add_action( 'admin_enqueue_scripts', array( |
| 59 |
$this, |
| 60 |
'pa_styles' |
| 61 |
)); |
| 62 |
|
| 63 |
|
| 64 |
add_action( 'wp_enqueue_scripts', array( |
| 65 |
$this, |
| 66 |
'pa_front_scripts' |
| 67 |
)); |
| 68 |
add_action( 'wp_enqueue_scripts', array( |
| 69 |
$this, |
| 70 |
'pa_front_styles' |
| 71 |
)); |
| 72 |
|
| 73 |
add_action( 'admin_menu', array( |
| 74 |
$this, |
| 75 |
'wpa_add_menu' |
| 76 |
)); |
| 77 |
|
| 78 |
// Insert Google Analytics Code |
| 79 |
if( get_option( 'analytify_code') == 1 ) { |
| 80 |
|
| 81 |
add_action( 'wp_head', array( |
| 82 |
$this, |
| 83 |
'analytify_add_analytics_code' |
| 84 |
)); |
| 85 |
} |
| 86 |
|
| 87 |
add_action( 'wp_ajax_nopriv_get_ajax_single_admin_analytics', array( |
| 88 |
$this, |
| 89 |
'get_ajax_single_admin_analytics' |
| 90 |
)); |
| 91 |
|
| 92 |
add_action( 'wp_ajax_get_ajax_single_admin_analytics', array( |
| 93 |
$this, |
| 94 |
'get_ajax_single_admin_analytics' |
| 95 |
)); |
| 96 |
|
| 97 |
add_action( 'wp_ajax_get_ajax_secret_keys', array( |
| 98 |
$this, |
| 99 |
'get_ajax_secret_keys' |
| 100 |
)); |
| 101 |
|
| 102 |
if( get_option( 'analytify_disable_front') == 0 ) { |
| 103 |
|
| 104 |
add_filter( 'the_content', array( |
| 105 |
$this, |
| 106 |
'get_single_front_analytics' |
| 107 |
)); |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
* load Analytics under the EDIT POST Screen |
| 112 |
* add action runs only for admin section and load metabox. |
| 113 |
*/ |
| 114 |
|
| 115 |
if ( is_admin() ) { |
| 116 |
add_action( 'load-post.php', array( |
| 117 |
$this, |
| 118 |
'load_metaboxes' |
| 119 |
)); |
| 120 |
} |
| 121 |
|
| 122 |
// Show welcome message when user activate plugin. |
| 123 |
if ( get_option( 'pa_welcome_message' ) == 0 ) { |
| 124 |
|
| 125 |
add_action( 'admin_print_footer_scripts', array( |
| 126 |
$this, |
| 127 |
'pa_welcome_message' |
| 128 |
) ); |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* localization language file filter |
| 133 |
*/ |
| 134 |
|
| 135 |
add_action( 'init', array( |
| 136 |
$this, |
| 137 |
'analytify_textdomain') ); |
| 138 |
|
| 139 |
//add_action( 'admin_footer', array( &$this, 'profile_warning' ) ); |
| 140 |
|
| 141 |
register_activation_hook( __FILE__, array( $this, 'install' ) ); |
| 142 |
register_deactivation_hook( __FILE__, array( $this, 'uninstall' ) ); |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* Set plugin language/localizations files directory |
| 147 |
*/ |
| 148 |
public function analytify_textdomain() { |
| 149 |
|
| 150 |
$plugin_dir = basename( dirname(__FILE__) ); |
| 151 |
load_plugin_textdomain( 'wp-analytify', false , $plugin_dir . '/lang/'); |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
* Show analytics sections under the posts/pages in the metabox. |
| 157 |
*/ |
| 158 |
|
| 159 |
function load_metaboxes() { |
| 160 |
|
| 161 |
add_action( 'add_meta_boxes', array( |
| 162 |
$this, |
| 163 |
'show_admin_single_analytics_add_metabox' |
| 164 |
)); |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
* Show metabox under each Post type to display Analytics of single post/page in wp-admin |
| 169 |
*/ |
| 170 |
public function show_admin_single_analytics_add_metabox() { |
| 171 |
|
| 172 |
$post_types = get_option( 'analytify_posts_stats' ); |
| 173 |
|
| 174 |
foreach ( $post_types as $post_type ) { |
| 175 |
|
| 176 |
add_meta_box('pa-single-admin-analytics', // $id |
| 177 |
'Analytify: Google Analytics of this page.', // $title |
| 178 |
array( |
| 179 |
'WP_Analytify', |
| 180 |
'show_admin_single_analytics' |
| 181 |
), // $callback |
| 182 |
|
| 183 |
$post_type, // $posts |
| 184 |
'normal', // $context |
| 185 |
'high' // $priority |
| 186 |
); |
| 187 |
} //$post_types as $post_type |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
public static function get_ajax_secret_keys(){ |
| 192 |
|
| 193 |
$response = wp_remote_get( "http://wp-analytify.com/secret/keys.json" ); |
| 194 |
if( is_wp_error( $response ) ) { |
| 195 |
$error_message = $response->get_error_message(); |
| 196 |
echo "Something went wrong: $error_message"; |
| 197 |
} else { |
| 198 |
print_r($response['body']); |
| 199 |
} |
| 200 |
die(); |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
* Show Analytics of single post/page in wp-admin under EDIT screen. |
| 205 |
*/ |
| 206 |
public static function show_admin_single_analytics() { |
| 207 |
|
| 208 |
global $post; |
| 209 |
|
| 210 |
if ( is_array( get_option( 'post_analytics_exclude_posts_back' ) ) ) { |
| 211 |
|
| 212 |
if ( in_array( $post->ID, get_option( 'post_analytics_exclude_posts_back' ) ) ) { |
| 213 |
|
| 214 |
_e('This post is exclude from stats'); |
| 215 |
|
| 216 |
return; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$urlPost = ''; |
| 221 |
$wp_analytify = new WP_Analytify(); |
| 222 |
$urlPost = parse_url( get_permalink( $post->ID ) ); |
| 223 |
$start_date = ''; $end_date = ''; $urlpost = '' ; |
| 224 |
$is_access_level = get_option( 'post_analytics_access_back' ); |
| 225 |
|
| 226 |
if( $wp_analytify->pa_check_roles( $is_access_level ) ){ ?> |
| 227 |
|
| 228 |
<div class="pa-filter"> |
| 229 |
<table cellspacing="0" cellpadding="0" width="400"> |
| 230 |
<tbody> |
| 231 |
<tr> |
| 232 |
<td width="0"> |
| 233 |
<input type="text" id="start_date" name="start_date"> |
| 234 |
</td> |
| 235 |
<td width="0"> |
| 236 |
<input type="text" id="end_date" name="end_date"> |
| 237 |
</td> |
| 238 |
<input type="hidden" name="urlpost" id="urlpost" value="<?php echo $urlPost['path']; ?>"> |
| 239 |
<td width="0"> |
| 240 |
<input type="button" id="view_analytics" name="view_analytics" value="View Analytics" class="button-primary btn-green"> |
| 241 |
</td> |
| 242 |
</tr> |
| 243 |
</tbody> |
| 244 |
</table> |
| 245 |
</div> |
| 246 |
<div class="loading" style="display:none"> |
| 247 |
<img src="<?php echo plugins_url('images/loading.gif', __FILE__);?>"> |
| 248 |
</div> |
| 249 |
<div class="show-hide"> |
| 250 |
<?php $wp_analytify->get_single_admin_analytics( $start_date, $end_date, $post->ID, 0 ); ?> |
| 251 |
</div> |
| 252 |
<?php |
| 253 |
} |
| 254 |
else{ |
| 255 |
echo _e( 'You are not allowed to see stats', 'wp-analytify' ); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// Add Google Analytics JS code |
| 260 |
public function analytify_add_analytics_code() { |
| 261 |
|
| 262 |
global $current_user; |
| 263 |
|
| 264 |
$roles = $current_user->roles; |
| 265 |
|
| 266 |
if ( isset( $roles[0] ) and in_array( $roles[0], get_option( 'display_tracking_code' ) )) { |
| 267 |
|
| 268 |
} |
| 269 |
else{ |
| 270 |
|
| 271 |
echo '<!-- This code is added by Analytify v - ' . ANALYTIFY_VERSION . ' http://wp-analytify.com/ !--> '; |
| 272 |
|
| 273 |
if ( get_option( 'analytify_tracking_code' ) == 'universal' ) { ?> |
| 274 |
|
| 275 |
<script> |
| 276 |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| 277 |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| 278 |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| 279 |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); |
| 280 |
ga('create', '<?php echo get_option( "webPropertyId" );?>', 'auto'); |
| 281 |
ga('send', 'pageview'); |
| 282 |
</script> |
| 283 |
|
| 284 |
<?php |
| 285 |
} |
| 286 |
|
| 287 |
if ( get_option( 'analytify_tracking_code' ) == 'ga' ) { ?> |
| 288 |
|
| 289 |
<script type="text/javascript">//<![CDATA[ |
| 290 |
var _gaq = _gaq || []; |
| 291 |
_gaq.push(['_setAccount', '<?php echo get_option( "webPropertyId" );?>']); |
| 292 |
_gaq.push(['_trackPageview']); |
| 293 |
(function () { |
| 294 |
var ga = document.createElement('script'); |
| 295 |
ga.type = 'text/javascript'; |
| 296 |
ga.async = true; |
| 297 |
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| 298 |
var s = document.getElementsByTagName('script')[0]; |
| 299 |
s.parentNode.insertBefore(ga, s); |
| 300 |
})(); |
| 301 |
//]]> |
| 302 |
</script> |
| 303 |
|
| 304 |
<?php |
| 305 |
} |
| 306 |
|
| 307 |
echo '<!-- This code is added by Analytify v - ' . ANALYTIFY_VERSION . ' !-->'; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Add a link to the settings page to the plugins list |
| 313 |
*/ |
| 314 |
public function pa_plugin_links( $links, $file ) { |
| 315 |
|
| 316 |
static $this_plugin; |
| 317 |
|
| 318 |
if ( empty( $this_plugin ) ){ |
| 319 |
|
| 320 |
$this_plugin = 'wp-analytify/wp-analytify.php'; |
| 321 |
} |
| 322 |
|
| 323 |
if ( $file == $this_plugin ) { |
| 324 |
|
| 325 |
$settings_link = '<a href="' . admin_url("admin.php?page=analytify-settings") . '">' . __( 'Settings', 'wp-analytify' ) . '</a> | <a href="' . admin_url("admin.php?page=analytify-dashboard") . '">' . __( 'Dashboard', 'wp-analytify' ) . '</a>'; |
| 326 |
array_unshift( $links, $settings_link ); |
| 327 |
} |
| 328 |
|
| 329 |
return $links; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Plugin row meta links |
| 334 |
* |
| 335 |
* @since 1.1 |
| 336 |
* @param array $input already defined meta links |
| 337 |
* @param string $file plugin file path and name being processed |
| 338 |
* @return array $input |
| 339 |
*/ |
| 340 |
function analytify_plugin_row_meta( $input, $file ) { |
| 341 |
if ( $file != 'wp-analytify/wp-analytify.php' ) |
| 342 |
return $input; |
| 343 |
|
| 344 |
$links = array( |
| 345 |
'<a href="http://wp-analytify.com/">' . esc_html__( 'Buy Pro Version', 'edd' ) . '</a>', |
| 346 |
'<a href="http://wp-analytify.com/add-ons/">' . esc_html__( 'Add Ons', 'edd' ) . '</a>', |
| 347 |
); |
| 348 |
|
| 349 |
$input = array_merge( $input, $links ); |
| 350 |
|
| 351 |
return $input; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
/** |
| 356 |
* Display warning if profiles are not selected. |
| 357 |
*/ |
| 358 |
public function pa_check_warnings(){ |
| 359 |
|
| 360 |
add_action( 'admin_footer', array( |
| 361 |
&$this, |
| 362 |
'profile_warning' |
| 363 |
)); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Get current screen details |
| 368 |
*/ |
| 369 |
public static function pa_page_file_path() { |
| 370 |
|
| 371 |
$screen = get_current_screen(); |
| 372 |
|
| 373 |
if ( strpos( $screen->base, 'analytify-settings' ) !== false ) { |
| 374 |
include( ANALYTIFY_ROOT_PATH . '/inc/analytify-settings.php' ); |
| 375 |
} |
| 376 |
else { |
| 377 |
include( ANALYTIFY_ROOT_PATH . '/inc/analytify-dashboard.php' ); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Styling: loading stylesheets for the plugin. |
| 383 |
*/ |
| 384 |
public function pa_styles( $page ) { |
| 385 |
|
| 386 |
wp_enqueue_style( 'wp-analytify-style', plugins_url('css/wp-analytify-style.css', __FILE__)); |
| 387 |
|
| 388 |
// wp_enqueue_style( 'ui-css', plugins_url('jquery-ui-theme/jquery-ui.css', __FILE__)); |
| 389 |
wp_enqueue_style( 'chosen', plugins_url('css/chosen.css', __FILE__)); |
| 390 |
// wp_enqueue_style( 'jquery-ui-tooltip-css', plugins_url('css/jquery.ui.tooltip.html.css', __FILE__) ); |
| 391 |
|
| 392 |
if ( get_option( 'pa_welcome_message' ) == '0' ) { |
| 393 |
|
| 394 |
wp_enqueue_style( 'wp-pointer' ); |
| 395 |
|
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
public function pa_front_styles( $page ) { |
| 400 |
|
| 401 |
if( get_option( 'analytify_disable_front') == 0 ) { |
| 402 |
|
| 403 |
wp_enqueue_style( 'front-end-style', plugins_url('css/frontend_styles.css', __FILE__),false,ANALYTIFY_VERSION); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Loading scripts js for the plugin. |
| 409 |
*/ |
| 410 |
public function pa_scripts( $page ) { |
| 411 |
|
| 412 |
wp_enqueue_script ( 'jquery' ); |
| 413 |
wp_enqueue_script ( 'charts_api_js', 'https://www.google.com/jsapi', false, ANALYTIFY_VERSION ); |
| 414 |
wp_enqueue_script ( 'chosen-js', plugins_url('js/chosen.jquery.js', __FILE__), false, ANALYTIFY_VERSION ); |
| 415 |
wp_enqueue_script ( 'script-js', plugins_url('js/wp-analytify.js', __FILE__), false, ANALYTIFY_VERSION ); |
| 416 |
wp_enqueue_script ( 'jquery-ui-tooltip' ); |
| 417 |
wp_enqueue_script ( 'jquery-ui-datepicker'); |
| 418 |
|
| 419 |
if ( get_option( 'pa_welcome_message' ) == '0' ) { |
| 420 |
|
| 421 |
wp_enqueue_script( 'wp-pointer' ); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Loading scripts js for the plugin. |
| 427 |
*/ |
| 428 |
public function pa_front_scripts( $page ) { |
| 429 |
|
| 430 |
if( get_option( 'analytify_disable_front') == 0 ){ |
| 431 |
|
| 432 |
wp_enqueue_script ('jquery'); |
| 433 |
wp_enqueue_script ('analytify-classie', plugins_url('js/classie.js', __FILE__), false, ANALYTIFY_VERSION); |
| 434 |
wp_enqueue_script ('analytify-selectfx', plugins_url('js/selectFx.js', __FILE__), false, ANALYTIFY_VERSION); |
| 435 |
wp_enqueue_script ('analytify-script', plugins_url('js/script.js', __FILE__), false, ANALYTIFY_VERSION); |
| 436 |
|
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* Create Analytics menu at the left side of dashboard |
| 442 |
*/ |
| 443 |
public static function wpa_add_menu() { |
| 444 |
|
| 445 |
add_menu_page( ANALYTIFY_NICK, 'Analytify', 'manage_options', 'analytify-dashboard', array( |
| 446 |
__CLASS__, |
| 447 |
'pa_page_file_path' |
| 448 |
), plugins_url('images/wp-analytics-logo.png', __FILE__),'2.1.9'); |
| 449 |
|
| 450 |
add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Dashboard', ' Dashboard', 'manage_options', 'analytify-dashboard', array( |
| 451 |
__CLASS__, |
| 452 |
'pa_page_file_path' |
| 453 |
)); |
| 454 |
|
| 455 |
add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Settings', '<b style="color:#f9845b">Settings</b>', 'manage_options', 'analytify-settings', array( |
| 456 |
__CLASS__, |
| 457 |
'pa_page_file_path' |
| 458 |
)); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Creating tabs for settings |
| 463 |
* @since 1.0 |
| 464 |
*/ |
| 465 |
|
| 466 |
public function pa_settings_tabs( $current = 'authentication' ) { |
| 467 |
|
| 468 |
$tabs = array( 'authentication' => 'Authentication', |
| 469 |
'profile' => 'Profile', |
| 470 |
|
| 471 |
'admin' => 'Admin' |
| 472 |
); |
| 473 |
|
| 474 |
echo '<div class="left-area">'; |
| 475 |
|
| 476 |
echo '<div id="icon-themes" class="icon32"><br></div>'; |
| 477 |
echo '<h2 class="nav-tab-wrapper">'; |
| 478 |
|
| 479 |
foreach( $tabs as $tab => $name ) { |
| 480 |
|
| 481 |
$class = ( $tab == $current ) ? ' nav-tab-active' : ''; |
| 482 |
echo "<a class='nav-tab$class' href='?page=analytify-settings&tab=$tab'>$name</a>"; |
| 483 |
} |
| 484 |
|
| 485 |
echo '</h2>'; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Get profiles from user Google Analytics account profiles. |
| 490 |
*/ |
| 491 |
public function pt_get_analytics_accounts() { |
| 492 |
|
| 493 |
try { |
| 494 |
|
| 495 |
if( get_option( 'pa_google_token' ) !='' ) { |
| 496 |
$profiles = $this->service->management_profiles->listManagementProfiles( "~all", "~all" ); |
| 497 |
return $profiles; |
| 498 |
} |
| 499 |
|
| 500 |
else{ |
| 501 |
echo '<br /><p class="description">' . __( 'You must authenticate to access your web profiles.', 'wp-analytify' ) . '</p>'; |
| 502 |
} |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
catch (Exception $e) { |
| 507 |
die('An error occured: ' . $e->getMessage() . '\n'); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
public function pa_setting_url() { |
| 512 |
|
| 513 |
return admin_url('admin.php?page=analytify-settings'); |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
public function pt_save_data( $key_google_token ) { |
| 519 |
|
| 520 |
update_option( 'post_analytics_token', $key_google_token ); |
| 521 |
$this->pa_connect(); |
| 522 |
|
| 523 |
return true; |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Warning messages. |
| 528 |
*/ |
| 529 |
public function profile_warning() { |
| 530 |
|
| 531 |
$profile_id = get_option( "pt_webprofile" ); |
| 532 |
$acces_token = get_option( "post_analytics_token" ); |
| 533 |
|
| 534 |
if (! isset( $acces_token ) || empty( $acces_token )) { |
| 535 |
|
| 536 |
echo "<div id='message' class='error'><p><strong>" . __( "Analytify is not active. Please <a href='" . menu_page_url ( 'analytify-settings', false ) ."'>Authenticate</a> in order to get started using this plugin.", 'wp-analytify' )."</p></div>"; |
| 537 |
} |
| 538 |
else{ |
| 539 |
|
| 540 |
if (! isset( $profile_id ) || empty( $profile_id )){ |
| 541 |
echo '<div class="error"><p><strong>' . __( 'Google Analytics Profile is not set. Set the <a href="' . menu_page_url ( 'analytify-settings', false ) . '&tab=profile">Profile</a> ', 'wp-analytify' ) . '</p></div>'; |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
public function get_single_front_analytics( $content ) { |
| 548 |
|
| 549 |
global $post, $wp_analytify; |
| 550 |
|
| 551 |
$front_access = get_option( 'post_analytics_access' ); |
| 552 |
|
| 553 |
if ( is_single() || is_page() ) { |
| 554 |
|
| 555 |
$post_type = get_post_type( $post->ID ); |
| 556 |
|
| 557 |
if( strlen( get_option( 'analytify_posts_stats_front' ) < 3) ) { |
| 558 |
return $content; |
| 559 |
} |
| 560 |
|
| 561 |
if( is_array( get_option( 'analytify_posts_stats_front' )) and !in_array( $post_type, get_option( 'analytify_posts_stats_front' ) ) ) { |
| 562 |
|
| 563 |
return $content; |
| 564 |
} |
| 565 |
|
| 566 |
if ( is_array( get_option( 'post_analytics_exclude_posts_front' ) ) ) { |
| 567 |
|
| 568 |
if ( in_array( get_the_ID(), get_option( 'post_analytics_exclude_posts_front' ) ) ) { |
| 569 |
|
| 570 |
return $content; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
// Showing stats to guests |
| 575 |
if ( $front_access[0] == 'every-one' || $this->pa_check_roles( $front_access )) { |
| 576 |
|
| 577 |
$post_analytics_settings_front = array(); |
| 578 |
$post_analytics_settings_front = get_option( 'post_analytics_settings_front' ); |
| 579 |
|
| 580 |
$urlPost = parse_url( get_permalink( $post->ID ) ); |
| 581 |
|
| 582 |
if ( $urlPost['host'] == 'localhost' ) |
| 583 |
$filter = 'ga:pagePath==/'; //.$u_post['path']; |
| 584 |
else |
| 585 |
$filter = 'ga:pagePath==' .$urlPost['path']. ''; |
| 586 |
|
| 587 |
if( get_the_time('Y', $post->ID) < 2005 ) { |
| 588 |
|
| 589 |
$start_date = '2005-01-01'; |
| 590 |
} |
| 591 |
else { |
| 592 |
|
| 593 |
$start_date = get_the_time('Y-m-d', $post->ID); |
| 594 |
} |
| 595 |
|
| 596 |
$end_date = date('Y-m-d'); |
| 597 |
|
| 598 |
ob_start(); |
| 599 |
|
| 600 |
include ANALYTIFY_ROOT_PATH . '/inc/front-menus.php'; |
| 601 |
|
| 602 |
if(! empty( $post_analytics_settings_front )){ |
| 603 |
|
| 604 |
if (is_array( $post_analytics_settings_front )){ |
| 605 |
|
| 606 |
$stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgSessionDuration,ga:users',$start_date, $end_date, false, false, $filter); |
| 607 |
|
| 608 |
if ( isset( $stats->totalsForAllResults ) ) { |
| 609 |
|
| 610 |
include ANALYTIFY_ROOT_PATH . '/views/front/general-stats.php'; |
| 611 |
pa_include_general( $this, $stats); |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
$content .= ob_get_contents(); |
| 617 |
ob_get_clean(); |
| 618 |
} |
| 619 |
|
| 620 |
} |
| 621 |
|
| 622 |
return $content; |
| 623 |
|
| 624 |
} |
| 625 |
|
| 626 |
/* |
| 627 |
* get the Analytics data from ajax() call |
| 628 |
* |
| 629 |
*/ |
| 630 |
|
| 631 |
public function get_ajax_single_admin_analytics() { |
| 632 |
|
| 633 |
$startDate = ''; |
| 634 |
$endDate = ''; |
| 635 |
$postID = 0 ; |
| 636 |
$startDate = $_POST['start_date']; |
| 637 |
$endDate = $_POST['end_date']; |
| 638 |
$postID = $_POST['post_id']; |
| 639 |
$this->get_single_admin_analytics( $startDate, $endDate, $postID, 1 ); |
| 640 |
|
| 641 |
die(); |
| 642 |
} |
| 643 |
|
| 644 |
/* |
| 645 |
* get the Analytics data for wp-admin posts/pages. |
| 646 |
* |
| 647 |
*/ |
| 648 |
public function get_single_admin_analytics( $start_date = '', $end_date = '', $postID = 0, $ajax = 0 ) { |
| 649 |
|
| 650 |
global $post; |
| 651 |
|
| 652 |
//$urlPost = parse_url( get_permalink( $post->ID ) ); |
| 653 |
|
| 654 |
if ( $postID == 0 ) { |
| 655 |
$u_post = '/'; //$urlPost['path']; |
| 656 |
} |
| 657 |
else{ |
| 658 |
$u_post = parse_url( get_permalink( $postID ) ); |
| 659 |
} |
| 660 |
|
| 661 |
if ( $u_post['host'] == 'localhost' ) |
| 662 |
$filter = false; |
| 663 |
else |
| 664 |
$filter = 'ga:pagePath==' .$u_post['path']. ''; |
| 665 |
|
| 666 |
|
| 667 |
if ( $start_date == '' ) { |
| 668 |
|
| 669 |
$s_date = get_the_time('Y-m-d', $post->ID); |
| 670 |
if(get_the_time('Y', $post->ID) < 2005){ |
| 671 |
$s_date = '2005-01-01'; |
| 672 |
} |
| 673 |
} |
| 674 |
else{ |
| 675 |
$s_date = $start_date; |
| 676 |
} |
| 677 |
|
| 678 |
if ( $end_date == '' ) { |
| 679 |
$e_date = date('Y-m-d'); |
| 680 |
} |
| 681 |
else{ |
| 682 |
$e_date = $end_date; |
| 683 |
} |
| 684 |
|
| 685 |
$show_settings = array(); |
| 686 |
$show_settings = get_option('post_analytics_settings_back'); |
| 687 |
|
| 688 |
// Stop here, if user has disable backend analytics i.e OFF |
| 689 |
if ( get_option( 'post_analytics_disable_back' ) == 1 and $ajax == 0) { |
| 690 |
return; |
| 691 |
} |
| 692 |
|
| 693 |
echo '<p> Displaying Analytics of this page from ' . date("jS F, Y", strtotime($s_date)) . ' to '. date("jS F, Y", strtotime($e_date)) . '</p>'; |
| 694 |
|
| 695 |
if( !empty( $show_settings )){ |
| 696 |
|
| 697 |
if (is_array( $show_settings )){ |
| 698 |
|
| 699 |
if (in_array( 'show-overall-back', $show_settings )) { |
| 700 |
|
| 701 |
$stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgSessionDuration,ga:users',$s_date, $e_date, false, false, $filter); |
| 702 |
|
| 703 |
if ( isset( $stats->totalsForAllResults ) ) { |
| 704 |
|
| 705 |
include ANALYTIFY_ROOT_PATH . '/views/admin/single-general-stats.php'; |
| 706 |
wpa_single_include_general( $this, $stats); |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
/* |
| 715 |
* Pretty numbers |
| 716 |
*/ |
| 717 |
function wpa_pretty_numbers( $num ) { |
| 718 |
|
| 719 |
return round(($num/1000),2).'k'; |
| 720 |
} |
| 721 |
|
| 722 |
/* |
| 723 |
* format numbers |
| 724 |
*/ |
| 725 |
function wpa_number_format( $num ) { |
| 726 |
|
| 727 |
return number_format($num); |
| 728 |
} |
| 729 |
|
| 730 |
/* |
| 731 |
* Pretty time to display |
| 732 |
*/ |
| 733 |
function pa_pretty_time( $time ) { |
| 734 |
|
| 735 |
// Check if numeric |
| 736 |
if ( is_numeric($time) ) { |
| 737 |
|
| 738 |
$value = array( |
| 739 |
"years" => '00', |
| 740 |
"days" => '00', |
| 741 |
"hours" => '', |
| 742 |
"minutes" => '', |
| 743 |
"seconds" => '' |
| 744 |
); |
| 745 |
|
| 746 |
if ($time >= 31556926) { |
| 747 |
$value["years"] = floor($time / 31556926); |
| 748 |
$time = ($time % 31556926); |
| 749 |
} //$time >= 31556926 |
| 750 |
|
| 751 |
if ($time >= 86400) |
| 752 |
{ |
| 753 |
$value["days"] = floor($time / 86400); |
| 754 |
$time = ($time % 86400); |
| 755 |
} //$time >= 86400 |
| 756 |
if ($time >= 3600) |
| 757 |
{ |
| 758 |
$value["hours"] = str_pad(floor($time / 3600), 1, 0, STR_PAD_LEFT); |
| 759 |
$time = ($time % 3600); |
| 760 |
} //$time >= 3600 |
| 761 |
if ($time >= 60) |
| 762 |
{ |
| 763 |
$value["minutes"] = str_pad(floor($time / 60), 1, 0, STR_PAD_LEFT); |
| 764 |
$time = ($time % 60); |
| 765 |
} //$time >= 60 |
| 766 |
$value["seconds"] = str_pad(floor($time), 1, 0, STR_PAD_LEFT); |
| 767 |
# Get the hour:minute:second version |
| 768 |
if($value['hours']!=''){ |
| 769 |
$attach_hours='<sub>h</sub> '; |
| 770 |
} |
| 771 |
if($value['minutes']!=''){ |
| 772 |
$attach_min='<sub>min</sub> '; |
| 773 |
} |
| 774 |
if($value['seconds']!=''){ |
| 775 |
$attach_sec='<sub>sec</sub>'; |
| 776 |
} |
| 777 |
return $value['hours'] .@$attach_hours. $value['minutes'] .@$attach_min. $value['seconds'].$attach_sec; |
| 778 |
//return $value['hours'] . ':' . $value['minutes'] . ':' . $value['seconds']; |
| 779 |
} //is_numeric($time) |
| 780 |
else |
| 781 |
{ |
| 782 |
return false; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
public function pa_check_roles( $access_level ) { |
| 788 |
|
| 789 |
if ( is_user_logged_in () && isset ( $access_level ) ) { |
| 790 |
|
| 791 |
global $current_user; |
| 792 |
$roles = $current_user->roles; |
| 793 |
|
| 794 |
/*if ( ( current_user_can ( 'manage_options' ) ) ) { |
| 795 |
|
| 796 |
return true; |
| 797 |
}*/ |
| 798 |
|
| 799 |
if ( in_array ( $roles[0], $access_level ) ) { |
| 800 |
|
| 801 |
return true; |
| 802 |
} |
| 803 |
else { |
| 804 |
|
| 805 |
return false; |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
public function pa_welcome_message() { |
| 811 |
|
| 812 |
$pointer_content = '<h3>Analytify - Google Analytics for WordPress.</h3>'; |
| 813 |
$pointer_content .= '<p>Thank you for activating Analytify Plugin. Enjoy Google Analytics for everything in WordPress.</p>'; |
| 814 |
?> |
| 815 |
|
| 816 |
<script type="text/javascript"> |
| 817 |
//<![CDATA[ |
| 818 |
jQuery(document).ready( function($) { |
| 819 |
|
| 820 |
$('#toplevel_page_pa-dashboard').pointer({ |
| 821 |
content: '<?php echo $pointer_content; ?>', |
| 822 |
position: 'left', |
| 823 |
close: function() { |
| 824 |
<?php update_option("pa_welcome_message",1) ?> |
| 825 |
} |
| 826 |
}).pointer('open'); |
| 827 |
}); |
| 828 |
//]]> |
| 829 |
</script> |
| 830 |
|
| 831 |
<?php |
| 832 |
} |
| 833 |
|
| 834 |
/* |
| 835 |
* Activate options by default on installing the plugin. |
| 836 |
*/ |
| 837 |
static function install() { |
| 838 |
|
| 839 |
update_option( 'analytify_posts_stats', array( 'post','page' )); |
| 840 |
update_option( 'post_analytics_disable_back' , 1 ); |
| 841 |
update_option( 'analytify_code' , 1 ); |
| 842 |
update_option( 'post_analytics_settings_back' , array( 'show-overall-back' ) ); |
| 843 |
update_option( 'post_analytics_access_back' , array( 'editor','administrator' ) ); |
| 844 |
update_option( 'display_tracking_code' , array( 'administrator' ) ); |
| 845 |
|
| 846 |
|
| 847 |
} |
| 848 |
|
| 849 |
static function uninstall() { |
| 850 |
|
| 851 |
delete_option( 'analytify_posts_stats' ); |
| 852 |
delete_option( 'pa_google_token' ); |
| 853 |
delete_option( 'pa_welcome_message' ); |
| 854 |
delete_option( 'post_analytics_token' ); |
| 855 |
|
| 856 |
} |
| 857 |
|
| 858 |
} |
| 859 |
|
| 860 |
$wp_analytify = new WP_Analytify(); |
| 861 |
|
| 862 |
$wp_analytify->pa_check_warnings(); |
| 863 |
|
| 864 |
} //end if |
| 865 |
?> |