Skip to:
Content

bbPress.org

Changeset 6751


Ignore:
Timestamp:
12/08/2017 02:09:08 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Intercept: first pass intercept API.

This change introduces 3 new functions for generating a default intercept value and comparing against it in specific places. If the return value differs from the default intercept value, we know that function call was intercepted by a filter, and that value will become the new return value without executing the remaining part of the function.

See #3184.

Location:
trunk/src/includes
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/abstraction.php

    r6739 r6751  
    7474}
    7575
     76/** Globals *******************************************************************/
     77
    7678/**
    7779 * Lookup and return a global variable
     
    144146    return bbp_get_global_object( 'wpdb', 'WPDB' );
    145147}
     148
     149/** Pagination ****************************************************************/
    146150
    147151/**
     
    302306}
    303307
     308/** Multisite *****************************************************************/
     309
    304310/**
    305311 * Is this a large bbPress installation?
     
    359365 * @since 2.6.0 bbPress (r6733)
    360366 */
    361 function bbp_restore_current_site( ) {
     367function bbp_restore_current_site() {
    362368
    363369    // Switch back to the original site
     
    365371        restore_current_blog();
    366372    }
     373}
     374
     375/** Interception **************************************************************/
     376
     377/**
     378 * Generate a default intercept value.
     379 *
     380 * @since 2.6.0
     381 *
     382 * @staticvar mixed $rand Null by default, random string on first call
     383 *
     384 * @return string
     385 */
     386function bbp_default_intercept() {
     387    static $rand = null;
     388
     389    // Generate a new random and unique string
     390    if ( null === $rand ) {
     391
     392        // If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
     393        $algo = function_exists( 'hash' )
     394            ? 'sha256'
     395            : 'sha1';
     396
     397        // Old WP installs may not have AUTH_SALT defined.
     398        $salt = defined( 'AUTH_SALT' ) && AUTH_SALT
     399            ? AUTH_SALT
     400            : (string) wp_rand();
     401
     402        // Create unique ID
     403        $rand = hash_hmac( $algo, uniqid( $salt, true ), $salt );
     404    }
     405
     406    // Return random string (from locally static variable)
     407    return $rand;
     408}
     409
     410/**
     411 * Whether a value has been intercepted
     412 *
     413 * @since 2.6.0
     414 *
     415 * @param bool $value
     416 */
     417function bbp_is_intercepted( $value = '' ) {
     418    return ( bbp_default_intercept() === $value );
     419}
     420
     421/**
     422 * Allow interception of a method or function call.
     423 *
     424 * @since 2.6.0
     425 *
     426 * @param string $action Typically the name of the caller function
     427 * @param array  $args   Typically the results of caller function func_get_args()
     428 *
     429 * @return mixed         Intercept results. Default bbp_default_intercept().
     430 */
     431function bbp_maybe_intercept( $action = '', $args = array() ) {
     432
     433    // Backwards compatibility juggle
     434    $hook      = ( false === strpos( $action, 'pre_' ) )
     435        ? "pre_{$action}"
     436        : $action;
     437
     438    // Sanitize the hook same
     439    $key       = sanitize_key( $hook );
     440
     441    // Default return value
     442    $default   = bbp_default_intercept();
     443
     444    // Filter and return
     445    return apply_filters( $key, $default, extract( (array) $args ) );
    367446}
    368447
  • trunk/src/includes/core/options.php

    r6705 r6751  
    263263    }
    264264
    265     // Bail if strategy is overloaded to false|null
    266     $strategy = apply_filters( 'bbp_pre_load_options', 'notoptions' );
    267     if ( empty( $strategy ) ) {
    268         return;
     265    // Maybe intercept
     266    $intercept = bbp_maybe_intercept( __FUNCTION__, 'notoptions' );
     267    if ( bbp_is_intercepted( $intercept ) ) {
     268        return $intercept;
    269269    }
    270270
  • trunk/src/includes/extend/buddypress/functions.php

    r6674 r6751  
    794794    }
    795795
    796     /**
    797      * Overrides the formatted activity action new activity string.
    798      *
    799      * @since 2.6.0 bbPress (r6370)
    800      *
    801      * @param string               $activity_action Activity action string value
    802      * @param string               $type            The type of post. Expects `topic` or `reply`.
    803      * @param string               $action          The current action string.
    804      * @param BP_Activity_Activity $activity        The BuddyPress activity object.
    805      */
    806     if ( $pre = apply_filters( 'bbp_pre_format_activity_action_new_post', false, $type, $action, $activity ) ) {
    807         return $pre;
     796    // Bail if intercepted
     797    $intercept = bbp_maybe_intercept( __FUNCTION__, func_get_args() );
     798    if ( bbp_is_intercepted( $intercept ) ) {
     799        return $intercept;
    808800    }
    809801
  • trunk/src/includes/topics/template.php

    r6737 r6751  
    321321function bbp_add_sticky_topics( &$query, $args = array() ) {
    322322
    323     // Bail if overloaded
    324     if ( null !== apply_filters( 'bbp_pre_add_stick_topics', null, $query, $args ) ) {
    325         return;
     323    // Bail if intercepted
     324    $intercept = bbp_maybe_intercept( __FUNCTION__, func_get_args() );
     325    if ( bbp_is_intercepted( $intercept ) ) {
     326        return $intercept;
    326327    }
    327328
  • trunk/src/includes/users/template.php

    r6745 r6751  
    535535        }
    536536
    537         // Allow early overriding of the profile URL to cut down on processing
    538         $early_profile_url = apply_filters( 'bbp_pre_get_user_profile_url', $user_id );
    539         if ( is_string( $early_profile_url ) ) {
    540             return $early_profile_url;
     537        // Bail if intercepted
     538        $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_profile_url', func_get_args() );
     539        if ( bbp_is_intercepted( $intercept ) ) {
     540            return $intercept;
    541541        }
    542542
     
    625625        }
    626626
    627         // Allow early overriding of the profile edit URL to cut down on processing
    628         $early_profile_url = apply_filters( 'bbp_pre_get_user_profile_edit_url', $user_id );
    629         if ( is_string( $early_profile_url ) ) {
    630             return $early_profile_url;
     627        // Bail if intercepted
     628        $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_profile_edit_url', func_get_args() );
     629        if ( bbp_is_intercepted( $intercept ) ) {
     630            return $intercept;
    631631        }
    632632
     
    984984        }
    985985
    986         // Allow early overriding of the profile URL to cut down on processing
    987         $early_profile_url = apply_filters( 'bbp_pre_get_favorites_permalink', $user_id );
    988         if ( is_string( $early_profile_url ) ) {
    989             return $early_profile_url;
     986        // Bail if intercepted
     987        $intercept = bbp_maybe_intercept( 'bbp_pre_get_favorites_permalink', func_get_args() );
     988        if ( bbp_is_intercepted( $intercept ) ) {
     989            return $intercept;
    990990        }
    991991
     
    11751175        }
    11761176
    1177         // Allow early overriding of the profile URL to cut down on processing
    1178         $early_profile_url = apply_filters( 'bbp_pre_get_subscriptions_permalink', $user_id );
    1179         if ( is_string( $early_profile_url ) ) {
    1180             return $early_profile_url;
     1177        // Bail if intercepted
     1178        $intercept = bbp_maybe_intercept( 'bbp_pre_get_subscriptions_permalink', func_get_args() );
     1179        if ( bbp_is_intercepted( $intercept ) ) {
     1180            return $intercept;
    11811181        }
    11821182
     
    16141614        }
    16151615
    1616         // Allow early overriding of the profile URL to cut down on processing
    1617         $early_url = apply_filters( 'bbp_pre_get_user_topics_created_url', $user_id );
    1618         if ( is_string( $early_url ) ) {
    1619             return $early_url;
     1616        // Bail if intercepted
     1617        $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_topics_created_url', func_get_args() );
     1618        if ( bbp_is_intercepted( $intercept ) ) {
     1619            return $intercept;
    16201620        }
    16211621
     
    16671667        }
    16681668
    1669         // Allow early overriding of the profile URL to cut down on processing
    1670         $early_url = apply_filters( 'bbp_pre_get_user_replies_created_url', $user_id );
    1671         if ( is_string( $early_url ) ) {
    1672             return $early_url;
     1669        // Bail if intercepted
     1670        $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_replies_created_url', func_get_args() );
     1671        if ( bbp_is_intercepted( $intercept ) ) {
     1672            return $intercept;
    16731673        }
    16741674
     
    17201720        }
    17211721
    1722         // Allow early overriding of the profile URL to cut down on processing
    1723         $early_url = apply_filters( 'bbp_pre_get_user_engagements_url', $user_id );
    1724         if ( is_string( $early_url ) ) {
    1725             return $early_url;
     1722        // Bail if intercepted
     1723        $intercept = bbp_maybe_intercept( 'bbp_pre_get_user_engagements_url', func_get_args() );
     1724        if ( bbp_is_intercepted( $intercept ) ) {
     1725            return $intercept;
    17261726        }
    17271727
Note: See TracChangeset for help on using the changeset viewer.