Skip to:
Content

bbPress.org


Ignore:
Timestamp:
12/08/2017 02:09:08 PM (9 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.