locked || ! isset( $wpdb->options, $wpdb->dbname ) || ! preg_match( '/^[A-Za-z0-9_]+$/D', $wpdb->options ) ) { throw new RuntimeException( 'connect_storage_unavailable' ); } $engine = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- storage engine probe on the options table; must be live $wpdb->prepare( 'SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s', $wpdb->dbname, $wpdb->options ) ); if ( $engine !== 'InnoDB' ) { throw new RuntimeException( 'connect_storage_unavailable' ); } $lock = self::lock_name( $identity ); if ( 1 !== (int) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 3)', $lock ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- named advisory lock; no table, nothing to cache throw new RuntimeException( 'connect_lock_unavailable' ); } $this->locked = true; try { return $callback( $this ); } finally { $this->locked = false; $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $lock ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- named advisory lock; no table, nothing to cache } } /** * Read the stored Connect state. * * @return string|null * @throws RuntimeException When the database lock is not held. */ public function read() { global $wpdb; $this->assert_locked(); $value = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read of the options row under the state lock; must bypass the object cache $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", self::OPTION ) ); return null === $value ? null : $value; } /** Read the independent managed catalog-key cleanup queue. */ public function read_catalog_key_cleanup() { return $this->read_option( self::CATALOG_KEY_CLEANUP_OPTION ); } /** Replace or delete the independent managed catalog-key cleanup queue. */ public function write_catalog_key_cleanup( $value ) { global $wpdb; $this->assert_locked(); if ( null !== $value && ! is_string( $value ) ) { throw new RuntimeException( 'connect_storage_unavailable' ); } $old = $this->read_catalog_key_cleanup(); if ( $old === $value ) { return; } if ( null === $value ) { $changed = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- compare-and-set on the options table under the state lock; must bypass the object cache $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND BINARY option_value = BINARY %s AND autoload = 'no'", self::CATALOG_KEY_CLEANUP_OPTION, $old ) ); } elseif ( null === $old ) { $changed = $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- insert into the options table under the state lock; must bypass the object cache $wpdb->options, array( 'option_name' => self::CATALOG_KEY_CLEANUP_OPTION, 'option_value' => $value, 'autoload' => 'no', ), array( '%s', '%s', '%s' ) ); } else { $changed = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- compare-and-set on the options table under the state lock; must bypass the object cache $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND BINARY option_value = BINARY %s AND autoload = 'no'", $value, self::CATALOG_KEY_CLEANUP_OPTION, $old ) ); } if ( 1 !== $changed ) { throw new RuntimeException( 'connect_state_conflict' ); } wp_cache_delete( self::CATALOG_KEY_CLEANUP_OPTION, 'options' ); if ( $this->read_catalog_key_cleanup() !== $value ) { throw new RuntimeException( 'connect_state_conflict' ); } } /** * Insert a new Connect state value. * * @param string $value State value. * @return void * @throws RuntimeException When the state conflicts with stored data. */ public function insert( $value ) { global $wpdb; $this->assert_locked(); if ( ! is_string( $value ) || $this->read() !== null || 1 !== $wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- insert into the options table under the state lock; must bypass the object cache $wpdb->options, array( 'option_name' => self::OPTION, 'option_value' => $value, 'autoload' => 'no' ), array( '%s', '%s', '%s' ) ) ) { throw new RuntimeException( 'connect_state_conflict' ); } $this->assert_readback( $value ); } /** * Replace an expected Connect state value. * * @param string $old Expected state value. * @param string $next Replacement state value. * @return void * @throws RuntimeException When the state conflicts with stored data. */ public function cas( $old, $next ) { global $wpdb; $this->assert_locked(); if ( ! is_string( $old ) || ! is_string( $next ) || $old === $next || 1 !== $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- compare-and-set on the options table under the state lock; must bypass the object cache $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND BINARY option_value = BINARY %s AND autoload = 'no'", $next, self::OPTION, $old ) ) ) { throw new RuntimeException( 'connect_state_conflict' ); } $this->assert_readback( $next ); } /** * Delete an expected Connect state value. * * @param string $old Expected state value. * @return void * @throws RuntimeException When the state conflicts with stored data. */ public function delete( $old ) { global $wpdb; $this->assert_locked(); if ( ! is_string( $old ) || 1 !== $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- compare-and-set on the options table under the state lock; must bypass the object cache $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND BINARY option_value = BINARY %s AND autoload = 'no'", self::OPTION, $old ) ) ) { throw new RuntimeException( 'connect_state_conflict' ); } wp_cache_delete( self::OPTION, 'options' ); if ( null !== $this->read() ) { throw new RuntimeException( 'connect_state_conflict' ); } } /** * Require the stored value to match a write. * * @param string $value Expected state value. * @return void * @throws RuntimeException When the readback differs. */ private function assert_readback( $value ) { wp_cache_delete( self::OPTION, 'options' ); if ( $this->read() !== $value ) { throw new RuntimeException( 'connect_state_conflict' ); } } /** Read one non-autoloaded option while holding the repository lock. */ private function read_option( $option ) { global $wpdb; $this->assert_locked(); $value = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read of the options row under the state lock; must bypass the object cache $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s AND autoload = 'no'", $option ) ); return null === $value ? null : $value; } /** * Require this instance to hold the database lock. * * @return void * @throws RuntimeException When the lock is not held. */ private function assert_locked() { if ( ! $this->locked ) { throw new RuntimeException( 'connect_lock_required' ); } } }