Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/01/2016 07:44:36 PM (11 years ago)
Author:
djpaul
Message:

Emails: move static token replacement function into its own function.

The token replacement, while intended for BP_Email, in theory could be used anywhere else, and having a common function would allow consistent filtering.
It also helps by reducing the amount of code in the BP_Email class, and therefore overall complexity.

See #6592

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-email.php

    r10489 r10491  
    244244
    245245                        case 'replace-tokens':
    246                                 $retval = self::replace_tokens( $retval, $this->get_tokens( 'raw' ) );
     246                                $retval = bp_core_replace_tokens_in_text( $retval, $this->get_tokens( 'raw' ) );
    247247                                // Fall through.
    248248
     
    949949                return apply_filters( 'bp_email_validate', $retval, $this );
    950950        }
    951 
    952 
    953         /*
    954          * Utility functions.
    955          *
    956          * Unlike other methods in this class, utility functions are not chainable.
    957          */
    958 
    959         /**
    960          * Replace all tokens in the input with appropriate values.
    961          *
    962          * Unlike most other methods in this class, this one is not chainable.
    963          *
    964          * @since 2.5.0
    965          *
    966          * @param string $text
    967          * @param array $tokens Token names and replacement values for the $text.
    968          * @return string
    969          */
    970         public static function replace_tokens( $text, $tokens ) {
    971                 $unescaped = array();
    972                 $escaped   = array();
    973 
    974                 foreach ( $tokens as $token => $value ) {
    975                         if ( is_callable( $value ) ) {
    976                                 $value = call_user_func( $value );
    977                         }
    978 
    979                         // Some tokens are objects or arrays for backwards compatibilty. See bp_core_deprecated_email_filters().
    980                         if ( ! is_scalar( $value ) ) {
    981                                 continue;
    982                         }
    983 
    984                         $unescaped[ '{{{' . $token . '}}}' ] = $value;
    985                         $escaped[ '{{' . $token . '}}' ]     = esc_html( $value );
    986                 }
    987 
    988                 $text = strtr( $text, $unescaped );  // Do first.
    989                 $text = strtr( $text, $escaped );
    990 
    991                 /**
    992                  * Filters text that has had tokens replaced.
    993                  *
    994                  * @since 2.5.0
    995                  *
    996                  * @param string $text
    997                  * @param array $tokens Token names and replacement values for the $text.
    998                  */
    999                 return apply_filters( 'bp_email_replace_tokens', $text, $tokens );
    1000         }
    1001951}
Note: See TracChangeset for help on using the changeset viewer.