WpMatomo.php
259 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 | * @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', [ $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 | add_action( 'admin_notices', [ $this, 'check_errors' ] ); |
| 101 | |
| 102 | $chart = new Chart(); |
| 103 | $chart->register_hooks(); |
| 104 | |
| 105 | /* |
| 106 | * @see https://github.com/matomo-org/matomo-for-wordpress/issues/434 |
| 107 | */ |
| 108 | $redirect = new RedirectOnActivation( $this ); |
| 109 | $redirect->register_hooks(); |
| 110 | } |
| 111 | |
| 112 | $tracking_code = new TrackingCode( self::$settings ); |
| 113 | $tracking_code->register_hooks(); |
| 114 | $annotations = new Annotations( self::$settings ); |
| 115 | $annotations->register_hooks(); |
| 116 | |
| 117 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 118 | new MatomoCommands(); |
| 119 | } |
| 120 | |
| 121 | add_filter( |
| 122 | 'plugin_action_links_' . plugin_basename( MATOMO_ANALYTICS_FILE ), |
| 123 | [ |
| 124 | $this, |
| 125 | 'add_settings_link', |
| 126 | ] |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | public function check_errors() { |
| 131 | if ( isset( $_GET['page'] ) && substr( sanitize_text_field( wp_unslash( $_GET['page'] ) ), 0, 7 ) === 'matomo-' ) { |
| 132 | $system_report = new \WpMatomo\Admin\SystemReport( self::$settings ); |
| 133 | if ( $system_report->errors_present() ) { |
| 134 | echo '<div class="notice notice-warning is-dismissible"> |
| 135 | <p>' . esc_html__( 'There are some errors in the', 'matomo' ) . |
| 136 | ' <a href="' . esc_url( admin_url( 'admin.php?page=matomo-systemreport' ) ) . '">' . esc_html__( 'Matomo Diagnostics System report', 'matomo' ) . '</a> ' . |
| 137 | esc_html__( 'that may prevent the plugin for working normally.', 'matomo' ) . '</p></div>'; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private function check_compatibility() { |
| 143 | if ( ! is_admin() ) { |
| 144 | return true; |
| 145 | } |
| 146 | if ( matomo_is_app_request() ) { |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | $paths = new Paths(); |
| 151 | $upload_path = $paths->get_upload_base_dir(); |
| 152 | |
| 153 | if ( $upload_path |
| 154 | && ! is_writable( dirname( $upload_path ) ) ) { |
| 155 | add_action( |
| 156 | 'init', |
| 157 | function () use ( $upload_path ) { |
| 158 | if ( self::is_admin_user() ) { |
| 159 | add_action( |
| 160 | 'admin_notices', |
| 161 | function () use ( $upload_path ) { |
| 162 | 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>'; |
| 163 | } |
| 164 | ); |
| 165 | } |
| 166 | } |
| 167 | ); |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | public static function is_admin_user() { |
| 176 | if ( ! function_exists( 'is_multisite' ) |
| 177 | || ! is_multisite() ) { |
| 178 | return current_user_can( 'administrator' ); |
| 179 | } |
| 180 | |
| 181 | return is_super_admin(); |
| 182 | } |
| 183 | |
| 184 | public static function is_safe_mode() { |
| 185 | if ( defined( 'MATOMO_SAFE_MODE' ) ) { |
| 186 | return MATOMO_SAFE_MODE; |
| 187 | } |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | private static function get_active_plugins() { |
| 193 | $plugins = []; |
| 194 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 195 | $muplugins = get_site_option( 'active_sitewide_plugins' ); |
| 196 | $plugins = array_keys( $muplugins ); |
| 197 | } |
| 198 | $plugins = array_merge( (array) get_option( 'active_plugins', [] ), $plugins ); |
| 199 | |
| 200 | return $plugins; |
| 201 | } |
| 202 | |
| 203 | public static function should_disable_addhandler() { |
| 204 | return defined( 'MATOMO_DISABLE_ADDHANDLER' ) && MATOMO_DISABLE_ADDHANDLER; |
| 205 | } |
| 206 | |
| 207 | public function add_settings_link( $links ) { |
| 208 | $get_started = new \WpMatomo\Admin\GetStarted( self::$settings ); |
| 209 | |
| 210 | if ( self::$settings->get_global_option( Settings::SHOW_GET_STARTED_PAGE ) && $get_started->can_user_manage() ) { |
| 211 | $links[] = '<a href="' . menu_page_url( Menu::SLUG_GET_STARTED, false ) . '">' . __( 'Get Started', 'matomo' ) . '</a>'; |
| 212 | } elseif ( current_user_can( Capabilities::KEY_SUPERUSER ) ) { |
| 213 | $links[] = '<a href="' . menu_page_url( Menu::SLUG_SETTINGS, false ) . '">' . __( 'Settings', 'matomo' ) . '</a>'; |
| 214 | } |
| 215 | |
| 216 | return $links; |
| 217 | } |
| 218 | |
| 219 | public function init_plugin() { |
| 220 | if ( ( is_admin() || matomo_is_app_request() ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { |
| 221 | $installer = new Installer( self::$settings ); |
| 222 | $installer->register_hooks(); |
| 223 | if ( $installer->looks_like_it_is_installed() ) { |
| 224 | if ( is_admin() && ( ! defined( 'MATOMO_ENABLE_AUTO_UPGRADE' ) || MATOMO_ENABLE_AUTO_UPGRADE ) ) { |
| 225 | $updater = new Updater( self::$settings ); |
| 226 | $updater->update_if_needed(); |
| 227 | } |
| 228 | } else { |
| 229 | if ( matomo_is_app_request() ) { |
| 230 | // we can't install if matomo is requested... there's some circular reference |
| 231 | wp_safe_redirect( admin_url() ); |
| 232 | exit; |
| 233 | } else { |
| 234 | if ( $installer->can_be_installed() ) { |
| 235 | $installer->install(); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | $tracking_code = new TrackingCode( self::$settings ); |
| 241 | if ( self::$settings->is_tracking_enabled() |
| 242 | && self::$settings->get_global_option( 'track_ecommerce' ) |
| 243 | && ! $tracking_code->is_hidden_user() ) { |
| 244 | $tracker = new AjaxTracker( self::$settings ); |
| 245 | |
| 246 | $woocommerce = new Woocommerce( $tracker ); |
| 247 | $woocommerce->register_hooks(); |
| 248 | |
| 249 | $easy_digital_downloads = new EasyDigitalDownloads( $tracker ); |
| 250 | $easy_digital_downloads->register_hooks(); |
| 251 | |
| 252 | $member_press = new MemberPress( $tracker ); |
| 253 | $member_press->register_hooks(); |
| 254 | |
| 255 | do_action( 'matomo_ecommerce_init', $tracker ); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 |