array( 'label' => __( 'Name', 'acs-contact-form' ), 'enabled' => '1', 'required' => '1', 'fixed' => true, ), 'email' => array( 'label' => __( 'Email address', 'acs-contact-form' ), 'enabled' => '1', 'required' => '1', 'fixed' => true, ), 'message' => array( 'label' => __( 'Message', 'acs-contact-form' ), 'enabled' => '1', 'required' => '1', 'fixed' => true, ), 'tel' => array( 'label' => __( 'Phone number', 'acs-contact-form' ), 'enabled' => '0', 'required' => '0', 'fixed' => false, ), 'company' => array( 'label' => __( 'Company', 'acs-contact-form' ), 'enabled' => '0', 'required' => '0', 'fixed' => false, ), 'address' => array( 'label' => __( 'Address', 'acs-contact-form' ), 'enabled' => '0', 'required' => '0', 'fixed' => false, ), 'subject' => array( 'label' => __( 'Subject', 'acs-contact-form' ), 'enabled' => '0', 'required' => '0', 'fixed' => false, ), ); } /** * Default attachment settings. * * @return array */ function acscf_get_default_file_settings() { return array( 'enabled' => '0', 'label' => __( 'Attach a file', 'acs-contact-form' ), 'types' => 'jpg, jpeg, png, gif, pdf, doc, docx', 'max_size' => '5', 'max_count' => '3', 'required' => '0', ); } /** * Default mail settings. * * @return array */ function acscf_get_default_email_settings() { return array( 'to' => get_option( 'admin_email' ), 'subject' => '【{form_name}】{subject}', 'auto_reply' => '0', 'auto_reply_subject' => __( '[Auto-reply] Thank you for your inquiry', 'acs-contact-form' ), 'auto_reply_body' => __( "Hello {name},\n\nThank you for getting in touch.\nWe have received your message as follows.\n\n---\n{all_fields}\n---\n\nOne of our team will get back to you shortly.", 'acs-contact-form' ), ); } /** * Fill in the missing mail settings. * * @param mixed $email_settings Stored settings. * @return array */ function acscf_normalize_email_settings( $email_settings ) { return wp_parse_args( is_array( $email_settings ) ? $email_settings : array(), acscf_get_default_email_settings() ); } /** * Default design settings. * * @return array */ function acscf_get_default_design_settings() { return array( 'primary_color' => '#0071e3', 'text_color' => '#1d1d1f', 'label_color' => '#1d1d1f', 'bg_color' => '#ffffff', 'input_bg_color' => '#fbfbfd', 'input_text_color' => '#1d1d1f', 'input_border_color' => '#d2d2d7', 'button_bg_color' => '#0071e3', 'button_text_color' => '#ffffff', 'required_bg_color' => '#0071e3', 'required_text_color' => '#ffffff', 'error_color' => '#e53935', 'radius' => '8', 'button_radius' => '20', ); } /** * Fill in the missing design settings. * * @param mixed $design Stored settings. * @return array */ function acscf_normalize_design_settings( $design ) { return wp_parse_args( is_array( $design ) ? $design : array(), acscf_get_default_design_settings() ); } /** * Default order of the fields. * * @param array $standard_fields Standard fields. * @param array $custom_fields Custom fields. * @param array $file_settings Attachment settings. * @return array */ function acscf_build_default_field_order( $standard_fields, $custom_fields, $file_settings ) { $order = array(); $preferred = array( 'name', 'company', 'address', 'tel', 'email', 'subject', 'message' ); foreach ( $preferred as $key ) { if ( isset( $standard_fields[ $key ] ) ) { $order[] = 'standard:' . $key; } } if ( is_array( $custom_fields ) ) { foreach ( array_keys( $custom_fields ) as $index ) { $order[] = 'custom:' . (int) $index; } } if ( ! empty( $file_settings ) ) { $order[] = 'file'; } return $order; } /** * Clean an order coming from the builder. * * @param mixed $raw_order Raw order. * @param array $standard_fields Standard fields. * @param array $custom_fields Custom fields. * @param array $custom_index_map Map of posted index to stored index. * @return array */ function acscf_sanitize_field_order( $raw_order, $standard_fields, $custom_fields, $custom_index_map = array() ) { $clean = array(); if ( is_array( $raw_order ) ) { foreach ( $raw_order as $item ) { $item = sanitize_text_field( $item ); if ( 'file' === $item ) { $clean[] = 'file'; continue; } if ( 0 === strpos( $item, 'standard:' ) ) { $key = substr( $item, strlen( 'standard:' ) ); if ( isset( $standard_fields[ $key ] ) ) { $clean[] = 'standard:' . $key; } continue; } if ( 0 === strpos( $item, 'custom:' ) ) { $raw_index = substr( $item, strlen( 'custom:' ) ); $mapped = array_key_exists( $raw_index, $custom_index_map ) ? $custom_index_map[ $raw_index ] : (int) $raw_index; if ( isset( $custom_fields[ $mapped ] ) ) { $clean[] = 'custom:' . (int) $mapped; } } } } foreach ( acscf_build_default_field_order( $standard_fields, $custom_fields, acscf_get_default_file_settings() ) as $fallback ) { if ( ! in_array( $fallback, $clean, true ) ) { $clean[] = $fallback; } } return array_values( array_unique( $clean ) ); } /** * Effective field order of a form. * * @param int $form_id Form post ID. * @param array $standard_fields Standard fields. * @param array $custom_fields Custom fields. * @param array $file_settings Attachment settings. * @return array */ function acscf_get_field_order( $form_id, $standard_fields, $custom_fields, $file_settings ) { $field_order = get_post_meta( $form_id, '_acscf_field_order', true ); if ( ! is_array( $field_order ) || empty( $field_order ) ) { return acscf_build_default_field_order( $standard_fields, $custom_fields, $file_settings ); } return acscf_sanitize_field_order( $field_order, $standard_fields, is_array( $custom_fields ) ? $custom_fields : array() ); } /** * The form builder. * * @param WP_Post $post Current form. * @return void */ function acscf_builder_meta_box_html( $post ) { wp_nonce_field( 'acscf_save_meta_box_data', 'acscf_meta_box_nonce' ); $standard_fields = get_post_meta( $post->ID, '_acscf_standard_fields', true ); if ( empty( $standard_fields ) ) { $standard_fields = acscf_get_default_standard_fields(); } $custom_fields = get_post_meta( $post->ID, '_acscf_custom_fields', true ); if ( ! is_array( $custom_fields ) ) { $custom_fields = array(); } $file_settings = get_post_meta( $post->ID, '_acscf_file_settings', true ); if ( empty( $file_settings ) ) { $file_settings = acscf_get_default_file_settings(); } $email_settings = acscf_normalize_email_settings( get_post_meta( $post->ID, '_acscf_email_settings', true ) ); $field_order = acscf_get_field_order( $post->ID, $standard_fields, $custom_fields, $file_settings ); ?>

↕

$field ) : ?>
>

