PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.7.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.7.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / OptOut.php
matomo / classes / WpMatomo Last commit date
Admin 4 months ago Commands 2 years ago Db 1 year ago Ecommerce 4 months ago Report 1 year ago Site 6 months ago TrackingCode 4 months ago Updater 4 years ago User 7 months ago Workarounds 2 years ago WpStatistics 6 months ago views 4 years ago AIBotTracking.php 4 months ago API.php 1 year ago Access.php 4 years ago AjaxTracker.php 4 months ago Annotations.php 4 years ago Bootstrap.php 10 months ago Capabilities.php 4 years ago Compatibility.php 2 years ago Email.php 2 years ago ErrorNotice.php 2 years ago Installer.php 6 months ago Logger.php 1 year ago OptOut.php 1 year ago Paths.php 6 months ago PluginAdminOverrides.php 2 years ago PrivacyBadge.php 4 years ago RedirectOnActivation.php 4 years ago Referral.php 2 years ago Roles.php 10 months ago ScheduledTasks.php 6 months ago Settings.php 4 months ago Site.php 3 years ago TrackingCode.php 1 year ago Uninstaller.php 6 months ago Updater.php 10 months ago User.php 4 years ago
OptOut.php
142 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 {
21 const OPT_OUT_DIV_ID = 'matomo-opt-out-form-embed';
22
23 private $language = null;
24
25 public function register_hooks() {
26 add_shortcode( 'matomo_opt_out', [ $this, 'show_classic_opt_out' ] );
27 add_shortcode( 'matomo_opt_out_form', [ $this, 'show_opt_out' ] );
28 add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
29 add_action( 'init', [ $this, 'load_block' ] );
30 }
31
32 public function load_scripts() {
33 if ( ! is_admin() ) {
34 wp_register_script( 'matomo_opt_out_js', plugins_url( 'assets/js/optout.js', MATOMO_ANALYTICS_FILE ), [], 1, true );
35 }
36 }
37
38 private function translate( $id ) {
39 return esc_html( Piwik::translate( $id, [], $this->language ) );
40 }
41
42 public function show_opt_out( $atts ) {
43 $this->language = $this->get_language_from_atts( $atts );
44 $this->language = isset( $this->language ) ? $this->language : 'auto';
45
46 $div_id = self::OPT_OUT_DIV_ID;
47
48 $url = 'app/index.php?module=CoreAdminHome&action=optOutJS&divId=' . $div_id . '&language=' . rawurlencode( $this->language ) . '&showIntro=1';
49 $url = plugins_url( $url, MATOMO_ANALYTICS_FILE );
50
51 wp_enqueue_script( 'matomo_opt_out_form_js', $url, [], 1, true ); // output in the footer
52
53 // phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
54 $content = "<div id=\"$div_id\"></div>";
55 return $content;
56 }
57
58 public function show_classic_opt_out( $atts ) {
59 $this->language = $this->get_language_from_atts( $atts );
60
61 try {
62 Bootstrap::do_bootstrap();
63 } catch ( Throwable $e ) {
64 $logger = new Logger();
65 $logger->log_exception( 'optout', $e );
66
67 return '<p>An error occurred. Please check Matomo system report in WP-Admin.</p>';
68 }
69
70 $dnt_checker = new DoNotTrackHeaderChecker();
71 $dnt_enabled = $dnt_checker->isDoNotTrackFound();
72
73 if ( ! empty( $dnt_enabled ) ) {
74 return '<p>' . $this->translate( 'CoreAdminHome_OptOutDntFound' ) . '</p>';
75 }
76
77 wp_enqueue_script( 'matomo_opt_out_js' );
78
79 $track_visits = empty( $_COOKIE['mtm_consent_removed'] );
80
81 $style_tracking_enabled = '';
82 $style_tracking_disabled = '';
83 $checkbox_attr = '';
84 if ( $track_visits ) {
85 $style_tracking_enabled = 'style="display:none;"';
86 $checkbox_attr = 'checked="checked"';
87 } else {
88 $style_tracking_disabled = 'style="display:none;"';
89 }
90
91 $content = '<p id="matomo_opted_out_intro" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_OptOutComplete' ) . ' ' . $this->translate( 'CoreAdminHome_OptOutCompleteBis' ) . '</p>';
92 $content .= '<p id="matomo_opted_in_intro" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouMayOptOut2' ) . ' ' . $this->translate( 'CoreAdminHome_YouMayOptOut3' ) . '</p>';
93
94 $content .= '<form>
95 <input type="checkbox" id="matomo_optout_checkbox" ' . $checkbox_attr . '/>
96 <label for="matomo_optout_checkbox"><strong>
97 <span id="matomo_opted_in_label" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouAreNotOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_UncheckToOptOut' ) . '</span>
98 <span id="matomo_opted_out_label" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_YouAreOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_CheckToOptIn' ) . '</span>
99 </strong></label></form>';
100 $content .= '<noscript><p><strong style="color: #ff0000;">This opt out feature requires JavaScript.</strong></p></noscript>';
101 $content .= '<p id="matomo_outout_err_cookies" style="display: none;"><strong>' . $this->translate( 'CoreAdminHome_OptOutErrorNoCookies' ) . '</strong></p>';
102
103 return $content;
104 }
105
106 public function load_block() {
107 // before WordPress 5.0
108 if ( ! function_exists( 'register_block_type' ) ) {
109 // Gutenberg is not active.
110 return;
111 }
112
113 wp_register_script(
114 'matomo-opt-out',
115 plugins_url( '/assets/js/blocks/matomo_opt_out.js', MATOMO_ANALYTICS_FILE ),
116 array( 'wp-blocks', 'wp-i18n', 'wp-element' ),
117 filemtime( plugin_dir_path( MATOMO_ANALYTICS_FILE ) . '/assets/js/blocks/matomo_opt_out.js' ),
118 true
119 );
120
121 register_block_type(
122 'matomo/matomo-opt-out',
123 array(
124 'editor_script' => 'matomo-opt-out',
125 )
126 );
127 }
128
129 private function get_language_from_atts( $atts ) {
130 $a = shortcode_atts(
131 [
132 'language' => null,
133 ],
134 $atts
135 );
136 if ( ! empty( $a['language'] ) && strlen( $a['language'] ) < 6 ) {
137 return $a['language'];
138 }
139 return null;
140 }
141 }
142