| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
|
| 6 |
global $wpbc_global_cache; // FixIn: 9.7.3.14. |
| 7 |
$wpbc_global_cache = array(); |
| 8 |
|
| 9 |
// GET Saved request --------------------------------------------------------------------------------------------------- |
| 10 |
|
| 11 |
/** |
| 12 |
* GET saved cache request result, if exists otherwise return null |
| 13 |
* |
| 14 |
* @param string $function_name 'wpbc__sql__get_booking_dates' - name of function, where saved DB request |
| 15 |
* @param string|array $params [ array with parameters in this function, that saved ] |
| 16 |
* |
| 17 |
* @return array|mixed|object|null if not saved previously then returned null |
| 18 |
*/ |
| 19 |
function wpbc_cache__get( $function_name, $params ){ |
| 20 |
|
| 21 |
// return null; // Stop caching hack |
| 22 |
|
| 23 |
global $wpbc_global_cache; |
| 24 |
|
| 25 |
if ( ! is_serialized( $params ) ) { |
| 26 |
$params_cache = maybe_serialize( $params ); |
| 27 |
} else { |
| 28 |
$params_cache = $params; |
| 29 |
} |
| 30 |
|
| 31 |
if ( |
| 32 |
( isset( $wpbc_global_cache[ $function_name ] ) ) |
| 33 |
&& ( isset( $wpbc_global_cache[ $function_name ][ $params_cache ] ) ) |
| 34 |
){ |
| 35 |
|
| 36 |
//return $wpbc_global_cache[ $function_name ][ $params_cache ]; |
| 37 |
|
| 38 |
// CLONING array of objects |
| 39 |
$clone_array_of_objects = wpbc_clone_array_of_objects( $wpbc_global_cache[ $function_name ][ $params_cache ] ); |
| 40 |
|
| 41 |
return $clone_array_of_objects; |
| 42 |
} |
| 43 |
|
| 44 |
return null; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
// SAVE ---------------------------------------------------------------------------------------------------------------- |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* SAVE request result to CACHE var |
| 53 |
* |
| 54 |
* @param string $function_name 'wpbc__sql__get_booking_dates' - name of function, where saved DB request |
| 55 |
* @param string|array $params [ array with parameters in this function, that saved ] |
| 56 |
* @param mixed $value_to_save value to save -- usually it's result from DB |
| 57 |
* |
| 58 |
* @return mixed return CLONED $value_to_save |
| 59 |
*/ |
| 60 |
function wpbc_cache__save( $function_name, $params, $value_to_save ){ |
| 61 |
|
| 62 |
global $wpbc_global_cache; |
| 63 |
|
| 64 |
|
| 65 |
if ( ! is_serialized( $params ) ) { |
| 66 |
$params_cache = maybe_serialize( $params ); |
| 67 |
} else { |
| 68 |
$params_cache = $params; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
if ( ! isset( $wpbc_global_cache[ $function_name ] ) ) { |
| 73 |
$wpbc_global_cache[ $function_name ] = array(); |
| 74 |
} |
| 75 |
|
| 76 |
//$wpbc_global_cache[ $function_name ][ $params_cache ] = $value_to_save; |
| 77 |
|
| 78 |
// CLONING array of objects |
| 79 |
$wpbc_global_cache[ $function_name ][ $params_cache ] = wpbc_clone_array_of_objects( $value_to_save ); |
| 80 |
|
| 81 |
return $wpbc_global_cache[ $function_name ][ $params_cache ]; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Clear all request-local cached results for one function namespace. |
| 87 |
* |
| 88 |
* Availability validation uses this immediately before and after a protected |
| 89 |
* booking save so a result loaded earlier in the request cannot outlive the |
| 90 |
* database state that was serialized by the booking availability guard. |
| 91 |
* |
| 92 |
* @param string $function_name Function cache namespace to clear. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
function wpbc_cache__clear( $function_name ) { |
| 97 |
|
| 98 |
global $wpbc_global_cache; |
| 99 |
|
| 100 |
if ( ! is_array( $wpbc_global_cache ) || ! isset( $wpbc_global_cache[ $function_name ] ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
unset( $wpbc_global_cache[ $function_name ] ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Clone array of Objects |
| 113 |
* Return independent array instances, instead of reference to previous objects. |
| 114 |
* Useful, if we do not need to apply changes in original array |
| 115 |
* |
| 116 |
* @param array|object|mixed $object_arr |
| 117 |
* |
| 118 |
* @return array|object|mixed |
| 119 |
* |
| 120 |
* |
| 121 |
* CLONING tips... |
| 122 |
* |
| 123 |
We clone array of booking Objects, because later we make manipulations with some properties, e.g. unset() |
| 124 |
for example on this line: |
| 125 |
unset( $this_date_bookings[ $booked_times__key ]->form ); // Hide booking details |
| 126 |
|
| 127 |
and for having (not changed Cache object from DB $wpbc_global_cache['wpbc__sql__get_booking_dates'][ $params_cache ] ) |
| 128 |
During loading bookings in wpbc_get__booked_dates__per_resources__arr(...) |
| 129 |
|
| 130 |
we need to clone each such object, otherwise if we change something in this variable, it will be changed |
| 131 |
in cache $wpbc_global_cache['wpbc__sql__get_booking_dates'][ $params_cache ] ), as well |
| 132 |
|
| 133 |
Otherwise, if we will not clone and use this, then we do not need to unset some properties: |
| 134 |
|
| 135 |
$this_date_bookings = $booked_dates__per_resources__arr[ $my_day_tag ][ $resource_id ]; |
| 136 |
v.s. |
| 137 |
$this_date_bookings = array_map( |
| 138 |
function ( $object ) { return clone $object; } |
| 139 |
, $booked_dates__per_resources__arr[ $my_day_tag ][ $resource_id ] |
| 140 |
); |
| 141 |
|
| 142 |
*/ |
| 143 |
function wpbc_clone_array_of_objects( $object_arr ){ |
| 144 |
|
| 145 |
$cloned_result = false; |
| 146 |
|
| 147 |
if ( is_array( $object_arr ) ) { |
| 148 |
|
| 149 |
$cloned_result = array_map( |
| 150 |
function ( $object ) { |
| 151 |
return ( ( is_object( $object ) ) ? ( clone $object ) : $object ); |
| 152 |
} |
| 153 |
, $object_arr |
| 154 |
); |
| 155 |
|
| 156 |
} else if ( is_object( $object_arr ) ) { |
| 157 |
|
| 158 |
$cloned_result = ( clone $object_arr ); |
| 159 |
|
| 160 |
} else { |
| 161 |
$cloned_result = $object_arr; |
| 162 |
} |
| 163 |
|
| 164 |
return $cloned_result; |
| 165 |
} |
| 166 |
|