Admin
3 years ago
Commands
4 years ago
Db
4 years ago
Ecommerce
3 years ago
Report
4 years ago
Site
3 years ago
TrackingCode
4 years ago
Updater
4 years ago
User
3 years ago
WpStatistics
4 years ago
views
4 years ago
API.php
4 years ago
Access.php
4 years ago
AjaxTracker.php
5 years ago
Annotations.php
4 years ago
Bootstrap.php
4 years ago
Capabilities.php
4 years ago
Compatibility.php
4 years ago
Email.php
4 years ago
Installer.php
4 years ago
Logger.php
4 years ago
OptOut.php
4 years ago
Paths.php
4 years ago
PrivacyBadge.php
4 years ago
RedirectOnActivation.php
4 years ago
Referral.php
4 years ago
Roles.php
4 years ago
ScheduledTasks.php
4 years ago
Settings.php
4 years ago
Site.php
3 years ago
TrackingCode.php
4 years ago
Uninstaller.php
4 years ago
Updater.php
4 years ago
User.php
4 years ago
OptOut.php
118 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 | private $language = null; |
| 22 | |
| 23 | public function register_hooks() { |
| 24 | add_shortcode( 'matomo_opt_out', array( $this, 'show_opt_out' ) ); |
| 25 | add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) ); |
| 26 | add_action( 'init', [ $this, 'load_block' ] ); |
| 27 | } |
| 28 | |
| 29 | public function load_scripts() { |
| 30 | if ( ! is_admin() ) { |
| 31 | wp_register_script( 'matomo_opt_out_js', plugins_url( 'assets/js/optout.js', MATOMO_ANALYTICS_FILE ), [], 1, true ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private function translate( $id ) { |
| 36 | return esc_html( Piwik::translate( $id, [], $this->language ) ); |
| 37 | } |
| 38 | |
| 39 | public function show_opt_out( $atts ) { |
| 40 | $a = shortcode_atts( |
| 41 | [ |
| 42 | 'language' => null, |
| 43 | ], |
| 44 | $atts |
| 45 | ); |
| 46 | if ( ! empty( $a['language'] ) && strlen( $a['language'] ) < 6 ) { |
| 47 | $this->language = $a['language']; |
| 48 | } |
| 49 | |
| 50 | try { |
| 51 | Bootstrap::do_bootstrap(); |
| 52 | } catch ( Throwable $e ) { |
| 53 | $logger = new Logger(); |
| 54 | $logger->log_exception( 'optout', $e ); |
| 55 | |
| 56 | return '<p>An error occurred. Please check Matomo system report in WP-Admin.</p>'; |
| 57 | } |
| 58 | |
| 59 | $dnt_checker = new DoNotTrackHeaderChecker(); |
| 60 | $dnt_enabled = $dnt_checker->isDoNotTrackFound(); |
| 61 | |
| 62 | if ( ! empty( $dnt_enabled ) ) { |
| 63 | return '<p>' . $this->translate( 'CoreAdminHome_OptOutDntFound' ) . '</p>'; |
| 64 | } |
| 65 | |
| 66 | wp_enqueue_script( 'matomo_opt_out_js' ); |
| 67 | |
| 68 | $track_visits = empty( $_COOKIE['mtm_consent_removed'] ); |
| 69 | |
| 70 | $style_tracking_enabled = ''; |
| 71 | $style_tracking_disabled = ''; |
| 72 | $checkbox_attr = ''; |
| 73 | if ( $track_visits ) { |
| 74 | $style_tracking_enabled = 'style="display:none;"'; |
| 75 | $checkbox_attr = 'checked="checked"'; |
| 76 | } else { |
| 77 | $style_tracking_disabled = 'style="display:none;"'; |
| 78 | } |
| 79 | |
| 80 | $content = '<p id="matomo_opted_out_intro" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_OptOutComplete' ) . ' ' . $this->translate( 'CoreAdminHome_OptOutCompleteBis' ) . '</p>'; |
| 81 | $content .= '<p id="matomo_opted_in_intro" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouMayOptOut2' ) . ' ' . $this->translate( 'CoreAdminHome_YouMayOptOut3' ) . '</p>'; |
| 82 | |
| 83 | $content .= '<form> |
| 84 | <input type="checkbox" id="matomo_optout_checkbox" ' . $checkbox_attr . '/> |
| 85 | <label for="matomo_optout_checkbox"><strong> |
| 86 | <span id="matomo_opted_in_label" ' . $style_tracking_disabled . '>' . $this->translate( 'CoreAdminHome_YouAreNotOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_UncheckToOptOut' ) . '</span> |
| 87 | <span id="matomo_opted_out_label" ' . $style_tracking_enabled . '>' . $this->translate( 'CoreAdminHome_YouAreOptedOut' ) . ' ' . $this->translate( 'CoreAdminHome_CheckToOptIn' ) . '</span> |
| 88 | </strong></label></form>'; |
| 89 | $content .= '<noscript><p><strong style="color: #ff0000;">This opt out feature requires JavaScript.</strong></p></noscript>'; |
| 90 | $content .= '<p id="matomo_outout_err_cookies" style="display: none;"><strong>' . $this->translate( 'CoreAdminHome_OptOutErrorNoCookies' ) . '</strong></p>'; |
| 91 | |
| 92 | return $content; |
| 93 | } |
| 94 | |
| 95 | public function load_block() { |
| 96 | // before WordPress 5.0 |
| 97 | if ( ! function_exists( 'register_block_type' ) ) { |
| 98 | // Gutenberg is not active. |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | wp_register_script( |
| 103 | 'matomo-opt-out', |
| 104 | plugins_url( '/assets/js/blocks/matomo_opt_out.js', MATOMO_ANALYTICS_FILE ), |
| 105 | array( 'wp-blocks', 'wp-i18n', 'wp-element' ), |
| 106 | filemtime( plugin_dir_path( MATOMO_ANALYTICS_FILE ) . '/assets/js/blocks/matomo_opt_out.js' ), |
| 107 | true |
| 108 | ); |
| 109 | |
| 110 | register_block_type( |
| 111 | 'matomo/matomo-opt-out', |
| 112 | array( |
| 113 | 'editor_script' => 'matomo-opt-out', |
| 114 | ) |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 |