Skip to content

Commit

Permalink
Fixed: Advanced tab is no longer locked when MA is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan0sz committed Sep 26, 2024
1 parent fde8bcd commit a0a041f
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 61 deletions.
3 changes: 1 addition & 2 deletions includes/admin/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ public function do_advanced_settings_tab() {
$this->generate_tab(
self::CAOS_ADMIN_SECTION_ADV_SETTINGS,
'dashicons-admin-settings',
__( 'Advanced Settings', 'host-analyticsjs-local' ),
CAOS::get( self::CAOS_BASIC_SETTING_TRACKING_CODE ) === 'minimal_ga4'
__( 'Advanced Settings', 'host-analyticsjs-local' )
);
}

Expand Down
169 changes: 122 additions & 47 deletions includes/admin/settings/class-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* * * * * * * * * * * * * * * * * * * */

class CAOS_Admin_Settings_Advanced extends CAOS_Admin_Settings_Builder {

/**
* CAOS_Admin_Settings_Advanced constructor.
*/
Expand Down Expand Up @@ -48,8 +47,8 @@ public function __construct() {
*/
public function do_description() {
?>
<p>
</p>
<p>
</p>
<?php
}

Expand All @@ -63,7 +62,14 @@ public function do_compatibility_mode() {
__( 'Compatibility Mode', 'host-analyticsjs-local' ),
CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE,
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ) != '' ? 'on' : '',
__( 'Check this option to use CAOS with any other Google Analytics plugin. Any reference to <code>googletagmanager.com/gtag/js</code> in your site\'s HTML will be replaced with the URL pointing to the local copy. <strong>Warning!</strong> Please make sure that CAOS\' <strong>Basic Settings</strong> match your Google Analytics plugin\'s configuration.', 'host-analyticsjs-local' )
__(
'Check this option to use CAOS with any other Google Analytics plugin. Any reference to <code>googletagmanager.com/gtag/js</code> in your site\'s HTML will be replaced with the URL pointing to the local copy. <strong>Warning!</strong> Please make sure that CAOS\' <strong>Basic Settings</strong> match your Google Analytics plugin\'s configuration.',
'host-analyticsjs-local'
),
CAOS::uses_minimal_analytics(),
true,
false,
__( 'The Minimal Analytics <strong>Tracking Code</strong> can\'t run in Compatibility Mode.', 'host-analyticsjs-local' )
);
}

Expand All @@ -76,7 +82,16 @@ public function do_cache_dir() {
CAOS_Admin_Settings::CAOS_ADV_SETTING_CACHE_DIR,
__( 'e.g. /uploads/caos/', 'host-analyticsjs-local' ),
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_CACHE_DIR, '/uploads/caos/' ),
__( "Change the path where the Gtag.js file is cached inside WordPress' content directory (usually <code>wp-content</code>). Defaults to <code>/uploads/caos/</code>.", 'host-analyticsjs-local' )
__(
"Change the path where the Gtag.js file is cached inside WordPress' content directory (usually <code>wp-content</code>). Defaults to <code>/uploads/caos/</code>.",
'host-analyticsjs-local'
),
true,
CAOS::uses_minimal_analytics(),
__(
'Minimal Analytics doesn\'t require cache. Set <strong>Tracking Code</strong> to Asynchronous to use this option.',
'host-analyticsjs-local'
)
);
}

Expand All @@ -89,7 +104,13 @@ public function do_cdn_url() {
CAOS_Admin_Settings::CAOS_ADV_SETTING_CDN_URL,
__( 'e.g. cdn.mydomain.com', 'host-analyticsjs-local' ),
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_CDN_URL ),
__( 'If you\'re using a CDN, enter the URL here to serve <code>gtag.js</code> from your CDN.', 'host-analyticsjs-local' )
__( 'If you\'re using a CDN, enter the URL here to serve <code>gtag.js</code> from your CDN.', 'host-analyticsjs-local' ),
true,
CAOS::uses_minimal_analytics(),
__(
'Minimal Analytics is an inline script and doesn\'t require a CDN. Set <strong>Tracking Code</strong> to Asynchronous to use this option.',
'host-analyticsjs-local'
)
);
}

