| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cron class. |
| 4 |
* |
| 5 |
* @package WPZinc\Social |
| 6 |
* @author WP Zinc |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPZinc\Social; |
| 10 |
|
| 11 |
/** |
| 12 |
* Functions to schedule, reschedule and unschedule events with WordPress' Cron for |
| 13 |
* - Log Cleanup (old log entries that are no longer needed) |
| 14 |
* - Media Cleanup (images auto generated by the Plugin that are no longer needed) |
| 15 |
* |
| 16 |
* @package WPZinc\Social |
| 17 |
* @author WP Zinc |
| 18 |
* @version 3.7.2 |
| 19 |
*/ |
| 20 |
class Cron { |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the base class object. |
| 24 |
* |
| 25 |
* @since 3.7.2 |
| 26 |
* |
| 27 |
* @var object |
| 28 |
*/ |
| 29 |
public $base; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor |
| 33 |
* |
| 34 |
* @since 3.7.2 |
| 35 |
* |
| 36 |
* @param object $base Base Plugin Class. |
| 37 |
*/ |
| 38 |
public function __construct( $base ) { |
| 39 |
|
| 40 |
// Store base class. |
| 41 |
$this->base = $base; |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Schedules the log cleanup event in the WordPress CRON on a daily basis |
| 47 |
* |
| 48 |
* @since 3.9.8 |
| 49 |
*/ |
| 50 |
public function schedule_log_cleanup_event() { |
| 51 |
|
| 52 |
// Bail if the preserve logs settings is indefinite. |
| 53 |
if ( ! $this->base->get_class( 'settings' )->get_setting( 'log', '[preserve_days]' ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Bail if the scheduled event already exists. |
| 58 |
$scheduled_event = $this->get_log_cleanup_event(); |
| 59 |
if ( $scheduled_event !== false ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Schedule event. |
| 64 |
$scheduled_date_time = gmdate( 'Y-m-d', strtotime( '+1 day' ) ) . ' 00:00:00'; |
| 65 |
wp_schedule_event( strtotime( $scheduled_date_time ), 'daily', $this->base->plugin->filter_name . '_log_cleanup_cron' ); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Unschedules the log cleanup event in the WordPress CRON. |
| 71 |
* |
| 72 |
* @since 3.9.8 |
| 73 |
*/ |
| 74 |
public function unschedule_log_cleanup_event() { |
| 75 |
|
| 76 |
wp_clear_scheduled_hook( $this->base->plugin->filter_name . '_log_cleanup_cron' ); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Reschedules the log cleanup event in the WordPress CRON, by unscheduling |
| 82 |
* and scheduling it. |
| 83 |
* |
| 84 |
* @since 3.9.8 |
| 85 |
*/ |
| 86 |
public function reschedule_log_cleanup_event() { |
| 87 |
|
| 88 |
$this->unschedule_log_cleanup_event(); |
| 89 |
$this->schedule_log_cleanup_event(); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the scheduled log cleanup event, if it exists |
| 95 |
* |
| 96 |
* @since 3.9.8 |
| 97 |
*/ |
| 98 |
public function get_log_cleanup_event() { |
| 99 |
|
| 100 |
return wp_get_schedule( $this->base->plugin->filter_name . '_log_cleanup_cron' ); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns the scheduled log cleanup event's next date and time to run, if it exists |
| 106 |
* |
| 107 |
* @since 3.9.8 |
| 108 |
* |
| 109 |
* @param mixed $format Format Timestamp (false | php date() compat. string). |
| 110 |
*/ |
| 111 |
public function get_log_cleanup_event_next_scheduled( $format = false ) { |
| 112 |
|
| 113 |
// Get timestamp for when the event will next run. |
| 114 |
$scheduled = wp_next_scheduled( $this->base->plugin->filter_name . '_log_cleanup_cron' ); |
| 115 |
|
| 116 |
// If no timestamp or we're not formatting the result, return it now. |
| 117 |
if ( ! $scheduled || ! $format ) { |
| 118 |
return $scheduled; |
| 119 |
} |
| 120 |
|
| 121 |
// Return formatted date/time. |
| 122 |
return gmdate( $format, $scheduled ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Runs the log cleanup CRON event |
| 127 |
* |
| 128 |
* @since 3.9.8 |
| 129 |
*/ |
| 130 |
public function log_cleanup() { |
| 131 |
|
| 132 |
// Bail if the preserve logs settings is indefinite. |
| 133 |
// We shouldn't ever call this function if this is the case, but it's a useful sanity check. |
| 134 |
$preserve_days = $this->base->get_class( 'settings' )->get_setting( 'log', '[preserve_days]' ); |
| 135 |
if ( ! $preserve_days ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
// Define the date cutoff. |
| 140 |
$date_time = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $preserve_days . ' days' ) ); |
| 141 |
// Delete log entries older than the date. |
| 142 |
$this->base->get_class( 'log' )->delete_by_request_sent_cutoff( $date_time ); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Schedules the media cleanup event in the WordPress CRON on a daily basis |
| 148 |
* |
| 149 |
* @since 4.2.0 |
| 150 |
*/ |
| 151 |
public function schedule_media_cleanup_event() { |
| 152 |
|
| 153 |
// Bail if the scheduled event already exists. |
| 154 |
$scheduled_event = $this->get_media_cleanup_event(); |
| 155 |
if ( $scheduled_event !== false ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
// Schedule event. |
| 160 |
$scheduled_date_time = gmdate( 'Y-m-d', strtotime( '+1 day' ) ) . ' 01:00:00'; |
| 161 |
wp_schedule_event( strtotime( $scheduled_date_time ), 'daily', $this->base->plugin->filter_name . '_media_cleanup_cron' ); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Unschedules the media cleanup event in the WordPress CRON. |
| 167 |
* |
| 168 |
* @since 4.2.0 |
| 169 |
*/ |
| 170 |
public function unschedule_media_cleanup_event() { |
| 171 |
|
| 172 |
wp_clear_scheduled_hook( $this->base->plugin->filter_name . '_media_cleanup_cron' ); |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Reschedules the media cleanup event in the WordPress CRON, by unscheduling |
| 178 |
* and scheduling it. |
| 179 |
* |
| 180 |
* @since 4.2.0 |
| 181 |
*/ |
| 182 |
public function reschedule_media_cleanup_event() { |
| 183 |
|
| 184 |
$this->unschedule_media_cleanup_event(); |
| 185 |
$this->schedule_media_cleanup_event(); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Returns the scheduled media cleanup event, if it exists |
| 191 |
* |
| 192 |
* @since 4.2.0 |
| 193 |
*/ |
| 194 |
public function get_media_cleanup_event() { |
| 195 |
|
| 196 |
return wp_get_schedule( $this->base->plugin->filter_name . '_media_cleanup_cron' ); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Returns the scheduled media cleanup event's next date and time to run, if it exists |
| 202 |
* |
| 203 |
* @since 4.2.0 |
| 204 |
* |
| 205 |
* @param mixed $format Format Timestamp (false | php date() compat. string). |
| 206 |
*/ |
| 207 |
public function get_media_cleanup_event_next_scheduled( $format = false ) { |
| 208 |
|
| 209 |
// Get timestamp for when the event will next run. |
| 210 |
$scheduled = wp_next_scheduled( $this->base->plugin->filter_name . '_media_cleanup_cron' ); |
| 211 |
|
| 212 |
// If no timestamp or we're not formatting the result, return it now. |
| 213 |
if ( ! $scheduled || ! $format ) { |
| 214 |
return $scheduled; |
| 215 |
} |
| 216 |
|
| 217 |
// Return formatted date/time. |
| 218 |
return gmdate( $format, $scheduled ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Unschedules the refresh token event in the WordPress CRON. |
| 223 |
* |
| 224 |
* @since 6.2.0 |
| 225 |
*/ |
| 226 |
public function unschedule_refresh_token_event() { |
| 227 |
|
| 228 |
wp_clear_scheduled_hook( $this->base->plugin->filter_name . '_refresh_token_cron' ); |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
|