PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / 1.1.0
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) v1.1.0
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.1.0, at wp-analytify.php

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