Admin
5 years ago
Commands
6 years ago
Db
6 years ago
Ecommerce
6 years ago
Report
6 years ago
Site
5 years ago
TrackingCode
5 years ago
User
5 years ago
views
6 years ago
API.php
5 years ago
Access.php
6 years ago
AjaxTracker.php
5 years ago
Annotations.php
6 years ago
Bootstrap.php
6 years ago
Capabilities.php
6 years ago
Compatibility.php
6 years ago
Email.php
5 years ago
Installer.php
6 years ago
Logger.php
5 years ago
OptOut.php
5 years ago
Paths.php
6 years ago
PrivacyBadge.php
6 years ago
Referral.php
6 years ago
Roles.php
6 years ago
ScheduledTasks.php
6 years ago
Settings.php
5 years ago
Site.php
6 years ago
TrackingCode.php
5 years ago
Uninstaller.php
6 years ago
Updater.php
6 years ago
User.php
6 years ago
ScheduledTasks.php
266 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 | namespace WpMatomo; |
| 11 | |
| 12 | use Piwik\CronArchive; |
| 13 | use Piwik\Option; |
| 14 | use Piwik\Plugins\GeoIp2\GeoIP2AutoUpdater; |
| 15 | use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2; |
| 16 | use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2\Php; |
| 17 | use Piwik\Plugins\UserCountry\LocationProvider; |
| 18 | use WpMatomo\Site\Sync as SiteSync; |
| 19 | use WpMatomo\User\Sync as UserSync; |
| 20 | |
| 21 | if ( ! defined( 'ABSPATH' ) ) { |
| 22 | exit; // if accessed directly |
| 23 | } |
| 24 | |
| 25 | class ScheduledTasks { |
| 26 | const EVENT_SYNC = 'matomo_scheduled_sync'; |
| 27 | const EVENT_ARCHIVE = 'matomo_scheduled_archive'; |
| 28 | const EVENT_GEOIP = 'matomo_scheduled_geoipdb'; |
| 29 | const EVENT_UPDATE = 'matomo_update_core'; |
| 30 | |
| 31 | const KEY_BEFORE_CRON = 'before-cron-'; |
| 32 | const KEY_AFTER_CRON = 'after-cron-'; |
| 33 | |
| 34 | /** |
| 35 | * @var Settings |
| 36 | */ |
| 37 | private $settings; |
| 38 | |
| 39 | /** |
| 40 | * @var Logger |
| 41 | */ |
| 42 | private $logger; |
| 43 | |
| 44 | public function __construct( Settings $settings ) { |
| 45 | $this->settings = $settings; |
| 46 | $this->logger = new Logger(); |
| 47 | } |
| 48 | |
| 49 | public function add_weekly_schedule( $schedules ) { |
| 50 | $schedules['matomo_monthly'] = array( |
| 51 | 'interval' => 60 * 60 * 24 * 30, |
| 52 | 'display' => __( 'Monthly', 'matomo' ), |
| 53 | ); |
| 54 | |
| 55 | return $schedules; |
| 56 | } |
| 57 | |
| 58 | public function schedule() { |
| 59 | add_action( self::EVENT_UPDATE, array( $this, 'perform_update' ) ); |
| 60 | add_filter( 'cron_schedules', array( $this, 'add_weekly_schedule' ) ); |
| 61 | |
| 62 | $self = $this; |
| 63 | $event_priority = 10; |
| 64 | |
| 65 | foreach ( $this->get_all_events() as $event_name => $event_config ) { |
| 66 | if ( ! wp_next_scheduled( $event_name ) ) { |
| 67 | wp_schedule_event( time(), $event_config['interval'], $event_name ); |
| 68 | } |
| 69 | |
| 70 | // logging last execution start time |
| 71 | add_action( |
| 72 | $event_name, |
| 73 | function () use ( $self, $event_name ) { |
| 74 | $self->set_last_time_before_cron( $event_name, time() ); |
| 75 | }, |
| 76 | $event_priority / 2, |
| 77 | $accepted_args = 0 |
| 78 | ); |
| 79 | |
| 80 | // actual event |
| 81 | add_action( $event_name, array( $this, $event_config['method'] ), $event_priority, $accepted_args = 0 ); |
| 82 | |
| 83 | // logging last execution end time |
| 84 | add_action( |
| 85 | $event_name, |
| 86 | function () use ( $self, $event_name ) { |
| 87 | $self->set_last_time_after_cron( $event_name, time() ); |
| 88 | }, |
| 89 | $event_priority * 2, |
| 90 | $accepted_args = 0 |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | register_deactivation_hook( MATOMO_ANALYTICS_FILE, array( $this, 'uninstall' ) ); |
| 95 | } |
| 96 | |
| 97 | public function get_last_time_before_cron( $event_name ) { |
| 98 | return get_option( Settings::OPTION_PREFIX . self::KEY_BEFORE_CRON . $event_name ); |
| 99 | } |
| 100 | |
| 101 | public function set_last_time_before_cron( $event_name, $time ) { |
| 102 | // we use settings prefix so data automatically gets removed when uninstalling |
| 103 | update_option( Settings::OPTION_PREFIX . self::KEY_BEFORE_CRON . $event_name, $time ); |
| 104 | } |
| 105 | |
| 106 | public function get_last_time_after_cron( $event_name ) { |
| 107 | return get_option( Settings::OPTION_PREFIX . self::KEY_AFTER_CRON . $event_name ); |
| 108 | } |
| 109 | |
| 110 | public function set_last_time_after_cron( $event_name, $time ) { |
| 111 | // we use settings prefix so data automatically gets removed when uninstalling |
| 112 | update_option( Settings::OPTION_PREFIX . self::KEY_AFTER_CRON . $event_name, $time ); |
| 113 | } |
| 114 | |
| 115 | public function get_all_events() { |
| 116 | return array( |
| 117 | self::EVENT_SYNC => array( |
| 118 | 'name' => 'Sync users & sites', |
| 119 | 'interval' => 'daily', |
| 120 | 'method' => 'sync', |
| 121 | ), |
| 122 | self::EVENT_ARCHIVE => array( |
| 123 | 'name' => 'Archive', |
| 124 | 'interval' => 'hourly', |
| 125 | 'method' => 'archive', |
| 126 | ), |
| 127 | self::EVENT_GEOIP => array( |
| 128 | 'name' => 'Update GeoIP DB', |
| 129 | 'interval' => 'matomo_monthly', |
| 130 | 'method' => 'update_geo_ip2_db', |
| 131 | ), |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | public function perform_update() { |
| 136 | $this->logger->log( 'Scheduled tasks perform update' ); |
| 137 | |
| 138 | try { |
| 139 | $updater = new Updater( $this->settings ); |
| 140 | $updater->update(); |
| 141 | } catch ( \Exception $e ) { |
| 142 | $this->logger->log_exception( 'cron_update', $e ); |
| 143 | throw $e; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | public function update_geo_ip2_db() { |
| 148 | $this->logger->log( 'Scheduled tasks update geoip database' ); |
| 149 | try { |
| 150 | Bootstrap::do_bootstrap(); |
| 151 | |
| 152 | $maxmind_license = $this->settings->get_global_option('maxmind_license_key'); |
| 153 | if (empty($maxmind_license)) { |
| 154 | $db_url = GeoIp2::getDbIpLiteUrl(); |
| 155 | } else { |
| 156 | $db_url = 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz&license_key=' . $maxmind_license; |
| 157 | } |
| 158 | |
| 159 | Option::set( GeoIP2AutoUpdater::LOC_URL_OPTION_NAME, $db_url); |
| 160 | $updater = new GeoIP2AutoUpdater(); |
| 161 | $updater->update(); |
| 162 | if ( LocationProvider::getCurrentProviderId() !== Php::ID && LocationProvider::getProviderById( Php::ID ) ) { |
| 163 | LocationProvider::setCurrentProvider( Php::ID ); |
| 164 | } |
| 165 | } catch ( \Exception $e ) { |
| 166 | $this->logger->log_exception( 'update_geoip2', $e ); |
| 167 | throw $e; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | public function sync() { |
| 172 | $this->logger->log( 'Scheduled tasks sync all sites and users' ); |
| 173 | |
| 174 | try { |
| 175 | $site = new SiteSync( $this->settings ); |
| 176 | $site->sync_all(); |
| 177 | $user = new UserSync(); |
| 178 | $user->sync_all(); |
| 179 | } catch ( \Exception $e ) { |
| 180 | $this->logger->log_exception( 'cron_sync', $e ); |
| 181 | throw $e; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public function archive( $force = false, $throw_exception = true ) { |
| 186 | if ( defined( 'MATOMO_DISABLE_WP_ARCHIVING' ) && MATOMO_DISABLE_WP_ARCHIVING ) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | $this->logger->log( 'Scheduled tasks archive data' ); |
| 191 | |
| 192 | try { |
| 193 | Bootstrap::do_bootstrap(); |
| 194 | } catch ( \Exception $e ) { |
| 195 | $this->logger->log_exception( 'archive_bootstrap', $e ); |
| 196 | throw $e; |
| 197 | } |
| 198 | |
| 199 | $archiver = new CronArchive(); |
| 200 | $archiver->concurrentRequestsPerWebsite = 1; |
| 201 | $archiver->maxConcurrentArchivers = 1; |
| 202 | |
| 203 | if ( $force ) { |
| 204 | $archiver->shouldArchiveAllSites = true; |
| 205 | $archiver->disableScheduledTasks = true; |
| 206 | $archiver->shouldArchiveAllPeriodsSince = true; |
| 207 | } |
| 208 | |
| 209 | if ( is_multisite() ) { |
| 210 | if ( is_network_admin() ) { |
| 211 | return; // nothing to archive |
| 212 | } else { |
| 213 | $blog_id = get_current_blog_id(); |
| 214 | $idsite = Site::get_matomo_site_id( $blog_id ); |
| 215 | if ( ! empty( $idsite ) ) { |
| 216 | $archiver->shouldArchiveSpecifiedSites = array( $idsite ); |
| 217 | } else { |
| 218 | // there is no site mapped to it so there's no point in archiving it |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | try { |
| 225 | $archiver->main(); |
| 226 | |
| 227 | $archive_errors = $archiver->getErrors(); |
| 228 | |
| 229 | } catch ( \Exception $e ) { |
| 230 | $this->logger->log_exception( 'archive_main' , $e); |
| 231 | $archive_errors = $archiver->getErrors(); |
| 232 | |
| 233 | if (!empty($archive_errors)) { |
| 234 | $message = ''; |
| 235 | foreach ($archiver->getErrors() as $error) { |
| 236 | $message .= var_export($error, 1) . ' '; |
| 237 | } |
| 238 | $message = new \Exception(trim($message)); |
| 239 | $this->logger->log_exception('archive_errors', $message); |
| 240 | } |
| 241 | |
| 242 | if ($throw_exception) { |
| 243 | throw $e; |
| 244 | } else { |
| 245 | $archive_errors[] = $e->getMessage(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return $archive_errors; |
| 250 | } |
| 251 | |
| 252 | public function uninstall() { |
| 253 | $this->logger->log( 'Scheduled tasks uninstall all events' ); |
| 254 | |
| 255 | foreach ( $this->get_all_events() as $event_name => $config ) { |
| 256 | $timestamp = wp_next_scheduled( $event_name ); |
| 257 | if ( $timestamp ) { |
| 258 | wp_unschedule_event( $timestamp, $event_name ); |
| 259 | } |
| 260 | } |
| 261 | foreach ( $this->get_all_events() as $event_name => $config ) { |
| 262 | wp_clear_scheduled_hook( $event_name ); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 |