| 1 |
<?php |
| 2 |
/** |
| 3 |
* Docket Cache. |
| 4 |
* |
| 5 |
* @author Nawawi Jamili |
| 6 |
* @license MIT |
| 7 |
* |
| 8 |
* @see https://github.com/nawawi/docket-cache |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* Store Transients in DB. |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Nawawi\DocketCache; |
| 16 |
|
| 17 |
\defined('ABSPATH') || exit; |
| 18 |
|
| 19 |
final class TransientDb |
| 20 |
{ |
| 21 |
public function set($transient, $value, $group, $timeout) |
| 22 |
{ |
| 23 |
$result = false; |
| 24 |
|
| 25 |
// we set autoload with 'no' to prevent it store in alloptions. |
| 26 |
// timeout in timestamp, always set expiration. |
| 27 |
if ('transient' === $group) { |
| 28 |
$transient_timeout = '_transient_timeout_'.$transient; |
| 29 |
$transient_option = '_transient_'.$transient; |
| 30 |
|
| 31 |
if (false === get_option($transient_option)) { |
| 32 |
add_option($transient_timeout, $timeout, '', 'no'); |
| 33 |
$result = add_option($transient_option, $value, '', 'no'); |
| 34 |
} else { |
| 35 |
update_option($transient_timeout, $timeout, 'no'); |
| 36 |
$result = update_option($transient_option, $value, 'no'); |
| 37 |
} |
| 38 |
} elseif ('site-transient' === $group) { |
| 39 |
$transient_timeout = '_site_transient_timeout_'.$transient; |
| 40 |
$option = '_site_transient_'.$transient; |
| 41 |
|
| 42 |
if (false === get_site_option($option)) { |
| 43 |
add_site_option($transient_timeout, $timeout); |
| 44 |
$result = add_site_option($option, $value); |
| 45 |
} else { |
| 46 |
update_site_option($transient_timeout, $timeout); |
| 47 |
$result = update_site_option($option, $value); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $result; |
| 52 |
} |
| 53 |
|
| 54 |
public function get($transient, $group) |
| 55 |
{ |
| 56 |
if ('transient' === $group) { |
| 57 |
$transient_option = '_transient_'.$transient; |
| 58 |
if (!wp_installing()) { |
| 59 |
$alloptions = wp_load_alloptions(); |
| 60 |
if (!isset($alloptions[$transient_option])) { |
| 61 |
$transient_timeout = '_transient_timeout_'.$transient; |
| 62 |
$timeout = get_option($transient_timeout); |
| 63 |
if (false !== $timeout && $timeout < time()) { |
| 64 |
delete_option($transient_option); |
| 65 |
delete_option($transient_timeout); |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return get_option($transient_option); |
| 73 |
} |
| 74 |
|
| 75 |
if ('site-transient' === $group) { |
| 76 |
$transient_option = '_site_transient_'.$transient; |
| 77 |
$transient_timeout = '_site_transient_timeout_'.$transient; |
| 78 |
$timeout = get_site_option($transient_timeout); |
| 79 |
if (false !== $timeout && $timeout < time()) { |
| 80 |
delete_site_option($transient_option); |
| 81 |
delete_site_option($transient_timeout); |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
return get_site_option($transient_option); |
| 87 |
} |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
public function delete($transient, $group) |
| 93 |
{ |
| 94 |
$result = false; |
| 95 |
|
| 96 |
if ('transient' === $group) { |
| 97 |
$option_timeout = '_transient_timeout_'.$transient; |
| 98 |
$option = '_transient_'.$transient; |
| 99 |
$result = delete_option($option); |
| 100 |
|
| 101 |
if ($result) { |
| 102 |
delete_option($option_timeout); |
| 103 |
} |
| 104 |
} elseif ('site-transient' === $group) { |
| 105 |
$option_timeout = '_site_transient_timeout_'.$transient; |
| 106 |
$option = '_site_transient_'.$transient; |
| 107 |
$result = delete_site_option($option); |
| 108 |
|
| 109 |
if ($result) { |
| 110 |
delete_site_option($option_timeout); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $result; |
| 115 |
} |
| 116 |
|
| 117 |
public function match_key($key) |
| 118 |
{ |
| 119 |
return preg_match('@^(_site)?(_transient)(_timeout)?_@', $key); |
| 120 |
} |
| 121 |
} |
| 122 |
|