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