Expand All @@ -99,17 +120,25 @@ public function do_cdn_url() {
* @return void
*/
public function do_randomize_client_id_promo() {
$description = __( 'Since GA4 only creates <em>first-party</em> (which are GDPR compliant in some countries) cookies, enabling this option for GA4 will generate a random user ID for each visitor of <u>your</u> website to ensure that tracking across different websites/platforms is no longer possible, but it\'ll still be possible to track users on your website. Enabling this option doesn\'t necessarily mean you no longer need a cookie banner.', 'host-analyticsjs-local' ) . ' ' . $this->promo;
$description = __(
'Since GA4 only creates <em>first-party</em> (which are GDPR compliant in some countries) cookies, enabling this option for GA4 will generate a random user ID for each visitor of <u>your</u> website to ensure that tracking across different websites/platforms is no longer possible, but it\'ll still be possible to track users on your website. Enabling this option doesn\'t necessarily mean you no longer need a cookie banner.',
'host-analyticsjs-local'
) . ' ' . $this->promo;

$this->do_checkbox(
__( 'Enable Randomize Client ID (Pro)', 'host-analyticsjs-local' ),
'pro_random_cid',
defined( 'CAOS_PRO_ACTIVE' ) && CAOS::get( 'pro_random_cid' ) ? 'on' : false,
$description,
! defined( 'CAOS_PRO_ACTIVE' ) || ( defined( 'CAOS_PRO_ACTIVE' ) && CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ) ),
! defined( 'CAOS_PRO_ACTIVE' ) ||
CAOS::uses_minimal_analytics() ||
( defined( 'CAOS_PRO_ACTIVE' ) && CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ) ),
true,
true,
__( 'Disable <strong>Compatibility Mode</strong> to enable it.', 'host-analyticsjs-local' )
__(
'Disable <strong>Compatibility Mode</strong> or set <strong>Tracking Code</strong> to Asynchronous to use this option.',
'host-analyticsjs-local'
)
);
}

