PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.0.5
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.0.5
5.13.0 5.12.1 5.12.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 / Admin / TrackingSettings.php
matomo / classes / WpMatomo / Admin Last commit date
TrackingSettings 6 years ago views 6 years ago AccessSettings.php 6 years ago Admin.php 6 years ago AdminSettings.php 6 years ago AdminSettingsInterface.php 6 years ago AdvancedSettings.php 6 years ago ExclusionSettings.php 6 years ago GeolocationSettings.php 6 years ago GetStarted.php 6 years ago Info.php 6 years ago Marketplace.php 6 years ago Menu.php 6 years ago PrivacySettings.php 6 years ago SafeModeMenu.php 6 years ago Summary.php 6 years ago SystemReport.php 6 years ago TrackingSettings.php 6 years ago
TrackingSettings.php
203 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\Admin;
11
12 use WpMatomo\Capabilities;
13 use WpMatomo\Settings;
14 use WpMatomo\Site;
15 use WpMatomo\TrackingCode\TrackingCodeGenerator;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // if accessed directly
19 }
20
21 class TrackingSettings implements AdminSettingsInterface {
22 const FORM_NAME = 'matomo';
23 const NONCE_NAME = 'matomo_settings';
24 const TRACK_MODE_DEFAULT = 'default';
25 const TRACK_MODE_DISABLED = 'disabled';
26 const TRACK_MODE_MANUALLY = 'manually';
27 const TRACK_MODE_TAGMANAGER = 'tagmanager';
28
29 /**
30 * @var Settings
31 */
32 private $settings;
33
34 /**
35 * @param Settings $settings
36 */
37 public function __construct( $settings ) {
38 $this->settings = $settings;
39 }
40
41 public function get_title() {
42 return esc_html__( 'Tracking', 'matomo' );
43 }
44
45 private function update_if_submitted() {
46 if ( isset( $_POST )
47 && ! empty( $_POST[ self::FORM_NAME ] )
48 && is_admin()
49 && check_admin_referer( self::NONCE_NAME )
50 && $this->can_user_manage() ) {
51 $this->apply_settings();
52
53 return true;
54 }
55
56 return false;
57 }
58
59 public function can_user_manage() {
60 return current_user_can( Capabilities::KEY_SUPERUSER );
61 }
62
63 private function apply_settings() {
64 $keys_to_keep = array(
65 'track_mode',
66 'track_across',
67 'track_across_alias',
68 'track_crossdomain_linking',
69 'track_feed',
70 'track_feed_addcampaign',
71 'track_feed_campaign',
72 'track_heartbeat',
73 'track_user_id',
74 'track_datacfasync',
75 'tagmanger_container_ids',
76 'set_download_extensions',
77 'set_download_classes',
78 'set_link_classes',
79 'track_admin',
80 'limit_cookies_referral',
81 'limit_cookies_session',
82 'limit_cookies_visitor',
83 'limit_cookies',
84 'force_post',
85 'disable_cookies',
86 'add_download_extensions',
87 'track_404',
88 'track_search',
89 'add_post_annotations',
90 'track_content',
91 'track_ecommerce',
92 'track_noscript',
93 'noscript_code',
94 'track_codeposition',
95 'tracking_code',
96 'force_protocol',
97 'track_js_endpoint',
98 'track_api_endpoint',
99 );
100
101 if ( matomo_has_tag_manager() ) {
102 $keys_to_keep[] = 'tagmanger_container_ids';
103 }
104
105 $values = array();
106
107 // default value in case no role/ post type is selected to make sure we unset it if no role /post type is selected
108 $values['add_post_annotations'] = array();
109 $values['tagmanger_container_ids'] = array();
110
111 if ( ! empty( $_POST[ self::FORM_NAME ]['track_mode'] ) ) {
112 $track_mode = $_POST[ self::FORM_NAME ]['track_mode'];
113 $previus_track_mode = $this->settings->get_global_option( 'track_mode' );
114
115 if ( self::TRACK_MODE_TAGMANAGER === $track_mode ) {
116 // no noscript mode in this case
117 $_POST[ self::FORM_NAME ]['track_noscript'] = '';
118 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
119 } else {
120 unset( $_POST['tagmanger_container_ids'] );
121 }
122
123 if ( self::TRACK_MODE_MANUALLY === $track_mode
124 || ( self::TRACK_MODE_DISABLED === $track_mode &&
125 in_array( $previus_track_mode, array( self::TRACK_MODE_DISABLED, self::TRACK_MODE_MANUALLY ) ) ) ) {
126 // We want to keep the tracking code when user switches between disabled and manually or disabled to disabled.
127 if ( ! empty( $_POST[ self::FORM_NAME ]['tracking_code'] ) ) {
128 $_POST[ self::FORM_NAME ]['tracking_code'] = stripslashes( $_POST[ self::FORM_NAME ]['tracking_code'] );
129 } else {
130 $_POST[ self::FORM_NAME ]['tracking_code'] = '';
131 }
132 if ( ! empty( $_POST[ self::FORM_NAME ]['noscript_code'] ) ) {
133 $_POST[ self::FORM_NAME ]['noscript_code'] = stripslashes( $_POST[ self::FORM_NAME ]['noscript_code'] );
134 } else {
135 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
136 }
137 } else {
138 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
139 $_POST[ self::FORM_NAME ]['tracking_code'] = '';
140 }
141 }
142
143 foreach ( $_POST[ self::FORM_NAME ] as $name => $value ) {
144 if ( in_array( $name, $keys_to_keep, true ) ) {
145 $values[ $name ] = $value;
146 }
147 }
148
149 $this->settings->apply_tracking_related_changes( $values );
150
151 return true;
152 }
153
154 public function show_settings() {
155 $was_updated = $this->update_if_submitted();
156 $settings = $this->settings;
157
158 $containers = $this->get_active_containers();
159
160 $track_modes = array(
161 self::TRACK_MODE_DISABLED => esc_html__( 'Disabled', 'matomo' ),
162 self::TRACK_MODE_DEFAULT => esc_html__( 'Default tracking', 'matomo' ),
163 self::TRACK_MODE_MANUALLY => esc_html__( 'Enter manually', 'matomo' ),
164 );
165
166 if ( ! empty( $containers ) ) {
167 $track_modes[ self::TRACK_MODE_TAGMANAGER ] = esc_html__( 'Tag Manager', 'matomo' );
168 }
169
170 $site = new Site();
171 $idsite = $site->get_current_matomo_site_id();
172
173 $tracking_code_generator = new TrackingCodeGenerator( $this->settings );
174 $matomo_default_tracking_code = $tracking_code_generator->prepare_tracking_code( $idsite );
175
176 include dirname( __FILE__ ) . '/views/tracking.php';
177 }
178
179 public function get_active_containers() {
180 // we don't use Matomo API here to avoid needing to bootstrap Matomo which is slow and could break things
181 $containers = array();
182 if ( matomo_has_tag_manager() ) {
183 global $wpdb;
184 $dbsettings = new \WpMatomo\Db\Settings();
185 $container_table = $dbsettings->prefix_table_name( 'tagmanager_container' );
186 try {
187 $containers = $wpdb->get_results( sprintf( 'SELECT `idcontainer`, `name` FROM %s where `status` = "active"', $container_table ) );
188 } catch ( \Exception $e ) {
189 // table may not exist yet etc
190 $containers = array();
191 }
192 }
193 $by_id = array();
194 foreach ( $containers as $container ) {
195 $by_id[ $container->idcontainer ] = $container->name;
196 }
197
198 return $by_id;
199 }
200
201
202 }
203