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