Expand All @@ -122,26 +151,35 @@ public function do_cloaked_affiliate_links_tracking_promo() {
$label = __( 'Track Cloaked Affiliate Links (Pro)', 'host-analyticsjs-local' );
$name = 'pro_cloaked_affiliate_links';
?>
<tr>
<th><?php echo esc_attr( $label ); ?></th>
<td>
<tr>
<th><?php echo esc_attr( $label ); ?></th>
<td>
<?php
$disabled = ! defined( 'CAOS_PRO_ACTIVE' ) || ( defined( 'CAOS_PRO_ACTIVE' ) && CAOS::get( CAOS_Admin_Settings::CAOS_BASIC_SETTING_TRACKING_CODE ) === 'minimal_ga4' );
$disabled = ! defined( 'CAOS_PRO_ACTIVE' ) || ( defined( 'CAOS_PRO_ACTIVE' ) && CAOS::uses_minimal_analytics() );
?>
<?php if ( $disabled && $this->display_reason( true ) ) : ?>
<p class="description option-disabled">
<?php echo wp_kses( sprintf( __( 'This option is disabled. %s', 'host-analyticsjs-local' ), __( 'Enable it by setting <strong>Service Provider</strong> to Google Analytics and/or set <strong>Tracking Code</strong> to Default or Asynchronous.', 'host-analyticsjs-local' ) ), 'post' ); ?>
</p>
<p class="description option-disabled">
<?php echo wp_kses(
sprintf(
__( 'This option is disabled. %s', 'host-analyticsjs-local' ),
__(
'Enable it by setting <strong>Service Provider</strong> to Google Analytics and/or set <strong>Tracking Code</strong> to Asynchronous.',
'host-analyticsjs-local'
)
),
'post'
); ?>
</p>
<?php else : ?>
<table class="track-cloaked-affiliate-links">
<tr>
<th><?php echo esc_attr( __( 'Path', 'host-analyticsjs-local' ) ); ?></th>
<th><?php echo esc_attr( __( 'Event Category', 'host-analyticsjs-local' ) ); ?></th>
<th></th>
</tr>
<tr>
<input type="hidden" name="caos_settings[<?php echo esc_attr( $name ); ?>]" value="0" />
</tr>
<table class="track-cloaked-affiliate-links">
<tr>
<th><?php echo esc_attr( __( 'Path', 'host-analyticsjs-local' ) ); ?></th>
<th><?php echo esc_attr( __( 'Event Category', 'host-analyticsjs-local' ) ); ?></th>
<th></th>
</tr>
<tr>
<input type="hidden" name="caos_settings[<?php echo esc_attr( $name ); ?>]" value="0"/>
</tr>
<?php
$affiliate_links = defined( 'CAOS_PRO_ACTIVE' ) && CAOS::get( $name ) ? CAOS::get( $name ) : [
0 => [
Expand All @@ -152,28 +190,53 @@ public function do_cloaked_affiliate_links_tracking_promo() {

foreach ( $affiliate_links as $key => $properties ) :
?>
<tr id="affiliate-link-row-<?php echo esc_attr( $key ); ?>">
<tr id="affiliate-link-row-<?php echo esc_attr( $key ); ?>">
<?php foreach ( $properties as $prop_key => $prop_value ) : ?>
<td id="affiliate-link-<?php echo esc_attr( $prop_key ); ?>-<?php echo esc_attr( $key ); ?>">
<input type="text" <?php echo $disabled ? 'disabled' : ''; ?> class="affiliate-link-<?php echo esc_attr( $prop_key ); ?>" name="caos_settings[<?php echo esc_attr( $name ); ?>][<?php echo esc_attr( $key ); ?>][<?php echo esc_attr( $prop_key ); ?>]" value="<?php echo esc_attr( $prop_value ); ?>" />
</td>
<td id="affiliate-link-<?php echo esc_attr( $prop_key ); ?>-<?php echo esc_attr( $key ); ?>">
<input type="text" <?php echo $disabled ? 'disabled' : ''; ?>
class="affiliate-link-<?php echo esc_attr( $prop_key ); ?>"
name="caos_settings[<?php echo esc_attr( $name ); ?>][<?php echo esc_attr(
$key
); ?>][<?php echo esc_attr( $prop_key ); ?>]" value="<?php echo esc_attr( $prop_value ); ?>"/>
</td>
<?php endforeach; ?>
<td>
<span class="dashicons dashicons-remove affiliate-link-remove" data-row="<?php echo esc_attr( $key ); ?>" <?php echo esc_attr( $disabled ? 'style="opacity: 15%;"' : '' ); ?>></span>
</td>
</tr>
<td>
<span class="dashicons dashicons-remove affiliate-link-remove"
data-row="<?php echo esc_attr( $key ); ?>" <?php echo esc_attr(
$disabled ? 'style="opacity: 15%;"' : ''
); ?>></span>
</td>
</tr>
<?php endforeach; ?>
</table>
<p>
<input type="button" <?php echo esc_attr( $disabled ); ?> class="button button-secondary" id="affiliate-link-add" value="<?php echo esc_attr( __( 'Add Link Path', 'host-analyticsjs-local' ) ); ?>" />
</p>
<p class="description">
<?php echo wp_kses( apply_filters( $name . '_setting_description', __( 'Send an event to Google Analytics whenever a Cloaked Affiliate Link is clicked. An event with the configured <strong>Event Category</strong> is sent to Google Analytics whenever a link containing the <strong>Path</strong> value is clicked. The <strong>Event Label</strong> will be the URL of the link. Depending on your server\'s capacity, this might not work properly with Stealth Mode enabled.', 'host-analyticsjs-local' ), $label, $name ) . ' ' . $this->promo, 'post' ); ?>
<?php echo wp_kses( defined( 'CAOS_PRO_ACTIVE' ) && CAOS::get( 'pro_stealth_mode' ) == 'on' ? __( 'If no events are registered in Google Analytics, your server might be too slow to send them in time. Please disable Stealth Mode if that\'s the case.', 'host-analyticsjs-local' ) : '', 'post' ); ?>
</p>
</table>
<p>
<input type="button" <?php echo esc_attr( $disabled ); ?> class="button button-secondary" id="affiliate-link-add"
value="<?php echo esc_attr( __( 'Add Link Path', 'host-analyticsjs-local' ) ); ?>"/>
</p>
<p class="description">
<?php echo wp_kses(
apply_filters(
$name . '_setting_description',
__(
'Send an event to Google Analytics whenever a Cloaked Affiliate Link is clicked. An event with the configured <strong>Event Category</strong> is sent to Google Analytics whenever a link containing the <strong>Path</strong> value is clicked. The <strong>Event Label</strong> will be the URL of the link. Depending on your server\'s capacity, this might not work properly with Stealth Mode enabled.',
'host-analyticsjs-local'
),
$label,
$name
) . ' ' . $this->promo,
'post'
); ?>
<?php echo wp_kses(
defined( 'CAOS_PRO_ACTIVE' ) && CAOS::get( 'pro_stealth_mode' ) == 'on' ? __(
'If no events are registered in Google Analytics, your server might be too slow to send them in time. Please disable Stealth Mode if that\'s the case.',
'host-analyticsjs-local'
) : '',
'post'
); ?>
</p>
<?php endif; ?>
</td>
</tr>
</td>
</tr>
<?php
}

Expand All @@ -185,11 +248,20 @@ public function do_advertising_features() {
__( 'Disable Advertising Features', 'host-analyticsjs-local' ),
CAOS_Admin_Settings::CAOS_ADV_SETTING_DISABLE_ADS_FEATURES,
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_DISABLE_ADS_FEATURES ),
sprintf( __( 'Override and disable all advertising reporting and remarketing features established in Google Analytics. <a href="%s" target="_blank">What\'s this?</a>', 'host-analyticsjs-local' ), 'https://support.google.com/analytics/answer/9050852?hl=en' ),
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ),
sprintf(
__(
'Override and disable all advertising reporting and remarketing features established in Google Analytics. <a href="%s" target="_blank">What\'s this?</a>',
'host-analyticsjs-local'
),
'https://support.google.com/analytics/answer/9050852?hl=en'
),
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ) || CAOS::uses_minimal_analytics(),
true,
false,
__( 'Disable <strong>Compatibility Mode</strong> to enable it.', 'host-analyticsjs-local' )
__(
'Disable <strong>Compatibility Mode</strong> and/or set <strong>Tracking Code</strong> to Asynchronous to use this option.',
'host-analyticsjs-local'
)
);
}

