name = 'wlt_' . substr( hash( 'sha256', DB_NAME . ':' . $wpdb->prefix ), 0, 56 ); } public function run( callable $callback, int $wait = 0 ): mixed { global $wpdb; // Reject reentry: GET_LOCK itself is reentrant on MySQL/MariaDB. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Session locks must execute on the database; a cache cannot provide exclusion. if ( isset( self::$active[ $this->name ] ) || $this->held || '1' !== (string) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, %d)', $this->name, $wait ) ) ) { return new WP_Error( 'busy', __( 'Another connection operation is running. Please retry shortly.', 'waitlistize' ), array( 'status' => 409 ) ); } $this->held = true; self::$active[ $this->name ] = true; try { return $callback(); } finally { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Release the database session lock. $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $this->name ) ); $this->held = false; unset( self::$active[ $this->name ] ); } } /** Fail closed if wpdb transparently reconnected while an HTTP call ran. */ public function owned(): bool { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Lock ownership must be observed from the active database session. return $this->held && '1' === (string) $wpdb->get_var( $wpdb->prepare( 'SELECT IS_USED_LOCK(%s) = CONNECTION_ID()', $this->name ) ); } }