| @@ -78,23 +78,60 @@ | ||
| 78 | 78 | $wpbc_bk_filter = array( $filter_type => array( $args ) ); |
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | -function remove_bk_filter( $filter_type, $filter ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound | |
| 83 | - global $wpbc_bk_filter; | |
| 82 | +/** | |
| 83 | + * Remove the first matching callback from an internal Booking Calendar filter. | |
| 84 | + * | |
| 85 | + * Registry entries must be deleted rather than replaced with null because the | |
| 86 | + * same request may apply this filter again after a request-local override ends. | |
| 87 | + * | |
| 88 | + * @param string $filter_type Internal filter identifier. | |
| 89 | + * @param callable $filter Callback registered with add_bk_filter(). | |
| 90 | + * | |
| 91 | + * @return void | |
| 92 | + */ | |
| 93 | +function remove_bk_filter( $filter_type, $filter ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound | |
| 94 | + global $wpbc_bk_filter; | |
| 95 | + | |
| 96 | + if ( | |
| 97 | + ! is_array( $wpbc_bk_filter ) | |
| 98 | + || empty( $wpbc_bk_filter[ $filter_type ] ) | |
| 99 | + || ! is_array( $wpbc_bk_filter[ $filter_type ] ) | |
| 100 | + ) { | |
| 101 | + return; | |
| 102 | + } | |
| 103 | + | |
| 104 | + foreach ( $wpbc_bk_filter[ $filter_type ] as $filter_index => $registered_filter ) { | |
| 105 | + if ( ! is_array( $registered_filter ) || ! array_key_exists( 0, $registered_filter ) ) { | |
| 106 | + continue; | |
| 107 | + } | |
| 108 | + | |
| 109 | + // Preserve the legacy loose callback comparison for object-method arrays. | |
| 110 | + if ( $registered_filter[0] == $filter ) { | |
| 111 | + unset( $wpbc_bk_filter[ $filter_type ][ $filter_index ] ); | |
| 112 | + $wpbc_bk_filter[ $filter_type ] = array_values( $wpbc_bk_filter[ $filter_type ] ); | |
| 113 | + | |
| 114 | + if ( empty( $wpbc_bk_filter[ $filter_type ] ) ) { | |
| 115 | + unset( $wpbc_bk_filter[ $filter_type ] ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + } | |
| 121 | +} | |
| 84 | 122 | |
| 85 | - if ( isset( $wpbc_bk_filter[ $filter_type ] ) ) { | |
| 86 | - for ( $i = 0; $i < count( $wpbc_bk_filter[ $filter_type ] ); $i++ ) { | |
| 87 | - if ( $wpbc_bk_filter[ $filter_type ][ $i ][0] == $filter ) { | |
| 88 | - $wpbc_bk_filter[ $filter_type ][ $i ] = null; | |
| 89 | - | |
| 90 | - return; | |
| 91 | - } | |
| 92 | - } | |
| 93 | - } | |
| 94 | -} | |
| 95 | - | |
| 96 | -function apply_bk_filter( $filter_type ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound | |
| 123 | +/** | |
| 124 | + * Apply callbacks registered for an internal Booking Calendar filter. | |
| 125 | + * | |
| 126 | + * Additional function arguments are forwarded to every registered callback. | |
| 127 | + * The first additional argument is returned unchanged when no callback exists. | |
| 128 | + * | |
| 129 | + * @param string $filter_type Internal filter identifier. | |
| 130 | + * | |
| 131 | + * @return mixed Last callback result, the first supplied value, or false. | |
| 132 | + */ | |
| 133 | +function apply_bk_filter( $filter_type ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound | |
| 97 | 134 | global $wpbc_bk_filter; |
| 98 | 135 | |
| 99 | 136 | |
| 100 | 137 | $args = array(); |
| @@ -107,17 +144,23 @@ | ||
| 107 | 144 | } else { |
| 108 | 145 | $value = false; |
| 109 | 146 | } |
| 110 | 147 | |
| 111 | - if ( is_array( $wpbc_bk_filter ) ) { | |
| 112 | - if ( isset( $wpbc_bk_filter[ $filter_type ] ) ) { | |
| 113 | - foreach ( $wpbc_bk_filter[ $filter_type ] as $filter ) { | |
| 114 | - $filter_func = array_shift( $filter ); | |
| 115 | - $parameter = $args; | |
| 116 | - $value = call_user_func_array( $filter_func, $parameter ); | |
| 117 | - } | |
| 118 | - } | |
| 119 | - } | |
| 148 | + if ( | |
| 149 | + is_array( $wpbc_bk_filter ) | |
| 150 | + && ! empty( $wpbc_bk_filter[ $filter_type ] ) | |
| 151 | + && is_array( $wpbc_bk_filter[ $filter_type ] ) | |
| 152 | + ) { | |
| 153 | + foreach ( $wpbc_bk_filter[ $filter_type ] as $filter ) { | |
| 154 | + if ( ! is_array( $filter ) || empty( $filter ) ) { | |
| 155 | + continue; | |
| 156 | + } | |
| 157 | + | |
| 158 | + $filter_func = array_shift( $filter ); | |
| 159 | + $parameter = $args; | |
| 160 | + $value = call_user_func_array( $filter_func, $parameter ); | |
| 161 | + } | |
| 162 | + } | |
| 120 | 163 | |
| 121 | 164 | return $value; |
| 122 | 165 | } |
| 123 | 166 | |