CacheBase.php
3 years ago
Chain.php
3 years ago
Crypto.php
3 years ago
File.php
3 years ago
PluginSilentUpgrader.php
3 years ago
PluginSilentUpgraderSkin.php
3 years ago
Templates.php
3 years ago
Transient.php
3 years ago
Transient.php
277 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPForms\Helpers; |
| 4 | |
| 5 | /** |
| 6 | * WPForms Transients implementation. |
| 7 | * |
| 8 | * @since 1.6.3.1 |
| 9 | */ |
| 10 | class Transient { |
| 11 | |
| 12 | /** |
| 13 | * Transient option name prefix. |
| 14 | * |
| 15 | * @since 1.6.3.1 |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | const OPTION_PREFIX = '_wpforms_transient_'; |
| 20 | |
| 21 | /** |
| 22 | * Transient timeout option name prefix. |
| 23 | * |
| 24 | * @since 1.6.3.1 |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | const TIMEOUT_PREFIX = '_wpforms_transient_timeout_'; |
| 29 | |
| 30 | /** |
| 31 | * Get the value of a transient. |
| 32 | * |
| 33 | * If the transient does not exist, does not have a value, or has expired, |
| 34 | * then the return value will be false. |
| 35 | * |
| 36 | * @since 1.6.3.1 |
| 37 | * |
| 38 | * @param string $transient Transient name. Expected to not be SQL-escaped. |
| 39 | * |
| 40 | * @return mixed Value of transient. |
| 41 | */ |
| 42 | public static function get( $transient ) { |
| 43 | |
| 44 | $transient_option = self::OPTION_PREFIX . $transient; |
| 45 | $transient_timeout = self::TIMEOUT_PREFIX . $transient; |
| 46 | |
| 47 | $alloptions = wp_load_alloptions(); |
| 48 | |
| 49 | // If option is not in alloptions, it is not autoloaded and thus has a timeout to check. |
| 50 | if ( ! isset( $alloptions[ $transient_option ] ) ) { |
| 51 | $is_expired = self::is_expired( $transient ); |
| 52 | } |
| 53 | |
| 54 | // Return the data if it's not expired. |
| 55 | if ( empty( $is_expired ) ) { |
| 56 | return self::get_option( $transient ); |
| 57 | } |
| 58 | |
| 59 | delete_option( $transient_option ); |
| 60 | delete_option( $transient_timeout ); |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set/update the value of a transient. |
| 67 | * |
| 68 | * You do not need to serialize values. If the value needs to be serialized, then |
| 69 | * it will be serialized before it is set. |
| 70 | * |
| 71 | * @since 1.6.3.1 |
| 72 | * |
| 73 | * @param string $transient Transient name. Expected to not be SQL-escaped. Must be |
| 74 | * 164 characters or fewer. |
| 75 | * @param mixed $value Transient value. Must be serializable if non-scalar. |
| 76 | * Expected to not be SQL-escaped. |
| 77 | * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
| 78 | * |
| 79 | * @return bool False if value was not set and true if value was set. |
| 80 | */ |
| 81 | public static function set( $transient, $value, $expiration = 0 ) { |
| 82 | |
| 83 | if ( false === self::get_option( $transient ) ) { |
| 84 | return self::add( $transient, $value, $expiration ); |
| 85 | } |
| 86 | |
| 87 | return self::update( $transient, $value, $expiration ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Create a new transient with a given value. |
| 92 | * |
| 93 | * Internal method, use Transient::set() instead. |
| 94 | * |
| 95 | * @since 1.6.3.1 |
| 96 | * |
| 97 | * @param string $transient Transient name. Expected to not be SQL-escaped. Must be |
| 98 | * 164 characters or fewer. |
| 99 | * @param mixed $value Transient value. Must be serializable if non-scalar. |
| 100 | * Expected to not be SQL-escaped. |
| 101 | * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
| 102 | * |
| 103 | * @return bool False if value was not set and true if value was set. |
| 104 | */ |
| 105 | private static function add( $transient, $value, $expiration ) { |
| 106 | |
| 107 | if ( $expiration ) { |
| 108 | add_option( self::TIMEOUT_PREFIX . $transient, time() + $expiration, '', 'no' ); |
| 109 | } |
| 110 | |
| 111 | // If there's an expiration, the option won't be autoloaded. |
| 112 | return add_option( self::OPTION_PREFIX . $transient, $value, '', $expiration ? 'no' : 'yes' ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Update the value of a transient. |
| 117 | * |
| 118 | * Internal method, use Transient::set() instead. |
| 119 | * |
| 120 | * @since 1.6.3.1 |
| 121 | * |
| 122 | * @param string $transient Transient name. Expected to not be SQL-escaped. Must be |
| 123 | * 164 characters or fewer. |
| 124 | * @param mixed $value Transient value. Must be serializable if non-scalar. |
| 125 | * Expected to not be SQL-escaped. |
| 126 | * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
| 127 | * |
| 128 | * @return bool False if value was not set and true if value was set. |
| 129 | */ |
| 130 | private static function update( $transient, $value, $expiration ) { |
| 131 | |
| 132 | $transient_option = self::OPTION_PREFIX . $transient; |
| 133 | $transient_timeout = self::TIMEOUT_PREFIX . $transient; |
| 134 | |
| 135 | if ( ! $expiration ) { |
| 136 | return update_option( $transient_option, $value ); |
| 137 | } |
| 138 | |
| 139 | $timeout = self::get_timeout( $transient ); |
| 140 | |
| 141 | if ( false !== $timeout ) { |
| 142 | update_option( $transient_timeout, time() + $expiration ); |
| 143 | return update_option( $transient_option, $value ); |
| 144 | } |
| 145 | |
| 146 | // If expiration is requested, but the transient has no timeout option, |
| 147 | // delete, then re-create transient rather than update. |
| 148 | delete_option( $transient_option ); |
| 149 | add_option( $transient_timeout, time() + $expiration, '', 'no' ); |
| 150 | |
| 151 | return add_option( $transient_option, $value, '', 'no' ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Delete a transient. |
| 156 | * |
| 157 | * @since 1.6.3.1 |
| 158 | * |
| 159 | * @param string $transient Transient name. Expected to not be SQL-escaped. |
| 160 | * |
| 161 | * @return bool true if successful, false otherwise |
| 162 | */ |
| 163 | public static function delete( $transient ) { |
| 164 | |
| 165 | $result = delete_option( self::OPTION_PREFIX . $transient ); |
| 166 | |
| 167 | if ( $result ) { |
| 168 | delete_option( self::TIMEOUT_PREFIX . $transient ); |
| 169 | } |
| 170 | |
| 171 | return $result; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Delete all WPForms transients. |
| 176 | * |
| 177 | * @since 1.6.3.1 |
| 178 | * |
| 179 | * @return int|false Number of rows affected/selected or false on error |
| 180 | */ |
| 181 | public static function delete_all() { |
| 182 | |
| 183 | global $wpdb; |
| 184 | |
| 185 | return $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching |
| 186 | $wpdb->prepare( |
| 187 | "DELETE FROM {$wpdb->options} |
| 188 | WHERE option_name LIKE %s", |
| 189 | $wpdb->esc_like( self::OPTION_PREFIX ) . '%' |
| 190 | ) |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Delete all expired WPForms transients. |
| 196 | * |
| 197 | * The multi-table delete syntax is used to delete the transient record |
| 198 | * from table a, and the corresponding transient_timeout record from table b. |
| 199 | * |
| 200 | * @since 1.6.3.1 |
| 201 | * |
| 202 | * @return int|false Number of rows affected/selected or false on error |
| 203 | */ |
| 204 | public static function delete_all_expired() { |
| 205 | |
| 206 | global $wpdb; |
| 207 | |
| 208 | return $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching |
| 209 | $wpdb->prepare( |
| 210 | "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b |
| 211 | WHERE a.option_name LIKE %s |
| 212 | AND a.option_name NOT LIKE %s |
| 213 | AND b.option_name = CONCAT( %s, SUBSTRING( a.option_name, %d ) ) |
| 214 | AND b.option_value < %d", |
| 215 | $wpdb->esc_like( self::OPTION_PREFIX ) . '%', |
| 216 | $wpdb->esc_like( self::TIMEOUT_PREFIX ) . '%', |
| 217 | self::TIMEOUT_PREFIX, |
| 218 | strlen( self::OPTION_PREFIX ) + 1, |
| 219 | time() |
| 220 | ) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Check if transient is expired. |
| 226 | * |
| 227 | * @since 1.6.3.1 |
| 228 | * |
| 229 | * @param string $transient Transient name. Expected to not be SQL-escaped. |
| 230 | * |
| 231 | * @return bool true if expired, false otherwise |
| 232 | */ |
| 233 | public static function is_expired( $transient ) { |
| 234 | |
| 235 | $timeout = self::get_timeout( $transient ); |
| 236 | |
| 237 | // If there's no timeout data found, the transient is considered to be valid. |
| 238 | if ( false === $timeout ) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | if ( $timeout >= time() ) { |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get a transient option value. |
| 251 | * |
| 252 | * @since 1.6.3.1 |
| 253 | * |
| 254 | * @param string $transient Transient name. Expected to not be SQL-escaped. |
| 255 | * |
| 256 | * @return mixed Value set for the option. |
| 257 | */ |
| 258 | private static function get_option( $transient ) { |
| 259 | |
| 260 | return get_option( self::OPTION_PREFIX . $transient ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Get a transient timeout option value. |
| 265 | * |
| 266 | * @since 1.6.3.1 |
| 267 | * |
| 268 | * @param string $transient Transient name. Expected to not be SQL-escaped. |
| 269 | * |
| 270 | * @return mixed Value set for the option. |
| 271 | */ |
| 272 | private static function get_timeout( $transient ) { |
| 273 | |
| 274 | return get_option( self::TIMEOUT_PREFIX . $transient ); |
| 275 | } |
| 276 | } |
| 277 |