| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @version 1.0 |
| 4 | 4 | * @package Booking Calendar |
| @@ -20,9 +20,36 @@ | ||
| 20 | 20 | // --------------------------------------------------------------------------------------------------------------------- |
| 21 | 21 | // Internal plugin action hooks system |
| 22 | 22 | // --------------------------------------------------------------------------------------------------------------------- |
| 23 | 23 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound |
| 24 | -global $wpbc_bk_action, $wpbc_bk_filter; | |
| 24 | +global $wpbc_bk_action, $wpbc_bk_filter; | |
| 25 | + | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * Mark whether destructive Booking Calendar deactivation is running in this request. | |
| 29 | + * | |
| 30 | + * WordPress cannot unload plugin PHP after deactivate_plugins() is called. This | |
| 31 | + * state lets already-loaded Pro and MultiUser callbacks avoid using deleted data. | |
| 32 | + * | |
| 33 | + * @param bool $is_deactivating Whether deactivation is in progress. | |
| 34 | + */ | |
| 35 | +function wpbc_set_plugin_deactivation_state( $is_deactivating ) { | |
| 36 | + global $wpbc_plugin_deactivation_in_progress; | |
| 37 | + | |
| 38 | + $wpbc_plugin_deactivation_in_progress = (bool) $is_deactivating; | |
| 39 | +} | |
| 40 | + | |
| 41 | + | |
| 42 | +/** | |
| 43 | + * Check whether destructive Booking Calendar deactivation is running. | |
| 44 | + * | |
| 45 | + * @return bool | |
| 46 | + */ | |
| 47 | +function wpbc_is_plugin_deactivation_in_progress() { | |
| 48 | + global $wpbc_plugin_deactivation_in_progress; | |
| 49 | + | |
| 50 | + return ! empty( $wpbc_plugin_deactivation_in_progress ); | |
| 51 | +} | |
| 25 | 52 | |
| 26 | 53 | |
| 27 | 54 | function add_bk_filter( $filter_type, $filter ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound |
| 28 | 55 | global $wpbc_bk_filter; |
| @@ -51,23 +78,60 @@ | ||
| 51 | 78 | $wpbc_bk_filter = array( $filter_type => array( $args ) ); |
| 52 | 79 | } |
| 53 | 80 | } |
| 54 | 81 | |
| 55 | -function remove_bk_filter( $filter_type, $filter ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound | |
| 56 | - 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 | +} | |
| 57 | 122 | |
| 58 | - if ( isset( $wpbc_bk_filter[ $filter_type ] ) ) { | |
| 59 | - for ( $i = 0; $i < count( $wpbc_bk_filter[ $filter_type ] ); $i++ ) { | |
| 60 | - if ( $wpbc_bk_filter[ $filter_type ][ $i ][0] == $filter ) { | |
| 61 | - $wpbc_bk_filter[ $filter_type ][ $i ] = null; | |
| 62 | - | |
| 63 | - return; | |
| 64 | - } | |
| 65 | - } | |
| 66 | - } | |
| 67 | -} | |
| 68 | - | |
| 69 | -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 | |
| 70 | 134 | global $wpbc_bk_filter; |
| 71 | 135 | |
| 72 | 136 | |
| 73 | 137 | $args = array(); |
| @@ -80,17 +144,23 @@ | ||
| 80 | 144 | } else { |
| 81 | 145 | $value = false; |
| 82 | 146 | } |
| 83 | 147 | |
| 84 | - if ( is_array( $wpbc_bk_filter ) ) { | |
| 85 | - if ( isset( $wpbc_bk_filter[ $filter_type ] ) ) { | |
| 86 | - foreach ( $wpbc_bk_filter[ $filter_type ] as $filter ) { | |
| 87 | - $filter_func = array_shift( $filter ); | |
| 88 | - $parameter = $args; | |
| 89 | - $value = call_user_func_array( $filter_func, $parameter ); | |
| 90 | - } | |
| 91 | - } | |
| 92 | - } | |
| 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 | + } | |
| 93 | 163 | |
| 94 | 164 | return $value; |
| 95 | 165 | } |
| 96 | 166 | |