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