<?php
/**
 * Migration: add show_cart_button column to WooCommerce presets.
 *
 * @package BoostBot
 * @since   2.0.0
 */

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Migration owns custom-table DDL. Table names pass through sanitize_identifier(); DDL cannot use prepare() placeholders for identifiers; caching is irrelevant for one-shot migrations.
defined( 'ABSPATH' ) || exit;

class BoostBot_Migration_Add_Show_Cart_Button_To_WooCommerce_Presets {

    /**
     * Run the migration.
     *
     * @return void
     */
    public static function run() {
        $instance = new self();
        $instance->up();
    }

    /**
     * Add the missing column for existing installs.
     *
     * @return void
     */
    public function up() {
        global $wpdb;

        $table = self::sanitize_identifier( $wpdb->prefix . 'boostbot_woocommerce_presets' );

        if ( ! $this->table_exists( $table ) || $this->column_exists( $table, 'show_cart_button' ) ) {
            return;
        }

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- DDL cannot use placeholders for identifiers; $table is sanitized, definitions are literals.
        $wpdb->query( "ALTER TABLE {$table} ADD COLUMN show_cart_button tinyint(1) NOT NULL DEFAULT 0 AFTER cart_redirect" );
    }

    /**
     * Check whether the table exists.
     *
     * @param string $table Table name.
     * @return bool
     */
    private function table_exists( string $table ): bool {
        global $wpdb;

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $result = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );

        return $table === $result;
    }

    /**
     * Check whether a column exists.
     *
     * @param string $table  Table name.
     * @param string $column Column name.
     * @return bool
     */
    private function column_exists( string $table, string $column ): bool {
        global $wpdb;

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- DDL cannot use placeholders for identifiers; $table is sanitized, definitions are literals.
        $results = $wpdb->get_results( $wpdb->prepare( "SHOW COLUMNS FROM {$table} LIKE %s", $column ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared

        return ! empty( $results );
    }

    /**
     * Restrict a SQL identifier to [A-Za-z0-9_] characters.
     *
     * DDL cannot use $wpdb->prepare() for identifiers, so any identifier
     * that ends up interpolated into a query first passes through this
     * allow-list.
     *
     * @param string $identifier Raw identifier.
     * @return string Identifier with disallowed characters removed.
     */
    private static function sanitize_identifier( $identifier ) {
        return preg_replace( '/[^A-Za-z0-9_]/', '', (string) $identifier );
    }
}
