| 1 |
<?php |
| 2 |
|
| 3 |
use Imagify\Traits\InstanceGetterTrait; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class that scans the custom folders to keep files in sync in the database. |
| 7 |
* |
| 8 |
* @since 1.7 |
| 9 |
*/ |
| 10 |
class Imagify_Cron_Sync_Files extends Imagify_Abstract_Cron { |
| 11 |
use InstanceGetterTrait; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class version. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
* @since 1.7 |
| 18 |
*/ |
| 19 |
const VERSION = '1.0'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Cron name. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
* @since 1.7 |
| 26 |
*/ |
| 27 |
protected $event_name = 'imagify_sync_files'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Cron recurrence. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
* @since 1.7 |
| 34 |
*/ |
| 35 |
protected $event_recurrence = 'daily'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Cron time. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
* @since 1.7 |
| 42 |
*/ |
| 43 |
protected $event_time = '02:00'; |
| 44 |
|
| 45 |
|
| 46 |
/** ----------------------------------------------------------------------------------------- */ |
| 47 |
/** HOOKS =================================================================================== */ |
| 48 |
/** ----------------------------------------------------------------------------------------- */ |
| 49 |
|
| 50 |
/** |
| 51 |
* The event action. |
| 52 |
* |
| 53 |
* @since 1.7 |
| 54 |
*/ |
| 55 |
public function do_event() { |
| 56 |
$folders_db = Imagify_Folders_DB::get_instance(); |
| 57 |
$files_db = Imagify_Files_DB::get_instance(); |
| 58 |
|
| 59 |
if ( ! $folders_db->can_operate() || ! $files_db->can_operate() ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! Imagify_Requirements::is_api_key_valid() ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if ( Imagify_Requirements::is_over_quota() ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$this->set_no_time_limit(); |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the folders from DB. |
| 75 |
*/ |
| 76 |
$folders = Imagify_Custom_Folders::get_folders(); |
| 77 |
|
| 78 |
if ( ! $folders ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
Imagify_Custom_Folders::synchronize_files_from_folders( $folders ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Attempts to set no limit to the PHP timeout for time intensive processes. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
protected function set_no_time_limit() { |
| 91 |
if ( |
| 92 |
function_exists( 'set_time_limit' ) |
| 93 |
&& |
| 94 |
false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) |
| 95 |
&& ! ini_get( 'safe_mode' ) // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved |
| 96 |
) { |
| 97 |
@set_time_limit( 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|