| 5 | | /** |
| 6 | | * Generate a default intercept value. |
| 7 | | * |
| 8 | | * @since 2.6.0 |
| 9 | | * |
| 10 | | * @staticvar mixed $rand Null by default, random string on first call |
| 11 | | * @return string |
| 12 | | */ |
| 13 | | function bbp_default_intercept() { |
| 14 | | static $rand = null; |
| 15 | | |
| 16 | | // Generate a new random and unique string |
| 17 | | if ( null === $rand ) { |
| 18 | | |
| 19 | | // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. |
| 20 | | $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; |
| 21 | | |
| 22 | | // Old WP installs may not have AUTH_SALT defined. |
| 23 | | $salt = defined( 'AUTH_SALT' ) && AUTH_SALT ? AUTH_SALT : (string) wp_rand(); |
| 24 | | |
| 25 | | // Create unique ID |
| 26 | | $rand = '{' . hash_hmac( $algo, uniqid( $salt, true ), $salt ) . '}'; |
| 27 | | } |
| 28 | | |
| 29 | | return $rand; |
| 30 | | } |
| 31 | | |
| 32 | | /** |
| 33 | | * Allow interception of a method or function call. |
| 34 | | * |
| 35 | | * @since 2.6.0 |
| 36 | | * |
| 37 | | * @param string $action Typically the name of the function |
| 38 | | * @param array $args Typically the results of func_get_args() |
| 39 | | * |
| 40 | | * @return mixed Intercept results. Default bbp_default_intercept(). |
| 41 | | */ |
| 42 | | function bbp_do_intercept( $action = '', $args = array() ) { |
| 43 | | |
| 44 | | // Sanitize the hook same |
| 45 | | $key = sanitize_key( "pre_{$action}_intercept" ); |
| 46 | | |
| 47 | | // Default return value |
| 48 | | $default = bbp_default_intercept(); |
| 49 | | |
| 50 | | // Intercept? |
| 51 | | $intercept = apply_filters( $key, $default, extract( $args ) ); |
| 52 | | |
| 53 | | // Bail if intercepted |
| 54 | | if ( $intercept !== $default ) { |
| 55 | | return $intercept; |
| 56 | | } |
| 57 | | |
| 58 | | // Not intercepted |
| 59 | | return $default; |
| 60 | | } |
| 61 | | }}} |
| 62 | | |
| 63 | | Then the in-function intercept would look something like: |
| 64 | | |
| 65 | | {{{ |
| 66 | | // Maybe intercept |
| 67 | | $intercept = bbp_do_intercept( __FUNCTION__, func_get_args() ); |
| 68 | | if ( bbp_default_intercept() !== $intercept ) { |
| 69 | | return $intercept; |
| 70 | | } |
| 71 | | }}} |
| 72 | | |
| 73 | | ---- |
| 74 | | |
| 75 | | WordPress has other considerations, namely backwards compatibility, but in an ideal world this could be relatively simple. |
| | 5 | https://bbpress.trac.wordpress.org/changeset/6751 |