Make WordPress Core


Ignore:
Timestamp:
10/21/2017 01:21:24 PM (8 years ago)
Author:
dd32
Message:

Transients: Clear expired transients from the database in a daily cron task.

Fixes #41699

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r41627 r41963  
    818818
    819819/**
     820 * Deletes all expired transients.
     821 *
     822 * The multi-table delete syntax is used to delete the transient record
     823 * from table a, and the corresponding transient_timeout record from table b.                           +
     824 *
     825 * @since 4.9.0
     826 *
     827 * @param bool $force_db Optional. Force cleanup to run against the database even when an external object cache is used.
     828 */
     829function delete_expired_transients( $force_db = false) {
     830    global $wpdb;
     831
     832    if ( ! $force_db && wp_using_ext_object_cache() ) {
     833        return;
     834    }
     835
     836    $wpdb->query( $wpdb->prepare(
     837        "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
     838            WHERE a.option_name LIKE %s
     839            AND a.option_name NOT LIKE %s
     840            AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
     841            AND b.option_value < %d",
     842        $wpdb->esc_like( '_transient_' ) . '%',
     843        $wpdb->esc_like( '_transient_timeout_' ) . '%',
     844        time()
     845    ) );
     846
     847    if ( ! is_multisite() ) {
     848        // non-Multisite stores site transients in the options table.
     849        $wpdb->query( $wpdb->prepare(
     850            "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
     851                WHERE a.option_name LIKE %s
     852                AND a.option_name NOT LIKE %s
     853                AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
     854                AND b.option_value < %d",
     855            $wpdb->esc_like( '_site_transient_' ) . '%',
     856            $wpdb->esc_like( '_site_transient_timeout_' ) . '%',
     857            time()
     858        ) );
     859    } elseif ( is_multisite() && is_main_site() && is_main_network() ) {
     860        // Multisite stores site transients in the sitemeta table.
     861        $wpdb->query( $wpdb->prepare(
     862            "DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
     863                WHERE a.meta_key LIKE %s
     864                AND a.meta_key NOT LIKE %s
     865                AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
     866                AND b.meta_value < %d",
     867            $wpdb->esc_like( '_site_transient_' ) . '%',
     868            $wpdb->esc_like( '_site_transient_timeout_' ) . '%',
     869            time()
     870        ) );
     871    }
     872}
     873
     874/**
    820875 * Saves and restores user interface settings stored in a cookie.
    821876 *
Note: See TracChangeset for help on using the changeset viewer.