| 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 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Clone array of Objects |
| 90 |
* Return independent array instances, instead of reference to previous objects. |
| 91 |
* Useful, if we do not need to apply changes in original array |
| 92 |
* |
| 93 |
* @param array|object|mixed $object_arr |
| 94 |
* |
| 95 |
* @return array|object|mixed |
| 96 |
* |
| 97 |
* |
| 98 |
* CLONING tips... |
| 99 |
* |
| 100 |
We clone array of booking Objects, because later we make manipulations with some properties, e.g. unset() |
| 101 |
for example on this line: |
| 102 |
unset( $this_date_bookings[ $booked_times__key ]->form ); // Hide booking details |
| 103 |
|
| 104 |
and for having (not changed Cache object from DB $wpbc_global_cache['wpbc__sql__get_booking_dates'][ $params_cache ] ) |
| 105 |
During loading bookings in wpbc_get__booked_dates__per_resources__arr(...) |
| 106 |
|
| 107 |
we need to clone each such object, otherwise if we change something in this variable, it will be changed |
| 108 |
in cache $wpbc_global_cache['wpbc__sql__get_booking_dates'][ $params_cache ] ), as well |
| 109 |
|
| 110 |
Otherwise, if we will not clone and use this, then we do not need to unset some properties: |
| 111 |
|
| 112 |
$this_date_bookings = $booked_dates__per_resources__arr[ $my_day_tag ][ $resource_id ]; |
| 113 |
v.s. |
| 114 |
$this_date_bookings = array_map( |
| 115 |
function ( $object ) { return clone $object; } |
| 116 |
, $booked_dates__per_resources__arr[ $my_day_tag ][ $resource_id ] |
| 117 |
); |
| 118 |
|
| 119 |
*/ |
| 120 |
function wpbc_clone_array_of_objects( $object_arr ){ |
| 121 |
|
| 122 |
$cloned_result = false; |
| 123 |
|
| 124 |
if ( is_array( $object_arr ) ) { |
| 125 |
|
| 126 |
$cloned_result = array_map( |
| 127 |
function ( $object ) { |
| 128 |
return ( ( is_object( $object ) ) ? ( clone $object ) : $object ); |
| 129 |
} |
| 130 |
, $object_arr |
| 131 |
); |
| 132 |
|
| 133 |
} else if ( is_object( $object_arr ) ) { |
| 134 |
|
| 135 |
$cloned_result = ( clone $object_arr ); |
| 136 |
|
| 137 |
} else { |
| 138 |
$cloned_result = $object_arr; |
| 139 |
} |
| 140 |
|
| 141 |
return $cloned_result; |
| 142 |
} |
| 143 |
|