Skip to:
Content

BuddyPress.org

Changeset 10491


Ignore:
Timestamp:
02/01/2016 07:44:36 PM (10 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

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r10487 r10491  
    30483048        ), $object );
    30493049}
     3050
     3051/**
     3052 * Replace all tokens in the input text with appropriate values.
     3053 *
     3054 * Intended for use with the email system introduced in BuddyPress 2.5.0.
     3055 *
     3056 * @since 2.5.0
     3057 *
     3058 * @param string $text
     3059 * @param array $tokens Token names and replacement values for the $text.
     3060 * @return string
     3061 */
     3062function bp_core_replace_tokens_in_text( $text, $tokens ) {
     3063        $unescaped = array();
     3064        $escaped   = array();
     3065
     3066        foreach ( $tokens as $token => $value ) {
     3067                if ( is_callable( $value ) ) {
     3068                        $value = call_user_func( $value );
     3069                }
     3070
     3071                // Tokens could be objects or arrays.
     3072                if ( ! is_scalar( $value ) ) {
     3073                        continue;
     3074                }
     3075
     3076                $unescaped[ '{{{' . $token . '}}}' ] = $value;
     3077                $escaped[ '{{' . $token . '}}' ]     = esc_html( $value );
     3078        }
     3079
     3080        $text = strtr( $text, $unescaped );  // Do first.
     3081        $text = strtr( $text, $escaped );
     3082
     3083        /**
     3084         * Filters text that has had tokens replaced.
     3085         *
     3086         * @since 2.5.0
     3087         *
     3088         * @param string $text
     3089         * @param array $tokens Token names and replacement values for the $text.
     3090         */
     3091        return apply_filters( 'bp_core_replace_tokens_in_text', $text, $tokens );
     3092}
  • 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.