class-ldlms-transients.php
5.04 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Utility class to contain all transient and cache related functions used within LearnDash.
*
* @since 3.1.0
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'LDLMS_Transients' ) ) {
/**
* Class to create the instance.
*
* @since 3.1.0
*/
class LDLMS_Transients {
/**
* Cache Group.
*
* @since 3.4.1
*
* @var string $cache_group;
*/
private static $cache_group = 'learndash';
/**
* Private constructor for class
*
* @since 3.1.0
*/
private function __construct() {
}
/**
* Get a Transient
*
* @since 3.1.0
*
* @param string $transient_key The transient key to retrieve.
*
* @return mixed $transient_data the retrieved transient data or false if expired.
*/
public static function get( $transient_key = '' ) {
$transient_data = false;
if ( ! empty( $transient_key ) ) {
/**
* Filters whether the to bypass cache.
*
* @since 3.4.1
*
* @param bool $bypass_cache Whether to bypass the existing cache.
* @param string $transient_key Transient Key.
*
* @return bool true to skip cache. False to use cache.
*/
if ( ! apply_filters( 'learndash_bypass_cache', false, $transient_key ) ) {
/**
* Filters whether the object cache is enabled.
*
* @since 3.4.1
*
* @param boolean $cache_enabled Whether the object cache is enabled.
* @param string $transient_key Transient Key.
*/
if ( apply_filters( 'learndash_object_cache_enabled', LEARNDASH_OBJECT_CACHE_ENABLED, $transient_key ) ) {
$found = false;
$transient_data = wp_cache_get( $transient_key, self::$cache_group, false, $found );
if ( false === $found ) {
$transient_data = false;
}
} elseif (
/**
* Filters whether the transients are disabled or not.
*
* @since 2.3.3
*
* @param boolean $transients_disabled Whether the transients are disabled or not.
* @param string $transient_key Transient Key.
*/
! apply_filters( 'learndash_transients_disabled', LEARNDASH_TRANSIENTS_DISABLED, $transient_key )
) {
$transient_data = get_transient( $transient_key );
}
}
}
return $transient_data;
}
/**
* Utility function to interface with WP set_transient function. This function allow for
* filtering if to actually write the transient.
*
* @since 3.1.0
*
* @param string $transient_key The transient key.
* @param mixed $transient_data Data to store in transient.
* @param integer $transient_expire Expiration time for transient.
*/
public static function set( $transient_key = '', $transient_data = '', $transient_expire = MINUTE_IN_SECONDS ) {
if ( ! empty( $transient_key ) ) {
$transient_expire = apply_filters( 'learndash_cache_expire', $transient_expire, $transient_key );
$transient_expire = absint( $transient_expire );
if ( ! empty( $transient_expire ) ) {
/** This filter is documented in includes/class-ld-transients.php */
if ( apply_filters( 'learndash_object_cache_enabled', LEARNDASH_OBJECT_CACHE_ENABLED, $transient_key ) ) {
return wp_cache_set( $transient_key, $transient_data, self::$cache_group, $transient_expire );
} elseif (
/** This filter is documented in includes/class-ld-transients.php */
! apply_filters( 'learndash_transients_disabled', LEARNDASH_TRANSIENTS_DISABLED, $transient_key )
) {
return set_transient( $transient_key, $transient_data, $transient_expire );
}
}
}
}
/**
* Delete object cache by key.
*
* @since 3.4.1
*
* @param string $transient_key The transient key.
*/
public static function delete( $transient_key = '' ) {
if ( ! empty( $transient_key ) ) {
/** This filter is documented in includes/class-ld-transients.php */
if ( apply_filters( 'learndash_object_cache_enabled', LEARNDASH_OBJECT_CACHE_ENABLED, $transient_key ) ) {
return wp_cache_delete( $transient_key, self::$cache_group );
} elseif (
/** This filter is documented in includes/class-ld-transients.php */
! apply_filters( 'learndash_transients_disabled', LEARNDASH_TRANSIENTS_DISABLED, $transient_key )
) {
return delete_transient( $transient_key );
}
}
}
/**
* Purge all transients.
*
* @since 3.1.0
*/
public static function purge_all() {
/** This filter is documented in includes/class-ld-transients.php */
if ( apply_filters( 'learndash_object_cache_enabled', LEARNDASH_OBJECT_CACHE_ENABLED, 'learndash_all_purge' ) ) {
wp_cache_flush();
} elseif (
/** This filter is documented in includes/class-ld-transients.php */
! apply_filters( 'learndash_transients_disabled', LEARNDASH_TRANSIENTS_DISABLED, 'learndash_all_purge' )
) {
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
'_transient_learndash_%',
'_transient_timeout_learndash_%'
)
);
}
}
// End of functions.
}
}