-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.php
117 lines (102 loc) · 4.17 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Database functions and hooks.
*
* @package Flush_Transients
* @author Felix Arntz <hello@felix-arntz.me>
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Flushes all transients stored in the database.
*
* This method is only relevant when not using a persistent object cache.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object
*
* @param string $type Optional. Which type of transient to flush. Either 'regular' or 'network'. Default 'regular'.
* @return bool True on success, false on failure.
*/
function flush_transients_flush_db_transients( $type = 'regular' ) {
global $wpdb;
$table_name = is_multisite() && 'network' === $type ? $wpdb->sitemeta : $wpdb->options;
$table_column = is_multisite() && 'network' === $type ? 'meta_key' : 'option_name';
$transient_prefix = 'network' === $type ? '_site_transient_' : '_transient_';
$timeout_prefix = 'network' === $type ? '_site_transient_timeout_' : '_transient_timeout_';
$result = $wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"DELETE FROM {$table_name} WHERE {$table_column} LIKE %s AND {$table_column} NOT LIKE %s",
$wpdb->esc_like( $transient_prefix ) . '%',
$wpdb->esc_like( $timeout_prefix ) . '%'
)
);
if ( $result ) {
// Flush options caches (since database transients are non-persistently cached there).
if ( function_exists( 'wp_cache_flush_group' ) && function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
$cache_group = is_multisite() && 'network' === $type ? 'site-options' : 'options';
wp_cache_flush_group( $cache_group );
} else {
// No better option than full flush. This is fine though since it's a non-persistent cache anyway.
wp_cache_flush();
}
// Flush the caches for the counts.
flush_transients_invalidate_caches();
}
return false !== $result ? true : false;
}
/**
* Runs a query to get the amount of transients stored in the database.
*
* This method is only relevant when not using a persistent object cache.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object
*
* @param string $type Optional. Which type of transient to query. Either 'regular' or 'network'. Default 'regular'.
* @return int Number of transients.
*/
function flush_transients_query_db_transient_count( $type = 'regular' ) {
global $wpdb;
$transient_count = wp_cache_get( "{$type}_transient_count", 'flush_transients' );
if ( false !== $transient_count ) {
return (int) $transient_count;
}
$table_name = is_multisite() && 'network' === $type ? $wpdb->sitemeta : $wpdb->options;
$table_column = is_multisite() && 'network' === $type ? 'meta_key' : 'option_name';
$transient_prefix = 'network' === $type ? '_site_transient_' : '_transient_';
$timeout_prefix = 'network' === $type ? '_site_transient_timeout_' : '_transient_timeout_';
$transient_count = (int) $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT COUNT({$table_column}) FROM {$table_name} WHERE {$table_column} LIKE %s AND {$table_column} NOT LIKE %s",
$wpdb->esc_like( $transient_prefix ) . '%',
$wpdb->esc_like( $timeout_prefix ) . '%'
)
);
wp_cache_set( "{$type}_transient_count", $transient_count, 'flush_transients' );
return $transient_count;
}
/**
* Invalidates the caches for transient counts of all types.
*
* This method is only relevant when not using a persistent object cache.
*
* @since 1.0.0
*/
function flush_transients_invalidate_caches() {
if ( function_exists( 'wp_cache_flush_group' ) && function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ) ) {
wp_cache_flush_group( 'flush_transients' );
return;
}
wp_cache_delete( 'regular_transient_count', 'flush_transients' );
wp_cache_delete( 'network_transient_count', 'flush_transients' );
}
add_action( 'setted_transient', 'flush_transients_invalidate_caches' );
add_action( 'deleted_transient', 'flush_transients_invalidate_caches' );
add_action( 'setted_site_transient', 'flush_transients_invalidate_caches' );
add_action( 'deleted_site_transient', 'flush_transients_invalidate_caches' );