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