PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.1
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.php
matomo / classes Last commit date
WpMatomo 5 months ago .htaccess 6 years ago WpMatomo.php 5 months ago index.php 6 years ago
WpMatomo.php
310 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 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // if accessed directly
12 }
13
14 use WpMatomo\Admin\AdBlockDetector;
15 use WpMatomo\Admin\Admin;
16 use WpMatomo\Admin\Chart;
17 use WpMatomo\Admin\Dashboard;
18 use WpMatomo\Admin\MarketplaceSetupWizard;
19 use WpMatomo\Admin\Menu;
20 use WpMatomo\AjaxTracker;
21 use WpMatomo\Annotations;
22 use WpMatomo\API;
23 use WpMatomo\Capabilities;
24 use WpMatomo\Commands\MatomoCommands;
25 use WpMatomo\Ecommerce\EasyDigitalDownloads;
26 use WpMatomo\Ecommerce\MemberPress;
27 use WpMatomo\Ecommerce\Woocommerce;
28 use WpMatomo\Installer;
29 use WpMatomo\OptOut;
30 use WpMatomo\Paths;
31 use WpMatomo\PluginAdminOverrides;
32 use WpMatomo\RedirectOnActivation;
33 use WpMatomo\Report\Renderer;
34 use WpMatomo\Roles;
35 use WpMatomo\ScheduledTasks;
36 use WpMatomo\Settings;
37 use WpMatomo\Site\Sync as SiteSync;
38 use WpMatomo\TrackingCode;
39 use WpMatomo\Updater;
40 use WpMatomo\User\Sync as UserSync;
41
42 class WpMatomo {
43
44 const VERSION = '5.8.1';
45
46 /**
47 * @var Settings
48 */
49 public static $settings;
50
51 public function __construct() {
52 $this->declare_woocommerce_hpos_compatible();
53
54 if ( ! $this->check_compatibility() ) {
55 return;
56 }
57
58 self::$settings = new Settings();
59
60 if ( self::is_safe_mode() ) {
61 if ( is_admin() ) {
62 new Admin( self::$settings, false );
63 new \WpMatomo\Admin\SafeModeMenu( self::$settings );
64 }
65
66 return;
67 }
68
69 add_action( 'init', [ $this, 'init_plugin' ] );
70
71 $adblock_detector = new AdBlockDetector();
72 $adblock_detector->register_hooks();
73
74 $capabilities = new Capabilities( self::$settings );
75 $capabilities->register_hooks();
76
77 $roles = new Roles( self::$settings );
78 $roles->register_hooks();
79
80 $compatibility = new \WpMatomo\Compatibility();
81 $compatibility->register_hooks();
82
83 $site_config = new SiteSync\SyncConfig( self::$settings );
84 $scheduled_tasks = new ScheduledTasks( self::$settings, $site_config );
85 $scheduled_tasks->schedule();
86 $scheduled_tasks->register_ajax();
87
88 $privacy_badge = new OptOut();
89 $privacy_badge->register_hooks();
90
91 $renderer = new Renderer();
92 $renderer->register_hooks();
93
94 $api = new API();
95 $api->register_hooks();
96
97 if ( is_admin() ) {
98 new Admin( self::$settings );
99 $scheduled_tasks->show_errors_if_admin();
100
101 $dashboard = new Dashboard();
102 $dashboard->register_hooks();
103
104 $site_sync = new SiteSync( self::$settings );
105 $site_sync->register_hooks();
106 $user_sync = new UserSync();
107 $user_sync->register_hooks();
108
109 $referral = new \WpMatomo\Referral();
110 if ( $referral->should_show() ) {
111 $referral->register_hooks();
112 }
113
114 $error_notice = new \WpMatomo\ErrorNotice( self::$settings );
115 $error_notice->register_hooks();
116
117 $chart = new Chart();
118 $chart->register_hooks();
119
120 /*
121 * @see https://github.com/matomo-org/matomo-for-wordpress/issues/434
122 */
123 $redirect = new RedirectOnActivation( $this );
124 $redirect->register_hooks();
125
126 $plugin_admin_overrides = new PluginAdminOverrides( self::$settings );
127 $plugin_admin_overrides->register_hooks();
128 }
129
130 add_action(
131 'init',
132 function () {
133 $whats_new_notifications = new \WpMatomo\Admin\WhatsNewNotifications( self::$settings );
134 if ( $whats_new_notifications->is_active() ) {
135 $whats_new_notifications->register_hooks();
136 }
137 $whats_new_notifications->register_ajax();
138 }
139 );
140
141 $tracking_code = new TrackingCode( self::$settings );
142 $tracking_code->register_hooks();
143
144 $annotations = new Annotations( self::$settings );
145 $annotations->register_hooks();
146
147 $ai_bot_tracking = new \WpMatomo\AIBotTracking( self::$settings );
148 $ai_bot_tracking->register_hooks();
149
150 if ( defined( 'WP_CLI' ) && WP_CLI ) {
151 new MatomoCommands();
152 }
153
154 add_filter(
155 'plugin_action_links_' . plugin_basename( MATOMO_ANALYTICS_FILE ),
156 [
157 $this,
158 'add_settings_link',
159 ]
160 );
161
162 // TODO: need better way of doing ajax?
163 MarketplaceSetupWizard::register_ajax();
164 WpMatomo\Admin\TrackingSettings::register_ajax();
165
166 \WpMatomo\Admin\GetStarted::register_hooks();
167 }
168
169 private function check_compatibility() {
170 if ( ! is_admin() ) {
171 return true;
172 }
173 if ( matomo_is_app_request() ) {
174 return true;
175 }
176
177 $paths = new Paths();
178 $upload_path = $paths->get_upload_base_dir();
179
180 if ( $upload_path
181 && ! is_writable( dirname( $upload_path ) ) ) {
182 add_action(
183 'init',
184 function () use ( $upload_path ) {
185 if ( self::is_admin_user() ) {
186 add_action(
187 'admin_notices',
188 function () use ( $upload_path ) {
189 echo '<div class="error"><p>' . sprintf( esc_html__( 'Matomo Analytics requires the uploads directory %s to be writable. Please make the directory writable for it to work.', 'matomo' ), '(' . esc_html( dirname( $upload_path ) ) . ')' ) . '</p></div>';
190 }
191 );
192 }
193 }
194 );
195
196 return false;
197 }
198
199 return true;
200 }
201
202 public static function is_admin_user() {
203 if ( ! function_exists( 'is_multisite' )
204 || ! is_multisite() ) {
205 return current_user_can( 'administrator' );
206 }
207
208 return is_super_admin();
209 }
210
211 public static function is_safe_mode() {
212 if ( defined( 'MATOMO_SAFE_MODE' ) ) {
213 return MATOMO_SAFE_MODE;
214 }
215
216 return false;
217 }
218
219 private static function get_active_plugins() {
220 $plugins = [];
221 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
222 $muplugins = get_site_option( 'active_sitewide_plugins' );
223 $plugins = array_keys( $muplugins );
224 }
225 $plugins = array_merge( (array) get_option( 'active_plugins', [] ), $plugins );
226
227 return $plugins;
228 }
229
230 public static function should_disable_addhandler() {
231 return defined( 'MATOMO_DISABLE_ADDHANDLER' ) && MATOMO_DISABLE_ADDHANDLER;
232 }
233
234 public function add_settings_link( $links ) {
235 $get_started = new \WpMatomo\Admin\GetStarted( self::$settings );
236
237 if ( self::$settings->get_global_option( Settings::SHOW_GET_STARTED_PAGE ) && $get_started->can_user_manage() ) {
238 $links[] = '<a href="' . menu_page_url( Menu::SLUG_GET_STARTED, false ) . '">' . __( 'Get Started', 'matomo' ) . '</a>';
239 } elseif ( current_user_can( Capabilities::KEY_SUPERUSER ) ) {
240 $links[] = '<a href="' . menu_page_url( Menu::SLUG_SETTINGS, false ) . '">' . __( 'Settings', 'matomo' ) . '</a>';
241 }
242
243 return $links;
244 }
245
246 public function init_plugin() {
247 if ( ( is_admin() || matomo_is_app_request() ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
248 $installer = new Installer( self::$settings );
249 $installer->register_hooks();
250 if ( $installer->looks_like_it_is_installed() ) {
251 if ( is_admin() && ( ! defined( 'MATOMO_ENABLE_AUTO_UPGRADE' ) || MATOMO_ENABLE_AUTO_UPGRADE ) ) {
252 $updater = new Updater( self::$settings );
253 $updater->update_if_needed();
254 }
255 } else {
256 if ( matomo_is_app_request() ) {
257 // we can't install if matomo is requested... there's some circular reference
258 wp_safe_redirect( admin_url() );
259 exit;
260 } else {
261 if ( $installer->can_be_installed() ) {
262 $installer->install();
263 }
264 }
265 }
266 }
267 $tracking_code = new TrackingCode( self::$settings );
268 if ( self::$settings->is_tracking_enabled()
269 && self::$settings->get_global_option( 'track_ecommerce' )
270 && ! $tracking_code->is_hidden_user() ) {
271 $tracker = new AjaxTracker( self::$settings );
272
273 $sync_config = new SiteSync\SyncConfig( self::$settings );
274
275 if ( function_exists( 'WC' ) ) {
276 $woocommerce = new Woocommerce( $tracker, self::$settings, $sync_config );
277 $woocommerce->register_hooks();
278 }
279
280 $easy_digital_downloads = new EasyDigitalDownloads( $tracker, self::$settings, $sync_config );
281 $easy_digital_downloads->register_hooks();
282
283 $member_press = new MemberPress( $tracker, self::$settings, $sync_config );
284 $member_press->register_hooks();
285
286 do_action( 'matomo_ecommerce_init', $tracker );
287 }
288 }
289
290 private function declare_woocommerce_hpos_compatible() {
291 add_action(
292 'before_woocommerce_init',
293 function() {
294 if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
295 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', 'matomo/matomo.php', true );
296 }
297 }
298 );
299 }
300
301 public static function is_async_archiving_manually_disabled() {
302 return ( defined( 'MATOMO_SUPPORT_ASYNC_ARCHIVING' ) && ! MATOMO_SUPPORT_ASYNC_ARCHIVING )
303 || self::is_async_archiving_disabled_by_setting();
304 }
305
306 private static function is_async_archiving_disabled_by_setting() {
307 return self::$settings->is_async_archiving_disabled_by_option();
308 }
309 }
310