*/ private static function steps() { return array( // Initial tables. '1.0.0' => array( __CLASS__, 'install' ), // Adds job_items.sort_order, scan_items.permalink_hash and redirects.destination_hash. '1.1.0' => array( __CLASS__, 'migrate_1_1_0' ), ); } /** * Run required migrations step by step while the stored version is behind. * * @return void */ public static function run() { $current_version = get_option( Schema::DB_VERSION_OPTION, '0.0.0' ); if ( ! version_compare( $current_version, Schema::DB_VERSION, '<' ) ) { return; } foreach ( self::steps() as $version => $callback ) { if ( version_compare( $current_version, $version, '<' ) ) { call_user_func( $callback ); update_option( Schema::DB_VERSION_OPTION, $version ); $current_version = $version; } } } /** * Install or upgrade tables using dbDelta. * * @return void */ public static function install() { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; foreach ( Schema::get_schemas() as $sql ) { dbDelta( $sql ); } } /** * Step 1.1.0: add columns, then backfill destination hashes for existing redirects. * * @return void */ public static function migrate_1_1_0() { global $wpdb; self::install(); $table = Schema::get_table_name( 'slugh_redirects' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, destination, destination_type FROM %i WHERE destination_hash = ''", $table ) ); foreach ( (array) $rows as $row ) { $hash = 'internal' === $row->destination_type ? \SlugH\Redirects\Normalizer::compute_hash( \SlugH\Redirects\Normalizer::normalize_path( $row->destination ) ) : ''; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->update( $table, array( 'destination_hash' => $hash ), array( 'id' => $row->id ), array( '%s' ), array( '%d' ) ); } } /** * Drop all custom tables (used only on uninstall when configured). * * @return void */ public static function drop_tables() { global $wpdb; foreach ( Schema::get_table_names() as $table ) { $prefixed = Schema::get_table_name( $table ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $prefixed ) ); } delete_option( Schema::DB_VERSION_OPTION ); } }