lockKey = BASE . $key . '_lock'; $this->timeout = $timeout; global $wpdb; $this->wpdb = $wpdb; } /** * Execute callback with lock, auto-release after */ public function withLock(callable $callback): void { $acquired = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT GET_LOCK(%s, %d)', $this->lockKey, $this->timeout ) ); if ((int) $acquired !== 1) { // Lock already held — just exit quietly return; } try { $callback(); } finally { $this->unlock(); } } public function unlock():void { $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $this->lockKey ) ); } }