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

WordPress Trac noreply at wordpress.org
Wed Apr 19 23:21:22 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:  trunk
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+------------------------------
Changes (by flixos90):

 * keywords:   => has-patch


Comment:

 After reviewing #37930 (and considering similar situations in Core of
 which there are quite a few) I figured we should continue working towards
 a sophisticated solution on short-circuiting processes.
 Passing `false` doesn't help in some cases (for example in Core's
 `pre_option_{$option}` filter, as you might actually wanna return
 `false`), and using another value such as `null` doesn't solve that
 problem, but only diminishes the chance for a collision (as we don't use
 `null` often in Core, but still sometimes, and we shouldn't rely on such
 silent conventions anyway). We need something like the above proposals.

 [attachment:40280.diff] is a patch with a possible implementation that
 takes both suggestions by @andy and @DrewAPicture into account.

 The patch introduces a simple class `WP_Short_Circuit_Result` that
 provides a storage for whether a process should be short-circuited and,
 optionally, for a value to use for the result of the short-circuit (only
 needed in case of a filter). To make it easy to use this feature, there
 are two new functions `wp_short_circuit_filter()` and
 `wp_short_circuit_action()`.

 Here is an example of how the filter function could be used:

 Possible patch for #37930:
 {{{
 #!php
 /**
  * Allows to short-circuit the process of retrieving the value of an
 existing option.
  *
  * @since 4.8.0
  *
  * @param mixed  $short_circuit_value Value to be filled with the short-
 circuit result.
  * @param string $option              Option name.
  * @param mixed  $default             The default value to return if the
 option does not
  *                                    exist in the database.
  */
 if ( wp_short_circuit_filter( 'pre_option', $short_circuit_value, array(
 $option, $default ) ) ) {
         return $short_circuit_value;
 }
 }}}

 Example usage of the above call for short-circuiting:
 {{{
 #!php
 add_filter( 'wp_short_circuit_filter_pre_option', function(
 $short_circuit_result, $option, $default ) {
         if ( 'foo' === $option ) {
                 $short_circuit_result->short_circuit( true );
                 $short_circuit_result->value( 'bar' );
         }

         return $short_circuit_result;
 }, 10, 3 );
 }}}

 `wp_short_circuit_action()` works similarly, except that it does not deal
 with any result value (so you would simply `return;` to stop execution of
 the function in case the `wp_short_circuit_action()` call returns true).

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


More information about the wp-trac mailing list