| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Abstract class for setting a basic lock to throttle some action. |
| 5 |
* |
| 6 |
* Class ActionScheduler_Lock |
| 7 |
*/ |
| 8 |
abstract class ActionScheduler_Lock { |
| 9 |
|
| 10 |
/** @var ActionScheduler_Lock */ |
| 11 |
private static $locker = NULL; |
| 12 |
|
| 13 |
/** @var int */ |
| 14 |
protected static $lock_duration = MINUTE_IN_SECONDS; |
| 15 |
|
| 16 |
/** |
| 17 |
* Check if a lock is set for a given lock type. |
| 18 |
* |
| 19 |
* @param string $lock_type A string to identify different lock types. |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public function is_locked( $lock_type ) { |
| 23 |
return ( $this->get_expiration( $lock_type ) >= time() ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Set a lock. |
| 28 |
* |
| 29 |
* @param string $lock_type A string to identify different lock types. |
| 30 |
* @return bool |
| 31 |
*/ |
| 32 |
abstract public function set( $lock_type ); |
| 33 |
|
| 34 |
/** |
| 35 |
* If a lock is set, return the timestamp it was set to expiry. |
| 36 |
* |
| 37 |
* @param string $lock_type A string to identify different lock types. |
| 38 |
* @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
| 39 |
*/ |
| 40 |
abstract public function get_expiration( $lock_type ); |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the amount of time to set for a given lock. 60 seconds by default. |
| 44 |
* |
| 45 |
* @param string $lock_type A string to identify different lock types. |
| 46 |
* @return int |
| 47 |
*/ |
| 48 |
protected function get_duration( $lock_type ) { |
| 49 |
return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @return ActionScheduler_Lock |
| 54 |
*/ |
| 55 |
public static function instance() { |
| 56 |
if ( empty( self::$locker ) ) { |
| 57 |
$class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
| 58 |
self::$locker = new $class(); |
| 59 |
} |
| 60 |
return self::$locker; |
| 61 |
} |
| 62 |
} |
| 63 |
|