| 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 |
use wpCloud\StatelessMedia\Utility; |
| 9 |
|
| 10 |
/** |
| 11 |
* Background process for synchronization of media library images |
| 12 |
*/ |
| 13 |
class ImageSync extends LibrarySync { |
| 14 |
|
| 15 |
/** |
| 16 |
* Make is singleton |
| 17 |
*/ |
| 18 |
use Singleton; |
| 19 |
|
| 20 |
/** |
| 21 |
* Unique action |
| 22 |
*/ |
| 23 |
protected $action = 'wps_bg_image_sync'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Allow sorting for this kind of sync |
| 27 |
*/ |
| 28 |
protected $allow_sorting = true; |
| 29 |
|
| 30 |
/** |
| 31 |
* Allow setting the limit for this kind of sync |
| 32 |
*/ |
| 33 |
protected $allow_limit = true; |
| 34 |
|
| 35 |
/** |
| 36 |
* Name |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function get_name() { |
| 41 |
return __('Media Library Images', ud_get_stateless_media()->domain); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get SQL condition to compose the query to get items to process by library sync |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function get_sql_condition() { |
| 50 |
return "AND post_mime_type LIKE 'image/%'"; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Helper window |
| 55 |
* |
| 56 |
* @return HelperWindow |
| 57 |
*/ |
| 58 |
public function get_helper_window() { |
| 59 |
return new HelperWindow( |
| 60 |
__('What are Media Library Images?', ud_get_stateless_media()->domain), |
| 61 |
__('All image files that were uploaded via the media library or via plugins that use standard uploading API.', ud_get_stateless_media()->domain) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Process 1 item from the queue |
| 67 |
* |
| 68 |
* @param mixed $id |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
protected function task($id) { |
| 72 |
try { |
| 73 |
if ($this->is_stopped()) return false; |
| 74 |
parent::before_task($id); |
| 75 |
|
| 76 |
timer_start(); |
| 77 |
@set_time_limit(-1); |
| 78 |
|
| 79 |
$image = Utility::process_image_by_id($id); |
| 80 |
$this->log(sprintf(__('%1$s (ID %2$s) was successfully synced in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($image->ID)), $image->ID, timer_stop())); |
| 81 |
|
| 82 |
if (!$this->is_stopped()) { |
| 83 |
$this->extend_queue(); |
| 84 |
} |
| 85 |
|
| 86 |
parent::task($id); |
| 87 |
return false; |
| 88 |
} catch (FatalException $e) { |
| 89 |
$this->log("Stopped due to error - {$e->getMessage()}"); |
| 90 |
$this->stop(); |
| 91 |
return false; |
| 92 |
} catch (UnprocessableException $e) { |
| 93 |
$this->log($e->getMessage()); |
| 94 |
return false; |
| 95 |
} catch (\Throwable $e) { |
| 96 |
$this->log("Stopped due to error - {$e->getMessage()}"); |
| 97 |
$this->stop(); |
| 98 |
return false; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|