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