[wp-trac] [WordPress Trac] #40280: Short-circuit filters can't return common values

WordPress Trac noreply at wordpress.org
Wed Dec 6 18:27:34 UTC 2017


#40280: Short-circuit filters can't return common values
-------------------------+------------------------------
 Reporter:  andy         |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  General      |     Version:  4.8
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+------------------------------

Comment (by johnjamesjacoby):

 In bbPress, @jmdodd and I are tossing this idea around:

 {{{

 /**
  * Generate a default intercept value.
  *
  * @since 2.6.0
  *
  * @staticvar mixed $rand Null by default, random string on first call
  * @return string
  */
 function bbp_default_intercept() {
         static $rand = null;

         // Generate a new random and unique string
         if ( null === $rand ) {

                 // If ext/hash is not present, compat.php's hash_hmac()
 does not support sha256.
                 $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';

                 // Old WP installs may not have AUTH_SALT defined.
                 $salt = defined( 'AUTH_SALT' ) && AUTH_SALT ? AUTH_SALT :
 (string) wp_rand();

                 // Create unique ID
                 $rand = '{' . hash_hmac( $algo, uniqid( $salt, true ),
 $salt ) . '}';
         }

         return $rand;
 }

 /**
  * Allow interception of a method or function call.
  *
  * @since 2.6.0
  *
  * @param string $action Typically the name of the function
  * @param array  $args   Typically the results of func_get_args()
  *
  * @return mixed         Intercept results. Default
 bbp_default_intercept().
  */
 function bbp_do_intercept( $action = '', $args = array() ) {

         // Sanitize the hook same
         $key       = sanitize_key( "pre_{$action}_intercept" );

         // Default return value
         $default   = bbp_default_intercept();

         // Intercept?
         $intercept = apply_filters( $key, $default, extract( $args ) );

         // Bail if intercepted
         if ( $intercept !== $default ) {
                 return $intercept;
         }

         // Not intercepted
         return $default;
 }
 }}}

 Then the in-function intercept would look something like:

 {{{
         // Maybe intercept
         $intercept = bbp_do_intercept( __FUNCTION__, func_get_args() );
         if ( bbp_default_intercept() !== $intercept ) {
                 return $intercept;
         }
 }}}

 ----

 WordPress has other considerations, namely backwards compatibility, but in
 an ideal world this could be relatively simple.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40280#comment:17>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list