Expand All @@ -201,7 +273,10 @@ public function do_uninstall_settings() {
__( 'Remove settings at Uninstall', 'host-analyticsjs-local' ),
CAOS_Admin_Settings::CAOS_ADV_SETTING_UNINSTALL_SETTINGS,
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_UNINSTALL_SETTINGS ),
'<strong>' . __( 'Warning!', 'host-analyticsjs-local' ) . '</strong> ' . __( 'This will remove the settings from the database upon plugin deletion!', 'host-analyticsjs-local' )
'<strong>' .
__( 'Warning!', 'host-analyticsjs-local' ) .
'</strong> ' .
__( 'This will remove the settings from the database upon plugin deletion!', 'host-analyticsjs-local' )
);
}
}
9 changes: 5 additions & 4 deletions includes/admin/settings/class-basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function do_gdpr_compliance_promo() {
'https://daan.dev/blog/wordpress/google-analytics-gdpr-compliance/'
) . ' ' . $this->promo, ! defined( 'CAOS_PRO_ACTIVE' ) || CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ),
true,
true
true,
__( 'Disable <strong>Compatibility Mode</strong> to use this option.', 'host-analyticsjs-local' )
);
}

Expand All @@ -114,7 +115,7 @@ public function do_allow_tracking() {
__( 'Minimal Analytics can\'t be used with Consent Mode.', 'host-analyticsjs-local' ),
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ),
false,
__( 'Enable it by disabling <strong>Compatibility Mode</strong>.', 'host-analyticsjs-local' )
__( 'Disable <strong>Compatibility Mode</strong> to use this option.', 'host-analyticsjs-local' )
);
}

