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_filter( 'woocommerce_form_field_select', array($this, 'wcfe_checkout_fields_select_field'), 10, 4 );
}
/**
* menu function.
*/
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'));
}
/**
* scripts function.
*/
function enqueue_admin_scripts() {
wp_enqueue_style ('wcfe-style', plugins_url('/assets/css/wcfe-style.css', dirname(__FILE__)));
wp_enqueue_script( 'wcfe-admin-script', plugins_url('/assets/js/wcfe-admin.js', dirname(__FILE__)), array('jquery','jquery-ui-dialog', 'jquery-ui-sortable',
'woocommerce_admin', 'select2', 'jquery-tiptip'), WCFE_VERSION, true );
wp_localize_script( 'wcfe-admin-script', 'WcfeAdmin', array(
'MSG_INVALID_NAME' => 'NAME contains only following ([a-z,A-Z]), digits ([0-9]) and dashes ("-") underscores ("_")'
));
}
/**
* wc_checkout_fields_scripts function.
*
*/
function wc_checkout_fields_scripts() {
global $wp_scripts;
if ( is_checkout() || is_account_page()) {
wp_enqueue_script( 'wc-checkout-editor-frontend', plugins_url('/assets/js/checkout.js', dirname(__FILE__)), array( 'jquery', 'jquery-ui-datepicker' ), WC()->version, true );
$jquery_version = isset( $wp_scripts->registered['jquery-ui-core']->ver ) ? $wp_scripts->registered['jquery-ui-core']->ver : '1.9.2';
wp_enqueue_style( 'jquery-ui-style', '//ajax.googleapis.com/ajax/libs/jqueryui/' . $jquery_version . '/themes/smoothness/jquery-ui.css' );
$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() )
) );
}
}
/**
* wcfe_checkout_fields_multiselect_field function.
*
* @param string $field (default: '')
* @param mixed $key
* @param mixed $args
* @param mixed $value
*/
function wcfe_checkout_fields_select_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'] ) . '"' : '';
$options = '';
$options .= '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
$options .= '';
}
$field = '';
if ( $args['label'] ) {
$field .= '';
}
$class = '';
$field .= '
' . $after;
}
return $field;
}
/**
* add_screen_id function.
*/
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.
*/
function reset_checkout_fields() {
delete_option('wc_fields_billing');
delete_option('wc_fields_shipping');
delete_option('wc_fields_additional');
echo ''. esc_html__('SUCCESS: Checkout fields successfully reset', 'wcfe') .'
';
}
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;
}
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.
*/
public function save_data($order_id, $posted){
$types = array('billing', 'shipping', 'additional');
foreach($types as $type){
$fields = $this->get_fields($type);
foreach($fields as $name => $field){
if(isset($field['custom']) && $field['custom'] && isset($posted[$name])){
$value = wc_clean($posted[$name]);
if($value){
update_post_meta($order_id, $name, $value);
}
}
}
}
}
public static function get_fields($key){
$fields = array_filter(get_option('wc_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', 'woocommerce'),
'placeholder' => _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce')
)
);
}
else if($key === 'account'){
$fields = array(
'account_username' => array(
'type' => 'text',
'label' => esc_html__('Email address', 'woocommerce')
),
'account_password' => array(
'type' => 'password',
'label' => esc_html__('Password', 'woocommerce')
)
);
}
}
return $fields;
}
function sort_fields_by_order($a, $b){
if(!isset($a['order']) || $a['order'] == $b['order']){
return 0;
}
return ($a['order'] < $b['order']) ? -1 : 1;
}
function get_field_types(){
return array(
'text' => 'Text',
'textarea' => 'Textarea',
'select' => 'Select'
);
}
/*
* New field form popup
*/
function wcfe_new_field_form_pp(){
$field_types = $this->get_field_types(); ?>
get_field_types(); ?>
'Checkout & Account Fields' );
$tab = isset( $_GET['tab'] ) ? esc_attr( $_GET['tab'] ) : 'fields';
$sections = ''; $section = '';
if($tab === 'fields') {
$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 ''.$value.'';
}
echo '
';
if(!empty($sections)){
echo '';
$size = sizeof($sections); $i = 0;
foreach( $sections as $key ) {
$i++;
$active = ( $key == $section ) ? 'current' : '';
$url = 'admin.php?page=checkout_form_editor&tab=fields§ion='.$key;
echo '- ';
echo ''.ucwords($key).' '.esc_html__('Fields', 'wcfe').'';
echo ($size > $i) ? ' ' : '';
echo '
';
}
echo '
';
}
if( tl_fields()->is_not_paying() ){
?>
get_current_tab();
$section = '';
if($tab === 'fields'){
$section = isset( $_GET['section'] ) ? esc_attr( $_GET['section'] ) : 'billing';
}
return $section;
}
function render_checkout_fields_heading_row() {
?>
|
|
|
|
|
|
|
|
|
|
|
|
get_current_tab();
if($tab === 'fields'){
$this->checkout_form_field_editor();
}
}
function checkout_form_field_editor() {
$section = $this->get_current_section();
echo '
';
$this->render_tabs_and_sections();
if ( isset( $_POST['save_fields'] ) )
echo $this->save_options( $section );
if ( isset( $_POST['reset_fields'] ) )
echo $this->reset_checkout_fields();
global $supress_field_modification;
$supress_field_modification = false;
if( $section != 'account' ) { ?>
wcfe_new_field_form_pp();
$this->wcfe_edit_field_form_pp();
?>
get_fields( $section );
$fields = $o_fields;
$f_order = ! empty( $_POST['f_order'] ) ? $_POST['f_order'] : array();
$f_names = ! empty( $_POST['f_name'] ) ? $_POST['f_name'] : array();
$f_names_new = ! empty( $_POST['f_name_new'] ) ? $_POST['f_name_new'] : array();
$f_types = ! empty( $_POST['f_type'] ) ? $_POST['f_type'] : array();
$f_labels = ! empty( $_POST['f_label'] ) ? $_POST['f_label'] : array();
$f_placeholder = ! empty( $_POST['f_placeholder'] ) ? $_POST['f_placeholder'] : array();
$f_maxlength = ! empty( $_POST['f_maxlength'] ) ? $_POST['f_maxlength'] : array();
if(isset($_POST['f_options'])){
$f_options = ! empty( $_POST['f_options'] ) ? $_POST['f_options'] : array();
}
$f_class = ! empty( $_POST['f_class'] ) ? $_POST['f_class'] : array();
$f_required = ! empty( $_POST['f_required'] ) ? $_POST['f_required'] : array();
$f_enabled = ! empty( $_POST['f_enabled'] ) ? $_POST['f_enabled'] : array();
$f_show_in_email = ! empty( $_POST['f_show_in_email'] ) ? $_POST['f_show_in_email'] : array();
$f_show_in_order = ! empty( $_POST['f_show_in_order'] ) ? $_POST['f_show_in_order'] : array();
$f_validation = ! empty( $_POST['f_validation'] ) ? $_POST['f_validation'] : array();
$f_deleted = ! empty( $_POST['f_deleted'] ) ? $_POST['f_deleted'] : array();
$f_position = ! empty( $_POST['f_position'] ) ? $_POST['f_position'] : array();
$f_display_options = ! empty( $_POST['f_display_options'] ) ? $_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] ) ) ) );
if(!empty($f_deleted[$i]) && $f_deleted[$i] == 1){
unset( $fields[$name] );
continue;
}
// Check reserved names
if($this->is_reserved_field_name( $new_name )){
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';
$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]['placeholder'] = empty( $f_placeholder[$i] ) ? '' : wc_clean( stripslashes( $f_placeholder[$i] ) );
$fields[$name]['options'] = empty( $f_options[$i] ) ? array() : array_map( 'wc_clean', explode( ',', trim(stripslashes($f_options[$i])) ) );
$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]['required'] = empty( $f_required[$i] ) ? false : true;
$fields[$name]['enabled'] = empty( $f_enabled[$i] ) ? false : true;
$fields[$name]['order'] = empty( $f_order[$i] ) ? '' : wc_clean( $f_order[$i] );
if (!empty( $fields[$name]['options'] )) {
$fields[$name]['options'] = array_combine( $fields[$name]['options'], $fields[$name]['options'] );
}
if (!in_array( $name, $this->locale_fields )){
$fields[$name]['validate'] = empty( $f_validation[$i] ) ? array() : explode( ',', $f_validation[$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;
} else {
$fields[$name]['custom'] = false;
}
$fields[$name]['label'] = esc_html__($fields[$name]['label'], 'woocommerce');
$fields[$name]['placeholder'] = esc_html__($fields[$name]['placeholder'], 'woocommerce');
}
uasort( $fields, array( $this, 'sort_fields_by_order' ) );
$result = update_option( 'wc_fields_' . $section, $fields );
if ( $result == true ) {
echo '' . esc_html__( 'Your changes were saved.', 'wcfe' ) . '
';
} else {
echo ' ' . esc_html__( 'Your changes were not saved due to an error (or you made none!).', 'wcfe' ) . '
';
}
}
}