From b6c4d9a852476b2db07e202d1fa5ccdde4d84cf8 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 6 May 2014 16:04:07 +0100 Subject: [PATCH] Trim commas and empty lines off address formats Fixes #5411 --- includes/class-wc-countries.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-countries.php b/includes/class-wc-countries.php index 20c4095ea21..e4a57990aeb 100644 --- a/includes/class-wc-countries.php +++ b/includes/class-wc-countries.php @@ -667,7 +667,7 @@ class WC_Countries { $full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state; // Substitute address parts into the string - $replace = apply_filters( 'woocommerce_formatted_address_replacements', array( + $replace = array_map( 'esc_html', apply_filters( 'woocommerce_formatted_address_replacements', array( '{first_name}' => $first_name, '{last_name}' => $last_name, '{name}' => $first_name . ' ' . $last_name, @@ -688,9 +688,7 @@ class WC_Countries { '{state_upper}' => strtoupper( $full_state ), '{postcode_upper}' => strtoupper( $postcode ), '{country_upper}' => strtoupper( $full_country ), - ), $args ) ; - - $replace = array_map( 'esc_html', $replace ); + ), $args ) ); $formatted_address = str_replace( array_keys( $replace ), $replace, $format ); @@ -698,13 +696,25 @@ class WC_Countries { $formatted_address = preg_replace( '/ +/', ' ', trim( $formatted_address ) ); $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address ); + // Break newlines apart and remove empty lines/trim commas and white space + $formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) ); + // Add html breaks - $formatted_address = nl2br( $formatted_address ); + $formatted_address = implode( '
', $formatted_address ); // We're done! return $formatted_address; } + /** + * trim white space and commans off a line + * @param string + * @return string + */ + private function trim_formatted_address_line( $line ) { + return trim( $line, ", " ); + } + /** * Returns the fields we show by default. This can be filtered later on.