Expand Down Expand Up @@ -167,7 +168,7 @@ public function do_tracking_code() {
__( 'Read more', 'host-analyticsjs-local' ) .
'</a>',
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ),
__( 'Enable it by disabling <strong>Compatibility Mode</strong>.', 'host-analyticsjs-local' )
__( 'Disable <strong>Compatibility Mode</strong> to use this option.', 'host-analyticsjs-local' )
);
}

Expand Down Expand Up @@ -213,7 +214,7 @@ public function do_script_position() {
),
CAOS::get( CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '' ),
false,
__( 'Enable it by disabling <strong>Compatibility Mode</strong>.', 'host-analyticsjs-local' )
__( 'Disable <strong>Compatibility Mode</strong> to use this option.', 'host-analyticsjs-local' )
);
}

Expand Down
26 changes: 18 additions & 8 deletions includes/class-caos.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function __construct() {
add_action( 'deactivated_plugin', [ $this, 'maybe_do_update' ] );
add_action( 'admin_init', [ $this, 'do_update_after_save' ] );
add_action( 'in_plugin_update_message-' . CAOS_PLUGIN_BASENAME, [ $this, 'render_update_notice' ], 11, 2 );

// Force Option Values
add_action( 'init', [ $this, 'maybe_force_option_values' ] );
}

/**
Expand Down Expand Up @@ -312,13 +315,6 @@ public static function find_replace_in( $file, $find, $replace ) {
return $file_manager->find_replace_in( $file, $find, $replace );
}

/**
*
*/
public static function uses_minimal_analytics() {
return self::get( CAOS_Admin_Settings::CAOS_BASIC_SETTING_TRACKING_CODE ) === 'minimal_ga4';
}

/**
* Run database migrations.
*
Expand Down Expand Up @@ -368,13 +364,20 @@ public function do_update_after_save() {
*
* @since 4.7.0
*/
if ( self::get( 'tracking_code' ) === 'minimal_ga4' ) {
if ( self::uses_minimal_analytics() ) {
return;
}

return $this->trigger_cron_script();
}

/**
*
*/
public static function uses_minimal_analytics() {
return self::get( CAOS_Admin_Settings::CAOS_BASIC_SETTING_TRACKING_CODE ) === 'minimal_ga4';
}

/**
* Render update notices if available.
*
Expand Down Expand Up @@ -486,4 +489,11 @@ private function clean( $var ) {

return is_scalar( $var ) ? sanitize_text_field( wp_unslash( $var ) ) : $var;
}

public function maybe_force_option_values() {
if ( self::uses_minimal_analytics() ) {
add_filter( 'caos_setting_' . CAOS_Admin_Settings::CAOS_ADV_SETTING_COMPATIBILITY_MODE, '__return_empty_string' );
add_filter( 'caos_setting_' . CAOS_Admin_Settings::CAOS_ADV_SETTING_DISABLE_ADS_FEATURES, '__return_empty_string' );
}
}
}

0 comments on commit a0a041f

Please sign in to comment.