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