| 1 |
<?php |
| 2 |
/** |
| 3 |
* AutomatorWP Cache |
| 4 |
* |
| 5 |
* Used to store commonly used query results |
| 6 |
* |
| 7 |
* @package AutomatorWP\Cache |
| 8 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
// Exit if accessed directly |
| 12 |
if( !defined( 'ABSPATH' ) ) exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Get a cached element. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
* |
| 19 |
* @param string $key Cache key |
| 20 |
* @param mixed $default Default value in case the cache is not found |
| 21 |
* @param bool $stored Whatever if the cache has been stored previously in the database or not |
| 22 |
* |
| 23 |
* @return mixed |
| 24 |
*/ |
| 25 |
function automatorwp_get_cache( $key = '', $default = null, $stored = true ) { |
| 26 |
|
| 27 |
if( isset( AutomatorWP()->cache[$key] ) ) { |
| 28 |
return AutomatorWP()->cache[$key]; |
| 29 |
} else if( $stored ) { |
| 30 |
|
| 31 |
$cached = get_option( 'automatorwp_cache_' . $key ); |
| 32 |
|
| 33 |
// If has been cached on options, then return the cached value |
| 34 |
if( $cached !== false ) { |
| 35 |
|
| 36 |
AutomatorWP()->cache[$key] = $cached; |
| 37 |
|
| 38 |
return AutomatorWP()->cache[$key]; |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
return $default; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Set a cached element. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @param string $key |
| 54 |
* @param mixed $value |
| 55 |
* @param bool $save |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
function automatorwp_set_cache( $key = '', $value = '', $save = false ) { |
| 60 |
|
| 61 |
// Just keep value on a floating cache |
| 62 |
// To make it persistent pass $save as true or use automatorwp_save_cache() function |
| 63 |
AutomatorWP()->cache[$key] = $value; |
| 64 |
|
| 65 |
if( $save === true ) { |
| 66 |
return automatorwp_save_cache( $key, $value ); |
| 67 |
} |
| 68 |
|
| 69 |
return true; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Save a cached element. |
| 75 |
* |
| 76 |
* @since 1.6.1 |
| 77 |
* |
| 78 |
* @param string $key |
| 79 |
* @param mixed $value |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
function automatorwp_save_cache( $key = '', $value = '' ) { |
| 84 |
|
| 85 |
// Allow to make value optional but just if element has been already cached |
| 86 |
if( empty( $value ) && isset( AutomatorWP()->cache[$key] ) ) { |
| 87 |
$value = AutomatorWP()->cache[$key]; |
| 88 |
} |
| 89 |
|
| 90 |
// Update the floating cache |
| 91 |
AutomatorWP()->cache[$key] = $value; |
| 92 |
|
| 93 |
return update_option( 'automatorwp_cache_' . $key, $value, false ); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Delete a cached element. |
| 99 |
* |
| 100 |
* @since 1.6.1 |
| 101 |
* |
| 102 |
* @param string $key |
| 103 |
* |
| 104 |
* @return bool |
| 105 |
*/ |
| 106 |
function automatorwp_delete_cache( $key = '' ) { |
| 107 |
|
| 108 |
return delete_option( 'automatorwp_cache_' . $key ); |
| 109 |
|
| 110 |
} |