PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.0.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.0.2
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 5 years ago views 5 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 Dashboard.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 5 years ago TrackingSettings.php 5 years ago
TrackingSettings.php
224 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_jserrors',
99 'track_api_endpoint',
100 Settings::SITE_CURRENCY
101 );
102
103 if ( matomo_has_tag_manager() ) {
104 $keys_to_keep[] = 'tagmanger_container_ids';
105 }
106
107 $values = array();
108
109 // default value in case no role/ post type is selected to make sure we unset it if no role /post type is selected
110 $values['add_post_annotations'] = array();
111 $values['tagmanger_container_ids'] = array();
112
113 $valid_currencies = $this->get_supported_currencies();
114
115 if ( empty( $_POST[ self::FORM_NAME ][Settings::SITE_CURRENCY] )
116 || !array_key_exists( $_POST[ self::FORM_NAME ][Settings::SITE_CURRENCY], $valid_currencies ) ) {
117 $_POST[ self::FORM_NAME ][Settings::SITE_CURRENCY] = 'USD';
118 }
119
120 if ( ! empty( $_POST[ self::FORM_NAME ]['track_mode'] ) ) {
121 $track_mode = $_POST[ self::FORM_NAME ]['track_mode'];
122 $previus_track_mode = $this->settings->get_global_option( 'track_mode' );
123
124 if ( self::TRACK_MODE_TAGMANAGER === $track_mode ) {
125 // no noscript mode in this case
126 $_POST[ self::FORM_NAME ]['track_noscript'] = '';
127 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
128 } else {
129 unset( $_POST['tagmanger_container_ids'] );
130 }
131
132 if ( self::TRACK_MODE_MANUALLY === $track_mode
133 || ( self::TRACK_MODE_DISABLED === $track_mode &&
134 in_array( $previus_track_mode, array( self::TRACK_MODE_DISABLED, self::TRACK_MODE_MANUALLY ) ) ) ) {
135 // We want to keep the tracking code when user switches between disabled and manually or disabled to disabled.
136 if ( ! empty( $_POST[ self::FORM_NAME ]['tracking_code'] ) ) {
137 $_POST[ self::FORM_NAME ]['tracking_code'] = stripslashes( $_POST[ self::FORM_NAME ]['tracking_code'] );
138 } else {
139 $_POST[ self::FORM_NAME ]['tracking_code'] = '';
140 }
141 if ( ! empty( $_POST[ self::FORM_NAME ]['noscript_code'] ) ) {
142 $_POST[ self::FORM_NAME ]['noscript_code'] = stripslashes( $_POST[ self::FORM_NAME ]['noscript_code'] );
143 } else {
144 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
145 }
146 } else {
147 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
148 $_POST[ self::FORM_NAME ]['tracking_code'] = '';
149 }
150 }
151
152 foreach ( $_POST[ self::FORM_NAME ] as $name => $value ) {
153 if ( in_array( $name, $keys_to_keep, true ) ) {
154 $values[ $name ] = $value;
155 }
156 }
157
158 $this->settings->apply_tracking_related_changes( $values );
159
160 return true;
161 }
162
163 public function show_settings() {
164 $was_updated = $this->update_if_submitted();
165 $settings = $this->settings;
166
167 $containers = $this->get_active_containers();
168
169 $track_modes = array(
170 self::TRACK_MODE_DISABLED => esc_html__( 'Disabled', 'matomo' ),
171 self::TRACK_MODE_DEFAULT => esc_html__( 'Default tracking', 'matomo' ),
172 self::TRACK_MODE_MANUALLY => esc_html__( 'Enter manually', 'matomo' ),
173 );
174
175 if ( ! empty( $containers ) ) {
176 $track_modes[ self::TRACK_MODE_TAGMANAGER ] = esc_html__( 'Tag Manager', 'matomo' );
177 }
178
179 $site = new Site();
180 $idsite = $site->get_current_matomo_site_id();
181
182 $matomo_currencies = $this->get_supported_currencies();
183
184 $tracking_code_generator = new TrackingCodeGenerator( $this->settings );
185 $matomo_default_tracking_code = $tracking_code_generator->prepare_tracking_code( $idsite );
186
187 include dirname( __FILE__ ) . '/views/tracking.php';
188 }
189
190 private function get_supported_currencies()
191 {
192 $all = include dirname( MATOMO_ANALYTICS_FILE ) . '/app/core/Intl/Data/Resources/currencies.php';
193 $currencies = array();
194 foreach ($all as $key => $single) {
195 $currencies[$key] = $single[0] . ' ' . $single[1];
196 }
197 return $currencies;
198 }
199
200 public function get_active_containers() {
201 // we don't use Matomo API here to avoid needing to bootstrap Matomo which is slow and could break things
202 $containers = array();
203 if ( matomo_has_tag_manager() ) {
204 global $wpdb;
205 $dbsettings = new \WpMatomo\Db\Settings();
206 $container_table = $dbsettings->prefix_table_name( 'tagmanager_container' );
207 try {
208 $containers = $wpdb->get_results( sprintf( 'SELECT `idcontainer`, `name` FROM %s where `status` = "active"', $container_table ) );
209 } catch ( \Exception $e ) {
210 // table may not exist yet etc
211 $containers = array();
212 }
213 }
214 $by_id = array();
215 foreach ( $containers as $container ) {
216 $by_id[ $container->idcontainer ] = $container->name;
217 }
218
219 return $by_id;
220 }
221
222
223 }
224