From 859da01c4aa2373cc2033ea079f7420775366f32 Mon Sep 17 00:00:00 2001
From: Andrew Nacin <wp@andrewnacin.com>
Date: Fri, 18 Oct 2013 07:48:09 +0000
Subject: [PATCH] Delete expired transients on database upgrades.

Reverts [25416], which had all transients being cleared. This leaves much to be desired, but we don't want a core update to be blamed for breaking a site that incorrectly assumes transients aren't transient.

props dartiss, pento.
fixes #20316.

Built from https://develop.svn.wordpress.org/trunk@25838


git-svn-id: http://core.svn.wordpress.org/trunk@25750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
 wp-admin/includes/schema.php  | 20 ++++++++++++++++++--
 wp-admin/includes/upgrade.php | 14 ++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php
index 037210af34..f88f79d052 100644
--- a/wp-admin/includes/schema.php
+++ b/wp-admin/includes/schema.php
@@ -546,8 +546,24 @@ function populate_options() {
 
 	// delete obsolete magpie stuff
 	$wpdb->query("DELETE FROM $wpdb->options WHERE option_name REGEXP '^rss_[0-9a-f]{32}(_ts)?$'");
-	// clear transient data
-	$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '\_transient\_%' OR option_name LIKE '\_site\_transient\_%'" );
+
+	// Deletes all expired transients.
+	// The multi-table delete syntax is used to delete the transient record from table a,
+	// and the corresponding transient_timeout record from table b.
+	$time = time();
+	$wpdb->query("DELETE a, b FROM $wpdb->options a, $wpdb->options b WHERE
+	        a.option_name LIKE '\_transient\_%' AND
+	        a.option_name NOT LIKE '\_transient\_timeout\_%' AND
+	        b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
+	        AND b.option_value < $time");
+
+	if ( is_main_site() && is_main_network() ) {
+		$wpdb->query("DELETE a, b FROM $wpdb->options a, $wpdb->options b WHERE
+			a.option_name LIKE '\_site\_transient\_%' AND
+			a.option_name NOT LIKE '\_site\_transient\_timeout\_%' AND
+			b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
+			AND b.option_value < $time");
+    }
 }
 
 /**
diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php
index ac05991a13..b1b85555e3 100644
--- a/wp-admin/includes/upgrade.php
+++ b/wp-admin/includes/upgrade.php
@@ -1229,6 +1229,20 @@ function upgrade_370() {
  */
 function upgrade_network() {
 	global $wp_current_db_version, $wpdb;
+
+	// Always
+	if ( is_main_network() ) {
+		// Deletes all expired transients.
+		// The multi-table delete syntax is used to delete the transient record from table a,
+		// and the corresponding transient_timeout record from table b.
+		$time = time();
+		$wpdb->query("DELETE a, b FROM $wpdb->sitemeta a, $wpdb->sitemeta b WHERE
+			a.meta_key LIKE '\_site\_transient\_%' AND
+			a.meta_key NOT LIKE '\_site\_transient\_timeout\_%' AND
+			b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
+			AND b.meta_value < $time");
+	}
+
 	// 2.8
 	if ( $wp_current_db_version < 11549 ) {
 		$wpmu_sitewide_plugins = get_site_option( 'wpmu_sitewide_plugins' );