PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.7.0
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.7.0
1.7.2 1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / vendor / woocommerce / action-scheduler / classes / ActionScheduler_OptionLock.php
hostinger-reach / vendor / woocommerce / action-scheduler / classes Last commit date
WP_CLI 10 months ago abstracts 10 months ago actions 10 months ago data-stores 10 months ago migration 10 months ago schedules 10 months ago schema 10 months ago ActionScheduler_ActionClaim.php 10 months ago ActionScheduler_ActionFactory.php 10 months ago ActionScheduler_AdminView.php 10 months ago ActionScheduler_AsyncRequest_QueueRunner.php 10 months ago ActionScheduler_Compatibility.php 10 months ago ActionScheduler_DataController.php 10 months ago ActionScheduler_DateTime.php 10 months ago ActionScheduler_Exception.php 10 months ago ActionScheduler_FatalErrorMonitor.php 10 months ago ActionScheduler_InvalidActionException.php 10 months ago ActionScheduler_ListTable.php 10 months ago ActionScheduler_LogEntry.php 10 months ago ActionScheduler_NullLogEntry.php 10 months ago ActionScheduler_OptionLock.php 10 months ago ActionScheduler_QueueCleaner.php 10 months ago ActionScheduler_QueueRunner.php 10 months ago ActionScheduler_RecurringActionScheduler.php 10 months ago ActionScheduler_SystemInformation.php 10 months ago ActionScheduler_Versions.php 10 months ago ActionScheduler_WPCommentCleaner.php 10 months ago ActionScheduler_wcSystemStatus.php 10 months 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