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