Skip to content

Commit cbe4e26

Browse files
committed
Legacy find/replace handling
Closes woocommerce#16705
1 parent 5cb9ddf commit cbe4e26

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

includes/emails/class-wc-email.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,23 @@ public function handle_multipart( $mailer ) {
250250
/**
251251
* Format email string.
252252
*
253-
* @param mixed $string
253+
* @param mixed $string Text to replace placeholders in.
254254
* @return string
255255
*/
256256
public function format_string( $string ) {
257-
// Legacy placeholders. @todo deprecate in 4.0.0.
257+
$find = array_keys( $this->placeholders );
258+
$replace = array_values( $this->placeholders );
259+
260+
// If using legacy find replace, add those to our find/replace arrays first. @todo deprecate in 4.0.0.
261+
if ( isset( $this->find, $this->replace ) ) {
262+
$find = array_merge( $this->find, $find );
263+
$replace = array_merge( $this->replace, $replace );
264+
}
265+
266+
// If using the older style filters for find and replace, ensure the array is associative and then pass through filters. @todo deprecate in 4.0.0.
258267
if ( has_filter( 'woocommerce_email_format_string_replace' ) || has_filter( 'woocommerce_email_format_string_find' ) ) {
259-
$legacy_find = array();
260-
$legacy_replace = array();
268+
$legacy_find = isset( $this->find ) ? $this->find : array();
269+
$legacy_replace = isset( $this->replace ) ? $this->replace : array();
261270

262271
foreach ( $this->placeholders as $find => $replace ) {
263272
$legacy_key = sanitize_title( str_replace( '_', '-', trim( $find, '{}' ) ) );
@@ -267,7 +276,13 @@ public function format_string( $string ) {
267276

268277
$string = str_replace( apply_filters( 'woocommerce_email_format_string_find', $legacy_find, $this ), apply_filters( 'woocommerce_email_format_string_replace', $legacy_replace, $this ), $string );
269278
}
270-
return str_replace( $this->find, $this->replace, $string );
279+
280+
/**
281+
* woocommerce_email_format_string filter for main find/replace code.
282+
*
283+
* @since 3.2.0
284+
*/
285+
return apply_filters( 'woocommerce_email_format_string', str_replace( $find, $replace, $string ), $this );
271286
}
272287

273288
/**

0 commit comments

Comments
 (0)