AJAX
8 months ago
Admin_Page
10 months ago
Click_Tracking
9 months ago
ColumnOptions
10 months ago
Cron
10 months ago
Custom_WordPress_Columns
9 months ago
Data_Pruning
9 months ago
Date_Picker
1 year ago
Date_Range
1 year ago
Ecommerce
9 months ago
Email_Reports
10 months ago
Examiner
1 year ago
Form_Submissions
8 months ago
Integrations
9 months ago
Interval
1 year ago
Menu_Bar_Stats
1 year ago
Migrations
9 months ago
Models
9 months ago
Overview
9 months ago
Public_API
10 months ago
Rows
9 months ago
Statistics
8 months ago
Tables
10 months ago
Utils
9 months ago
Views
10 months ago
WooCommerceOrderMetaBox
10 months ago
Appearance.php
1 year ago
Campaign_Builder.php
1 year ago
Capability_Manager.php
1 year ago
Chart.php
10 months ago
Chart_Data.php
1 year ago
Click_Tracking.php
1 year ago
Cron_Job.php
10 months ago
Cron_Manager.php
10 months ago
Current_Traffic_Finder.php
1 year ago
Dashboard_Options.php
10 months ago
Dashboard_Widget.php
2 years ago
Database.php
10 months ago
Database_Manager.php
1 year ago
Empty_Report_Option.php
2 years ago
Env.php
1 year ago
Examiner_Config.php
1 year ago
Filters.php
10 months ago
Geo_Database_Health_Check_Job.php
10 months ago
Geo_Database_Manager.php
10 months ago
Geoposition.php
1 year ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
1 year ago
Independent_Analytics.php
9 months ago
Interrupt.php
1 year ago
Known_Referrers.php
10 months ago
MainWP.php
1 year ago
Map.php
10 months ago
Map_Data.php
1 year ago
Migration_Fixer_Job.php
9 months ago
Patch.php
1 year ago
Payload_Validator.php
10 months ago
Plugin_Conflict_Detector.php
10 months ago
Plugin_Group.php
9 months ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
9 months ago
Quick_Stats.php
1 year ago
REST_API.php
10 months ago
Real_Time.php
10 months ago
Report.php
1 year ago
Report_Finder.php
1 year ago
Report_Options_Parser.php
10 months ago
Resource_Identifier.php
10 months ago
Settings.php
1 year ago
Sort_Configuration.php
10 months ago
Tables.php
9 months ago
Track_Resource_Changes.php
1 year ago
View_Counter.php
1 year ago
Views_Over_Time_Finder.php
1 year ago
WP_Option_Cache_Bust.php
2 years ago
Cron_Job.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | /** @internal */ |
| 7 | abstract class Cron_Job |
| 8 | { |
| 9 | protected $name = ''; |
| 10 | protected $interval = 'daily'; |
| 11 | public abstract function handle() : void; |
| 12 | public function register_handler() : void |
| 13 | { |
| 14 | \add_action($this->name, function () { |
| 15 | if ($this->should_execute_handler()) { |
| 16 | $this->handle(); |
| 17 | } |
| 18 | }); |
| 19 | } |
| 20 | public function unschedule() |
| 21 | { |
| 22 | \wp_unschedule_hook($this->name); |
| 23 | } |
| 24 | public function schedule() |
| 25 | { |
| 26 | $scheduled_at_timestamp = \wp_next_scheduled($this->name); |
| 27 | if ($scheduled_at_timestamp === \false) { |
| 28 | \wp_schedule_event($this->timestamp_for_next_interval($this->interval), $this->interval, $this->name); |
| 29 | } |
| 30 | } |
| 31 | public function timestamp_for_next_interval(string $interval_id) : ?int |
| 32 | { |
| 33 | // Run hourly intervals on the hour |
| 34 | if ($this->interval === 'hourly') { |
| 35 | $now = CarbonImmutable::now()->startOfSecond(); |
| 36 | $next_hour = $now->addHour()->startOfHour(); |
| 37 | $seconds_until_next_hour = $next_hour->diffInSeconds($now); |
| 38 | return \time() + $seconds_until_next_hour; |
| 39 | } |
| 40 | return \time() + \wp_get_schedules()[$interval_id]['interval']; |
| 41 | } |
| 42 | public function should_execute_handler() : bool |
| 43 | { |
| 44 | return \true; |
| 45 | } |
| 46 | public static function register_custom_intervals() : void |
| 47 | { |
| 48 | \add_filter('cron_schedules', function ($schedules) { |
| 49 | $schedules['monthly'] = ['interval' => \MONTH_IN_SECONDS, 'display' => 'Once a Month']; |
| 50 | $schedules['five_minutes'] = ['interval' => 300, 'display' => 'Every 5 minutes']; |
| 51 | $schedules['every_minute'] = ['interval' => 60, 'display' => 'Every minute']; |
| 52 | return $schedules; |
| 53 | }); |
| 54 | } |
| 55 | } |
| 56 |