Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HTML tag processor to strip formatted addresses of tags #46775

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ protected static function get_order_personal_data( $order ) {
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 ) ) ) {
Expand Down
20 changes: 20 additions & 0 deletions plugins/woocommerce/includes/wc-formatting-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1574,3 +1574,23 @@ function wc_sanitize_endpoint_slug( $raw_value ) {
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;
}
4 changes: 2 additions & 2 deletions plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ public function render_billing_address_column( WC_Order $order ): void {
$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 */
Expand All @@ -1102,7 +1102,7 @@ public function render_shipping_address_column( WC_Order $order ): void {
$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>';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in plugins/woocommerce/templates/emails/plain/email-addresses.php

View workflow job for this annotation

GitHub Actions / Analyze Branch Changes

TEMPLATES

This template may require a version bump! Expected 8.9.0
/**
* Email Addresses (plain)
*
Expand All @@ -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
Expand All @@ -45,7 +45,7 @@

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
Expand Down