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

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