| 1 | <?php |
| 2 | /** |
| 3 | * Plugin API: WP_Short_Circuit_Result class |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @since 4.9.0 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Core class to store a short-circuit result. |
| 11 | * |
| 12 | * An instance of this class can be passed to an action to determine whether the surrounding |
| 13 | * function or method should be short-circuited, and optionally define a return value. |
| 14 | * |
| 15 | * @since 4.9.0 |
| 16 | */ |
| 17 | class WP_Short_Circuit_Result { |
| 18 | |
| 19 | /** |
| 20 | * Whether the current process should be short-circuited. |
| 21 | * |
| 22 | * @since 4.9.0 |
| 23 | * @access private |
| 24 | * @var bool |
| 25 | */ |
| 26 | private $short_circuit = false; |
| 27 | |
| 28 | /** |
| 29 | * Value of the short-circuit. |
| 30 | * |
| 31 | * @since 4.9.0 |
| 32 | * @access private |
| 33 | * @var mixed |
| 34 | */ |
| 35 | private $value = null; |
| 36 | |
| 37 | /** |
| 38 | * Toggles or retrieves whether the current process should be short-circuited. |
| 39 | * |
| 40 | * @since 4.9.0 |
| 41 | * @access public |
| 42 | * |
| 43 | * @param bool $set Optional. Whether to short-circuit the current process. If this is |
| 44 | * omitted, the current value is retrieved. Default null. |
| 45 | * @return bool The previous value of the short-circuit flag if $set was passed, or the |
| 46 | * current value of the flag otherwise. |
| 47 | */ |
| 48 | public function short_circuit( $set = null ) { |
| 49 | $current = $this->short_circuit; |
| 50 | |
| 51 | if ( func_num_args() > 0 ) { |
| 52 | $this->short_circuit = (bool) $set; |
| 53 | } |
| 54 | |
| 55 | return $current; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sets or retrieves the result value of the short-circuit. |
| 60 | * |
| 61 | * @since 4.9.0 |
| 62 | * @access public |
| 63 | * |
| 64 | * @param bool $set Optional. Result value of the short-circuit. If this is |
| 65 | * omitted, the current value is retrieved. Default null. |
| 66 | * @return bool The previous short-circuit result value if $set was passed, or the |
| 67 | * current result value otherwise. |
| 68 | */ |
| 69 | public function value( $set = null ) { |
| 70 | $current = $this->value; |
| 71 | |
| 72 | if ( func_num_args() > 0 ) { |
| 73 | $this->value = $set; |
| 74 | } |
| 75 | |
| 76 | return $current; |
| 77 | } |
| 78 | } |