take( $this->name( $post_id, $paged ) ); } /** * Takes one of a fixed number of render slots, so only a few pages are rendered at the same time. * * @param int $max Number of slots. * @return int|null The slot number, or null when every slot is in use. */ public function take_slot( int $max ): ?int { for ( $slot = 1; $slot <= $max; $slot++ ) { if ( $this->take( 'agentwelcome_slot_' . $slot ) ) { return $slot; } } return null; } /** * Gives a render slot back. * * @param int $slot Slot number returned by take_slot(). * @return void */ public function release_slot( int $slot ): void { global $wpdb; $wpdb->delete( $wpdb->options, array( 'option_name' => 'agentwelcome_slot_' . $slot ) ); } /** * Takes a lock by name. A lock older than TTL_SECONDS is treated as abandoned and replaced. * * @param string $name Option name of the lock. * @return bool True when the lock was taken. */ private function take( string $name ): bool { global $wpdb; if ( $this->insert( $name ) ) { return true; } $held = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) ); if ( null === $held || time() - (int) $held <= self::TTL_SECONDS ) { return false; } $removed = $wpdb->delete( $wpdb->options, array( 'option_name' => $name, 'option_value' => $held, ) ); if ( 1 !== $removed ) { return false; } return $this->insert( $name ); } /** * Releases the generation lock of a page. * * @param int $post_id Page ID. * @param int $paged Number of the paginated view, 1 for the page itself. * @return void */ public function release( int $post_id, int $paged = 1 ): void { global $wpdb; $wpdb->delete( $wpdb->options, array( 'option_name' => $this->name( $post_id, $paged ) ) ); } /** * Inserts the lock row if it does not exist yet. * * @param string $name Option name of the lock. * @return bool True when this call created the row. */ private function insert( string $name ): bool { global $wpdb; return 1 === (int) $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' ) /* LOCK */", $name, (string) time() ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Core table name. } /** * Option name of the generation lock of a page. * * @param int $post_id Page ID. * @param int $paged Number of the paginated view, 1 for the page itself. * @return string Option name. */ private function name( int $post_id, int $paged = 1 ): string { return 'agentwelcome_lock_' . $post_id . ( $paged > 1 ? '_' . $paged : '' ); } }