Admin
1 month ago
Commands
2 years ago
Db
1 year ago
Ecommerce
1 month ago
Report
2 months ago
Site
2 months ago
TrackingCode
4 months ago
Updater
4 years ago
User
2 months ago
Workarounds
2 years ago
WpStatistics
5 months ago
views
1 month ago
AIBotTracking.php
4 months ago
API.php
2 months ago
Access.php
4 years ago
AjaxTracker.php
4 months ago
Annotations.php
2 months ago
Bootstrap.php
10 months ago
Capabilities.php
2 months ago
Compatibility.php
2 months ago
Email.php
2 years ago
ErrorNotice.php
1 month ago
Feature.php
2 months ago
Installer.php
1 month ago
Logger.php
1 year ago
OptOut.php
1 month ago
Paths.php
5 months ago
PluginActionLinks.php
2 months ago
PluginAdminOverrides.php
2 months ago
PluginInit.php
2 months ago
PrivacyBadge.php
4 years ago
RedirectOnActivation.php
2 months ago
Referral.php
2 months ago
Roles.php
2 months ago
ScheduledTasks.php
2 months ago
Settings.php
4 months ago
Site.php
3 years ago
TrackingCode.php
2 months ago
Uninstaller.php
5 months ago
Updater.php
10 months ago
User.php
4 years ago
OptOut.php
143 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo; |
| 11 | |
| 12 | use Piwik\Piwik; |
| 13 | use Piwik\Plugins\PrivacyManager\DoNotTrackHeaderChecker; |
| 14 | use Throwable; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; // if accessed directly |
| 18 | } |
| 19 | |
| 20 | class OptOut extends Feature { |
| 21 | |
| 22 | const OPT_OUT_DIV_ID = 'matomo-opt-out-form-embed'; |
| 23 | |
| 24 | private $language = null; |
| 25 | |
| 26 | public function register_hooks() { |
| 27 | add_shortcode( 'matomo_opt_out', [ $this, 'show_classic_opt_out' ] ); |
| 28 | add_shortcode( 'matomo_opt_out_form', [ $this, 'show_opt_out' ] ); |
| 29 | add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) ); |
| 30 | add_action( 'init', [ $this, 'load_block' ] ); |
| 31 | } |
| 32 | |
| 33 | public function load_scripts() { |
| 34 | if ( ! is_admin() ) { |
| 35 | wp_register_script( 'matomo_opt_out_js', plugins_url( 'assets/js/optout.js', MATOMO_ANALYTICS_FILE ), [], matomo_get_asset_version(), true ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private function translate( $id ) { |
| 40 | return esc_html( Piwik::translate( $id, [], $this->language ) ); |
| 41 | } |
| 42 | |
| 43 | public function show_opt_out( $atts ) { |
| 44 | $this->language = $this->get_language_from_atts( $atts ); |
| 45 | $this->language = isset( $this->language ) ? $this->language : 'auto'; |
| 46 | |
| 47 | $div_id = self::OPT_OUT_DIV_ID; |
| 48 | |
| 49 | $url = 'app/index.php?module=CoreAdminHome&action=optOutJS&divId=' . $div_id . '&language=' . rawurlencode( $this->language ) . '&showIntro=1'; |
| 50 | $url = plugins_url( $url, MATOMO_ANALYTICS_FILE ); |
| 51 | |
| 52 | wp_enqueue_script( 'matomo_opt_out_form_js', $url, [], matomo_get_asset_version(), true ); // output in the footer |
| 53 | |
| 54 | // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript |
| 55 | $content = "<div id=\"$div_id\"></div>"; |
| 56 | return $content; |
| 57 | } |
| 58 | |
| 59 | public function show_classic_opt_out( $atts ) { |
| 60 | $this->language = $this->get_language_from_atts( $atts ); |
| 61 | |
| 62 | try { |
| 63 | Bootstrap::do_bootstrap(); |
| 64 | } catch ( Throwable $e ) { |
| 65 | $logger = new Logger(); |
| 66 | $logger->log_exception( 'optout', $e ); |
| 67 | |
| 68 | return '<p>An error occurred. Please check Matomo system report in WP-Admin.</p>'; |
| 69 | } |
| 70 | |
| 71 | $dnt_checker = new DoNotTrackHeaderChecker(); |
| 72 | $dnt_enabled = $dnt_checker->isDoNotTrackFound(); |
| 73 | |
| 74 | if ( ! empty( $dnt_enabled ) ) { |
| 75 | return '<p>' . $this->translate( 'CoreAdminHome_OptOutDntFound' ) . '</p>'; |
| 76 | } |
| 77 | |
| 78 | wp_enqueue_script( 'matomo_opt_out_js' ); |
| 79 | |
| 80 | $track_visits = empty( $_COOKIE['mtm_consent_removed'] ); |
| 81 | |
| 82 | $style_tracking_enabled = ''; |
| 83 | $style_tracking_disabled = ''; |
| 84 | $checkbox_attr = ''; |
| 85 | if ( $track_visits ) { |
| 86 | $style_tracking_enabled = 'style="display:none;"'; |
| 87 | $checkbox_attr = 'checked="checked"'; |
| 88 | } else { |
| 89 | $style_tracking_disabled = 'style="display:none;"'; |
| 90 | } |
| 91 | |
| 92 | $content = '<p id="matomo_opted_out_intro" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_OptOutComplete' ) . ' ' . $this->translate( 'CoreAdminHome_OptOutCompleteBis' ) . '</p>'; |
| 93 | $content .= '<p id="matomo_opted_in_intro" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouMayOptOut2' ) . ' ' . $this->translate( 'CoreAdminHome_YouMayOptOut3' ) . '</p>'; |
| 94 | |
| 95 | $content .= '<form> |
| 96 | <input type="checkbox" id="matomo_optout_checkbox" ' . $checkbox_attr . '/> |
| 97 | <label for="matomo_optout_checkbox"><strong> |
| 98 | <span id="matomo_opted_in_label" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouAreNotOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_UncheckToOptOut' ) . '</span> |
| 99 | <span id="matomo_opted_out_label" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_YouAreOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_CheckToOptIn' ) . '</span> |
| 100 | </strong></label></form>'; |
| 101 | $content .= '<noscript><p><strong style="color: #ff0000;">This opt out feature requires JavaScript.</strong></p></noscript>'; |
| 102 | $content .= '<p id="matomo_outout_err_cookies" style="display: none;"><strong>' . $this->translate( 'CoreAdminHome_OptOutErrorNoCookies' ) . '</strong></p>'; |
| 103 | |
| 104 | return $content; |
| 105 | } |
| 106 | |
| 107 | public function load_block() { |
| 108 | // before WordPress 5.0 |
| 109 | if ( ! function_exists( 'register_block_type' ) ) { |
| 110 | // Gutenberg is not active. |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | wp_register_script( |
| 115 | 'matomo-opt-out', |
| 116 | plugins_url( '/assets/js/blocks/matomo_opt_out.js', MATOMO_ANALYTICS_FILE ), |
| 117 | array( 'wp-blocks', 'wp-i18n', 'wp-element' ), |
| 118 | filemtime( plugin_dir_path( MATOMO_ANALYTICS_FILE ) . '/assets/js/blocks/matomo_opt_out.js' ), |
| 119 | true |
| 120 | ); |
| 121 | |
| 122 | register_block_type( |
| 123 | 'matomo/matomo-opt-out', |
| 124 | array( |
| 125 | 'editor_script' => 'matomo-opt-out', |
| 126 | ) |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | private function get_language_from_atts( $atts ) { |
| 131 | $a = shortcode_atts( |
| 132 | [ |
| 133 | 'language' => null, |
| 134 | ], |
| 135 | $atts |
| 136 | ); |
| 137 | if ( ! empty( $a['language'] ) && strlen( $a['language'] ) < 6 ) { |
| 138 | return $a['language']; |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | } |
| 143 |