WP_CLI
1 year ago
abstracts
1 year ago
actions
1 year ago
data-stores
1 year ago
migration
1 year ago
schedules
1 year ago
schema
1 year ago
ActionScheduler_ActionClaim.php
1 year ago
ActionScheduler_ActionFactory.php
1 year ago
ActionScheduler_AdminView.php
1 year ago
ActionScheduler_AsyncRequest_QueueRunner.php
1 year ago
ActionScheduler_Compatibility.php
1 year ago
ActionScheduler_DataController.php
1 year ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
4 years ago
ActionScheduler_FatalErrorMonitor.php
1 year ago
ActionScheduler_InvalidActionException.php
1 year ago
ActionScheduler_ListTable.php
1 year ago
ActionScheduler_LogEntry.php
1 year ago
ActionScheduler_NullLogEntry.php
1 year ago
ActionScheduler_OptionLock.php
2 years ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
4 years ago
index.php
3 years ago
ActionScheduler_OptionLock.php
65 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class ActionScheduler_OptionLock extends ActionScheduler_Lock { |
| 4 | public function set( $lock_type ) { |
| 5 | global $wpdb; |
| 6 | $lock_key = $this->get_key( $lock_type ); |
| 7 | $existing_lock_value = $this->get_existing_lock( $lock_type ); |
| 8 | $new_lock_value = $this->new_lock_value( $lock_type ); |
| 9 | // The lock may not exist yet, or may have been deleted. |
| 10 | if ( empty( $existing_lock_value ) ) { |
| 11 | return (bool) $wpdb->insert( |
| 12 | $wpdb->options, |
| 13 | array( |
| 14 | 'option_name' => $lock_key, |
| 15 | 'option_value' => $new_lock_value, |
| 16 | 'autoload' => 'no', |
| 17 | ) |
| 18 | ); |
| 19 | } |
| 20 | if ( $this->get_expiration_from( $existing_lock_value ) >= time() ) { |
| 21 | return false; |
| 22 | } |
| 23 | // Otherwise, try to obtain the lock. |
| 24 | return (bool) $wpdb->update( |
| 25 | $wpdb->options, |
| 26 | array( 'option_value' => $new_lock_value ), |
| 27 | array( |
| 28 | 'option_name' => $lock_key, |
| 29 | 'option_value' => $existing_lock_value, |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | public function get_expiration( $lock_type ) { |
| 34 | return $this->get_expiration_from( $this->get_existing_lock( $lock_type ) ); |
| 35 | } |
| 36 | private function get_expiration_from( $lock_value ) { |
| 37 | $lock_string = explode( '|', $lock_value ); |
| 38 | // Old style lock? |
| 39 | if ( count( $lock_string ) === 1 && is_numeric( $lock_string[0] ) ) { |
| 40 | return (int) $lock_string[0]; |
| 41 | } |
| 42 | // New style lock? |
| 43 | if ( count( $lock_string ) === 2 && is_numeric( $lock_string[1] ) ) { |
| 44 | return (int) $lock_string[1]; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | protected function get_key( $lock_type ) { |
| 49 | return sprintf( 'action_scheduler_lock_%s', $lock_type ); |
| 50 | } |
| 51 | private function get_existing_lock( $lock_type ) { |
| 52 | global $wpdb; |
| 53 | // Now grab the existing lock value, if there is one. |
| 54 | return (string) $wpdb->get_var( |
| 55 | $wpdb->prepare( |
| 56 | "SELECT option_value FROM $wpdb->options WHERE option_name = %s", |
| 57 | $this->get_key( $lock_type ) |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | private function new_lock_value( $lock_type ) { |
| 62 | return uniqid( '', true ) . '|' . ( time() + $this->get_duration( $lock_type ) ); |
| 63 | } |
| 64 | } |
| 65 |