jetformbuilder
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
abstracts
/
ActionScheduler_Lock.php
ActionScheduler.php
1 month ago
ActionScheduler_Abstract_ListTable.php
1 year ago
ActionScheduler_Abstract_QueueRunner.php
1 month ago
ActionScheduler_Abstract_RecurringSchedule.php
1 year ago
ActionScheduler_Abstract_Schedule.php
1 year ago
ActionScheduler_Abstract_Schema.php
1 year ago
ActionScheduler_Lock.php
1 year ago
ActionScheduler_Logger.php
1 year ago
ActionScheduler_Store.php
1 year ago
ActionScheduler_TimezoneHelper.php
1 year ago
ActionScheduler_WPCLI_Command.php
1 year ago
ActionScheduler_Lock.php
75 lines
| 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 | /** |
| 11 | * Instance. |
| 12 | * |
| 13 | * @var ActionScheduler_Lock |
| 14 | */ |
| 15 | private static $locker = null; |
| 16 | |
| 17 | /** |
| 18 | * Duration of lock. |
| 19 | * |
| 20 | * @var int |
| 21 | */ |
| 22 | protected static $lock_duration = MINUTE_IN_SECONDS; |
| 23 | |
| 24 | /** |
| 25 | * Check if a lock is set for a given lock type. |
| 26 | * |
| 27 | * @param string $lock_type A string to identify different lock types. |
| 28 | * @return bool |
| 29 | */ |
| 30 | public function is_locked( $lock_type ) { |
| 31 | return ( $this->get_expiration( $lock_type ) >= time() ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Set a lock. |
| 36 | * |
| 37 | * To prevent race conditions, implementations should avoid setting the lock if the lock is already held. |
| 38 | * |
| 39 | * @param string $lock_type A string to identify different lock types. |
| 40 | * @return bool |
| 41 | */ |
| 42 | abstract public function set( $lock_type ); |
| 43 | |
| 44 | /** |
| 45 | * If a lock is set, return the timestamp it was set to expiry. |
| 46 | * |
| 47 | * @param string $lock_type A string to identify different lock types. |
| 48 | * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
| 49 | */ |
| 50 | abstract public function get_expiration( $lock_type ); |
| 51 | |
| 52 | /** |
| 53 | * Get the amount of time to set for a given lock. 60 seconds by default. |
| 54 | * |
| 55 | * @param string $lock_type A string to identify different lock types. |
| 56 | * @return int |
| 57 | */ |
| 58 | protected function get_duration( $lock_type ) { |
| 59 | return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get instance. |
| 64 | * |
| 65 | * @return ActionScheduler_Lock |
| 66 | */ |
| 67 | public static function instance() { |
| 68 | if ( empty( self::$locker ) ) { |
| 69 | $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
| 70 | self::$locker = new $class(); |
| 71 | } |
| 72 | return self::$locker; |
| 73 | } |
| 74 | } |
| 75 |