| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpCloud\StatelessMedia\Sync; |
| 4 |
|
| 5 |
use wpCloud\StatelessMedia\UnprocessableException; |
| 6 |
|
| 7 |
abstract class LibrarySync extends BackgroundSync { |
| 8 |
|
| 9 |
/** |
| 10 |
* Condition SQL |
| 11 |
* Should be defined in child classes |
| 12 |
*/ |
| 13 |
abstract public function get_sql_condition(); |
| 14 |
|
| 15 |
/** |
| 16 |
* Get transient key for total items value |
| 17 |
*/ |
| 18 |
private function get_total_items_trans_key() { |
| 19 |
return "{$this->action}_total_items"; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Start the process |
| 24 |
* |
| 25 |
* @param array $args [] |
| 26 |
* @return bool |
| 27 |
*/ |
| 28 |
public final function start($args = []) { |
| 29 |
try { |
| 30 |
if ($this->is_processing()) throw new UnprocessableException(__('Process already running', ud_get_stateless_media()->domain)); |
| 31 |
|
| 32 |
// Make sure there is no orphaned data and state |
| 33 |
delete_site_option($this->get_stopped_option_key()); |
| 34 |
delete_transient($this->get_total_items_trans_key()); |
| 35 |
$this->clear_process_meta(); |
| 36 |
|
| 37 |
$settings = wp_parse_args($args, [ |
| 38 |
'limit' => null, |
| 39 |
'order' => null |
| 40 |
]); |
| 41 |
|
| 42 |
$limit = $settings['limit'] ? intval($settings['limit']) : 0; |
| 43 |
$order = in_array($settings['order'], ['desc', 'asc']) ? $settings['order'] : 'desc'; |
| 44 |
|
| 45 |
global $wpdb; |
| 46 |
$sql = "SELECT ID FROM $wpdb->posts |
| 47 |
WHERE post_type = 'attachment' |
| 48 |
{$this->get_sql_condition()} |
| 49 |
AND post_date < %s |
| 50 |
ORDER BY ID $order |
| 51 |
LIMIT %d"; |
| 52 |
$query = $wpdb->prepare($sql, $datetime = current_time('mysql'), $this->get_max_batch_size()); |
| 53 |
$ids = $wpdb->get_col($query); |
| 54 |
|
| 55 |
$total = 0; |
| 56 |
foreach ($ids as $id) { |
| 57 |
if (!$limit || $total < $limit) { |
| 58 |
$this->push_to_queue($id); |
| 59 |
$total++; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$this->save_process_meta([ |
| 64 |
'limit' => $limit, |
| 65 |
'datetime' => $datetime, |
| 66 |
'order' => $order, |
| 67 |
'last_id' => $id |
| 68 |
]); |
| 69 |
|
| 70 |
$this->save()->dispatch(); |
| 71 |
$this->log('Started'); |
| 72 |
return true; |
| 73 |
} catch (UnprocessableException $e) { |
| 74 |
$this->log(sprintf(__('Could not start the new process: %s'), $e->getMessage())); |
| 75 |
return true; |
| 76 |
} catch (\Throwable $e) { |
| 77 |
$this->log(sprintf(__('Could not start the process due to the error: %s'), $e->getMessage())); |
| 78 |
$this->stop(); |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Provide the queue with the new data if available |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
*/ |
| 88 |
public function extend_queue() { |
| 89 |
try { |
| 90 |
global $wpdb; |
| 91 |
|
| 92 |
$meta = $this->get_process_meta(); |
| 93 |
$last_id = isset($meta['last_id']) ? $meta['last_id'] : false; |
| 94 |
|
| 95 |
if (!$last_id) return; |
| 96 |
|
| 97 |
$limit = isset($meta['limit']) ? $meta['limit'] : 0; |
| 98 |
$order = isset($meta['order']) ? $meta['order'] : 'desc'; |
| 99 |
$datetime = isset($meta['datetime']) ? $meta['datetime'] : current_time('mysql'); |
| 100 |
|
| 101 |
$range_condition = $order === 'desc' ? $wpdb->prepare("AND ID < %d", $last_id) : $wpdb->prepare("AND ID > %d", $last_id); |
| 102 |
|
| 103 |
$sql = "SELECT ID FROM $wpdb->posts |
| 104 |
WHERE post_type = 'attachment' |
| 105 |
{$this->get_sql_condition()} |
| 106 |
AND post_date < %s |
| 107 |
$range_condition |
| 108 |
ORDER BY ID $order |
| 109 |
LIMIT %d"; |
| 110 |
$query = $wpdb->prepare($sql, $datetime, $this->get_max_batch_size()); |
| 111 |
$ids = $wpdb->get_col($query); |
| 112 |
|
| 113 |
$total = $this->get_queue_size(); |
| 114 |
foreach ($ids as $id) { |
| 115 |
if (!$limit || $total < $limit) { |
| 116 |
$this->push_to_queue($id); |
| 117 |
$total++; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if (!empty($this->data)) { |
| 122 |
$this->save()->save_process_meta([ |
| 123 |
'last_id' => $id |
| 124 |
]); |
| 125 |
} else { |
| 126 |
$this->save_process_meta([ |
| 127 |
'last_id' => 0 |
| 128 |
]); |
| 129 |
} |
| 130 |
|
| 131 |
return true; |
| 132 |
} catch (\Throwable $e) { |
| 133 |
$this->log(sprintf('Something went wrong while extending the queue: %s. Stopping the whole process.', $e->getMessage())); |
| 134 |
$this->stop(); |
| 135 |
return false; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Thing to do in complete |
| 141 |
*/ |
| 142 |
protected function complete() { |
| 143 |
parent::complete(); |
| 144 |
|
| 145 |
// @todo do something when complete |
| 146 |
$this->log("Complete"); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get Total Items (caching utilized) |
| 151 |
* |
| 152 |
* @return int |
| 153 |
*/ |
| 154 |
public function get_total_items() { |
| 155 |
$cached = get_transient($this->get_total_items_trans_key()); |
| 156 |
if ($cached) return intval($cached); |
| 157 |
|
| 158 |
global $wpdb; |
| 159 |
$sql = "SELECT count(*) |
| 160 |
FROM $wpdb->posts |
| 161 |
WHERE post_type = 'attachment' |
| 162 |
{$this->get_sql_condition()}"; |
| 163 |
$total = $wpdb->get_var($sql); |
| 164 |
|
| 165 |
set_transient($this->get_total_items_trans_key(), $total, MINUTE_IN_SECONDS * 5); |
| 166 |
return intval($total); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Notice if process seemed to be stuck |
| 171 |
* |
| 172 |
* @return string|false |
| 173 |
*/ |
| 174 |
public function get_process_notice() { |
| 175 |
$notices = parent::get_process_notice(); |
| 176 |
$last = intval($this->get_process_meta('last_at')); |
| 177 |
if (!$last) { |
| 178 |
$last = strtotime( $this->get_process_meta('datetime') ?? '' ); |
| 179 |
if (false === $last) return $notices; |
| 180 |
} |
| 181 |
|
| 182 |
if (!property_exists($this, 'cron_interval')) return $notices; |
| 183 |
|
| 184 |
$waiting = current_time('timestamp') - $last; |
| 185 |
if ($waiting < 5 * MINUTE_IN_SECONDS * $this->cron_interval) return $notices; |
| 186 |
|
| 187 |
$notices[] = sprintf(__('This process takes longer than it should. Please, make sure loopback connections and WP Cron are enabled and working, or try restarting the process.', ud_get_stateless_media()->domain)); |
| 188 |
return $notices; |
| 189 |
} |
| 190 |
} |
| 191 |
|