MB
>

' . esc_html__( 'Settings', 'acs-contact-form' ) . '' ); ?>

ID, '_acscf_design', true ) ); $colors = array( 'primary_color' => __( 'Accent color', 'acs-contact-form' ), 'text_color' => __( 'Text color', 'acs-contact-form' ), 'label_color' => __( 'Label color', 'acs-contact-form' ), 'bg_color' => __( 'Background color', 'acs-contact-form' ), 'input_bg_color' => __( 'Input background color', 'acs-contact-form' ), 'input_text_color' => __( 'Input text color', 'acs-contact-form' ), 'input_border_color' => __( 'Input border color', 'acs-contact-form' ), 'button_bg_color' => __( 'Button background color', 'acs-contact-form' ), 'button_text_color' => __( 'Button text color', 'acs-contact-form' ), 'required_bg_color' => __( 'Required badge background color', 'acs-contact-form' ), 'required_text_color' => __( 'Required badge text color', 'acs-contact-form' ), 'error_color' => __( 'Error text color', 'acs-contact-form' ), ); $defaults = acscf_get_default_design_settings(); ?>

$label ) : ?>

px

px

$field ) { if ( ! is_array( $field ) ) { continue; } $key = sanitize_key( $key ); $sanitized_sf[ $key ] = array( 'label' => isset( $field['label'] ) ? $field['label'] : $key, 'enabled' => isset( $field['enabled'] ) ? '1' : '0', 'required' => isset( $field['required'] ) ? '1' : '0', 'fixed' => isset( $field['fixed'] ) && '1' === $field['fixed'], ); } foreach ( array( 'name', 'email', 'message' ) as $fixed_key ) { if ( isset( $sanitized_sf[ $fixed_key ] ) && $sanitized_sf[ $fixed_key ]['fixed'] ) { $sanitized_sf[ $fixed_key ]['enabled'] = '1'; } } update_post_meta( $post_id, '_acscf_standard_fields', $sanitized_sf ); } // Custom fields. $sanitized_cf = array(); $custom_index_map = array(); if ( isset( $_POST['acscf_custom'] ) && is_array( $_POST['acscf_custom'] ) ) { $posted_custom = map_deep( wp_unslash( $_POST['acscf_custom'] ), 'sanitize_textarea_field' ); foreach ( $posted_custom as $posted_index => $custom_field ) { if ( ! is_array( $custom_field ) ) { continue; } $label = isset( $custom_field['label'] ) ? sanitize_text_field( $custom_field['label'] ) : ''; if ( '' === $label ) { continue; } $type = isset( $custom_field['type'] ) ? sanitize_key( $custom_field['type'] ) : 'text'; $new_index = count( $sanitized_cf ); $sanitized_cf[] = array( 'label' => $label, 'type' => in_array( $type, array( 'text', 'textarea', 'select', 'radio', 'checkbox' ), true ) ? $type : 'text', 'options' => isset( $custom_field['options'] ) ? $custom_field['options'] : '', 'placeholder' => isset( $custom_field['placeholder'] ) ? sanitize_text_field( $custom_field['placeholder'] ) : '', 'required' => isset( $custom_field['required'] ) ? '1' : '0', ); $custom_index_map[ sanitize_text_field( $posted_index ) ] = $new_index; } } update_post_meta( $post_id, '_acscf_custom_fields', $sanitized_cf ); // Attachment settings. if ( isset( $_POST['acscf_file'] ) && is_array( $_POST['acscf_file'] ) ) { $posted_file = map_deep( wp_unslash( $_POST['acscf_file'] ), 'sanitize_text_field' ); $sanitized_file = array( 'enabled' => isset( $posted_file['enabled'] ) ? '1' : '0', 'label' => isset( $posted_file['label'] ) ? $posted_file['label'] : '', 'types' => isset( $posted_file['types'] ) ? $posted_file['types'] : '', 'max_size' => isset( $posted_file['max_size'] ) ? max( 1, min( 20, (int) $posted_file['max_size'] ) ) : 5, 'max_count' => isset( $posted_file['max_count'] ) ? max( 1, min( 10, (int) $posted_file['max_count'] ) ) : 3, 'required' => isset( $posted_file['required'] ) ? '1' : '0', ); } else { $sanitized_file = acscf_get_default_file_settings(); } update_post_meta( $post_id, '_acscf_file_settings', $sanitized_file ); // Field order. $posted_order = array(); if ( isset( $_POST['acscf_order'] ) && is_array( $_POST['acscf_order'] ) ) { $posted_order = map_deep( wp_unslash( $_POST['acscf_order'] ), 'sanitize_text_field' ); } update_post_meta( $post_id, '_acscf_field_order', acscf_sanitize_field_order( $posted_order, $sanitized_sf, $sanitized_cf, $custom_index_map ) ); // Mail settings. if ( isset( $_POST['acscf_email'] ) && is_array( $_POST['acscf_email'] ) ) { $posted_email = map_deep( wp_unslash( $_POST['acscf_email'] ), 'sanitize_textarea_field' ); update_post_meta( $post_id, '_acscf_email_settings', array( 'to' => isset( $posted_email['to'] ) ? sanitize_email( $posted_email['to'] ) : get_option( 'admin_email' ), 'subject' => isset( $posted_email['subject'] ) ? sanitize_text_field( $posted_email['subject'] ) : '', 'auto_reply' => isset( $posted_email['auto_reply'] ) ? '1' : '0', 'auto_reply_subject' => isset( $posted_email['auto_reply_subject'] ) ? sanitize_text_field( $posted_email['auto_reply_subject'] ) : '', 'auto_reply_body' => isset( $posted_email['auto_reply_body'] ) ? $posted_email['auto_reply_body'] : '', ) ); } // Design settings. if ( isset( $_POST['acscf_design'] ) && is_array( $_POST['acscf_design'] ) ) { $posted_design = map_deep( wp_unslash( $_POST['acscf_design'] ), 'sanitize_text_field' ); $defaults = acscf_get_default_design_settings(); $sanitized_design = array( 'radius' => isset( $posted_design['radius'] ) ? max( 0, min( 60, (int) $posted_design['radius'] ) ) : (int) $defaults['radius'], 'button_radius' => isset( $posted_design['button_radius'] ) ? max( 0, min( 60, (int) $posted_design['button_radius'] ) ) : (int) $defaults['button_radius'], ); foreach ( array_keys( $defaults ) as $key ) { if ( 'radius' === $key || 'button_radius' === $key ) { continue; } $value = isset( $posted_design[ $key ] ) ? sanitize_hex_color( $posted_design[ $key ] ) : ''; $sanitized_design[ $key ] = $value ? $value : $defaults[ $key ]; } update_post_meta( $post_id, '_acscf_design', $sanitized_design ); } } add_action( 'save_post', 'acscf_save_meta_box_data' );