prefix . 'cfep_fields'; // Check if is_default column exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $column_exists = $wpdb->get_results("SHOW COLUMNS FROM $fields_table LIKE 'is_default'"); if (empty($column_exists)) { // Add is_default column // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $wpdb->query("ALTER TABLE $fields_table ADD COLUMN is_default tinyint(1) NOT NULL DEFAULT 0 AFTER enabled"); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log('CFEP: Added is_default column to fields table'); } // Check if can_delete column exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $column_exists = $wpdb->get_results("SHOW COLUMNS FROM $fields_table LIKE 'can_delete'"); if (empty($column_exists)) { // Add can_delete column // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $wpdb->query("ALTER TABLE $fields_table ADD COLUMN can_delete tinyint(1) NOT NULL DEFAULT 1 AFTER is_default"); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log('CFEP: Added can_delete column to fields table'); } } /** * Plugin deactivation */ public static function deactivate() { // Clean up transients delete_transient('cfep_activation_redirect'); } /** * Create database tables */ private static function create_tables() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); // Fields table $fields_table = $wpdb->prefix . 'cfep_fields'; $sql_fields = "CREATE TABLE IF NOT EXISTS $fields_table ( id bigint(20) NOT NULL AUTO_INCREMENT, field_key varchar(100) NOT NULL, field_type varchar(50) NOT NULL, section varchar(50) NOT NULL DEFAULT 'billing', label varchar(255) DEFAULT NULL, placeholder varchar(255) DEFAULT NULL, description text DEFAULT NULL, required tinyint(1) DEFAULT 0, enabled tinyint(1) DEFAULT 1, is_default tinyint(1) DEFAULT 0, can_delete tinyint(1) DEFAULT 1, position int(11) DEFAULT 0, field_class varchar(255) DEFAULT NULL, options longtext DEFAULT NULL, conditional_logic longtext DEFAULT NULL, validation_rules longtext DEFAULT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP, updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY field_key (field_key), KEY section (section), KEY enabled (enabled) ) $charset_collate;"; // Templates table $templates_table = $wpdb->prefix . 'cfep_templates'; $sql_templates = "CREATE TABLE IF NOT EXISTS $templates_table ( id bigint(20) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, description text DEFAULT NULL, category varchar(100) DEFAULT 'general', fields longtext DEFAULT NULL, is_active tinyint(1) DEFAULT 1, is_default tinyint(1) DEFAULT 0, created_at datetime DEFAULT CURRENT_TIMESTAMP, updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY category (category), KEY is_active (is_active) ) $charset_collate;"; // Block Checkout fields table $block_fields_table = $wpdb->prefix . 'cfep_block_fields'; $sql_block_fields = "CREATE TABLE IF NOT EXISTS $block_fields_table ( id bigint(20) NOT NULL AUTO_INCREMENT, field_key varchar(100) NOT NULL, field_type varchar(50) NOT NULL, location varchar(50) NOT NULL DEFAULT 'order', label varchar(255) DEFAULT NULL, placeholder varchar(255) DEFAULT NULL, description text DEFAULT NULL, required tinyint(1) DEFAULT 0, enabled tinyint(1) DEFAULT 1, position int(11) DEFAULT 0, field_class varchar(255) DEFAULT '', options longtext DEFAULT NULL, is_default tinyint(1) DEFAULT 0, can_delete tinyint(1) DEFAULT 1, created_at datetime DEFAULT CURRENT_TIMESTAMP, updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY field_key (field_key), KEY location (location), KEY enabled (enabled) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql_fields); dbDelta($sql_templates); dbDelta($sql_block_fields); } /** * Create default templates */ private static function create_default_templates() { global $wpdb; $table = $wpdb->prefix . 'cfep_templates'; // Check if templates already exist // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $count = $wpdb->get_var("SELECT COUNT(*) FROM $table WHERE is_default = 1"); if ($count > 0) { return; } // General Store Template $general_template = array( 'name' => __('General E-commerce Store', 'akz-checkout-fields-manager-for-woocommerce'), 'description' => __('Standard fields for most online stores', 'akz-checkout-fields-manager-for-woocommerce'), 'category' => 'general', 'is_default' => 1, 'fields' => wp_json_encode(array( array( 'field_key' => 'delivery_instructions', 'field_type' => 'textarea', 'label' => __('Delivery Instructions', 'akz-checkout-fields-manager-for-woocommerce'), 'placeholder' => __('Gate code, doorbell instructions, etc.', 'akz-checkout-fields-manager-for-woocommerce'), 'section' => 'shipping', 'required' => 0, 'position' => 100 ), array( 'field_key' => 'is_gift', 'field_type' => 'checkbox', 'label' => __('This is a gift', 'akz-checkout-fields-manager-for-woocommerce'), 'section' => 'billing', 'required' => 0, 'position' => 110 ), array( 'field_key' => 'gift_message', 'field_type' => 'textarea', 'label' => __('Gift Message', 'akz-checkout-fields-manager-for-woocommerce'), 'placeholder' => __('Enter your gift message...', 'akz-checkout-fields-manager-for-woocommerce'), 'section' => 'billing', 'required' => 0, 'position' => 120, 'conditional_logic' => wp_json_encode(array( 'show_if' => array( 'field' => 'is_gift', 'operator' => 'equals', 'value' => '1' ) )) ) )) ); // Fashion Store Template $fashion_template = array( 'name' => __('Fashion & Apparel Store', 'akz-checkout-fields-manager-for-woocommerce'), 'description' => __('Perfect for clothing and fashion retailers', 'akz-checkout-fields-manager-for-woocommerce'), 'category' => 'fashion', 'is_default' => 1, 'fields' => wp_json_encode(array( array( 'field_key' => 'preferred_fit', 'field_type' => 'select', 'label' => __('Preferred Fit', 'akz-checkout-fields-manager-for-woocommerce'), 'section' => 'billing', 'required' => 0, 'position' => 100, 'options' => wp_json_encode(array( 'xs' => 'XS - Extra Small', 's' => 'S - Small', 'm' => 'M - Medium', 'l' => 'L - Large', 'xl' => 'XL - Extra Large', 'xxl' => 'XXL - Double Extra Large', 'xxxl' => 'XXXL - Triple Extra Large', '4xl' => '4XL', '5xl' => '5XL' )) ), array( 'field_key' => 'size_notes', 'field_type' => 'textarea', 'label' => __('Size Notes', 'akz-checkout-fields-manager-for-woocommerce'), 'placeholder' => __('Any special sizing requirements?', 'akz-checkout-fields-manager-for-woocommerce'), 'section' => 'billing', 'required' => 0, 'position' => 110 ) )) ); // Digital Products Template $digital_template = array( 'name' => __('Digital Products Store', 'akz-checkout-fields-manager-for-woocommerce'), 'description' => __('Optimized for digital downloads and services', 'akz-checkout-fields-manager-for-woocommerce'), 'category' => 'digital', 'is_default' => 1, 'fields' => wp_json_encode(array( array( 'field_key' => 'license_email', 'field_type' => 'email', 'label' => __('License Email (if different)', 'akz-checkout-fields-manager-for-woocommerce'), 'placeholder' => __('your@email.com', 'akz-checkout-fields-manager-for-woocommerce'), 'description' => __('We will send license keys to this email', 'akz-checkout-fields-manager-for-woocommerce'), 'section' => 'billing', 'required' => 0, 'position' => 100 ), array( 'field_key' => 'company_name', 'field_type' => 'text', 'label' => __('Company Name (optional)', 'akz-checkout-fields-manager-for-woocommerce'), 'section' => 'billing', 'required' => 0, 'position' => 110 ) )) ); // Insert templates // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $wpdb->insert($table, $general_template); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $wpdb->insert($table, $fashion_template); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $wpdb->insert($table, $digital_template); } /** * Set default options */ private static function set_default_options() { $default_options = array( 'cfep_enable_block_checkout' => 'yes', 'cfep_enable_classic_checkout' => 'yes', 'cfep_show_in_emails' => 'yes', 'cfep_show_in_order_details' => 'yes', 'cfep_position_billing' => 'after', 'cfep_position_shipping' => 'after', ); foreach ($default_options as $key => $value) { if (get_option($key) === false) { add_option($key, $value); } } } /** * Run Block Checkout fields migration */ private static function run_block_migration() { require_once CFEP_PLUGIN_DIR . 'includes/migrations/class-cfep-migration-block-fields.php'; CFEP_Migration_Block_Fields::migrate(); } /** * Insert default WooCommerce checkout fields into database * This ensures proper field ordering on fresh installations */ private static function insert_default_woocommerce_fields() { // Load the WooCommerce fields loader require_once CFEP_PLUGIN_DIR . 'includes/class-cfep-wc-fields-loader.php'; $loader = CFEP_WC_Fields_Loader::instance(); // Insert default fields $inserted = $loader->insert_default_woocommerce_fields(); if ($inserted > 0) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log("CFEP: Inserted {$inserted} default WooCommerce fields on activation"); } } }