PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / 1.2.5
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) v1.2.5
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / wp-analytify.php

wp-analytify.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) 1.2.5, at wp-analytify.php

1,122 lines 31.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Google Analytics Dashboard By Analytify
4 * Plugin URI: http://wp-analytify.com/details
5 * Description: Analytify brings a brand new and modern feeling Google Analytics superbly integrated with WordPress Dashboard. 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 which is easy to understand & looks good.
6 * Version: 1.2.5
7 * Author: WPBrigade
8 * Author URI: http://wpbrigade.com/
9 * License: GPLv3
10 * Text Domain: wp-analytify
11 * Min WP Version: 3.0
12 * Max WP Version: 4.4
13 * Domain Path: /lang
14 * @package WP_ANALYTIFY
15 */
16
17 // Exit if accessed directly.
18 if ( ! defined( 'ABSPATH' ) ) { exit; }
19
20 define( 'WP_ANALYTIFY_FILE', __FILE__ );
21
22 include_once( 'inc/class-wpa-ajax.php' );
23 include_once( 'inc/wpa-core-functions.php' );
24 include_once( 'inc/class-wpa-adminbar.php' );
25
26 if ( ! class_exists( 'WP_Analytify' ) ) {
27
28 if ( ! class_exists( 'Analytify_General_FREE' ) ) {
29
30 require_once 'analytify-general.php';
31 }
32
33 /**
34 * WP_Analytify Extending class from General class.
35 */
36 class WP_Analytify extends Analytify_General_FREE{
37
38 /**
39 * [$token access token.]
40 * @var boolean
41 */
42 public $token = false;
43
44 /**
45 * [$client client object.]
46 * @var null
47 */
48 public $client = null;
49
50 /**
51 * Initialize code.
52 */
53 function __construct() {
54
55 parent::__construct();
56
57 if ( ! class_exists( 'Analytify_Google_Client' ) ) {
58
59 require_once ANALYTIFY_LIB_PATH . 'Google/Client.php';
60 require_once ANALYTIFY_LIB_PATH . 'Google/Service/Analytics.php';
61 }
62
63 add_action( 'plugin_action_links', array(
64 $this,
65 'pa_plugin_links',
66 ),10,2);
67
68 add_action( 'plugin_row_meta', array(
69 $this,
70 'analytify_plugin_row_meta',
71 ),10,2);
72
73 add_action( 'admin_enqueue_scripts', array(
74 $this,
75 'pa_scripts',
76 ));
77 add_action( 'admin_enqueue_scripts', array(
78 $this,
79 'pa_styles',
80 ));
81
82 add_action( 'wp_enqueue_scripts', array(
83 $this,
84 'pa_front_scripts',
85 ));
86 add_action( 'wp_enqueue_scripts', array(
87 $this,
88 'pa_front_styles',
89 ));
90
91 add_action( 'admin_menu', array(
92 $this,
93 'wpa_add_menu',
94 ));
95
96 add_action( 'admin_init', array(
97 $this,
98 'wpa_check_authentication',
99 ));
100
101 /*Insert Google Analytics Code*/
102 if ( '1' === get_option( 'analytify_code' ) ) {
103
104 add_action( 'wp_head', array(
105 $this,
106 'analytify_add_analytics_code',
107 ));
108 }
109
110 add_action( 'wp_ajax_nopriv_get_ajax_single_admin_analytics', array(
111 $this,
112 'get_ajax_single_admin_analytics',
113 ));
114
115 add_action( 'wp_ajax_get_ajax_single_admin_analytics', array(
116 $this,
117 'get_ajax_single_admin_analytics',
118 ));
119
120 add_action( 'wp_ajax_get_ajax_secret_keys', array(
121 $this,
122 'get_ajax_secret_keys',
123 ));
124
125 if ( 0 === get_option( 'analytify_disable_front' ) ) {
126
127 add_filter( 'the_content', array(
128 $this,
129 'get_single_front_analytics',
130 ));
131 }
132
133 /**
134 * Load Analytics under the EDIT POST Screen
135 * Add action runs only for admin section and load metabox.
136 */
137
138 if ( is_admin() ) {
139 add_action( 'load-post.php', array(
140 $this,
141 'load_metaboxes',
142 ));
143 }
144
145 // Show welcome message when user activate plugin.
146 if ( 1 !== get_option( 'show_tracking_pointer_1' ) ) {
147
148 add_action( 'admin_print_footer_scripts', array(
149 $this,
150 'pa_welcome_message',
151 ) );
152 }
153
154 /*
155 * localization language file filter
156 */
157
158 add_action( 'init', array(
159 $this,
160 'analytify_textdomain',
161 ) );
162
163 // add_action( 'admin_footer', array( &$this, 'profile_warning' ) );
164 /*
165 add_action( 'admin_footer', array(
166 &$this,
167 'profile_warning'
168 ));*/
169
170 add_action('admin_notices', array(
171 $this,
172 'analytify_admin_notice',
173 )
174 );
175
176 add_action('admin_init', array(
177 $this,
178 'analytify_nag_ignore',
179 )
180 );
181
182 add_action( 'admin_init', array(
183 $this,
184 'analytify_review_notice',
185 ) );
186
187 }
188
189 /**
190 * Save Authentication code on return from redirecting url after login.
191 * @since 1.2.0
192 */
193 public function wpa_check_authentication() {
194
195 if ( isset( $_GET['code'] ) && isset( $_GET['state'] ) ) {
196
197 $key_google_token = sanitize_text_field( $_GET['code'] );
198 WP_Analytify::pt_save_data( $key_google_token );
199 wp_redirect( esc_url_raw( $_GET['state'] .'&tab=profile' ) );
200 }
201 }
202
203 /*
204 * Set plugin language/localizations files directory
205 */
206 public function analytify_textdomain() {
207
208 $plugin_dir = basename( dirname( __FILE__ ) );
209 load_plugin_textdomain( 'wp-analytify', false , $plugin_dir . '/lang/' );
210
211 }
212
213 /*
214 * Show analytics sections under the posts/pages in the metabox.
215 */
216
217 public function load_metaboxes() {
218
219 add_action( 'add_meta_boxes', array(
220 $this,
221 'show_admin_single_analytics_add_metabox',
222 ));
223 }
224
225 /*
226 * Show metabox under each Post type to display Analytics of single post/page in wp-admin
227 */
228 public function show_admin_single_analytics_add_metabox() {
229
230 global $post;
231
232 if ( ! isset( $post ) ) {
233 return false; }
234
235 // Don't show statistics on posts which are not published.
236 if ( $post->post_status !== 'publish' ) { return false; }
237
238 $post_types = get_option( 'analytify_posts_stats' );
239
240 // Don't load boxes/sections if no any post type is selected.
241 if ( ! empty( $post_types ) ) {
242 foreach ( $post_types as $post_type ) {
243
244 add_meta_box('pa-single-admin-analytics', // $id
245 'Analytify: Google Analytics of this page.', // $title
246 array(
247 'WP_Analytify',
248 'show_admin_single_analytics',
249 ), // $callback
250 $post_type, // $posts
251 'normal', // $context
252 'high' // $priority
253 );
254 } //$post_types as $post_type
255 } }
256
257
258 public static function get_ajax_secret_keys() {
259
260 $response = wp_remote_get( 'http://wp-analytify.com/secret/keys.json' );
261 if ( is_wp_error( $response ) ) {
262 $error_message = $response->get_error_message();
263 echo "Something went wrong: $error_message";
264 } else {
265 print_r( $response['body'] );
266 }
267 die();
268 }
269
270 /*
271 * Show Analytics of single post/page in wp-admin under EDIT screen.
272 */
273 public static function show_admin_single_analytics() {
274
275 global $post;
276
277 // // Don't show statistics on posts which are not published.
278 // if ( get_post_status ( $post->ID ) != 'publish' ) {
279 // esc_html_e( 'Statistics will be loaded after you publish this content.', 'wp-analytify' );
280 // return false;
281 // }
282 $back_exclude_posts = explode( ',', get_option( 'post_analytics_exclude_posts_back' ) );
283
284 if ( is_array( $back_exclude_posts ) ) {
285
286 if ( in_array( $post->ID, $back_exclude_posts ) ) {
287
288 _e( 'This post is excluded and will not show Analytics.' );
289
290 return;
291 }
292 }
293
294 $urlPost = '';
295 $start_date = '';
296 $end_date = '';
297
298 $wp_analytify = new WP_Analytify();
299 $urlPost = parse_url( get_permalink( $post->ID ) );
300
301 if ( get_the_time( 'Y', $post->ID ) < 2005 ) {
302
303 $start_date = '2005-01-01';
304 } else {
305
306 $start_date = get_the_time( 'Y-m-d', $post->ID );
307 }
308
309 $end_date = date( 'Y-m-d' );
310
311 $is_access_level = get_option( 'post_analytics_access_back' );
312
313 if ( $wp_analytify->pa_check_roles( $is_access_level ) ) { ?>
314
315 <div class="pa-filter">
316 <table cellspacing="0" cellpadding="0" width="400">
317 <tbody>
318 <tr>
319 <td width="0">
320 <input type="text" id="start_date" name="start_date" value="<?php echo $start_date;?>">
321 </td>
322 <td width="0">
323 <input type="text" id="end_date" name="end_date" value="<?php echo $end_date;?>">
324 </td>
325 <input type="hidden" name="urlpost" id="urlpost" value="<?php echo $urlPost['path']; ?>">
326 <td width="0">
327 <input type="button" id="view_analytics" name="view_analytics" value="View Analytics" class="button-primary btn-green">
328 </td>
329 </tr>
330 </tbody>
331 </table>
332 </div>
333 <div class="loading" style="display:none">
334 <img src="<?php echo plugins_url( 'images/loading.gif', __FILE__ );?>">
335 </div>
336 <div class="show-hide">
337 <?php $wp_analytify->get_single_admin_analytics( $start_date, $end_date, $post->ID, 0 ); ?>
338 </div>
339 <?php
340 } else {
341 echo _e( 'You are not allowed to see stats', 'wp-analytify' );
342 }
343 }
344
345 // Add Google Analytics JS code
346 public function analytify_add_analytics_code() {
347
348 global $current_user;
349
350 $roles = $current_user->roles;
351
352 if ( isset( $roles[0] ) and in_array( $roles[0], get_option( 'display_tracking_code' ) ) ) {
353 echo '<!-- This user is disabled from tracking by Analytify !-->';
354 } else {
355
356 echo '<!-- This site uses Google Analytics code by Analytify version - ' . ANALYTIFY_VERSION . ' https://wp-analytify.com/ !--> ';
357
358 if ( get_option( 'analytify_tracking_code' ) == 'universal' ) { ?>
359
360 <script>
361 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
362 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
363 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
364 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
365 ga('create', '<?php echo get_option( 'webPropertyId' );?>', 'auto');
366 ga('send', 'pageview');
367 </script>
368
369 <?php
370 }
371
372 if ( get_option( 'analytify_tracking_code' ) == 'ga' ) { ?>
373
374 <script type="text/javascript">//<![CDATA[
375 var _gaq = _gaq || [];
376 _gaq.push(['_setAccount', '<?php echo get_option( 'webPropertyId' );?>']);
377 _gaq.push(['_trackPageview']);
378 (function () {
379 var ga = document.createElement('script');
380 ga.type = 'text/javascript';
381 ga.async = true;
382 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
383 var s = document.getElementsByTagName('script')[0];
384 s.parentNode.insertBefore(ga, s);
385 })();
386 //]]>
387 </script>
388
389 <?php
390 }
391
392 echo '<!-- Google Analytics code by Analytify version - ' . ANALYTIFY_VERSION . ' !-->';
393 }
394 }
395
396 /**
397 * Add a link to the settings page to the plugins list
398 */
399 public function pa_plugin_links( $links, $file ) {
400
401 static $this_plugin;
402
403 if ( empty( $this_plugin ) ) {
404
405 $this_plugin = 'wp-analytify/wp-analytify.php';
406 }
407
408 if ( $file == $this_plugin ) {
409
410 $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>';
411 array_unshift( $links, $settings_link );
412 }
413
414 return $links;
415 }
416
417 /**
418 * Plugin row meta links
419 *
420 * @since 1.1
421 * @param array $input already defined meta links
422 * @param string $file plugin file path and name being processed
423 * @return array $input
424 */
425 function analytify_plugin_row_meta( $input, $file ) {
426 if ( $file != 'wp-analytify/wp-analytify.php' ) {
427 return $input; }
428
429 $links = array(
430 '<a href="http://wp-analytify.com/">' . esc_html__( 'Buy Pro Version', 'edd' ) . '</a>',
431 '<a href="http://wp-analytify.com/add-ons/">' . esc_html__( 'Add Ons', 'edd' ) . '</a>',
432 );
433
434 $input = array_merge( $input, $links );
435
436 return $input;
437 }
438
439
440 /**
441 * Display warning if profiles are not selected.
442 */
443 public function pa_check_warnings() {
444
445 add_action( 'admin_footer', array(
446 &$this,
447 'profile_warning',
448 ));
449 }
450
451 /**
452 * Get current screen details
453 */
454 public static function pa_page_file_path() {
455
456 $screen = get_current_screen();
457
458 if ( strpos( $screen->base, 'analytify-settings' ) !== false ) {
459 include( ANALYTIFY_ROOT_PATH . '/inc/analytify-settings.php' );
460 } else if ( strpos( $screen->base, 'analytify-campaigns' ) !== false ) {
461 include_once( ANALYTIFY_ROOT_PATH . '/inc/analytify-campaigns.php' );
462 } else if ( strpos( $screen->base, 'analytify-addons' ) !== false ) {
463 include_once( ANALYTIFY_ROOT_PATH . '/inc/page-addons.php' );
464 } else {
465 include( ANALYTIFY_ROOT_PATH . '/inc/analytify-dashboard.php' );
466 }
467 }
468
469 /**
470 * Styling: loading stylesheets for the plugin.
471 */
472 public function pa_styles( $page ) {
473
474 wp_enqueue_style( 'wp-analytify-style', plugins_url( 'css/wp-analytify-style.css', __FILE__ ) );
475
476 // wp_enqueue_style( 'ui-css', plugins_url('jquery-ui-theme/jquery-ui.css', __FILE__));
477 wp_enqueue_style( 'chosen', plugins_url( 'css/chosen.css', __FILE__ ) );
478 // wp_enqueue_style( 'jquery-ui-tooltip-css', plugins_url('css/jquery.ui.tooltip.html.css', __FILE__) );
479 if ( get_option( 'show_tracking_pointer_1' ) != 1 ) { wp_enqueue_style( 'wp-pointer' ); }
480 }
481
482 public function pa_front_styles( $page ) {
483
484 if ( get_option( 'analytify_disable_front' ) == 0 ) {
485
486 wp_enqueue_style( 'front-end-style', plugins_url( 'css/frontend_styles.css', __FILE__ ),false,ANALYTIFY_VERSION );
487 }
488 }
489
490 /**
491 * Loading scripts js for the plugin.
492 */
493 public function pa_scripts( $page ) {
494
495 wp_enqueue_script( 'jquery' );
496 wp_enqueue_script( 'charts_api_js', 'https://www.google.com/jsapi', false, ANALYTIFY_VERSION );
497 wp_enqueue_script( 'chosen-js', plugins_url( 'js/chosen.jquery.js', __FILE__ ), false, ANALYTIFY_VERSION );
498 wp_enqueue_script( 'script-js', plugins_url( 'js/wp-analytify.js', __FILE__ ), false, ANALYTIFY_VERSION );
499 wp_enqueue_script( 'jquery-ui-tooltip' );
500 wp_enqueue_script( 'jquery-ui-datepicker' );
501
502 if ( get_option( 'show_tracking_pointer_1' ) != 1 ) { wp_enqueue_script( 'wp-pointer' ); }
503
504 }
505
506 /**
507 * Loading scripts js for the plugin.
508 */
509 public function pa_front_scripts( $page ) {
510
511 if ( get_option( 'analytify_disable_front' ) == 0 ) {
512
513 wp_enqueue_script( 'jquery' );
514 wp_enqueue_script( 'analytify-classie', plugins_url( 'js/classie.js', __FILE__ ), false, ANALYTIFY_VERSION );
515 wp_enqueue_script( 'analytify-selectfx', plugins_url( 'js/selectFx.js', __FILE__ ), false, ANALYTIFY_VERSION );
516 wp_enqueue_script( 'analytify-script', plugins_url( 'js/script.js', __FILE__ ), false, ANALYTIFY_VERSION );
517
518 }
519 }
520
521 /**
522 * Create Analytics menu at the left side of dashboard
523 */
524 public static function wpa_add_menu() {
525
526 add_menu_page( ANALYTIFY_NICK, 'Analytify', 'manage_options', 'analytify-dashboard', array(
527 __CLASS__,
528 'pa_page_file_path',
529 ), plugins_url( 'images/wp-analytics-logo.png', __FILE__ ),'2.1.9');
530
531 add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Dashboard', ' Dashboard', 'manage_options', 'analytify-dashboard', array(
532 __CLASS__,
533 'pa_page_file_path',
534 ));
535
536 add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Campaign', ' Campaigns', 'manage_options', 'analytify-campaigns', array(
537 __CLASS__,
538 'pa_page_file_path',
539 ));
540
541 add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Settings', '<b style="color:#f9845b">Settings</b>', 'manage_options', 'analytify-settings', array(
542 __CLASS__,
543 'pa_page_file_path',
544 ));
545
546 add_submenu_page( 'analytify-dashboard', ANALYTIFY_NICK . ' Addons', '<b>Add-ons</b>', 'manage_options', 'analytify-addons', array(
547 __CLASS__,
548 'pa_page_file_path',
549 ));
550 }
551
552 /**
553 * Creating tabs for settings
554 * @since 1.0
555 */
556
557 public function pa_settings_tabs( $current = 'authentication' ) {
558
559 $tabs = array(
560 'authentication' => 'Authentication',
561 'profile' => 'Profile',
562 'admin' => 'Admin',
563 'advanced' => 'Advanced',
564 );
565
566 echo '<div class="left-area">';
567 echo '<div id="icon-themes" class="icon32"><br></div>';
568 echo '<h2 class="nav-tab-wrapper">';
569
570 foreach ( $tabs as $tab => $name ) {
571
572 $class = ( $tab == $current ) ? ' nav-tab-active' : '';
573 echo "<a class='nav-tab$class' href='?page=analytify-settings&tab=$tab'>$name</a>";
574 }
575
576 echo '</h2>';
577 }
578
579 /**
580 * Get profiles from user Google Analytics account profiles.
581 */
582 public function pt_get_analytics_accounts() {
583
584 try {
585
586 if ( get_option( 'pa_google_token' ) != '' ) {
587 $profiles = $this->service->management_profiles->listManagementProfiles( '~all', '~all', array( 'max-results' => 1000 ) );
588 return $profiles;
589 } else {
590 echo '<br /><p class="description">' . __( 'You must authenticate to access your web profiles.', 'wp-analytify' ) . '</p>';
591 }
592 } catch (Exception $e) {
593 die( 'An error occured: ' . $e->getMessage() . '\n' );
594 }
595 }
596
597 public function pa_setting_url() {
598
599 return admin_url( 'admin.php?page=analytify-settings' );
600
601 }
602
603
604 public function pt_save_data( $key_google_token ) {
605
606 update_option( 'post_analytics_token', $key_google_token );
607 $this->pa_connect();
608
609 return true;
610 }
611
612 /**
613 * Warning messages.
614 */
615 public function profile_warning() {
616
617 $profile_id = get_option( 'pt_webprofile' );
618 $acces_token = get_option( 'post_analytics_token' );
619
620 if ( ! isset( $acces_token ) || empty( $acces_token ) ) {
621
622 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>';
623 } else {
624
625 if ( ! isset( $profile_id ) || empty( $profile_id ) ) {
626 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>';
627 }
628 }
629 }
630
631
632 public function get_single_front_analytics( $content ) {
633
634 global $post, $wp_analytify;
635
636 $front_access = get_option( 'post_analytics_access' );
637
638 if ( is_single() || is_page() ) {
639
640 $post_type = get_post_type( $post->ID );
641
642 if ( strlen( get_option( 'analytify_posts_stats_front' ) < 3 ) ) {
643 return $content;
644 }
645
646 if ( is_array( get_option( 'analytify_posts_stats_front' ) ) and ! in_array( $post_type, get_option( 'analytify_posts_stats_front' ) ) ) {
647
648 return $content;
649 }
650
651 if ( is_array( get_option( 'post_analytics_exclude_posts_front' ) ) ) {
652
653 if ( in_array( get_the_ID(), get_option( 'post_analytics_exclude_posts_front' ) ) ) {
654
655 return $content;
656 }
657 }
658
659 // Showing stats to guests
660 if ( $front_access[0] == 'every-one' || $this->pa_check_roles( $front_access ) ) {
661
662 $post_analytics_settings_front = array();
663 $post_analytics_settings_front = get_option( 'post_analytics_settings_front' );
664
665 $urlPost = parse_url( get_permalink( $post->ID ) );
666
667 if ( $urlPost['host'] == 'localhost' ) {
668 $filter = 'ga:pagePath==/'; // .$u_post['path'];
669 } else {
670 $filter = 'ga:pagePath==' .$urlPost['path']. ''; }
671
672 if ( get_the_time( 'Y', $post->ID ) < 2005 ) {
673
674 $start_date = '2005-01-01';
675 } else {
676
677 $start_date = get_the_time( 'Y-m-d', $post->ID );
678 }
679
680 $end_date = date( 'Y-m-d' );
681
682 ob_start();
683
684 include ANALYTIFY_ROOT_PATH . '/inc/front-menus.php';
685
686 if ( ! empty( $post_analytics_settings_front ) ) {
687
688 if ( is_array( $post_analytics_settings_front ) ) {
689
690 $stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgTimeOnPage,ga:users',$start_date, $end_date, false, false, $filter );
691
692 if ( isset( $stats->totalsForAllResults ) ) {
693
694 include ANALYTIFY_ROOT_PATH . '/views/front/general-stats.php';
695 pa_include_general( $this, $stats );
696 }
697 }
698 }
699
700 $content .= ob_get_contents();
701 ob_get_clean();
702 }
703 }
704
705 return $content;
706
707 }
708
709 /*
710 * get the Analytics data from ajax() call
711 *
712 */
713
714 public function get_ajax_single_admin_analytics() {
715
716 $startDate = '';
717 $endDate = '';
718 $postID = 0 ;
719 $startDate = $_POST['start_date'];
720 $endDate = $_POST['end_date'];
721 $postID = $_POST['post_id'];
722 $this->get_single_admin_analytics( $startDate, $endDate, $postID, 1 );
723
724 die();
725 }
726
727 /*
728 * get the Analytics data for wp-admin posts/pages.
729 *
730 */
731 public function get_single_admin_analytics( $start_date = '', $end_date = '', $postID = 0, $ajax = 0 ) {
732
733 global $post;
734
735 if ( wpa_check_profile_selection( 'Analytify', '<br /><b class="wpa_notice_error">Select your website profile at Analytify->settings->profile tab to load stats.</b>' ) ) { return; }
736
737 if ( $postID == 0 ) {
738 $u_post = '/'; // $urlPost['path'];
739 } else {
740 $u_post = parse_url( get_permalink( $postID ) );
741 }
742
743 if ( $u_post['host'] == 'localhost' ) {
744 $filter = false; } else {
745 $filter = 'ga:pagePath==' .$u_post['path']. ''; }
746
747 if ( $start_date == '' ) {
748
749 $s_date = get_the_time( 'Y-m-d', $post->ID );
750 if ( get_the_time( 'Y', $post->ID ) < 2005 ) {
751 $s_date = '2005-01-01';
752 }
753 } else {
754 $s_date = $start_date;
755 }
756
757 if ( $end_date == '' ) {
758 $e_date = date( 'Y-m-d' );
759 } else {
760 $e_date = $end_date;
761 }
762
763 $show_settings = array();
764 $show_settings = get_option( 'post_analytics_settings_back' );
765
766 // Stop here, if user has disable backend analytics i.e OFF
767 if ( get_option( 'post_analytics_disable_back' ) == 1 and $ajax == 0 ) {
768 return;
769 }
770
771 echo '<p> Displaying Analytics of this page from ' . date( 'jS F, Y', strtotime( $s_date ) ) . ' to '. date( 'jS F, Y', strtotime( $e_date ) ) . '</p>';
772
773 if ( ! empty( $show_settings ) ) {
774
775 if ( is_array( $show_settings ) ) {
776
777 if ( in_array( 'show-overall-back', $show_settings ) ) {
778
779 $stats = $this->pa_get_analytics( 'ga:sessions,ga:bounces,ga:newUsers,ga:entrances,ga:pageviews,ga:sessionDuration,ga:avgTimeOnPage,ga:users',$s_date, $e_date, false, false, $filter );
780
781 if ( isset( $stats->totalsForAllResults ) ) {
782
783 include ANALYTIFY_ROOT_PATH . '/views/admin/single-general-stats.php';
784 wpa_single_include_general( $this, $stats );
785 }
786 }
787 }
788 }
789 }
790
791
792 /*
793 * Pretty numbers
794 */
795 function wpa_pretty_numbers( $num ) {
796
797 return round( ($num / 1000),2 ).'k';
798 }
799
800 /*
801 * format numbers
802 */
803 function wpa_number_format( $num ) {
804
805 return number_format( $num );
806 }
807
808 /*
809 * Pretty time to display
810 */
811 function pa_pretty_time( $time ) {
812
813 // Check if numeric
814 if ( is_numeric( $time ) ) {
815
816 $value = array(
817 'years' => '00',
818 'days' => '00',
819 'hours' => '',
820 'minutes' => '',
821 'seconds' => '',
822 );
823
824 if ( $time >= 31556926 ) {
825 $value['years'] = floor( $time / 31556926 );
826 $time = ($time % 31556926);
827 } //$time >= 31556926
828
829 if ( $time >= 86400 ) {
830 $value['days'] = floor( $time / 86400 );
831 $time = ($time % 86400);
832 } //$time >= 86400
833 if ( $time >= 3600 ) {
834 $value['hours'] = str_pad( floor( $time / 3600 ), 1, 0, STR_PAD_LEFT );
835 $time = ($time % 3600);
836 } //$time >= 3600
837 if ( $time >= 60 ) {
838 $value['minutes'] = str_pad( floor( $time / 60 ), 1, 0, STR_PAD_LEFT );
839 $time = ($time % 60);
840 } //$time >= 60
841 $value['seconds'] = str_pad( floor( $time ), 1, 0, STR_PAD_LEFT );
842 // Get the hour:minute:second version
843 if ( $value['hours'] != '' ) {
844 $attach_hours = '<sub>h</sub> ';
845 }
846 if ( $value['minutes'] != '' ) {
847 $attach_min = '<sub>min</sub> ';
848 }
849 if ( $value['seconds'] != '' ) {
850 $attach_sec = '<sub>sec</sub>';
851 }
852 return $value['hours'] .@$attach_hours. $value['minutes'] .@$attach_min. $value['seconds'].$attach_sec;
853 // return $value['hours'] . ':' . $value['minutes'] . ':' . $value['seconds'];
854 } //is_numeric($time)
855 else {
856 return false;
857 }
858 }
859
860
861 public function pa_check_roles( $access_level ) {
862
863 if ( is_user_logged_in() && isset( $access_level ) ) {
864
865 global $current_user;
866 $roles = $current_user->roles;
867
868 if ( in_array( $roles[0], $access_level, true ) ) {
869
870 return true;
871 } else {
872
873 return false;
874 }
875 }
876 }
877
878 /**
879 * [pa_welcome_message Show pointers for announcements. ]
880 * @return void
881 */
882 public function pa_welcome_message() {
883
884 $pointer_content = '<h3>Announcement:</h3>';
885 $pointer_content .= '<p><input type="checkbox" name="wpa_allow_tracking" value="1" id="wpa_allow_tracking"> ';
886 $pointer_content .= 'Help us making Analytify even better by sharing very basic plugin usage data.';
887 $pointer_content .= ' Opt-in and receive a $5 Off coupon for <a href="https://wp-analytify.com/upgrade-from-free">Analytify PRO</a>.</p>';
888 ?>
889
890 <script type="text/javascript">
891 //<![CDATA[
892 jQuery(document).ready( function($) {
893
894 if(typeof(jQuery().pointer) != 'undefined') {
895
896 $('#toplevel_page_analytify-dashboard').pointer({
897
898 content: '<?php echo $pointer_content; ?>',
899 position: {
900 edge: 'left',
901 align: 'center'
902 },
903 close: function() {
904 $.post( ajaxurl, {
905 pointer: 'tracking',
906 wpa_allow: $('#wpa_allow_tracking:checked').val(),
907 action: 'analytify_dismiss_pointer'
908 });
909
910 if($('#wpa_allow_tracking:checked').val() == 1) alert('Thankyou!\nYour Coupon code is Analytify2015');
911 }
912 }).pointer('open');
913 };
914 });
915 //]]>
916 </script>
917
918 <?php
919 }
920
921 /**
922 * [analytify_admin_notice Display a notice that can be dismissed.]
923 * @return void
924 */
925 function analytify_admin_notice() {
926
927 $profile_id = get_option( 'pt_webprofile' );
928 $acces_token = get_option( 'post_analytics_token' );
929
930 if ( current_user_can( 'install_plugins' ) ) {
931
932 global $current_user ;
933 $user_id = $current_user->ID;
934 /* Check that the user hasn't already clicked to ignore the message */
935 if ( ! get_user_meta( $user_id, 'analytify_ignore_notice110' ) ) {
936
937 echo '<div class="updated"><p>';
938 printf( __( '<b>Note:</b> Enhanced eCommerce Tracking for <b><a href="https://wp-analytify.com/go/WooCommerce" target="_blank">WooCommerce</a></b> is finally available with Analytify! Increase sales and track - Product Clicks, Adds to cart, Checkouts and <a href="https://wp-analytify.com/go/WooCommerce-Announcement-blog" target="_blank">many more</a>. <a href="%1$s">[Hide Notice]</a>' ), esc_url( admin_url( 'admin.php?page=analytify-dashboard&analytify_nag_ignore=0' ) ) );
939 echo '</p></div>';
940
941 }
942 }
943
944 /* Show notices */
945 if ( ! isset( $acces_token ) || empty( $acces_token ) || ! get_option( 'pa_google_token' ) ) { // Input var okay.
946
947 echo '<div class="error notice is-dismissible"><p><b>Notice:</b> <b><a style="text-decoration:none" href="' . esc_url( menu_page_url( 'analytify-settings', false ) ) . '">Connect</a></b> Analytify with your Google account.</p></div>';
948 } else {
949
950 if ( ( ! isset( $profile_id ) || empty( $profile_id )) && ! isset( $_POST['save_profile'] ) ) { // Input var okay.
951
952 echo '<div class="error notice is-dismissible"><p>Congratulations! Analytify is now authenticated. Select your website profile <a href="' . esc_url( menu_page_url( 'analytify-settings', false ) ) . '&tab=profile">here</a></p></div>';
953 }
954 }
955 }
956
957 /**
958 * [analytify_nag_ignore Ignore notice]
959 * @return void
960 */
961 function analytify_nag_ignore() {
962
963 global $current_user;
964
965 $user_id = $current_user->ID;
966 /* If user clicks to ignore the notice, add that to their user meta */
967 if ( isset( $_GET['analytify_nag_ignore'] ) && '0' === $_GET['analytify_nag_ignore'] ) { // Input var okay.
968 add_user_meta( $user_id, 'analytify_ignore_notice110', 'true', true );
969 }
970 }
971
972 /**
973 * Check and Dismiss review message.
974 *
975 * @since 1.2.2
976 */
977 private function review_dismissal() {
978
979 if ( ! is_admin() ||
980 ! current_user_can( 'manage_options' ) ||
981 ! isset( $_GET['_wpnonce'] ) || // Input var okay.
982 ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'analytify-review-nonce' ) || // Input var okay.
983 ! isset( $_GET['wp_analytify_review_dismiss'] ) ) { // Input var okay.
984
985 return;
986 }
987
988 add_site_option( 'wp_analytify_review_dismiss', 'yes' );
989 }
990
991 /**
992 * Ask users to review our plugin on .org
993 *
994 * @since 1.2.2
995 * @return boolean false
996 */
997 public function analytify_review_notice() {
998
999 $this->review_dismissal();
1000
1001 $activation_time = get_site_option( 'wp_analytify_active_time' );
1002 $review_dismissal = get_site_option( 'wp_analytify_review_dismiss' );
1003
1004 if ( 'yes' === $review_dismissal ) {
1005 return;
1006 }
1007
1008 if ( ! $activation_time ) {
1009
1010 $activation_time = time();
1011 add_site_option( 'wp_analytify_active_time', $activation_time );
1012 }
1013
1014 // 1296000 = 15 Days in seconds
1015 if ( time() - $activation_time > 1296000 ) {
1016 add_action( 'admin_notices' , array( $this, 'analytify_review_notice_message' ) );
1017 }
1018
1019 }
1020
1021 /**
1022 * Review notice message
1023 *
1024 * @since 1.2.2
1025 * @return void
1026 */
1027 public function analytify_review_notice_message() {
1028
1029 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false; // Input var okay.
1030 $scheme = ( parse_url( $request_uri, PHP_URL_QUERY ) ) ? '&' : '?';
1031 $url = $request_uri . $scheme . 'wp_analytify_review_dismiss=yes';
1032 $dismiss_url = wp_nonce_url( $url, 'analytify-review-nonce' );
1033
1034 echo '<div class="updated">
1035 <p>' . esc_html__( 'You have been using the ', 'wp-analytify' ) . '<a href="' . esc_url( admin_url( 'admin.php?page=analytify-dashboard' ) ) . '">WP Analytify</a>' . esc_html__( ' for some time now, do you like it? If so, please consider leaving us a review on WordPress.org! It would help us out a lot and we would really appreciate it.', 'wp-analytify' ) . '
1036 <br><br>
1037 <a onclick="location.href=\'' . esc_url( $dismiss_url ) . '\';" class="button button-primary" href="' . esc_url( 'https://wordpress.org/support/view/plugin-reviews/wp-analytify?rate=5#postform' ) . '" target="_blank">' . esc_html__( 'Leave a Review', 'wp-analytify' ) . '</a>
1038 <a href="' . esc_url( $dismiss_url ) . '">' . esc_html__( 'No thanks', 'wp-analytify' ) . '</a>
1039 </p>
1040 </div>';
1041
1042 }
1043 }
1044
1045 $wp_analytify = new WP_Analytify();
1046
1047 } //end if
1048
1049 register_activation_hook( __FILE__, 'install' );
1050 register_deactivation_hook( __FILE__, 'uninstall' );
1051 register_uninstall_hook( __FILE__, 'delete' );
1052
1053 /**
1054 * [install Activate options by default on installing the plugin.]
1055 * @return void
1056 */
1057 function install() {
1058
1059 update_option( 'analytify_posts_stats', array( 'post', 'page' ) );
1060 update_option( 'post_analytics_disable_back', '1' );
1061 update_option( 'analytify_code', '1' );
1062 update_option( 'post_analytics_settings_back' , array( 'show-overall-back' ) );
1063 update_option( 'post_analytics_access_back' , array( 'editor', 'administrator' ) );
1064 update_option( 'display_tracking_code' , array( 'administrator' ) );
1065 }
1066
1067 /**
1068 * [uninstall unisntall status]
1069 * @return void
1070 */
1071 function uninstall() {
1072
1073 if ( 1 === get_option( 'wpa_allow_tracking' ) ) { send_status_analytify( get_option( 'admin_email' ), 'in-active' ); }
1074
1075 delete_option( 'analytify_posts_stats' );
1076 delete_option( 'pa_google_token' );
1077 delete_option( 'show_tracking_pointer_1' );
1078 delete_option( 'post_analytics_token' );
1079
1080 }
1081
1082 /**
1083 * [delete delete]
1084 * @return void
1085 */
1086 function delete() {
1087
1088 if ( 1 === get_option( 'wpa_allow_tracking' ) ) { send_status_analytify( get_option( 'admin_email' ), 'delete' ); }
1089 }
1090
1091 /**
1092 * [send_status_analytify Send status email.]
1093 * @param string $email users email.
1094 * @param string $status plugin status.
1095 */
1096 function send_status_analytify( $email, $status ) {
1097
1098 $url = 'https://wp-analytify.com/plugin-manager/';
1099
1100 if ( '' === $email ) {
1101 $email = 'track@wp-analytify.com';
1102 }
1103
1104 $fields = array(
1105 'email' => $email,
1106 'site' => get_site_url(),
1107 'status' => $status,
1108 'type' => 'FREE',
1109 );
1110
1111 wp_remote_post( $url, array(
1112 'method' => 'POST',
1113 'timeout' => 5,
1114 'httpversion' => '1.0',
1115 'blocking' => false,
1116 'headers' => array(),
1117 'body' => $fields,
1118 )
1119 );
1120 }
1121 ?>
1122