| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpCloud\StatelessMedia\Sync; |
| 4 |
|
| 5 |
use wpCloud\StatelessMedia\FatalException; |
| 6 |
use wpCloud\StatelessMedia\Singleton; |
| 7 |
use wpCloud\StatelessMedia\UnprocessableException; |
| 8 |
|
| 9 |
class NonLibrarySync extends BackgroundSync { |
| 10 |
|
| 11 |
// Make it singleton |
| 12 |
use Singleton; |
| 13 |
|
| 14 |
/** |
| 15 |
* Unique action |
| 16 |
*/ |
| 17 |
protected $action = 'wps_bg_non_library_sync'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Items holder |
| 21 |
*/ |
| 22 |
private $items = []; |
| 23 |
|
| 24 |
/** |
| 25 |
* Extended construct |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
//$this->items = array_filter(array_unique(apply_filters('sm:sync::nonMediaFiles', []))); |
| 29 |
parent::__construct(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get sync name |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public function get_name() { |
| 38 |
return __('Compatibility Files <span class="label">Beta</span>', ud_get_stateless_media()->domain); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Sync helper window |
| 43 |
* |
| 44 |
* @return HelperWindow |
| 45 |
*/ |
| 46 |
public function get_helper_window() { |
| 47 |
return new HelperWindow( |
| 48 |
__('What are Compatibility Files?', ud_get_stateless_media()->domain), |
| 49 |
__('All kind of files that were created by themes and plugins in custom folders out of standard Media Library, and that WP-Stateless has a Compatibility Support for. Limit and Sorting is not supported.', ud_get_stateless_media()->domain) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Start the process |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public function start() { |
| 59 |
try { |
| 60 |
if ($this->is_processing()) throw new UnprocessableException(__('Process already running', ud_get_stateless_media()->domain)); |
| 61 |
|
| 62 |
// Make sure there is no orphaned data and state |
| 63 |
delete_site_option($this->get_stopped_option_key()); |
| 64 |
$this->clear_process_meta(); |
| 65 |
$this->items = array_filter(array_unique(apply_filters('sm:sync::nonMediaFiles', []))); |
| 66 |
|
| 67 |
$chunks = array_chunk($this->items, $this->get_max_batch_size()); |
| 68 |
if (!empty($chunks)) { |
| 69 |
foreach ($chunks as $chunk) { |
| 70 |
foreach ($chunk as $item) { |
| 71 |
$this->push_to_queue($item); |
| 72 |
} |
| 73 |
|
| 74 |
$this->save(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$this->dispatch(); |
| 79 |
|
| 80 |
$this->save_process_meta([ |
| 81 |
'starttime' => current_time('timestamp') |
| 82 |
]); |
| 83 |
$this->log('Started'); |
| 84 |
return true; |
| 85 |
} catch (UnprocessableException $e) { |
| 86 |
$this->log(sprintf(__('Could not start the new process: %s'), $e->getMessage())); |
| 87 |
return true; |
| 88 |
} catch (\Throwable $e) { |
| 89 |
$this->log(sprintf(__('Could not start the process due to the error: %s'), $e->getMessage())); |
| 90 |
$this->stop(); |
| 91 |
return false; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Process one item from the queue |
| 97 |
*/ |
| 98 |
protected function task($item) { |
| 99 |
try { |
| 100 |
parent::before_task($item); |
| 101 |
|
| 102 |
timer_start(); |
| 103 |
|
| 104 |
if (ud_get_stateless_media()->is_connected_to_gs() !== true) { |
| 105 |
throw new FatalException(__('Not connected to GCS', ud_get_stateless_media()->domain)); |
| 106 |
} |
| 107 |
|
| 108 |
if (is_multisite() && ($blog_id = get_current_blog_id()) != 1) { |
| 109 |
switch_to_blog(1); |
| 110 |
$upload_dir = wp_upload_dir(); |
| 111 |
switch_to_blog($blog_id); |
| 112 |
} else { |
| 113 |
$upload_dir = wp_upload_dir(); |
| 114 |
} |
| 115 |
|
| 116 |
$file_path = trim($item, '/'); |
| 117 |
$basedir = ud_get_stateless_media()->is_mode('stateless') ? ud_get_stateless_media()->get_gs_path() : $upload_dir['basedir']; |
| 118 |
$fullsizepath = $basedir . '/' . $file_path; |
| 119 |
|
| 120 |
do_action('sm:sync::syncFile', $file_path, $fullsizepath, true, ['remove_from_queue' => true, 'manual_sync' => true]); |
| 121 |
|
| 122 |
$this->log(sprintf(__('%1$s (ID %2$s) was successfully synchronised in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($file_path)), $file_path, timer_stop())); |
| 123 |
|
| 124 |
parent::task($item); |
| 125 |
return false; |
| 126 |
} catch (FatalException $e) { |
| 127 |
$this->log("Stopped due to error - {$e->getMessage()}"); |
| 128 |
$this->stop(); |
| 129 |
return false; |
| 130 |
} catch (UnprocessableException $e) { |
| 131 |
$this->log($e->getMessage()); |
| 132 |
return false; |
| 133 |
} catch (\Throwable $e) { |
| 134 |
$this->log("Stopped due to error - {$e->getMessage()}"); |
| 135 |
$this->stop(); |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Thing to do in complete |
| 142 |
*/ |
| 143 |
protected function complete() { |
| 144 |
parent::complete(); |
| 145 |
|
| 146 |
// @todo do something when complete |
| 147 |
$this->log("Complete"); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Notice if process seemed to be stuck |
| 152 |
* |
| 153 |
* @return string|false |
| 154 |
*/ |
| 155 |
public function get_process_notice() { |
| 156 |
$notices = parent::get_process_notice(); |
| 157 |
$last = intval($this->get_process_meta('last_at')); |
| 158 |
if (!$last) { |
| 159 |
$last = intval($this->get_process_meta('starttime')); |
| 160 |
if (!$last) return $notices; |
| 161 |
} |
| 162 |
|
| 163 |
if (!property_exists($this, 'cron_interval')) return $notices; |
| 164 |
|
| 165 |
$waiting = current_time('timestamp') - $last; |
| 166 |
if ($waiting < 5 * MINUTE_IN_SECONDS * $this->cron_interval) return $notices; |
| 167 |
|
| 168 |
$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)); |
| 169 |
return $notices; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get count of items |
| 174 |
* |
| 175 |
* @return int |
| 176 |
*/ |
| 177 |
public function get_total_items() { |
| 178 |
return ud_stateless_db()->get_total_non_media_files(); |
| 179 |
} |
| 180 |
|
| 181 |
public function extend_queue() { |
| 182 |
// Not needed for this kind of sync |
| 183 |
} |
| 184 |
} |
| 185 |
|