| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable |
| 2 |
/** |
| 3 |
* Profile Management Component for WP Analytify |
| 4 |
* |
| 5 |
* This file contains all profile settings and GA4 setup functions |
| 6 |
* that were previously in the main plugin file. |
| 7 |
* |
| 8 |
* @package WP_Analytify |
| 9 |
* @since 8.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
// Include GA4 Property Management class for direct instantiation. |
| 17 |
require_once __DIR__ . '/ga4-property-management.php'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Profile Management Component Class |
| 21 |
*/ |
| 22 |
class Analytify_Profile_Management { |
| 23 |
|
| 24 |
/** |
| 25 |
* Main plugin instance |
| 26 |
* |
| 27 |
* @var WP_Analytify |
| 28 |
*/ |
| 29 |
private $analytify; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor |
| 33 |
* |
| 34 |
* @version 7.0.5 |
| 35 |
* @param WP_Analytify $analytify Main plugin instance. |
| 36 |
*/ |
| 37 |
public function __construct( $analytify ) { |
| 38 |
$this->analytify = $analytify; |
| 39 |
$this->init_hooks(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Initialize hooks |
| 44 |
* |
| 45 |
* @since 7.0.5 |
| 46 |
* @version 7.0.5 |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
private function init_hooks() { |
| 50 |
// Profile management hooks. |
| 51 |
add_action( 'update_option_wp-analytify-profile', array( $this, 'update_profiles_list_summary' ), 10, 2 ); |
| 52 |
add_action( 'update_option_wp-analytify-advanced', array( $this, 'update_selected_profiles' ), 10, 2 ); |
| 53 |
add_action( 'admin_init', array( $this, 'update_profile_list_summary_on_update' ), 1 ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Update profiles list summary when profile option is updated |
| 58 |
* |
| 59 |
* @since 7.0.5 |
| 60 |
* @version 7.0.5 |
| 61 |
* @param mixed $old_value Old profile value. |
| 62 |
* @param mixed $new_value New profile value. |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function update_profiles_list_summary( $old_value, $new_value ) { |
| 66 |
if ( isset( $new_value['hide_profiles_list'] ) && 'on' === $new_value['hide_profiles_list'] && ( $new_value['hide_profiles_list'] !== $old_value['hide_profiles_list'] ) && isset( $new_value['profile_for_dashboard'] ) ) { |
| 67 |
$accounts = get_option( 'profiles_list_summary' ); |
| 68 |
|
| 69 |
update_option( 'profiles_list_summary_backup', $accounts, 'no' ); |
| 70 |
|
| 71 |
$new_properties = array(); |
| 72 |
if ( ! empty( $accounts ) ) { |
| 73 |
foreach ( $accounts->getItems() as $account ) { |
| 74 |
foreach ( $account->getWebProperties() as $property ) { |
| 75 |
foreach ( $property->getProfiles() as $profile ) { |
| 76 |
// Get Property ID i.e UA Code. |
| 77 |
if ( $profile->getId() === $new_value['profile_for_dashboard'] ) { |
| 78 |
$new_properties[ $account->getId() ] = $property; |
| 79 |
} |
| 80 |
if ( $profile->getId() === $new_value['profile_for_posts'] ) { |
| 81 |
$new_properties[ $account->getId() ] = $property; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
update_option( 'profiles_list_summary', $new_properties ); |
| 89 |
} |
| 90 |
|
| 91 |
// Update stream and save measurement id when user selects the GA4 property for tracking. (Profile for posts (Backend/Front-end)). |
| 92 |
if ( isset( $new_value['profile_for_posts'] ) && $new_value['profile_for_posts'] && substr( $new_value['profile_for_posts'], 0, 3 ) === 'ga4' ) { |
| 93 |
$property_id = explode( ':', $new_value['profile_for_posts'] )[1]; |
| 94 |
$ga4_property_management = new Analytify_GA4_Property_Management( $this->analytify ); |
| 95 |
$ga4_property_management->setup_property( $property_id, 'tracking' ); |
| 96 |
} |
| 97 |
|
| 98 |
// Update stream and save measurement id when user selects the GA4 property for reporting. (Profile for dashboard). |
| 99 |
if ( isset( $new_value['profile_for_dashboard'] ) && $new_value['profile_for_dashboard'] && substr( $new_value['profile_for_dashboard'], 0, 3 ) === 'ga4' ) { |
| 100 |
$ga4_update_number = wp_rand( 10, 100 ); |
| 101 |
update_option( 'ga4_update_number', 'updated_' . $ga4_update_number ); |
| 102 |
$property_id = explode( ':', $new_value['profile_for_dashboard'] )[1]; |
| 103 |
$ga4_property_management = new Analytify_GA4_Property_Management( $this->analytify ); |
| 104 |
$ga4_property_management->setup_property( $property_id, 'reporting' ); |
| 105 |
} else { |
| 106 |
$ua_update_number = wp_rand( 10, 100 ); |
| 107 |
update_option( 'ua_update_number', 'updated_' . $ua_update_number ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Update selected profiles when advanced option is updated. |
| 114 |
* |
| 115 |
* @since 7.0.5 |
| 116 |
* @version 8.1.2 |
| 117 |
* @param mixed $old_value Old advanced value. |
| 118 |
* @param mixed $new_value New advanced value. |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public function update_selected_profiles( $old_value, $new_value ) { |
| 122 |
|
| 123 |
if ( isset( $new_value['google_analytics_version'] ) && ( isset( $old_value['google_analytics_version'] ) && $new_value['google_analytics_version'] !== $old_value['google_analytics_version'] ) ) { |
| 124 |
$analytify_profile_section = get_option( 'wp-analytify-profile' ); |
| 125 |
if ( isset( $analytify_profile_section['profile_for_dashboard'] ) && $analytify_profile_section['profile_for_dashboard'] ) { |
| 126 |
$analytify_profile_section['profile_for_dashboard'] = ''; |
| 127 |
} |
| 128 |
if ( isset( $analytify_profile_section['profile_for_posts'] ) && $analytify_profile_section['profile_for_posts'] ) { |
| 129 |
$analytify_profile_section['profile_for_posts'] = ''; |
| 130 |
} |
| 131 |
update_option( 'wp-analytify-profile', $analytify_profile_section ); |
| 132 |
delete_option( 'analytify-ga-properties-summery' ); |
| 133 |
delete_option( 'analytify_ga4_exceptions' ); |
| 134 |
} |
| 135 |
|
| 136 |
// if user change the stream update the ua code and mp secret. |
| 137 |
if ( isset( $new_value['ga4_web_data_stream'] ) && isset( $old_value['ga4_web_data_stream'] ) && $new_value['ga4_web_data_stream'] !== $old_value['ga4_web_data_stream'] ) { |
| 138 |
// TODO: legacy code with wrong naming. |
| 139 |
$ua_code = get_option( 'analytify_ua_code' ); |
| 140 |
// check if the ua code is same if it's then return. |
| 141 |
if ( $ua_code === $new_value['ga4_web_data_stream'] ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
// Update the tracking code. |
| 145 |
update_option( 'analytify_ua_code', $new_value['ga4_web_data_stream'] ); |
| 146 |
|
| 147 |
new Analytify_Host_Analytics( 'gtag', false, true ); // update the locally host analytics file. |
| 148 |
|
| 149 |
// Get the stored data for currect property and stream. |
| 150 |
$property_info = get_option( 'analytify_tracking_property_info' ); |
| 151 |
$all_streams = get_option( 'analytify-ga4-streams' ); |
| 152 |
|
| 153 |
if ( ! empty( $property_info ) ) { |
| 154 |
// Extract the current property id. |
| 155 |
$property_id = $property_info['property_id']; |
| 156 |
|
| 157 |
// get all the data for currently selected stream from the all streams array. |
| 158 |
$stream_data = $all_streams[ $property_id ][ $new_value['ga4_web_data_stream'] ] ?? false; |
| 159 |
|
| 160 |
// Set mp secret value initially to null (must be string for GA4 Measurement Protocol). |
| 161 |
$new_secret_value = null; |
| 162 |
|
| 163 |
if ( isset( $stream_data['full_name'] ) ) { |
| 164 |
|
| 165 |
$get_secret = $this->analytify->analytify_get_mp_secret( $stream_data['full_name'] ); |
| 166 |
if ( ! empty( $get_secret ) && isset( $get_secret['secretValue'] ) && is_string( $get_secret['secretValue'] ) ) { |
| 167 |
$new_secret_value = $get_secret['secretValue']; |
| 168 |
} |
| 169 |
if ( null === $new_secret_value ) { |
| 170 |
$create_secret = $this->analytify->analytify_create_mp_secret( $property_id, $stream_data['full_name'], $stream_data['measurement_id'] ); |
| 171 |
if ( ! empty( $create_secret ) && isset( $create_secret['secretValue'] ) && is_string( $create_secret['secretValue'] ) ) { |
| 172 |
$new_secret_value = $create_secret['secretValue']; |
| 173 |
} |
| 174 |
} |
| 175 |
if ( null !== $new_secret_value ) { |
| 176 |
WPANALYTIFY_Utils::update_option( 'measurement_protocol_secret', 'wp-analytify-advanced', $new_secret_value ); |
| 177 |
} |
| 178 |
update_option( 'analytify_tracking_property_info', $stream_data ); |
| 179 |
update_option( 'analytify_reporting_property_info', $stream_data ); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Update profile list summary on plugin update |
| 187 |
* |
| 188 |
* @since 7.1.1 |
| 189 |
* @version 8.1.0 |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public function update_profile_list_summary_on_update() { |
| 193 |
if ( version_compare( ANALYTIFY_VERSION, get_option( 'WP_ANALYTIFY_PLUGIN_VERSION' ), '>' ) ) { |
| 194 |
$option = get_option( 'wp-analytify-profile' ); |
| 195 |
|
| 196 |
if ( isset( $option['hide_profiles_list'] ) && 'on' === $option['hide_profiles_list'] ) { |
| 197 |
$accounts = get_option( 'profiles_list_summary' ); |
| 198 |
|
| 199 |
if ( ! $accounts ) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
// Means that its run already. |
| 204 |
if ( is_array( $accounts ) ) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
update_option( 'profiles_list_summary_backup', $accounts, 'no' ); |
| 209 |
|
| 210 |
$new_value['profile_for_dashboard'] = $option['profile_for_dashboard']; |
| 211 |
$new_value['profile_for_posts'] = $option['profile_for_posts']; |
| 212 |
|
| 213 |
$new_properties = array(); |
| 214 |
|
| 215 |
foreach ( $accounts->getItems() as $account ) { |
| 216 |
foreach ( $account->getWebProperties() as $property ) { |
| 217 |
foreach ( $property->getProfiles() as $profile ) { |
| 218 |
// Get Property ID i.e UA Code. |
| 219 |
if ( $profile->getId() === $new_value['profile_for_dashboard'] ) { |
| 220 |
$new_properties[ $account->getId() ] = $property; |
| 221 |
} |
| 222 |
if ( $profile->getId() === $new_value['profile_for_posts'] ) { |
| 223 |
$new_properties[ $account->getId() ] = $property; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
update_option( 'profiles_list_summary', $new_properties ); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|