| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable for this plugin structure |
| 2 |
/** |
| 3 |
* Analytify Host Analytics Class |
| 4 |
* |
| 5 |
* This class handles locally hosted analytics files for the Analytify plugin, |
| 6 |
* providing functionality to serve Google Analytics files from the local server. |
| 7 |
* |
| 8 |
* @package WP_Analytify |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Analytify_Host_Analytics' ) ) { |
| 18 |
/** |
| 19 |
* Resolves the locally hosted gtag bundle URL and coordinates download, alias storage, and cleanup. |
| 20 |
*/ |
| 21 |
class Analytify_Host_Analytics extends Analytify_Host_Analytics_Abstract { |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Gtag file alias. |
| 26 |
* |
| 27 |
* @var array<string, mixed>|false |
| 28 |
*/ |
| 29 |
public $file_aliases; |
| 30 |
|
| 31 |
/** |
| 32 |
* Host Analytics Locally option Off or On. |
| 33 |
* |
| 34 |
* @var mixed |
| 35 |
*/ |
| 36 |
private $host_analytics_locally; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
const GTAG_URL = 'https://www.googletagmanager.com'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Define class variables and calls parent class constructor. |
| 44 |
* |
| 45 |
* @param string $tracking_mode Google analytics tracking mode. |
| 46 |
* @param boolean $doing_cron If the constructor is called by cron hook. |
| 47 |
* @param boolean $settings_updated If the tracking id in settings is changed. |
| 48 |
*/ |
| 49 |
public function __construct( $tracking_mode = 'gtag', $doing_cron = true, $settings_updated = false ) { |
| 50 |
|
| 51 |
$this->tracking_mode = $tracking_mode; |
| 52 |
|
| 53 |
$this->file_aliases = get_option( 'analytics_file_aliases' ); |
| 54 |
|
| 55 |
// TODO: Need to change the function name its GA4 but title of function is UA. |
| 56 |
$this->tracking_id = WP_ANALYTIFY_FUNCTIONS::get_UA_code(); |
| 57 |
|
| 58 |
$this->host_analytics_locally = WPANALYTIFY_Utils::get_option( 'locally_host_analytics', 'wp-analytify-advanced', false ); |
| 59 |
|
| 60 |
$this->host_analytics_locally = false === $this->host_analytics_locally || 'off' === $this->host_analytics_locally ? false : $this->host_analytics_locally; |
| 61 |
|
| 62 |
if ( ! $this->host_analytics_locally && $this->file_already_exist() ) { |
| 63 |
|
| 64 |
$dir = WP_ANALYTIFY_LOCAL_DIR; |
| 65 |
$paths_to_remove = array( $dir . 'gtag.js', $dir . 'gtag.min.js' ); |
| 66 |
$alias = $this->get_file_alias(); |
| 67 |
if ( $alias ) { |
| 68 |
array_unshift( $paths_to_remove, $dir . $alias ); |
| 69 |
} |
| 70 |
|
| 71 |
foreach ( array_unique( $paths_to_remove ) as $path ) { |
| 72 |
if ( file_exists( $path ) && is_file( $path ) ) { |
| 73 |
wp_delete_file( $path ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! $this->host_analytics_locally || ( ! $settings_updated && $this->file_already_exist() && ! $doing_cron ) ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
parent::__construct(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if the local gtag library file exists. |
| 89 |
* |
| 90 |
* @return boolean |
| 91 |
*/ |
| 92 |
public function file_already_exist() { |
| 93 |
|
| 94 |
if ( file_exists( WP_ANALYTIFY_LOCAL_DIR . 'gtag.js' ) || file_exists( WP_ANALYTIFY_LOCAL_DIR . 'gtag.min.js' ) || file_exists( WP_ANALYTIFY_LOCAL_DIR . $this->get_file_alias() ) ) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if the local gtag library file exists. |
| 103 |
* |
| 104 |
* @return mixed |
| 105 |
*/ |
| 106 |
public function local_analytics_file_url() { |
| 107 |
|
| 108 |
if ( ! $this->host_analytics_locally || ! $this->file_already_exist() ) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
$url = content_url() . str_replace( WP_CONTENT_DIR, '', WP_ANALYTIFY_LOCAL_DIR ) . 'gtag.min.js'; |
| 113 |
|
| 114 |
if ( strpos( home_url(), 'https://' ) !== false && ! is_ssl() ) { |
| 115 |
$url = str_replace( 'http://', 'https://', $url ); |
| 116 |
} |
| 117 |
|
| 118 |
$file_alias = self::get_file_alias(); |
| 119 |
|
| 120 |
if ( ! $file_alias ) { |
| 121 |
return $url; |
| 122 |
} |
| 123 |
|
| 124 |
$url = str_replace( 'gtag.min.js', $file_alias, $url ); |
| 125 |
|
| 126 |
return $url; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* This functions check and return file alias for gtag library. |
| 131 |
* |
| 132 |
* @return mixed |
| 133 |
*/ |
| 134 |
public function get_file_alias() { |
| 135 |
if ( ! $this->file_aliases ) { |
| 136 |
return ''; |
| 137 |
} |
| 138 |
|
| 139 |
return $this->file_aliases['gtag'] ?? ''; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* This function update file alias for gtag in database options. |
| 144 |
* |
| 145 |
* @param string $key this is the analytics tracking mode gtag. |
| 146 |
* @param string $alias this is the randomly generated alias for the file. |
| 147 |
* |
| 148 |
* @return boolean |
| 149 |
*/ |
| 150 |
public function set_file_alias( $key, $alias ) { |
| 151 |
if ( false === $this->file_aliases ) { |
| 152 |
$this->file_aliases = array(); |
| 153 |
} |
| 154 |
$this->file_aliases[ $key ] = $alias; |
| 155 |
return update_option( 'analytics_file_aliases', $this->file_aliases ); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|