PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.02
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.02
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
fluent-booking / vendor / woocommerce / action-scheduler / classes / ActionScheduler_OptionLock.php

ActionScheduler_OptionLock.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.02, at vendor/woocommerce/action-scheduler/classes/ActionScheduler_OptionLock.php

136 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Provide a way to set simple transient locks to block behaviour
5 * for up-to a given duration.
6 *
7 * Class ActionScheduler_OptionLock
8 * @since 3.0.0
9 */
10 class ActionScheduler_OptionLock extends ActionScheduler_Lock {
11
12 /**
13 * Set a lock using options for a given amount of time (60 seconds by default).
14 *
15 * Using an autoloaded option avoids running database queries or other resource intensive tasks
16 * on frequently triggered hooks, like 'init' or 'shutdown'.
17 *
18 * For example, ActionScheduler_QueueRunner->maybe_dispatch_async_request() uses a lock to avoid
19 * calling ActionScheduler_QueueRunner->has_maximum_concurrent_batches() every time the 'shutdown',
20 * hook is triggered, because that method calls ActionScheduler_QueueRunner->store->get_claim_count()
21 * to find the current number of claims in the database.
22 *
23 * @param string $lock_type A string to identify different lock types.
24 * @bool True if lock value has changed, false if not or if set failed.
25 */
26 public function set( $lock_type ) {
27 global $wpdb;
28
29 $lock_key = $this->get_key( $lock_type );
30 $existing_lock_value = $this->get_existing_lock( $lock_type );
31 $new_lock_value = $this->new_lock_value( $lock_type );
32
33 // The lock may not exist yet, or may have been deleted.
34 if ( empty( $existing_lock_value ) ) {
35 return (bool) $wpdb->insert(
36 $wpdb->options,
37 array(
38 'option_name' => $lock_key,
39 'option_value' => $new_lock_value,
40 'autoload' => 'no',
41 )
42 );
43 }
44
45 if ( $this->get_expiration_from( $existing_lock_value ) >= time() ) {
46 return false;
47 }
48
49 // Otherwise, try to obtain the lock.
50 return (bool) $wpdb->update(
51 $wpdb->options,
52 array( 'option_value' => $new_lock_value ),
53 array(
54 'option_name' => $lock_key,
55 'option_value' => $existing_lock_value,
56 )
57 );
58 }
59
60 /**
61 * If a lock is set, return the timestamp it was set to expiry.
62 *
63 * @param string $lock_type A string to identify different lock types.
64 * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire.
65 */
66 public function get_expiration( $lock_type ) {
67 return $this->get_expiration_from( $this->get_existing_lock( $lock_type ) );
68 }
69
70 /**
71 * Given the lock string, derives the lock expiration timestamp (or false if it cannot be determined).
72 *
73 * @param string $lock_value String containing a timestamp, or pipe-separated combination of unique value and timestamp.
74 *
75 * @return false|int
76 */
77 private function get_expiration_from( $lock_value ) {
78 $lock_string = explode( '|', $lock_value );
79
80 // Old style lock?
81 if ( count( $lock_string ) === 1 && is_numeric( $lock_string[0] ) ) {
82 return (int) $lock_string[0];
83 }
84
85 // New style lock?
86 if ( count( $lock_string ) === 2 && is_numeric( $lock_string[1] ) ) {
87 return (int) $lock_string[1];
88 }
89
90 return false;
91 }
92
93 /**
94 * Get the key to use for storing the lock in the transient
95 *
96 * @param string $lock_type A string to identify different lock types.
97 * @return string
98 */
99 protected function get_key( $lock_type ) {
100 return sprintf( 'action_scheduler_lock_%s', $lock_type );
101 }
102
103 /**
104 * Supplies the existing lock value, or an empty string if not set.
105 *
106 * @param string $lock_type A string to identify different lock types.
107 *
108 * @return string
109 */
110 private function get_existing_lock( $lock_type ) {
111 global $wpdb;
112
113 // Now grab the existing lock value, if there is one.
114 return (string) $wpdb->get_var(
115 $wpdb->prepare(
116 "SELECT option_value FROM $wpdb->options WHERE option_name = %s",
117 $this->get_key( $lock_type )
118 )
119 );
120 }
121
122 /**
123 * Supplies a lock value consisting of a unique value and the current timestamp, which are separated by a pipe
124 * character.
125 *
126 * Example: (string) "649de012e6b262.09774912|1688068114"
127 *
128 * @param string $lock_type A string to identify different lock types.
129 *
130 * @return string
131 */
132 private function new_lock_value( $lock_type ) {
133 return uniqid( '', true ) . '|' . ( time() + $this->get_duration( $lock_type ) );
134 }
135 }
136