locale_fields = array(
'billing_address_1', 'billing_address_2', 'billing_state', 'billing_postcode', 'billing_city',
'shipping_address_1', 'shipping_address_2', 'shipping_state', 'shipping_postcode', 'shipping_city',
'order_comments'
);
add_action('admin_menu', array($this, 'admin_menu'));
add_filter('woocommerce_screen_ids', array($this, 'add_screen_id'));
add_action('woocommerce_checkout_update_order_meta', array($this, 'save_data'), 10, 2);
add_action( 'wp_enqueue_scripts', array($this, 'wc_checkout_fields_scripts'));
add_action('wp_ajax_save_custom_form_fields', array($this, 'save_wcfe_options'));
add_action( 'wp_ajax_wcfe_save_checkout_fields', array( $this, 'ajax_save_checkout_fields' ) );
add_action( 'wp_ajax_wcfe_reset_checkout_fields', array( $this, 'ajax_reset_checkout_fields' ) );
add_filter( 'woocommerce_form_field_text', array($this, 'wcfe_checkout_fields_text_field'), 10, 4 );
add_filter( 'woocommerce_form_field_checkbox', array($this, 'wcfe_checkout_fields_checkbox_field'), 10, 4 );
add_filter( 'woocommerce_form_field_checkboxgroup', array($this, 'wcfe_checkout_fields_checkboxgroup_field'), 10, 4 );
add_filter( 'woocommerce_form_field_date', array($this, 'wcfe_checkout_fields_date_picker_field'), 10, 4 );
add_filter( 'woocommerce_form_field_timepicker', array($this, 'wcfe_checkout_fields_timepicker_field'), 10, 4 );
add_filter( 'woocommerce_form_field_radio', array($this, 'wcfe_checkout_fields_radio_field'), 10, 4 );
add_filter( 'woocommerce_form_field_select', array($this, 'wcfe_checkout_fields_select_field'), 10, 4 );
add_filter( 'woocommerce_form_field_multiselect', array($this, 'wcfe_checkout_fields_multiselect_field'), 10, 4 );
add_filter( 'woocommerce_form_field_heading', array($this, 'wcfe_checkout_fields_heading_field'), 10, 4 );
add_filter( 'woocommerce_form_field_paragraph', array($this, 'wcfe_checkout_fields_paragraph_field'), 10, 4 );
add_filter( 'woocommerce_form_field_url', array($this, 'wcfe_url_field'), 10, 4 );
}
/**
* Admin Menu function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function admin_menu() {
$this->screen_id = add_submenu_page('woocommerce', esc_html__('WooCommerce Checkout & Register Form Editor', 'wcfe'), esc_html__('Checkout & Register Form', 'wcfe'),
'manage_woocommerce', 'checkout_form_editor', array($this, 'the_editor'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
}
/**
* Admin Script function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function enqueue_admin_scripts( $hook ) {
if ( 'woocommerce_page_checkout_form_editor' !== $hook ) {
return;
}
wp_enqueue_style( 'wcfe-style', plugins_url( '/assets/css/wcfe-style.css', dirname( __FILE__ ) ), array(), WCFE_VERSION );
wp_enqueue_style( 'wp-components' );
if ( is_rtl() ) {
wp_enqueue_style( 'wcfe-style-rtl', plugins_url( '/assets/css/wcfe-rtl.css', dirname( __FILE__ ) ), array(), WCFE_VERSION );
wp_enqueue_style( 'wcfe-rtl-ui', plugins_url( '/assets/css/rtl-css-ui.css', dirname( __FILE__ ) ), array(), WCFE_VERSION );
}
$modal_asset_file = dirname( __FILE__ ) . '/../assets/js/wcfe-admin-modal.asset.php';
$modal_asset = file_exists( $modal_asset_file ) ? include $modal_asset_file : array(
'dependencies' => array( 'wp-element', 'wp-components', 'wp-i18n', 'wp-dom-ready' ),
'version' => WCFE_VERSION,
);
if ( ! wp_script_is( 'react-jsx-runtime', 'registered' ) ) {
wp_register_script(
'react-jsx-runtime',
plugins_url( '/assets/js/react-jsx-runtime-shim.js', dirname( __FILE__ ) ),
array( 'react' ),
WCFE_VERSION,
true
);
}
$modal_deps = isset( $modal_asset['dependencies'] ) && is_array( $modal_asset['dependencies'] )
? $modal_asset['dependencies']
: array( 'wp-element', 'wp-components', 'wp-i18n', 'wp-dom-ready' );
$modal_ver = isset( $modal_asset['version'] ) ? $modal_asset['version'] : WCFE_VERSION;
wp_enqueue_script(
'wcfe-admin-modal',
plugins_url( '/assets/js/wcfe-admin-modal.js', dirname( __FILE__ ) ),
$modal_deps,
$modal_ver,
true
);
$modal_style = dirname( __FILE__ ) . '/../assets/js/style-wcfe-admin-modal.css';
if ( file_exists( $modal_style ) ) {
// filemtime so CSS-only rebuilds bust browser cache (asset.php hash tracks JS).
$modal_style_ver = (string) filemtime( $modal_style );
wp_enqueue_style(
'wcfe-admin-modal',
plugins_url( '/assets/js/style-wcfe-admin-modal.css', dirname( __FILE__ ) ),
array( 'wp-components' ),
$modal_style_ver
);
}
$c_type = ( isset( $_GET['c_type'] ) && 'block' === sanitize_key( wp_unslash( $_GET['c_type'] ) ) ) ? 'block' : 'classic';
$section = ( isset( $_GET['section'] ) && 'block' === $c_type ) ? sanitize_key( wp_unslash( $_GET['section'] ) ) : '';
$classic_section = ( isset( $_GET['section'] ) && 'block' !== $c_type )
? sanitize_key( wp_unslash( $_GET['section'] ) )
: 'billing';
$wcfe_admin_data = array(
'MSG_INVALID_NAME' => 'NAME can not contain spaces',
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'checkout_nounce' => wp_create_nonce( 'wcfe_checkout_nounce' ),
'i18n_cancel' => __( 'Cancel', 'wcfe' ),
'i18n_add_field' => __( 'Add New Field', 'wcfe' ),
'i18n_update_field' => __( 'Update Field', 'wcfe' ),
'i18n_saving' => __( 'Saving…', 'wcfe' ),
'i18n_saved' => __( 'Your changes were saved.', 'wcfe' ),
'i18n_save_error' => __( 'Your changes were not saved due to an error (or you made none!).', 'wcfe' ),
'i18n_save_failed' => __( 'Could not save changes. Please try again.', 'wcfe' ),
'i18n_resetting' => __( 'Resetting…', 'wcfe' ),
'i18n_reset_title' => __( 'Reset to default fields', 'wcfe' ),
'i18n_reset_confirm' => __( 'Are you sure you want to reset to default fields? All your changes will be deleted.', 'wcfe' ),
'i18n_reset_confirm_block' => __( 'Are you sure you want to reset block checkout fields to defaults?', 'wcfe' ),
'i18n_reset_confirm_btn' => __( 'Reset fields', 'wcfe' ),
'i18n_reset_success' => __( 'Checkout fields successfully reset.', 'wcfe' ),
'i18n_reset_success_block' => __( 'Block checkout fields reset to defaults.', 'wcfe' ),
'i18n_reset_failed' => __( 'Could not reset fields. Please try again.', 'wcfe' ),
'checkout_type' => $c_type,
'section' => 'block' === $c_type ? $section : $classic_section,
'block_section' => $section,
'block_field_types_by_section'=> class_exists( 'WCFE_Utils_Block' ) ? WCFE_Utils_Block::get_block_field_types_by_section() : array(),
'field_types' => $this->get_field_types(),
);
// Available to React modal immediately (loads before wcfe-admin-script).
wp_localize_script( 'wcfe-admin-modal', 'WcfeAdmin', $wcfe_admin_data );
// filemtime so Hostinger/LiteSpeed caches pick up admin JS fixes immediately.
$admin_js_path = dirname( __FILE__ ) . '/../assets/js/wcfe-admin.js';
$admin_js_ver = file_exists( $admin_js_path ) ? (string) filemtime( $admin_js_path ) : WCFE_VERSION;
wp_enqueue_script(
'wcfe-admin-script',
plugins_url( '/assets/js/wcfe-admin.js', dirname( __FILE__ ) ),
array( 'jquery', 'jquery-ui-sortable', 'woocommerce_admin', 'select2', 'jquery-tiptip', 'wcfe-admin-modal' ),
$admin_js_ver,
true
);
}
/**
* Checkout Fields Front-end Scripts function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wc_checkout_fields_scripts() {
global $wp_scripts;
if ( is_checkout() ) {
wp_enqueue_style( 'wcfe-style-front', plugins_url( '/assets/css/wcfe-style-front.css', dirname( __FILE__ ) ), array(), WCFE_VERSION );
if ( is_rtl() ) {
wp_enqueue_style( 'wcfe-style-front-rtl', plugins_url( '/assets/css/wcfe-front-end-rtl.css', dirname( __FILE__ ) ), array(), WCFE_VERSION );
}
if ( is_checkout() ) {
$currentScreen = 'checkout';
} else {
$currentScreen = 'account';
}
// Prefer WooCommerce-bundled jQuery UI CSS (no external CDN).
if ( function_exists( 'WC' ) && WC() ) {
wp_enqueue_style(
'jquery-ui-style',
WC()->plugin_url() . '/assets/css/jquery-ui/jquery-ui.min.css',
array(),
defined( 'WC_VERSION' ) ? WC_VERSION : null
);
} else {
wp_enqueue_style( 'wp-jquery-ui-dialog' );
}
$pattern = array(
//day
'd', //day of the month
'j', //3 letter name of the day
'l', //full name of the day
'z', //day of the year
'S',
//month
'F', //Month name full
'M', //Month name short
'n', //numeric month no leading zeros
'm', //numeric month leading zeros
//year
'Y', //full numeric year
'y' //numeric year: 2 digit
);
$replace = array(
'dd','d','DD','o','',
'MM','M','m','mm',
'yy','y'
);
foreach( $pattern as &$p ) {
$p = '/' . $p . '/';
}
wp_localize_script( 'wc-checkout-editor-frontend', 'wc_checkout_fields', array(
'date_format' => preg_replace( $pattern, $replace, wc_date_format() )
) );
}
}
/**
* Checkout Field Text Fields function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_text_field( $field, $key, $args, $value ) {
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
$data_validations = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$data_validations = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$data_rules_action = '';
$data_rules = '';
if(isset($args['rules_action_ajax']) && !empty($args['rules_action_ajax'])){
$data_rules_action = $args['rules_action_ajax'];
$data_rules = urldecode($args['rules_ajax']);
}
$singleq = "'";
$fieldLabel = '';
$field = '';
if ( $args['label'] ) {
$fieldLabel = $args['label'];
$field .= '' . esc_html( $args['label'] ) . $required . ' ';
}
$field .= ' ';
$field .= '
' . $after;
return $field;
}
/**
* Woo Function rest field
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_url_field($field, $key, $args, $value) {
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
$data_validations = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$data_validations = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$data_rules_action = '';
$data_rules = '';
$singleq = "'";
$fieldLabel = '';
$field = '';
if ( $args['label'] ) {
$fieldLabel = $args['label'];
$field .= '' . esc_html( $args['label'] ) . $required . ' ';
}
$field .= ' ';
$field .= '
' . $after;
return $field;
}
/**
* Save Data function Checkout Fields Check box Field Function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_checkbox_field( $field, $key, $args, $value ) {
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$data_rules_action = '';
$data_rules = '';
if(isset($args['rules_action_ajax']) && !empty($args['rules_action_ajax'])){
$data_rules_action = $args['rules_action_ajax'];
$data_rules = urldecode($args['rules_ajax']);
}
$singleq = "'";
$field = '';
$field .= '
' . $required;
if( isset($args['price']) && !empty($args['price']) || isset($args['price_type']) && $args['price_type'] == 'custom' ){
$price_type = "";
if(isset($args['price_type'])){
$price_type = $args['price_type'];
}
$price_unit = 0;
if(isset($args['price_unit']) && !empty($args['price_unit'])){
$price_unit = $args['price_unit'];
}
$taxable = "";
if(isset($args['taxable'])){
$taxable = $args['taxable'];
}
$tax_class = "";
if(isset($args['tax_class'])){
$tax_class = $args['tax_class'];
}
$field .= ' ' . esc_html( $args['label'] ) . ' ';
} else {
$field .= ' ' . esc_html( $args['label'] ) . ' ';
}
$field .= ' ' . $after;
return $field;
}
/**
* Check box group function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_checkboxgroup_field( $field, $key, $args, $value ) {
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$data_rules_action = '';
$data_rules = '';
if(isset($args['rules_action_ajax']) && !empty($args['rules_action_ajax'])){
$data_rules_action = $args['rules_action_ajax'];
$data_rules = urldecode($args['rules_ajax']);
}
$singleq = "'";
$field = '';
$field .= '
' . esc_html( $args['label'] ) . $required . ' ';
if ( ! empty( $args['options_json'] ) ) {
foreach ( $args['options_json'] as $option ) {
if( isset($option['price']) && !empty($option['price']) ){
$hasPricing = true;
$field .= ' ' . esc_html( $option['text'] ) . ' ';
} else {
$field .= ' ' . esc_html( $option['text'] ) . ' ';
}
}
}
$field .= ' ' . $after;
return $field;
}
/**
* Radio Field function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_radio_field( $field, $key, $args, $value ) {
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$data_rules_action = '';
$data_rules = '';
if(isset($args['rules_action_ajax']) && !empty($args['rules_action_ajax'])){
$data_rules_action = $args['rules_action_ajax'];
$data_rules = rawurldecode($args['rules_ajax']);
}
$singleq = "'";
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$field = '';
$field .= '
' . esc_html( $args['label'] ) . $required . ' ';
if ( ! empty( $args['options_json'] ) ) {
foreach ( $args['options_json'] as $option ) {
if( isset($option['price']) && !empty($option['price']) ){
$hasPricing = true;
$field .= ' ' . esc_html( $option['text'] ) . ' ';
} else {
$field .= ' ' . esc_html( preg_replace('/\+/', ' ', $option['text']) ) . ' ';
}
}
}
$field .= ' ' . $after;
return $field;
}
/**
* Checkout Field Time picker Fields function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_timepicker_field( $field, $key, $args, $value ){
$customer_user_id = get_current_user_id(); // current user ID here for example
// Getting current customer orders
$customer_orders = wc_get_orders( array(
'meta_key' => '_customer_user',
'meta_value' => $customer_user_id,
'posts_per_page'=>1,
'orderby'=>'ID',
'orderby'=>'DESC'
) );
$selectedVal = '';
// Loop through each customer WC_Order objects
foreach($customer_orders as $order ){
// Order ID (added WooCommerce 3+ compatibility)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$valArr = "";
$valArr = get_post_meta( $order_id, $key, true );
if(!empty($valArr) && is_array($valArr)){
foreach($valArr as $selectedVal){
$selectedVal = $selectedVal;
}
}
}
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$hasPricing =false;
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$options = '';
$field = '';
if ( $args['label'] ) {
$fieldLabel = $args['label'];
$field .= '' . esc_html( $args['label'] ) . $required . ' ';
}
$min_time = strtotime( isset( $args['min_time'] ) ? $args['min_time'] : '00:00' );
$max_time = strtotime( isset( $args['max_time'] ) ? $args['max_time'] : '23:59' );
$time_step = isset( $args['time_step'] ) ? absint( $args['time_step'] ) : 15;
if ( $time_step < 1 ) {
$time_step = 15;
}
$time_format = ! empty( $args['time_format'] ) ? $args['time_format'] : 'H:i';
$iterations = 0;
$max_iterations = 200;
for ( $hours = 0; $hours < 24; $hours++ ) {
for ( $mins = 0; $mins < 60; $mins += $time_step ) {
if ( ++$iterations > $max_iterations ) {
break 2;
}
$rawtime = str_pad( ( $hours % 24 ), 2, '0', STR_PAD_LEFT ) . ':' . str_pad( $mins, 2, '0', STR_PAD_LEFT );
$rawtime = strtotime( $rawtime );
$rawtime = date( $time_format, $rawtime );
if ( strtotime( $rawtime ) >= $min_time && strtotime( $rawtime ) <= $max_time ) {
$options .= '' . esc_html( $rawtime ) . ' ';
}
}
}
$class = '';
$field .= '';
$field .= $options;
$field .= '
' . $after;
return $field;
}
/**
* Checkout Fields Date Picker function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_date_picker_field( $field, $key, $args, $value ) {
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
if ( ! empty( $args['validate'] ) ) {
foreach( $args['validate'] as $validate ) {
$args['class'][] = 'validate-' . $validate;
}
}
$singleq = "'";
$field = '';
if ( $args['label'] ) {
$field .= '' . esc_html( $args['label'] ) . $required . ' ';
}
$field .= '
' . $after;
return $field;
}
/**
* Multi-select Fields function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_multiselect_field( $field, $key, $args, $value ) {
$customer_user_id = get_current_user_id(); // current user ID here for example
// Getting current customer orders
$customer_orders = wc_get_orders( array(
'meta_key' => '_customer_user',
'meta_value' => $customer_user_id,
'posts_per_page'=>1,
'orderby'=>'ID',
'orderby'=>'DESC'
) );
$selectedVal = '';
// Loop through each customer WC_Order objects
foreach($customer_orders as $order ) {
// Order ID (added WooCommerce 3+ compatibility)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$valArr = "";
$valArr = get_post_meta( $order_id, $key, true );
if(!empty($valArr) && is_array($valArr)){
foreach($valArr as $selectedVal){
$selectedVal = $selectedVal;
}
}
}
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$data_rules_action = '';
$data_rules = '';
if(isset($args['rules_action_ajax']) && !empty($args['rules_action_ajax'])){
$data_rules_action = $args['rules_action_ajax'];
$data_rules = rawurldecode($args['rules_ajax']);
}
$singleq = "'";
$options = '';
$hasPricing = false;
if ( ! empty( $args['options_json'] ) ) {
foreach ( $args['options_json'] as $option ) {
if(empty( selected( $selectedVal, $option['key'], false )) ){
$options .= '' . esc_html( $option['text'] ) .' ';
} else {
$options .= '' . esc_attr( $option['text'] ) .' ';
}
}
$field = '';
if ( $args['label'] ) {
$fieldLabel = $args['label'];
$field .= '' . esc_html( $args['label'] ) . $required . ' ';
}
$class = '';
$field .= '';
$field .= $options;
$field .= '
' . $after;
}
return $field;
}
/**
* Checkout Select Field
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_select_field( $field, $key, $args, $value ) {
$customer_user_id = get_current_user_id(); // current user ID here for example
// Getting current customer orders
$customer_orders = wc_get_orders( array(
'meta_key' => '_customer_user',
'meta_value' => $customer_user_id,
'posts_per_page'=>1,
'orderby'=>'ID',
'orderby'=>'DESC'
) );
$selectedVal = '';
// Loop through each customer WC_Order objects
foreach($customer_orders as $order ){
// Order ID (added WooCommerce 3+ compatibility)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$valArr = "";
$valArr = get_post_meta( $order_id, $key, true );
if(!empty($valArr) && is_array($valArr)){
foreach($valArr as $selectedVal){
$selectedVal = $selectedVal;
}
}
}
$data_rules_action = '';
$data_rules = '';
if(isset($args['rules_action_ajax']) && !empty($args['rules_action_ajax'])){
$data_rules_action = $args['rules_action_ajax'];
$data_rules = urldecode($args['rules_ajax']);
}
$singleq = "'";
if ( ( ! empty( $args['clear'] ) ) ) $after = '
'; else $after = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' * ';
} else {
$required = '';
}
$hasPricing =false;
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$options = '';
//$options .= ''. esc_html__('Please Select','wcfe') .' ';
if ( ! empty( $args['options_json'] ) ) {
foreach ( $args['options_json'] as $option ) {
if(empty( selected( $selectedVal, $option['key'], false )) ){
$options .= '' . esc_html( $option['text'] ) .' ';
} else {
$options .= '' . esc_attr( $option['text'] ) .' ';
}
}
$field = '';
if ( $args['label'] ) {
$fieldLabel = $args['label'];
$field .= '' . esc_html( $args['label'] ) . $required . ' ';
}
$class = '';
$field .= '';
$field .= $options;
$field .= '
' . $after;
}
return $field;
}
/**
* Checkout Field Heading Fields
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_heading_field( $field, $key, $args, $value ) {
$field = '';
return $field;
}
/**
* Checkout Field Paragraph Fields
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function wcfe_checkout_fields_paragraph_field( $field, $key, $args, $value ) {
$field = '' . esc_html( $args['label'] ) . '
';
return $field;
}
/**
* Add_screen_id function.
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function add_screen_id($ids){
$ids[] = 'woocommerce_page_checkout_form_editor';
$ids[] = strtolower(esc_html__('WooCommerce', 'wcfe')) .'_page_checkout_form_editor';
return $ids;
}
/**
* Reset checkout fields.
*
* @param bool $echo Whether to echo an admin notice.
* @return array
*/
function reset_checkout_fields( $echo = true ) {
delete_option('wcfe_account_label');
delete_option('wcfe_account_sync_fields');
if ( class_exists( 'WCFE_Utils' ) ) {
WCFE_Utils::delete_all_checkout_fields();
} else {
delete_option('wc_fields_account');
delete_option('wc_fields_billing');
delete_option('wc_fields_shipping');
delete_option('wc_fields_additional');
}
if ( class_exists( 'WCFE_Utils_Block' ) ) {
delete_option( WCFE_Utils_Block::OPTION_KEY_BLOCK_SECTIONS );
}
$result = array(
'success' => true,
'reload' => true,
'message' => __( 'Checkout fields successfully reset.', 'wcfe' ),
);
if ( $echo ) {
echo '' . esc_html( $result['message'] ) . '
';
}
return $result;
}
/**
* Whether the current AJAX request has a valid WCFE admin nonce.
*
* Accepts localized `security` or classic/block form nonce fields.
* Avoids HTTP 403 JSON responses — some hosts (LiteSpeed/WAF) replace those bodies.
*
* @param string $checkout_type classic|block
* @return bool
*/
function wcfe_ajax_request_has_valid_nonce( $checkout_type = 'classic' ) {
if (
isset( $_POST['security'] ) &&
wp_verify_nonce(
sanitize_text_field( wp_unslash( $_POST['security'] ) ),
'wcfe_checkout_nounce'
)
) {
return true;
}
if ( 'block' === $checkout_type ) {
return isset( $_POST['wcfe_block_checkout_fields_nonce'] ) &&
wp_verify_nonce(
sanitize_text_field( wp_unslash( $_POST['wcfe_block_checkout_fields_nonce'] ) ),
'wcfe_block_checkout_fields'
);
}
return isset( $_POST['wcfe_checkout_fields_saves'] ) &&
wp_verify_nonce(
sanitize_text_field( wp_unslash( $_POST['wcfe_checkout_fields_saves'] ) ),
'wcfe_checkout_fields_nounce'
);
}
/**
* AJAX: reset classic or block checkout fields.
*/
function ajax_reset_checkout_fields() {
if (
! current_user_can( 'manage_woocommerce' ) &&
! current_user_can( 'manage_options' )
) {
wp_send_json_error(
array( 'message' => __( 'You do not have permission to reset these fields.', 'wcfe' ) )
);
}
$checkout_type = isset( $_POST['checkout_type'] )
? sanitize_key( wp_unslash( $_POST['checkout_type'] ) )
: 'classic';
if ( ! $this->wcfe_ajax_request_has_valid_nonce( $checkout_type ) ) {
wp_send_json_error(
array( 'message' => __( 'Security check failed. Please reload the page and try again.', 'wcfe' ) )
);
}
if ( 'block' === $checkout_type ) {
if ( ! class_exists( 'WCFE_Admin_Block_Fields' ) ) {
wp_send_json_error(
array( 'message' => __( 'Block checkout editor is unavailable.', 'wcfe' ) )
);
}
$block_admin = new WCFE_Admin_Block_Fields();
// Nonce already verified above — skip duplicate form-nonce check.
$result = $block_admin->reset_fields_from_request( false, true );
if ( ! empty( $result['success'] ) ) {
wp_send_json_success( $result );
}
wp_send_json_error( $result );
}
$result = $this->reset_checkout_fields( false );
if ( ! empty( $result['success'] ) ) {
wp_send_json_success( $result );
}
wp_send_json_error( $result );
}
/**
* Reserved Field Name
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function is_reserved_field_name( $field_name ){
if($field_name && in_array($field_name, array(
'billing_first_name', 'billing_last_name', 'billing_company', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_country', 'billing_postcode', 'billing_phone', 'billing_email', 'shipping_first_name', 'shipping_last_name', 'shipping_company', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_country', 'shipping_postcode', 'customer_note', 'order_comments', 'account_username','account_password'
))) {
return true;
}
return false;
}
/**
* Default Field Name
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function is_default_field_name( $field_name ) {
if($field_name && in_array($field_name, array (
'billing_first_name', 'billing_last_name', 'billing_company', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_country', 'billing_postcode', 'billing_phone', 'billing_email', 'shipping_first_name', 'shipping_last_name', 'shipping_company', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_country', 'shipping_postcode', 'customer_note', 'order_comments', 'account_username','account_password'
))) {
return true;
}
return false;
}
/**
* Save Data function
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function save_data( $order_id, $posted ) {
if ( get_option( 'wcfe_account_sync_fields' ) && get_option( 'wcfe_account_sync_fields' ) == 'on' ) {
$types = array( 'account', 'billing', 'shipping', 'additional' );
} else {
$types = array( 'billing', 'shipping', 'additional' );
}
$order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : false;
foreach ( $types as $type ) {
$fields = $this->get_fields( $type );
if ( ! is_array( $fields ) ) {
continue;
}
foreach ( $fields as $name => $field ) {
// Only persist registered custom field keys.
if ( empty( $field['custom'] ) ) {
continue;
}
if ( isset( $_SESSION[ $name ] ) ) {
$session_value = is_array( $_SESSION[ $name ] )
? implode( ',', array_map( 'sanitize_text_field', wp_unslash( $_SESSION[ $name ] ) ) )
: sanitize_text_field( wp_unslash( $_SESSION[ $name ] ) );
if ( $order ) {
$order->update_meta_data( $name, $session_value );
} else {
update_post_meta( $order_id, $name, $session_value );
}
unset( $_SESSION[ $name ] );
}
if ( ! isset( $posted[ $name ] ) ) {
continue;
}
$value = $posted[ $name ];
if ( is_array( $value ) ) {
$value = implode( ',', array_map( 'sanitize_text_field', wp_unslash( $value ) ) );
} elseif ( isset( $field['type'] ) && 'textarea' === $field['type'] ) {
$value = sanitize_textarea_field( wp_unslash( $value ) );
} else {
$value = sanitize_text_field( wp_unslash( $value ) );
}
if ( '' === $value && ! in_array( ( $field['type'] ?? '' ), array( 'checkbox', 'checkboxgroup' ), true ) ) {
continue;
}
if ( $order ) {
$order->update_meta_data( $name, $value );
} else {
update_post_meta( $order_id, $name, $value );
}
}
}
if ( $order ) {
$order->save();
}
}
/**
* Get Fields
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
public static function get_fields($key){
$fields = array_filter( WCFE_Utils::get_checkout_fields( $key, array() ) );
if(empty($fields) || sizeof($fields) == 0){
if($key === 'billing' || $key === 'shipping'){
$fields = WC()->countries->get_address_fields(WC()->countries->get_base_country(), $key . '_');
} else if($key === 'additional') {
$fields = array(
'order_comments' => array(
'type' => 'textarea',
'class' => array('notes'),
'label' => esc_html__('Order Notes', 'wcfe'),
'placeholder' => _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'wcfe')
)
);
} else if($key === 'account'){
$fields = array(
'account_username' => array(
'type' => 'text',
'label' => esc_html__('Email address', 'wcfe')
),
'account_password' => array(
'type' => 'password',
'label' => esc_html__('Password', 'wcfe')
)
);
}
}
return $fields;
}
/**
* Short fields by order
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function sort_fields_by_order($a, $b){
if(!isset($a['order']) || $a['order'] == $b['order']){
return 0;
}
return ($a['order'] < $b['order']) ? -1 : 1;
}
/**
* Get Fields Type
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function get_field_types(){
return array(
'text' => 'Text',
'number' => 'Number',
'hidden' => 'Hidden',
'password' => 'Password',
'email' => 'Email',
'phone' => 'Phone',
'textarea' => 'Textarea',
'select' => 'Select',
'multiselect' => 'Multi-Select',
'checkbox' => 'Checkbox',
'checkboxgroup' => 'Checkbox Group',
'radio' => 'Radio',
'heading' => 'Heading',
'date' => 'Date',
'timepicker' => 'Time',
'url' => 'URL',
'paragraph' => 'Paragraph',
);
}
/**
* Mount point for the React field editor modal (WordPress components).
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.7.0
*/
function wcfe_render_admin_modal_root() {
echo '
';
}
/**
* @deprecated 1.7.0 React modal replaced jQuery UI dialogs.
*/
function wcfe_new_field_form_pp() {
// Kept as empty stub for any third-party hooks expecting the method.
}
/**
* @deprecated 1.7.0 React modal replaced jQuery UI dialogs.
*/
function wcfe_edit_field_form_pp() {
// Kept as empty stub for any third-party hooks expecting the method.
}
/**
* Render Tabs And Sections
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function render_tabs_and_sections(){
$tabs = array(
'fields' => __('Checkout & Account Fields', 'wcfe'),
'settings' => __('Settings', 'wcfe'),
'backup_import' => __('Backup & Import', 'wcfe'),
'premium_features' => __('Premium Features', 'wcfe'),
'our_plugins' => __('Our Plugins', 'wcfe'),
);
$tab = isset( $_GET['tab'] ) ? esc_attr( $_GET['tab'] ) : 'fields';
$c_type = isset( $_GET['c_type'] ) ? sanitize_key( wp_unslash( $_GET['c_type'] ) ) : 'classic';
$sections = ''; $section = '';
if($tab === 'fields'){
if ( 'block' === $c_type ) {
$sections = array_keys( WCFE_Utils_Block::get_default_section_names() );
$section = isset( $_GET['section'] ) ? esc_attr( $_GET['section'] ) : 'contact';
} else {
$sections = array( 'billing', 'shipping', 'additional', 'account' );
$section = isset( $_GET['section'] ) ? esc_attr( $_GET['section'] ) : 'billing';
}
}
echo '';
foreach( $tabs as $key => $value ) {
$active = ( $key == $tab ) ? 'nav-tab-active' : '';
echo ''.esc_html($value).' ';
}
echo ' ';
if ( 'fields' === $tab && class_exists( 'WCFE_Admin_Block_Fields' ) ) {
$block_admin = new WCFE_Admin_Block_Fields();
$block_admin->render_checkout_types();
}
if(!empty($sections)){
echo '';
$size = sizeof($sections); $i = 0;
foreach( $sections as $key ) {
$i++;
$active = ( $key == $section ) ? 'current' : '';
if ( 'block' === $c_type ) {
$section_label = WCFE_Utils_Block::get_default_section_names();
$label_text = isset( $section_label[ $key ] ) ? $section_label[ $key ] : ucwords( str_replace( '_', ' ', $key ) );
$url = 'admin.php?page=checkout_form_editor&tab=fields&c_type=block§ion=' . $key;
} else {
$label_text = ucwords( $key ) . ' ' . esc_html__( 'Fields', 'wcfe' );
$url = 'admin.php?page=checkout_form_editor&tab=fields§ion=' . $key;
}
echo '';
echo ''. esc_html( $label_text ) .' ';
echo ($size > $i) ? ' ' : '';
echo ' ';
}
echo ' ';
}
?>
get_current_tab();
$section = '';
if($tab === 'fields'){
$c_type = isset( $_GET['c_type'] ) ? sanitize_key( wp_unslash( $_GET['c_type'] ) ) : 'classic';
if ( 'block' === $c_type ) {
$section = isset( $_GET['section'] ) ? esc_attr( $_GET['section'] ) : 'contact';
} else {
$section = isset( $_GET['section'] ) ? esc_attr( $_GET['section'] ) : 'billing';
}
}
return $section;
}
/**
* Heading on Dashboard
*
* @package WooCommerce Checkout & Account Field Editor
* @since 1.0
*/
function render_checkout_fields_heading_row() { ?>
get_current_tab();
if( $tab === 'fields' ){
$this->checkout_form_field_editor();
} elseif ( 'settings' === $tab && class_exists( 'WCFE_Admin_Settings' ) ) {
$settings_admin = new WCFE_Admin_Settings();
$settings_admin->render_page( $this );
} elseif ( 'backup_import' === $tab && class_exists( 'WCFE_Admin_Backup' ) ) {
$backup_admin = new WCFE_Admin_Backup();
$backup_admin->render_page( $this );
} elseif($tab === 'premium_features') {
$this->checkout_form_premium_feature();
} elseif($tab === 'our_plugins') {
$this->checkout_form_premium_our_plugins();
}
}
function checkout_form_premium_feature() {
$section = $this->get_current_section();
echo '';
$this->render_tabs_and_sections();
?>
Key Features of WooCommerce Checkout Field Editor Pro
Checkout Field Editor For WooCommerce plugin comes with several advanced features that let you create an organized checkout page. With these premium features, bring your checkout page to its next level.
17 Custom Checkout Field Types
Custom section which can be placed at 15 different positions on the checkout page
Display fields conditionally
Display sections conditionally
Price fields with a set of price types
Custom validations
Change address display format
Display fields based on Shipping option or Payment method
Compatibility with other plugins
Zapier support
WPML Compatibility
Reset all settings on a single click
Manage field display in emails and order details pages
Display custom fields optionally at My Account page
Customise, Disable or delete default WooCommerce fields
Developer friendly with custom hooks
Rearrange all fields and sections as per convenience
Create your own custom classes for styling the field
Manage your customer registration forms
Sync billing fields into registration fields
Upgrade to Premium Version
Available Field types
Following are the custom checkout field types available in the Checkout Field Editor plugin.
Text
Hidden
Password
Telephone
Email
Number
Textarea
Select
Multi Select
Radio
Checkbox
Checkbox Group
Date picker
Time picker
File Upload
Multiple File Upload
Heading
Label
Display Sections Conditionally
Display the custom sections on your checkout page based on the conditions you set. Following are the positions where these checkout sections can be displayed:
Before customer details
After customer details
Before billing form
After billing form
Before shipping form
After shipping form
Before registration form
After registration form
Before order notes
After order notes
Before terms and conditions
After terms and conditions
Before submit button
After submit button
Inside a custom step created using WooCommerce MultiStep Checkout
Display Fields conditionally
Display the custom and default checkout fields based on the conditions you provide. Conditions on which the fields can be displayed are:
Cart Contents
Cart Subtotal
Cart Total
User Roles
Product
Product Variation
Product Category
Based on other field values
Add price fields and choose the price type
With the premium version of the Checkout Page Editor plugin, add an extra price value to the total price by creating a field with price into the checkout form.The available price types that can be added to WooCommerce checkout fields are:
Fixed Price
Custom Price
Percentage of Cart Total
Percentage of Subtotal
Percent of Subtotal excluding tax
Dynamic Price
get_current_section();
echo '';
$this->render_tabs_and_sections();
$action = 'install-plugin';
?>
Using the compatibility feature of remove add-to-cart and add custom button in your WooCommerce products
Install now
get_current_section();
$c_type = isset( $_GET['c_type'] ) ? sanitize_key( wp_unslash( $_GET['c_type'] ) ) : 'classic';
echo '';
$this->render_tabs_and_sections();
if ( 'block' === $c_type && class_exists( 'WCFE_Admin_Block_Fields' ) ) {
$block_admin = new WCFE_Admin_Block_Fields();
$block_admin->render_page( $this );
$this->wcfe_render_admin_modal_root();
echo '
';
return;
}
if ( isset( $_POST['save_fields'] ) ) {
$this->save_options( $section );
}
if ( isset( $_POST['reset_fields'] ) ) {
if (
( current_user_can( 'manage_options' ) || current_user_can( 'manage_woocommerce' ) ) &&
isset( $_POST['wcfe_checkout_fields_saves'] ) &&
wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wcfe_checkout_fields_saves'] ) ), 'wcfe_checkout_fields_nounce' )
) {
$this->reset_checkout_fields( true );
}
}
global $supress_field_modification;
$supress_field_modification = false;
if( $section != 'account' ) { ?>
wcfe_render_admin_modal_root(); ?>
__( 'You do not have permission to save these fields.', 'wcfe' ) )
);
}
$checkout_type = isset( $_POST['checkout_type'] )
? sanitize_key( wp_unslash( $_POST['checkout_type'] ) )
: 'classic';
if ( 'block' === $checkout_type ) {
$section = isset( $_POST['wcfe_block_section'] )
? sanitize_key( wp_unslash( $_POST['wcfe_block_section'] ) )
: ( isset( $_POST['section'] ) ? sanitize_key( wp_unslash( $_POST['section'] ) ) : 'contact' );
if ( ! class_exists( 'WCFE_Admin_Block_Fields' ) ) {
wp_send_json_error(
array( 'message' => __( 'Block checkout editor is unavailable.', 'wcfe' ) )
);
}
$block_admin = new WCFE_Admin_Block_Fields();
$result = $block_admin->save_fields_from_request( $section, false );
if ( ! empty( $result['success'] ) ) {
wp_send_json_success( $result );
}
wp_send_json_error( $result );
}
$section = isset( $_POST['wcfe_section'] )
? sanitize_key( wp_unslash( $_POST['wcfe_section'] ) )
: ( isset( $_POST['section'] ) ? sanitize_key( wp_unslash( $_POST['section'] ) ) : 'billing' );
$result = $this->save_options( $section, false );
if ( ! empty( $result['success'] ) ) {
wp_send_json_success( $result );
}
wp_send_json_error( $result );
}
/**
* Save Plug in Options
*
* @param string $section Section key.
* @param bool $echo Whether to echo admin notices (false for AJAX).
* @return array|false
*/
function save_options( $section, $echo = true ) {
if (
! isset( $_POST['wcfe_checkout_fields_saves'] ) ||
( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_woocommerce' ) ) ||
! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wcfe_checkout_fields_saves'] ) ), 'wcfe_checkout_fields_nounce' )
) {
$result = array(
'success' => false,
'message' => __( 'Security check failed. Please reload the page and try again.', 'wcfe' ),
);
if ( $echo ) {
echo '' . esc_html( $result['message'] ) . '
';
}
return $echo ? false : $result;
}
$o_fields = $this->get_fields( $section );
$fields = $o_fields;
$f_options = array();
$result = false;
$f_order = !empty( $_POST['f_order'] ) ? wp_unslash($_POST['f_order']) : array();
$f_names = !empty( $_POST['f_name'] ) ? wp_unslash($_POST['f_name']) : array();
$f_names_new = !empty( $_POST['f_name_new'] ) ? wp_unslash($_POST['f_name_new']) : array();
$f_types = !empty( $_POST['f_type'] ) ? wp_unslash($_POST['f_type']) : array();
$f_labels = !empty( $_POST['f_label'] ) ? wp_unslash($_POST['f_label']) : array();
$f_extoptions = !empty( $_POST['f_extoptions'] ) ? wp_unslash($_POST['f_extoptions']) : array();
$f_access = !empty( $_POST['f_access'] ) ? wp_unslash($_POST['f_access']) : array();
$f_placeholder = !empty( $_POST['f_placeholder'] ) ? wp_unslash($_POST['f_placeholder']) : array();
$i_price = !empty( $_POST['i_price'] ) ? wp_unslash($_POST['i_price']) : array();
$i_price_unit = !empty( $_POST['i_price_unit'] ) ? wp_unslash($_POST['i_price_unit']) : array();
$i_price_type = !empty( $_POST['i_price_type'] ) ? wp_unslash($_POST['i_price_type']) : array();
$i_min_time = !empty( $_POST['i_min_time'] ) ? wp_unslash($_POST['i_min_time']) : array();
$i_max_time = !empty( $_POST['i_max_time'] ) ? wp_unslash($_POST['i_max_time']) : array();
$i_time_step = !empty( $_POST['i_time_step'] ) ? wp_unslash($_POST['i_time_step']) : array();
$i_time_format = !empty( $_POST['i_time_format'] ) ? wp_unslash($_POST['i_time_format']) : array();
$i_taxable = !empty( $_POST['i_taxable'] ) ? wp_unslash($_POST['i_taxable']) : array();
$i_tax_class = !empty( $_POST['i_tax_class'] ) ? wp_unslash($_POST['i_tax_class']) : array();
$f_maxlength = !empty( $_POST['f_maxlength'] ) ? wp_unslash($_POST['f_maxlength']) : array();
$f_options = !empty( $_POST['f_options'] ) ? wp_unslash($_POST['f_options']) : array();
$f_class = !empty( $_POST['f_class'] ) ? wp_unslash($_POST['f_class']) : array();
$f_label_class = !empty( $_POST['f_label_class'] ) ? wp_unslash($_POST['f_label_class']) : array();
$f_rules_action = !empty( $_POST['f_rules_action'] ) ? wp_unslash($_POST['f_rules_action']) : array();
$f_rules = !empty( $_POST['f_rules'] ) ? wp_unslash($_POST['f_rules']) : array();
$f_rules_action_ajax = !empty( $_POST['f_rules_action_ajax'] ) ? wp_unslash($_POST['f_rules_action_ajax']) : array();
$f_rules_ajax = !empty( $_POST['f_rules_ajax'] ) ? wp_unslash($_POST['f_rules_ajax']) : array();
$f_required = !empty( $_POST['f_required'] ) ? wp_unslash($_POST['f_required']) : array();
$f_enabled = !empty( $_POST['f_enabled'] ) ? wp_unslash($_POST['f_enabled']) : array();
$f_show_in_email = !empty( $_POST['f_show_in_email'] ) ? wp_unslash($_POST['f_show_in_email']) : array();
$f_show_in_order = !empty( $_POST['f_show_in_order'] ) ? wp_unslash($_POST['f_show_in_order']) : array();
$f_show_in_my_account = !empty( $_POST['f_show_in_my_account'] ) ? wp_unslash($_POST['f_show_in_my_account']) : array();
$f_validation = !empty( $_POST['f_validation'] ) ? wp_unslash($_POST['f_validation']) : array();
$f_deleted = !empty( $_POST['f_deleted'] ) ? wp_unslash($_POST['f_deleted']) : array();
$f_position = !empty( $_POST['f_position'] ) ? wp_unslash($_POST['f_position']) : array();
$f_display_options = !empty( $_POST['f_display_options'] ) ? wp_unslash($_POST['f_display_options']) : array();
$max = max( array_map( 'absint', array_keys( $f_names ) ) );
for ( $i = 0; $i <= $max; $i ++ ) {
$name = empty( $f_names[$i] ) ? '' : urldecode( sanitize_title( wc_clean( stripslashes( $f_names[$i] ) ) ) );
$new_name = empty( $f_names_new[$i] ) ? '' : urldecode( sanitize_title( wc_clean( stripslashes( $f_names_new[$i] ) ) ) );
$allow_override = apply_filters('wcfe_allow_default_field_override_'.$new_name, false);
if(!empty($f_deleted[$i]) && $f_deleted[$i] == 1){
unset( $fields[$name] );
continue;
}
// Check reserved names
if($this->is_reserved_field_name( $new_name ) && !$allow_override){
continue;
}
//if update field
if( $name && $new_name && $new_name !== $name ){
if ( isset( $fields[$name] ) ) {
$fields[$new_name] = $fields[$name];
} else {
$fields[$new_name] = array();
}
unset( $fields[$name] );
$name = $new_name;
} else {
$name = $name ? $name : $new_name;
}
if(!$name){
continue;
}
//if new field
if ( !isset( $fields[$name] ) ) {
$fields[$name] = array();
}
$o_type = isset( $o_fields[$name]['type'] ) ? $o_fields[$name]['type'] : 'text';
$allowed_tags = array(
'a' => array(
'class' => array(),
'href' => array(),
'rel' => array(),
'title' => array(),
),
'abbr' => array(
'title' => array(),
),
'b' => array(),
'blockquote' => array(
'cite' => array(),
),
'cite' => array(
'title' => array(),
),
'code' => array(),
'del' => array(
'datetime' => array(),
'title' => array(),
),
'dd' => array(),
'div' => array(
'class' => array(),
'title' => array(),
'style' => array(),
),
'dl' => array(),
'dt' => array(),
'em' => array(),
'h1' => array(),
'h2' => array(),
'h3' => array(),
'h4' => array(),
'h5' => array(),
'h6' => array(),
'i' => array(),
'img' => array(
'alt' => array(),
'class' => array(),
'height' => array(),
'src' => array(),
'width' => array(),
),
'li' => array(
'class' => array(),
),
'ol' => array(
'class' => array(),
),
'p' => array(
'class' => array(),
),
'q' => array(
'cite' => array(),
'title' => array(),
),
'span' => array(
'class' => array(),
'title' => array(),
'style' => array(),
),
'strike' => array(),
'strong' => array(),
'ul' => array(
'class' => array(),
),
);
$fields[$name]['type'] = empty( $f_types[$i] ) ? $o_type : wc_clean( $f_types[$i] );
$fields[$name]['label'] = empty( $f_labels[$i] ) ? '' : wp_kses_post( trim( stripslashes( $f_labels[$i] ) ) );
$fields[$name]['access'] = empty( $f_access[$i] ) ? false : true;
$fields[$name]['placeholder'] = empty( $f_placeholder[$i] ) ? '' : wc_clean( stripslashes( $f_placeholder[$i] ) );
$fields[$name]['price'] = empty( $i_price[$i] ) ? '' : wc_clean( stripslashes( $i_price[$i] ) );
$fields[$name]['price_unit'] = empty( $i_price_unit[$i] ) ? '' : wc_clean( stripslashes( $i_price_unit[$i] ) );
$fields[$name]['price_type'] = empty( $i_price_type[$i] ) ? '' : wc_clean( stripslashes( $i_price_type[$i] ) );
$fields[$name]['taxable'] = empty( $i_taxable[$i] ) ? '' : wc_clean( stripslashes( $i_taxable[$i] ) );
$fields[$name]['tax_class'] = empty( $i_tax_class[$i] ) ? '' : wc_clean( stripslashes( $i_tax_class[$i] ) );
$fields[$name]['min_time'] = empty( $i_min_time[$i] ) ? '' : wc_clean( stripslashes( $i_min_time[$i] ) );
$fields[$name]['max_time'] = empty( $i_max_time[$i] ) ? '' : wc_clean( stripslashes( $i_max_time[$i] ) );
$fields[$name]['time_step'] = empty( $i_time_step[$i] ) ? '' : wc_clean( stripslashes( $i_time_step[$i] ) );
$fields[$name]['time_format'] = empty( $i_time_format[$i] ) ? '' : wc_clean( stripslashes( $i_time_format[$i] ) );
$fields[$name]['options_json'] = empty( $f_options[$i] ) ? '' : json_decode(urldecode($f_options[$i]),true);
$fields[$name]['maxlength'] = empty( $f_maxlength[$i] ) ? '' : wc_clean( stripslashes( $f_maxlength[$i] ) );
$fields[$name]['class'] = empty( $f_class[$i] ) ? array() : array_map( 'wc_clean', explode( ',', $f_class[$i] ) );
$fields[$name]['label_class'] = empty( $f_label_class[$i] ) ? array() : array_map( 'wc_clean', explode( ',', $f_label_class[$i] ) );
$fields[$name]['rules_action'] = empty( $f_rules_action[$i] ) ? '' : sanitize_text_field( $f_rules_action[$i] );
$fields[$name]['rules'] = empty( $f_rules[$i] ) ? '' : sanitize_text_field( $f_rules[$i] );
$fields[$name]['rules_action_ajax'] = empty( $f_rules_action_ajax[$i] ) ? '' : sanitize_text_field( $f_rules_action_ajax[$i] );
$fields[$name]['rules_ajax'] = empty( $f_rules_ajax[$i] ) ? '' : sanitize_text_field( $f_rules_ajax[$i] );
$fields[$name]['required'] = empty( $f_required[$i] ) ? false : true;
$fields[$name]['enabled'] = empty( $f_enabled[$i] ) ? false : true;
$fields[$name]['order'] = isset($f_order[$i]) && is_numeric($f_order[$i]) ? wc_clean( $f_order[$i] ) : '';
if (!in_array( $name, $this->locale_fields )){
$fields[$name]['validate'] = empty( $f_validation[$i] ) ? array() : explode( ',', $f_validation[$i] );
}
$fields[$name]['extoptions'] = empty( $f_extoptions[$i] ) ? array() : explode( ',', $f_extoptions[$i] );
if (!$this->is_default_field_name( $name )){
$fields[$name]['custom'] = true;
$fields[$name]['show_in_email'] = empty( $f_show_in_email[$i] ) ? false : true;
$fields[$name]['show_in_order'] = empty( $f_show_in_order[$i] ) ? false : true;
$fields[$name]['show_in_my_account'] = empty( $f_show_in_my_account[$i] ) ? false : true;
} else {
$fields[$name]['custom'] = false;
}
if ( is_numeric( $fields[ $name ]['order'] ) ) {
$priority_field = array(
'order' => (int) $fields[ $name ]['order'],
'custom' => ! empty( $fields[ $name ]['custom'] ),
);
if ( class_exists( 'WCFE_Utils_Block' ) ) {
$fields[ $name ]['priority'] = WCFE_Utils_Block::get_field_index( $priority_field );
} else {
$fields[ $name ]['priority'] = ( (int) $fields[ $name ]['order'] + 1 ) * 10;
}
}
$fields[$name]['label'] = $fields[$name]['label'];
$fields[$name]['placeholder'] = esc_html__($fields[$name]['placeholder'], 'woocommerce');
$fields[$name]['maxlength'] = esc_html__($fields[$name]['maxlength'], 'woocommerce');
}
uasort( $fields, array( $this, 'sort_fields_by_order' ) );
$updated = WCFE_Utils::update_checkout_fields( $section, $fields );
if ( $updated && class_exists( 'WCFE_Utils_I18n' ) ) {
WCFE_Utils_I18n::register_fields( $fields );
}
// update_option() returns false when the value is unchanged — still a successful save.
$response = array(
'success' => true,
'changed' => (bool) $updated,
'message' => $updated
? __( 'Your changes were saved.', 'wcfe' )
: __( 'No changes to save.', 'wcfe' ),
);
if ( $echo ) {
$class = $updated ? 'updated' : 'updated';
echo '' . esc_html( $response['message'] ) . '
';
}
return $response;
}
}