Make WordPress Core


Ignore:
Timestamp:
10/29/2009 09:53:57 PM (14 years ago)
Author:
ryan
Message:

Introduce get/set/delete_site_transient(). Make theme_roots a site transient.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r12052 r12128  
    31913191}
    31923192
     3193function delete_site_option( $key ) {
     3194    $result = delete_option($key);
     3195    do_action( "delete_site_option_{$key}", $key );
     3196    return $result;
     3197}
     3198
    31933199// expects $key, $value not to be SQL escaped
    31943200function update_site_option( $key, $value ) {
     
    31983204    do_action( "update_site_option_{$key}", $key, $value );
    31993205    return $result;
     3206}
     3207
     3208/**
     3209 * Delete a site transient
     3210 *
     3211 * @since 2.890
     3212 * @package WordPress
     3213 * @subpackage Transient
     3214 *
     3215 * @param string $transient Transient name. Expected to not be SQL-escaped
     3216 * @return bool true if successful, false otherwise
     3217 */
     3218function delete_site_transient($transient) {
     3219    global $_wp_using_ext_object_cache, $wpdb;
     3220
     3221    if ( $_wp_using_ext_object_cache ) {
     3222        return wp_cache_delete($transient, 'site-transient');
     3223    } else {
     3224        $transient = '_site_transient_' . esc_sql($transient);
     3225        return delete_site_option($transient);
     3226    }
     3227}
     3228
     3229/**
     3230 * Get the value of a site transient
     3231 *
     3232 * If the transient does not exist or does not have a value, then the return value
     3233 * will be false.
     3234 *
     3235 * @since 2.9.0
     3236 * @package WordPress
     3237 * @subpackage Transient
     3238 *
     3239 * @param string $transient Transient name. Expected to not be SQL-escaped
     3240 * @return mixed Value of transient
     3241 */
     3242function get_site_transient($transient) {
     3243    global $_wp_using_ext_object_cache, $wpdb;
     3244
     3245    $pre = apply_filters( 'pre_site_transient_' . $transient, false );
     3246    if ( false !== $pre )
     3247        return $pre;
     3248
     3249    if ( $_wp_using_ext_object_cache ) {
     3250        $value = wp_cache_get($transient, 'site-transient');
     3251    } else {
     3252        $transient_option = '_site_transient_' . esc_sql($transient);
     3253        $transient_timeout = '_site_transient_timeout_' . esc_sql($transient);
     3254        if ( get_site_option($transient_timeout) < time() ) {
     3255            delete_site_option($transient_option);
     3256            delete_site_option($transient_timeout);
     3257            return false;
     3258        }
     3259
     3260        $value = get_site_option($transient_option);
     3261    }
     3262
     3263    return apply_filters('site_transient_' . $transient, $value);
     3264}
     3265
     3266/**
     3267 * Set/update the value of a site transient
     3268 *
     3269 * You do not need to serialize values, if the value needs to be serialize, then
     3270 * it will be serialized before it is set.
     3271 *
     3272 * @since 2.9.0
     3273 * @package WordPress
     3274 * @subpackage Transient
     3275 *
     3276 * @param string $transient Transient name. Expected to not be SQL-escaped
     3277 * @param mixed $value Transient value.
     3278 * @param int $expiration Time until expiration in seconds, default 0
     3279 * @return bool False if value was not set and true if value was set.
     3280 */
     3281function set_site_transient($transient, $value, $expiration = 0) {
     3282    global $_wp_using_ext_object_cache, $wpdb;
     3283
     3284    if ( $_wp_using_ext_object_cache ) {
     3285        return wp_cache_set($transient, $value, 'site-transient', $expiration);
     3286    } else {
     3287        $transient_timeout = '_site_transient_timeout_' . $transient;
     3288        $transient = '_site_transient_' . $transient;
     3289        $safe_transient = esc_sql($transient);
     3290        if ( false === get_site_option( $safe_transient ) ) {
     3291            if ( 0 != $expiration )
     3292                add_site_option($transient_timeout, time() + $expiration);
     3293            return add_site_option($transient, $value);
     3294        } else {
     3295            if ( 0 != $expiration )
     3296                update_site_option($transient_timeout, time() + $expiration);
     3297            return update_site_option($transient, $value);
     3298        }
     3299    }
    32003300}
    32013301
Note: See TracChangeset for help on using the changeset viewer.