prefix . 'cfep_block_fields'; // Check if table exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) { return; } // Check if company field exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $company_exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE field_key = %s", 'shipping_company' ) ); // If company doesn't exist, insert all default fields if ($company_exists == 0) { $this->insert_default_woocommerce_fields(); } } /** * Insert default WooCommerce fields */ private function insert_default_woocommerce_fields() { global $wpdb; $table = $wpdb->prefix . 'cfep_block_fields'; $default_fields = array( // Contact Information array( 'field_key' => 'billing_email', 'field_type' => 'email', 'label' => 'Email address', 'location' => 'contact', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 1 ), // Shipping Address Fields array( 'field_key' => 'shipping_first_name', 'field_type' => 'text', 'label' => 'First name', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 1 ), array( 'field_key' => 'shipping_last_name', 'field_type' => 'text', 'label' => 'Last name', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 2 ), array( 'field_key' => 'shipping_company', 'field_type' => 'text', 'label' => 'Company (optional)', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, // Enabled by default 'required' => 0, 'position' => 3 ), array( 'field_key' => 'shipping_address_1', 'field_type' => 'text', 'label' => 'Street address', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 4 ), array( 'field_key' => 'shipping_address_2', 'field_type' => 'text', 'label' => 'Apartment, suite, etc. (optional)', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, // Enabled by default 'required' => 0, 'position' => 5 ), array( 'field_key' => 'shipping_city', 'field_type' => 'text', 'label' => 'City', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 6 ), array( 'field_key' => 'shipping_state', 'field_type' => 'text', 'label' => 'State', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 7 ), array( 'field_key' => 'shipping_postcode', 'field_type' => 'text', 'label' => 'Postcode / ZIP', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 8 ), array( 'field_key' => 'shipping_country', 'field_type' => 'select', 'label' => 'Country / Region', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, 'required' => 1, 'position' => 9 ), array( 'field_key' => 'shipping_phone', 'field_type' => 'tel', 'label' => 'Phone (optional)', 'location' => 'address', 'is_default' => 1, 'enabled' => 1, // Enabled by default 'required' => 0, 'position' => 10 ), // Additional Information array( 'field_key' => 'order_comments', 'field_type' => 'textarea', 'label' => 'Order notes (optional)', 'location' => 'additional', 'is_default' => 1, 'enabled' => 1, 'required' => 0, 'position' => 1 ), ); // Insert each field foreach ($default_fields as $field) { // Check if field already exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE field_key = %s", $field['field_key'] ) ); if ($exists == 0) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert($table, $field); } } // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log('CFEP: Inserted default WooCommerce fields including company'); } }