From 5c5e6fb015a12d80dfce25b9cbade9f8271c24a6 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Fri, 3 Jul 2015 20:37:47 +0100 Subject: [PATCH] Refactor woocommerce_form_field so that the container, label and description are not repeated for every field type. --- includes/wc-template-functions.php | 175 +++++------------------------ 1 file changed, 27 insertions(+), 148 deletions(-) diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index f5b3815db16..73779245986 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -1610,12 +1610,6 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $args = wp_parse_args( $args, $defaults ); $args = apply_filters( 'woocommerce_form_field_args', $args, $key, $value ); - if ( ( ! empty( $args['clear'] ) ) ) { - $after = '
'; - } else { - $after = ''; - } - if ( $args['required'] ) { $args['class'][] = 'validate-required'; $required = ' *'; @@ -1648,6 +1642,10 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { } } + $field = ''; + $label_id = $args['id']; + $field_container = '

%3$s

'; + switch ( $args['type'] ) { case 'country' : @@ -1655,27 +1653,13 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { if ( sizeof( $countries ) == 1 ) { - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } - $field .= '' . current( array_values( $countries ) ) . ''; $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - } else { - $field = '

' - . '' - . '' . ''; foreach ( $countries as $ckey => $cvalue ) { @@ -1686,12 +1670,6 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - } break; @@ -1704,25 +1682,12 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { if ( is_array( $states ) && empty( $states ) ) { - $field = ''; - if ( $args['label'] ) { - $field .= ''; - } $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - } elseif ( is_array( $states ) ) { - $field = '

'; - - if ( $args['label'] ) - $field .= ''; $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - } else { - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - } break; case 'textarea' : - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } - $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - break; case 'checkbox' : - $field = '

-

' . $after; - break; case 'password' : - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } - $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - break; case 'text' : - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } - $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - break; case 'email' : - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } - $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - break; case 'tel' : - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } - $field .= ''; - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; - break; case 'select' : @@ -1871,31 +1752,15 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { $options .= ''; } - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } - $field .= ''; - - if ( $args['description'] ) { - $field .= '' . esc_attr( $args['description'] ) . ''; - } - - $field .= '

' . $after; } break; case 'radio' : - $field = '

'; - - if ( $args['label'] ) { - $field .= ''; - } + $label_id = current( array_keys( $args['options'] ) ); if ( ! empty( $args['options'] ) ) { foreach ( $args['options'] as $option_key => $option_text ) { @@ -1904,14 +1769,28 @@ if ( ! function_exists( 'woocommerce_form_field' ) ) { } } - $field .= '

' . $after; - break; - default : + } - $field = ''; + if ( ! empty( $field ) ) { + $field_html = ''; - break; + if ( $args['label'] && 'checkbox' != $args['type'] ) { + $field_html .= ''; + } + + $field_html .= $field; + + if ( $args['description'] ) { + $field_html .= '' . esc_attr( $args['description'] ) . ''; + } + + $container_class = 'form-row ' . esc_attr( implode( ' ', $args['class'] ) ); + $container_id = esc_attr( $args['id'] ) . '_field'; + + $after = ! empty( $args['clear'] ) ? '
' : ''; + + $field = sprintf( $field_container, $container_class, $container_id, $field_html ) . $after; } $field = apply_filters( 'woocommerce_form_field_' . $args['type'], $field, $key, $args, $value );