0 && self::$held?->get() === $wpdb ) { ++self::$depth; return self::$held_previous; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- a session setting, nothing to cache $previous = (string) ( $wpdb->get_var( 'SELECT @@SESSION.sql_mode' ) ?? '' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- a session setting, nothing to cache $wpdb->query( $wpdb->prepare( 'SET SESSION sql_mode = %s', self::RELAXED ) ); // Read back rather than assume: the SET can miss, or land on another connection. // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- a session setting, nothing to cache $now = (string) ( $wpdb->get_var( 'SELECT @@SESSION.sql_mode' ) ?? '' ); SqlLiteral::session_mode_changed( $wpdb, $now ); if ( str_contains( strtoupper( $now ), 'NO_BACKSLASH_ESCAPES' ) ) { throw new \RuntimeException( 'The database session kept NO_BACKSLASH_ESCAPES after it was asked to drop it, so SQL cannot be written for the other site safely.' ); } self::$depth = 1; self::$held = \WeakReference::create( $wpdb ); self::$held_previous = $previous; return $previous; } /** * Puts back what relax() returned. The shape check keeps anything but a comma * list of mode names out of the built query. Never throws; callers are finally blocks. */ public static function restore( \wpdb $wpdb, string $previous ): void { if ( self::$depth > 1 && self::$held?->get() === $wpdb ) { --self::$depth; return; } self::$depth = 0; self::$held = null; if ( ! preg_match( '/^[A-Za-z0-9_, ]*$/', $previous ) ) { $previous = ''; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- a session setting, nothing to cache $wpdb->query( $wpdb->prepare( 'SET SESSION sql_mode = %s', $previous ) ); SqlLiteral::session_mode_changed( $wpdb ); } }