PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.1
2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / lib / action-scheduler / classes / ActionScheduler_OptionLock.php
sureforms / inc / lib / action-scheduler / classes Last commit date
WP_CLI 2 years ago abstracts 5 months ago actions 2 years ago data-stores 2 years ago migration 2 years ago schedules 2 years ago schema 2 years ago ActionScheduler_ActionClaim.php 2 years ago ActionScheduler_ActionFactory.php 2 years ago ActionScheduler_AdminView.php 2 years ago ActionScheduler_AsyncRequest_QueueRunner.php 2 years ago ActionScheduler_Compatibility.php 2 years ago ActionScheduler_DataController.php 2 years ago ActionScheduler_DateTime.php 2 years ago ActionScheduler_Exception.php 2 years ago ActionScheduler_FatalErrorMonitor.php 2 years ago ActionScheduler_InvalidActionException.php 2 years ago ActionScheduler_ListTable.php 2 years ago ActionScheduler_LogEntry.php 2 years ago ActionScheduler_NullLogEntry.php 2 years ago ActionScheduler_OptionLock.php 2 years ago ActionScheduler_QueueCleaner.php 2 years ago ActionScheduler_QueueRunner.php 2 years ago ActionScheduler_Versions.php 2 years ago ActionScheduler_WPCommentCleaner.php 2 years ago ActionScheduler_wcSystemStatus.php 2 years ago
ActionScheduler_OptionLock.php
137 lines
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 *
9 * @since 3.0.0
10 */
11 class ActionScheduler_OptionLock extends ActionScheduler_Lock {
12
13 /**
14 * Set a lock using options for a given amount of time (60 seconds by default).
15 *
16 * Using an autoloaded option avoids running database queries or other resource intensive tasks
17 * on frequently triggered hooks, like 'init' or 'shutdown'.
18 *
19 * For example, ActionScheduler_QueueRunner->maybe_dispatch_async_request() uses a lock to avoid
20 * calling ActionScheduler_QueueRunner->has_maximum_concurrent_batches() every time the 'shutdown',
21 * hook is triggered, because that method calls ActionScheduler_QueueRunner->store->get_claim_count()
22 * to find the current number of claims in the database.
23 *
24 * @param string $lock_type A string to identify different lock types.
25 * @bool True if lock value has changed, false if not or if set failed.
26 */
27 public function set( $lock_type ) {
28 global $wpdb;
29
30 $lock_key = $this->get_key( $lock_type );
31 $existing_lock_value = $this->get_existing_lock( $lock_type );
32 $new_lock_value = $this->new_lock_value( $lock_type );
33
34 // The lock may not exist yet, or may have been deleted.
35 if ( empty( $existing_lock_value ) ) {
36 return (bool) $wpdb->insert(
37 $wpdb->options,
38 array(
39 'option_name' => $lock_key,
40 'option_value' => $new_lock_value,
41 'autoload' => 'no',
42 )
43 );
44 }
45
46 if ( $this->get_expiration_from( $existing_lock_value ) >= time() ) {
47 return false;
48 }
49
50 // Otherwise, try to obtain the lock.
51 return (bool) $wpdb->update(
52 $wpdb->options,
53 array( 'option_value' => $new_lock_value ),
54 array(
55 'option_name' => $lock_key,
56 'option_value' => $existing_lock_value,
57 )
58 );
59 }
60
61 /**
62 * If a lock is set, return the timestamp it was set to expiry.
63 *
64 * @param string $lock_type A string to identify different lock types.
65 * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire.
66 */
67 public function get_expiration( $lock_type ) {
68 return $this->get_expiration_from( $this->get_existing_lock( $lock_type ) );
69 }
70
71 /**
72 * Given the lock string, derives the lock expiration timestamp (or false if it cannot be determined).
73 *
74 * @param string $lock_value String containing a timestamp, or pipe-separated combination of unique value and timestamp.
75 *
76 * @return false|int
77 */
78 private function get_expiration_from( $lock_value ) {
79 $lock_string = explode( '|', $lock_value );
80
81 // Old style lock?
82 if ( count( $lock_string ) === 1 && is_numeric( $lock_string[0] ) ) {
83 return (int) $lock_string[0];
84 }
85
86 // New style lock?
87 if ( count( $lock_string ) === 2 && is_numeric( $lock_string[1] ) ) {
88 return (int) $lock_string[1];
89 }
90
91 return false;
92 }
93
94 /**
95 * Get the key to use for storing the lock in the transient
96 *
97 * @param string $lock_type A string to identify different lock types.
98 * @return string
99 */
100 protected function get_key( $lock_type ) {
101 return sprintf( 'action_scheduler_lock_%s', $lock_type );
102 }
103
104 /**
105 * Supplies the existing lock value, or an empty string if not set.
106 *
107 * @param string $lock_type A string to identify different lock types.
108 *
109 * @return string
110 */
111 private function get_existing_lock( $lock_type ) {
112 global $wpdb;
113
114 // Now grab the existing lock value, if there is one.
115 return (string) $wpdb->get_var(
116 $wpdb->prepare(
117 "SELECT option_value FROM $wpdb->options WHERE option_name = %s",
118 $this->get_key( $lock_type )
119 )
120 );
121 }
122
123 /**
124 * Supplies a lock value consisting of a unique value and the current timestamp, which are separated by a pipe
125 * character.
126 *
127 * Example: (string) "649de012e6b262.09774912|1688068114"
128 *
129 * @param string $lock_type A string to identify different lock types.
130 *
131 * @return string
132 */
133 private function new_lock_value( $lock_type ) {
134 return uniqid( '', true ) . '|' . ( time() + $this->get_duration( $lock_type ) );
135 }
136 }
137