Plugin Directory

Changeset 2664664


Ignore:
Timestamp:
01/25/2022 03:12:58 PM (4 years ago)
Author:
schweizersolutions
Message:

2.0

  • Added: Support for Google Analytics 4 (GA4)
  • Added: MonsterInsights GA4 support
  • Added: ExactMetrics GA4 support
  • Added: Analytify GA4 support
  • Added: GA Google Analytics GA4 support
  • Updated: Support for WordPress 5.9
  • Tweaks: Several code tweaks & cleanup
Location:
opt-out-for-google-analytics
Files:
39 added
9 edited

Legend:

Unmodified
Added
Removed
  • opt-out-for-google-analytics/trunk/constants.php

    r2574575 r2664664  
    88    defined( 'GAOO_PLUGIN_URL' ) || define( 'GAOO_PLUGIN_URL', WP_PLUGIN_URL . DIRECTORY_SEPARATOR . GAOO_PLUGIN_NAME );
    99    defined( 'GAOO_PREFIX' ) || define( 'GAOO_PREFIX', '_gaoo_' );
    10     defined( 'GAOO_VERSION' ) || define( 'GAOO_VERSION', '1.9' );
     10    defined( 'GAOO_VERSION' ) || define( 'GAOO_VERSION', '2.0' );
    1111    defined( 'GAOO_SHORTCODE' ) || define( 'GAOO_SHORTCODE', '[ga_optout]' );
    1212    defined( 'GAOO_CAPABILITY' ) || define( 'GAOO_CAPABILITY', 'manage_options' );
  • opt-out-for-google-analytics/trunk/ga-opt-out.php

    r2574575 r2664664  
    44     * Plugin URI: https://www.schweizersolutions.com/?utm_source=wordpress&utm_medium=plugin&utm_campaign=plugin_uri
    55     * Description: Adds the possibility for the user to opt out from Google Analytics. The user will not be tracked by Google Analytics on this site until he allows it again, clears his cookies or uses a different browser.
    6      * Version: 1.9
     6     * Version: 2.0
    77     * Author: Schweizer Solutions GmbH
    88     * Author URI: https://www.schweizersolutions.com/?utm_source=wordpress&utm_medium=plugin&utm_campaign=author_uri
  • opt-out-for-google-analytics/trunk/inc/admin.class.php

    r2574575 r2664664  
    337337            // Validate UA code if entered manually, otherwise empty field.
    338338            if ( $data[ 'ga_plugin' ] == 'manual' && ! empty( $data[ 'ua_code' ] ) ) {
    339                 $data[ 'ua_code' ] = GAOO_Utils::validate_ua_code( $data[ 'ua_code' ] );
     339                $data[ 'ua_code' ] = GAOO_Utils::validate_ua_code( strtoupper( $data[ 'ua_code' ] ) );
    340340
    341341                if ( empty( $data[ 'ua_code' ] ) ) {
    342                     $this->messages->addError( esc_html__( "Please enter a valid UA Code. Format: UA-XXXXXX-Y", 'opt-out-for-google-analytics' ) );
     342                    $this->messages->addError( esc_html__( "Please enter a valid UA- or G-Code (Google Analytics 4). Format: UA-XXXXXX-Y or G-XXXXXXXXXX", 'opt-out-for-google-analytics' ) );
    343343                }
    344344            }
    345345            else {
    346                 $data[ 'ua_code' ] = '';
     346                $data[ 'ua_code' ]       = '';
     347                $data[ 'tracking_code' ] = '';
    347348            }
    348349
  • opt-out-for-google-analytics/trunk/inc/public.class.php

    r2179559 r2664664  
    11<?php
    22
    3 // If this file is called directly, abort.
    4 defined( 'WPINC' ) || die;
     3    // If this file is called directly, abort.
     4    defined( 'WPINC' ) || die;
    55
    6 class GAOO_Public {
    7     private $csstidy;
     6    class GAOO_Public {
     7        private $csstidy;
    88
    9     /**
    10     * GAOO_Public constructor.
    11     */
    12     public function __construct() {
    13         add_action( 'wp_head', array( $this, 'head_script' ), - 1 );
    14         add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
     9        /**
     10        * GAOO_Public constructor.
     11        */
     12        public function __construct() {
     13            add_action( 'wp_head', array( $this, 'head_script' ), -1 );
     14            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    1515
    16         add_shortcode( 'ga_optout', array( $this, 'shortcode' ) );
     16            add_shortcode( 'ga_optout', array( $this, 'shortcode' ) );
    1717
    18         if ( has_filter( 'widget_text', 'do_shortcode' ) !== false ) {
    19             add_filter( 'widget_text', 'do_shortcode' );
    20         }
     18            if ( has_filter( 'widget_text', 'do_shortcode' ) !== false ) {
     19                add_filter( 'widget_text', 'do_shortcode' );
     20            }
    2121
    22         $this->csstidy = new csstidy();
    23         $this->csstidy->load_template( 'highest_compression' );
    24     }
     22            $this->csstidy = new csstidy();
     23            $this->csstidy->load_template( 'highest_compression' );
     24        }
    2525
    26     /**
    27     * Enqueue scripts and styles for the public pages.
    28     */
    29     public function enqueue_scripts() {
    30         $min = SCRIPT_DEBUG ? '' : '.min';
     26        /**
     27        * Enqueue scripts and styles for the public pages.
     28        */
     29        public function enqueue_scripts() {
     30            $min = SCRIPT_DEBUG ? '' : '.min';
    3131
    32         wp_register_script( 'gaoo-public', GAOO_PLUGIN_URL . '/assets/public' . $min . '.js', array(), false, true );
    33     }
     32            wp_register_script( 'gaoo-public', GAOO_PLUGIN_URL . '/assets/public' . $min . '.js', array(), false, true );
     33        }
    3434
    35     /**
    36     * Handle the shortcode.
    37     *
    38     * @return string HTML code or empty string on error.
    39     */
    40     public function shortcode() {
    41         $form_data = GAOO_Utils::get_options();
    42         $ua_code   = GAOO_Utils::get_ua_code( $form_data['ga_plugin'], $form_data['ua_code'] );
     35        /**
     36        * Handle the shortcode.
     37        *
     38        * @return string HTML code or empty string on error.
     39        */
     40        public function shortcode() {
     41            $form_data = GAOO_Utils::get_options();
     42            $ua_code   = GAOO_Utils::get_code( $form_data[ 'ga_plugin' ], $form_data[ 'ua_code' ] );
    4343
    44         // Disable shortcode if status is deactivated or set to manual but empty UA-Code
    45         if ( $form_data['status'] == 'off' || empty( $ua_code ) ) {
    46             return '';
    47         }
     44            // Disable shortcode if status is deactivated or set to manual but empty UA-Code
     45            if ( $form_data[ 'status' ] == 'off' || empty( $ua_code ) ) {
     46                return '';
     47            }
    4848
    49         $current_status = isset( $_COOKIE[ 'ga-disable-' . $ua_code ] ) && $_COOKIE[ 'ga-disable-' . $ua_code ] == true ? 'activate' : 'deactivate';
    50         $json_data      = GAOO_Utils::get_json( $form_data, $ua_code );
     49            $current_status = isset( $_COOKIE[ 'ga-disable-' . $ua_code ] ) && $_COOKIE[ 'ga-disable-' . $ua_code ] == true ? 'activate' : 'deactivate';
     50            $json_data      = GAOO_Utils::get_json( $form_data, $ua_code );
    5151
    52         if ( empty( $json_data ) ) {
    53             return '';
    54         }
     52            if ( empty( $json_data ) ) {
     53                return '';
     54            }
    5555
    56         wp_localize_script( 'gaoo-public', 'gaoo_data', $json_data );
    57         wp_enqueue_script( 'gaoo-public' );
     56            wp_localize_script( 'gaoo-public', 'gaoo_data', $json_data );
     57            wp_enqueue_script( 'gaoo-public' );
    5858
    59         do_action( 'gaoo_before_shortcode', $ua_code, $current_status );
     59            do_action( 'gaoo_before_shortcode', $ua_code, $current_status );
    6060
    61         $html = '<a href="javascript:gaoo_handle_optout();" id="gaoo-link" class="gaoo-link-' . esc_attr( $current_status ) . '">' . esc_html( $json_data[ "link_" . $current_status ] ) . '</a>';
     61            $html = '<a href="javascript:gaoo_handle_optout();" id="gaoo-link" class="gaoo-link-' . esc_attr( $current_status ) . '">' . esc_html( $json_data[ "link_" . $current_status ] ) . '</a>';
    6262
    63         if ( ! empty( $form_data['custom_css'] ) && $this->csstidy->parse( $form_data['custom_css'] ) ) {
    64             $html .= '<style type="text/css">' . $this->csstidy->print->plain() . '</style>';
    65         }
     63            if ( ! empty( $form_data[ 'custom_css' ] ) && $this->csstidy->parse( $form_data[ 'custom_css' ] ) ) {
     64                $html .= '<style type="text/css">' . $this->csstidy->print->plain() . '</style>';
     65            }
    6666
    67         do_action( 'gaoo_after_shortcode', $ua_code, $current_status );
     67            do_action( 'gaoo_after_shortcode', $ua_code, $current_status );
    6868
    69         return $html;
    70     }
     69            return $html;
     70        }
    7171
    72     /**
    73     * Adds the GA Opt-Out code to the header.
    74     */
    75     public function head_script() {
    76         $form_data = GAOO_Utils::get_options();
    77         $ua_code   = GAOO_Utils::get_ua_code( $form_data['ga_plugin'], $form_data['ua_code'] );
     72        /**
     73        * Adds the GA Opt-Out code to the header.
     74        */
     75        public function head_script() {
     76            $form_data = GAOO_Utils::get_options();
     77            $ua_code   = GAOO_Utils::get_code( $form_data[ 'ga_plugin' ], $form_data[ 'ua_code' ] );
    7878
    79         do_action( 'gaoo_before_head_script', $ua_code );
     79            do_action( 'gaoo_before_head_script', $ua_code );
    8080
    81         if ( $form_data['status'] == 'on' && ! empty( $ua_code ) ) {
    82             echo "<script>var disableStr = 'ga-disable-{$ua_code}'; if (document.cookie.indexOf(disableStr + '=true') > -1) { window[disableStr] = true; }</script>";
    83         }
     81            if ( $form_data[ 'status' ] == 'on' && ! empty( $ua_code ) ) {
     82                echo "<script>var disableStr = 'ga-disable-{$ua_code}'; if (document.cookie.indexOf(disableStr + '=true') > -1) { window[disableStr] = true; }</script>";
     83            }
    8484
    85         if ( ! empty( $form_data['tracking_code'] ) ) {
    86             echo preg_replace( '/\s+/', '', stripslashes( $form_data['tracking_code'] ) );
    87         }
     85            if ( ! empty( $form_data[ 'tracking_code' ] ) ) {
     86                echo preg_replace( '/\s+/', ' ', stripslashes( $form_data['tracking_code'] ) );
     87            }
    8888
    89         do_action( 'gaoo_after_head_script', $ua_code );
    90     }
     89            do_action( 'gaoo_after_head_script', $ua_code );
     90        }
    9191
    92 }
     92    }
  • opt-out-for-google-analytics/trunk/inc/utils.class.php

    r2574575 r2664664  
    3838
    3939            if ( empty( $ua_code ) && ! empty( $form_data ) ) {
    40                 $ua_code = GAOO_Utils::get_ua_code( $form_data[ 'ga_plugin' ], $form_data[ 'ua_code' ] );
     40                $ua_code = GAOO_Utils::get_code( $form_data[ 'ga_plugin' ], $form_data[ 'ua_code' ] );
    4141            }
    4242
     
    128128
    129129        /**
    130          * Returns the UA-Code
     130         * Returns the UA- or G-Code
    131131         *
    132132         * @param string $ga_plugin Key (monsterinsights, gadash, analytify, manual) of choosen GA Plugin
    133133         * @param string $ua_code   The UA-Code if GA Plugin is set to manual (Default: null)
    134134         *
    135          * @return string The UA-Code (UA-XXXXXX-Y)
    136          */
    137         public static function get_ua_code( $ga_plugin, $ua_code = null ) {
     135         * @return string The UA- or G-Code (UA-XXXXXX-Y or G-XXXXXXXXXX)
     136         */
     137        public static function get_code( $ga_plugin, $ua_code = null ) {
    138138
    139139            switch ( $ga_plugin ) {
    140140                case 'monsterinsights':
    141 
    142                     if ( function_exists( 'monsterinsights_get_ua_to_output' ) ) {
     141                    $ua_code = null;
     142
     143                    if ( function_exists( 'monsterinsights_get_v4_id_to_output' ) ) {
     144                        $ua_code = monsterinsights_get_v4_id_to_output();
     145                    }
     146
     147                    if ( function_exists( 'monsterinsights_get_ua_to_output' ) && empty( $ua_code ) ) {
    143148                        $ua_code = monsterinsights_get_ua_to_output();
    144149                    }
     
    147152
    148153                case 'gadash':
     154                    $ua_code = null;
    149155
    150156                    // backward compatibility to older versions
     
    167173                        $ua_code      = $profile_info[ 2 ];
    168174                    }
    169 
    170                     if ( function_exists( 'exactmetrics_get_ua' ) ) {
     175                    elseif ( function_exists( 'exactmetrics_get_v4_id' ) ) {
     176                        $ua_code = exactmetrics_get_v4_id();
     177                    }
     178
     179                    if ( function_exists( 'exactmetrics_get_ua' ) && empty( $ua_code ) ) {
    171180                        $ua_code = exactmetrics_get_ua();
    172181                    }
     
    268277            $ua_code = str_replace( array( '–', '—', '−' ), '-', $ua_code );
    269278
    270             if ( preg_match( "/^(UA|YT|MO)-\d{4,}-\d+$/", strval( $ua_code ) ) ) {
     279            if ( preg_match( "/^UA-\d{4,}-\d+$/", $ua_code ) || self::is_ga4_code( $ua_code ) ) {
    271280                return $ua_code;
    272281            }
    273282
    274283            return '';
     284        }
     285
     286        /**
     287         * Check if the code is a Google Analytics 4 code.
     288         *
     289         * @param string $code Code to check.
     290         *
     291         * @return bool True if it is GA4, otherwise false.
     292         */
     293        public static function is_ga4_code( $code ) {
     294            return ( empty( $code ) ? false : boolval( preg_match( "/^G-[A-Za-z\d]+$/", $code ) ) );
    275295        }
    276296
     
    393413                $anonymip_enabled = $analytify->settings->get_option( 'anonymize_ip', 'wp-analytify-advanced' );
    394414                $anonymip_url     = admin_url( 'admin.php?page=analytify-settings#wp-analytify-advanced' );
    395 
    396                 $uacode_url = admin_url( 'admin.php?page=analytify-settings#wp-analytify-advanced' );
    397 
     415                $uacode_url       = admin_url( 'admin.php?page=analytify-settings#wp-analytify-authentication' );
    398416            }
    399417            elseif ( $data[ 'ga_plugin' ] == 'gaga' && ! empty( $GLOBALS[ 'GA_Google_Analytics' ] ) ) {
     
    403421                $anonymip_enabled = ( ! empty( $options[ 'gap_anonymize' ] ) ) ? $options[ 'gap_anonymize' ] : false;
    404422                $anonymip_url     = admin_url( 'options-general.php?page=ga-google-analytics#gap-panel-settings' );
    405 
    406                 $uacode_url = admin_url( 'options-general.php?page=ga-google-analytics#gap-panel-settings' );
     423                $uacode_url       = admin_url( 'options-general.php?page=ga-google-analytics#gap-panel-settings' );
    407424
    408425            }
    409426            elseif ( $data[ 'ga_plugin' ] == 'sitekit' && ( $analytics = get_option( 'googlesitekit_analytics_settings' ) ) ) {
    410427                $anonymip_url     = null;
    411                 $uacode_url       = admin_url( 'admin.php?page=googlesitekit-module-analytics' );
     428                $uacode_url       = admin_url( 'admin.php?page=googlesitekit-settings' );
    412429                $anonymip_enabled = ! empty( $analytics[ 'anonymizeIP' ] );
    413430            }
     
    415432                $anonymip_enabled = $anonymip_url = $uacode_url = null;
    416433
    417                 if ( ! empty( $data[ 'tracking_code' ] ) ) {
     434                if ( GAOO_Utils::is_ga4_code( $data[ 'ua_code' ] ) ) {
     435                    $anonymip_enabled = true;
     436                }
     437                elseif ( ! empty( $data[ 'tracking_code' ] ) ) {
    418438                    $anonymip_enabled = (bool) preg_match_all( '/(anonymizeIp|anonymize_ip).*true/mi', $data[ 'tracking_code' ] );
    419439                }
    420440            }
    421441
    422             $ua_code     = GAOO_Utils::get_ua_code( $data[ 'ga_plugin' ] );
     442            $ua_code     = GAOO_Utils::get_code( $data[ 'ga_plugin' ] );
    423443            $ua_code_txt = empty( $ua_code ) ? '' : " ($ua_code)";
    424444            $checklist   = array(
     
    428448                ),
    429449                array(
    430                     'label'   => esc_html__( 'Found valid UA-Code', 'opt-out-for-google-analytics' ) . $ua_code_txt,
     450                    'label'   => esc_html__( 'Found valid code', 'opt-out-for-google-analytics' ) . $ua_code_txt,
    431451                    'checked' => ( ! empty( $ua_code ) ),
    432452                    'url'     => $uacode_url,
  • opt-out-for-google-analytics/trunk/readme.txt

    r2574575 r2664664  
    33Tags: google analytics, opt-out, dsgvo, gdpr, analytics
    44Requires at least: 3.5
    5 Tested up to: 5.8
     5Tested up to: 5.9
    66Requires PHP: 7.0
    7 Stable tag: 1.9
     7Stable tag: 2.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2020**Features**
    2121
    22 * **Full integration of the new WordPress DSGVO / GDPR features**
     22* **Support for Google Analytics 4 (GA4)**
     23* Full integration of the new WordPress DSGVO / GDPR features
    2324* Weekly check whether the settings are still data protection compliant!
    2425* Compatible with Google Tag Manager.
     
    287288== Changelog ==
    288289
     290= 2.0 =
     291* Added: Support for Google Analytics 4 (GA4)
     292* Added: MonsterInsights GA4 support
     293* Added: ExactMetrics GA4 support
     294* Added: Analytify GA4 support
     295* Added: GA Google Analytics GA4 support
     296* Updated: Support for WordPress 5.9
     297* Tweaks: Several code tweaks & cleanup
     298
    289299= 1.9 =
    290300* Updated: Support for WordPress 5.8
  • opt-out-for-google-analytics/trunk/templates/settings.php

    r2524476 r2664664  
    2323        <span class="gaoo-clipboard dashicons dashicons-admin-page" title="<?php esc_attr_e( 'Click to copy the shortcode!', 'opt-out-for-google-analytics' ); ?>" data-copy="<?php echo esc_attr( GAOO_SHORTCODE ); ?>"></span>
    2424        <br />
    25         <small><?php printf( __( "Do you have a data processing agreement for Google Analytics? <a href='%s' target='_blank'>More infos.</a>", 'opt-out-for-google-analytics' ), esc_url( 'https://support.google.com/analytics/answer/3379636?hl=' . ( empty( $language ) ?: $language ) ) ); ?></small>
     25
     26        <small><?php esc_html_e( "Do you have a data processing agreement for Google Analytics?", 'opt-out-for-google-analytics' ); ?>
     27            &nbsp;<?php printf( "<a href='%s' target='_blank'>%s</a>", esc_url( 'https://support.google.com/analytics/answer/3379636?hl=' . ( empty( $language ) ?: $language ) ), esc_html__( 'More infos.', 'opt-out-for-google-analytics' ) ); ?></small>
    2628    </p>
    2729
     
    4749            <tr>
    4850                <th scope="row">
    49                     <label><?php esc_html_e( "UA-Code", 'opt-out-for-google-analytics' ); ?></label>
     51                    <label><?php esc_html_e( "Tracking ID", 'opt-out-for-google-analytics' ); ?></label>
    5052                </th>
    5153                <td>
     
    5860                        </label>
    5961
    60                         <p <?php echo $is_manual ? '' : 'class="hide"'; ?>>
    61                             <input type="text" id="gaoo-ua-code" name="gaoo[ua_code]" class="text" placeholder="UA-XXXXXX-Y" value="<?php echo esc_attr( $ua_code ); ?>" /><br>
     62                        <div <?php echo $is_manual ? '' : 'class="hide"'; ?>>
     63                            <input type="text" id="gaoo-ua-code" name="gaoo[ua_code]" class="regular-text" placeholder="<?php esc_attr_e( 'G-XXXXXXXXXX or UA-XXXXXX-Y', 'opt-out-for-google-analytics' ); ?>" value="<?php echo esc_attr( $ua_code ); ?>" /><br>
     64
     65                            <p>
     66                                <small><?php esc_html_e( "Searching for your code?", 'opt-out-for-google-analytics' ); ?>
     67                                    &nbsp;<?php printf( "<a href='%s' target='_blank'>%s</a>", esc_url( 'https://support.google.com/analytics/answer/9539598?hl=' . ( empty( $language ) ?: $language ) ), esc_html__( 'More infos.', 'opt-out-for-google-analytics' ) ); ?></small>
     68                            </p>
     69
    6270                            <textarea id="ga-plugin-tracking-code" class="regular-text" rows="10" name="gaoo[tracking_code]" placeholder="<?php esc_attr_e( 'Enter here your tracking code for Google Analytics to insert it on your whole website. Leave empty if you do not want it.', 'opt-out-for-google-analytics' ); ?>"><?php echo stripslashes( $tracking_code ); ?></textarea>
    63                         </p>
     71                        </div>
    6472                        <br />
    6573
Note: See TracChangeset for help on using the changeset viewer.