Use HTML tag processor to strip formatted addresses of tags

This commit is contained in:
Joshua Flowers 2024-04-20 15:57:04 +02:00
parent 55f878021e
commit 1ee4fac758
4 changed files with 25 additions and 5 deletions

View File

@ -279,7 +279,7 @@ class WC_Privacy_Exporters {
break;
case 'formatted_billing_address':
case 'formatted_shipping_address':
$value = preg_replace( '#<br\s*/?>#i', ', ', $order->{"get_$prop"}() );
$value = wc_get_address_plain_text( $order->{"get_$prop"}() );
break;
default:
if ( is_callable( array( $order, 'get_' . $prop ) ) ) {

View File

@ -1574,3 +1574,23 @@ add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_ed
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_payment_methods_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_myaccount_lost_password_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_logout_endpoint', 'wc_sanitize_endpoint_slug', 10, 1 );
/**
* Get the address in plain text, stripped of tags.
*
* @param $address string Address to format.
* @return string
*/
function wc_get_address_plain_text( $address, $replace = ', ' ) {
$p = new WP_HTML_Tag_Processor( $address );
$content = '';
while ( $p->next_token() ) {
if( 'BR' === $p->get_tag() ) {
$content .= $replace;
continue;
}
$token = $p->get_modifiable_text();
$content .= $token;
}
return $content;
}

View File

@ -1080,7 +1080,7 @@ class ListTable extends WP_List_Table {
$address = $order->get_formatted_billing_address();
if ( $address ) {
echo esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) );
echo wc_get_address_plain_text( $address );
if ( $order->get_payment_method() ) {
/* translators: %s: payment method */
@ -1102,7 +1102,7 @@ class ListTable extends WP_List_Table {
$address = $order->get_formatted_shipping_address();
if ( $address ) {
echo '<a target="_blank" href="' . esc_url( $order->get_shipping_address_map_url() ) . '">' . esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) ) . '</a>';
echo '<a target="_blank" href="' . esc_url( $order->get_shipping_address_map_url() ) . '">' . wc_get_address_plain_text( $address ) . '</a>';
if ( $order->get_shipping_method() ) {
/* translators: %s: shipping method */
echo '<span class="description">' . sprintf( esc_html__( 'via %s', 'woocommerce' ), esc_html( $order->get_shipping_method() ) ) . '</span>';

View File

@ -18,7 +18,7 @@
defined( 'ABSPATH' ) || exit;
echo "\n" . esc_html( wc_strtoupper( esc_html__( 'Billing address', 'woocommerce' ) ) ) . "\n\n";
echo preg_replace( '#<br\s*/?>#i', "\n", $order->get_formatted_billing_address() ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo wc_get_address_plain_text( $order->get_formatted_billing_address() ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ( $order->get_billing_phone() ) {
echo $order->get_billing_phone() . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
@ -45,7 +45,7 @@ if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() ) {
if ( $shipping ) {
echo "\n" . esc_html( wc_strtoupper( esc_html__( 'Shipping address', 'woocommerce' ) ) ) . "\n\n";
echo preg_replace( '#<br\s*/?>#i', "\n", $shipping ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo wc_get_address_plain_text( $shipping, "\n" ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ( $order->get_shipping_phone() ) {
echo $order->get_shipping_phone() . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped