<?php
/**
 * File Path: includes/migrations/class-mbs-migration-1-4.php
 *
 * MBS_Migration_1_4 — Rename Coming Soon / Preorder postmeta keys
 *
 * Migrate _mbs_coming_soon and _mbs_preorder_link to unprefixed meta keys.
 *
 * @package MyBookShowroom
 * @since   3.2.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class MBS_Migration_1_4
 *
 * Migrate _mbs_coming_soon and _mbs_preorder_link to unprefixed meta keys.
 *
 * @implements MBS_Migration
 */
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- DAO/migration: table names via $wpdb->prefix are trusted server-controlled values, not user input
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter -- same reason: $wpdb->prefix table names are safe; $where_sql is built from sanitized server-controlled inputs
class MBS_Migration_1_4 implements MBS_Migration { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * {@inheritdoc}
	 *
	 * @return string
	 */
	public function version(): string {
		return '1.4';
	}

	/**
	 * {@inheritdoc}
	 *
	 * Migrate _mbs_coming_soon and _mbs_preorder_link to unprefixed meta keys.
	 *
	 * @return void
	 */
	public function up(): void {
		global $wpdb;
		$pm = $wpdb->postmeta;
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- no user input
		$wpdb->query( "DELETE o FROM {$pm} o INNER JOIN {$pm} n ON o.post_id = n.post_id AND n.meta_key = 'mbs_coming_soon' WHERE o.meta_key = '_mbs_coming_soon'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- DAO layer -- direct DB access is intentional per MBS architecture
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( "UPDATE {$pm} SET meta_key = 'mbs_coming_soon' WHERE meta_key = '_mbs_coming_soon'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- DAO layer -- direct DB access is intentional per MBS architecture
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- no user input
		$wpdb->query( "DELETE o FROM {$pm} o INNER JOIN {$pm} n ON o.post_id = n.post_id AND n.meta_key = 'mbs_preorder_link' WHERE o.meta_key = '_mbs_preorder_link'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- DAO layer -- direct DB access is intentional per MBS architecture
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( "UPDATE {$pm} SET meta_key = 'mbs_preorder_link' WHERE meta_key = '_mbs_preorder_link'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- DAO layer -- direct DB access is intentional per MBS architecture
	}
}
