PluginProbe
Tag Groups is the Advanced Way to Display Your Taxonomy Terms / trunk
Tag Groups is the Advanced Way to Display Your Taxonomy Terms vtrunk
2.2.2 2.2.1 2.2.0 trunk 1.40.0 1.40.2 1.41.0 1.42.1 1.43.0 1.43.1 1.43.10 1.43.10.1 1.43.2 1.43.3 1.43.4 1.43.5 1.43.6 1.43.7 1.43.9 1.44.0 1.44.1 1.44.3 1.44.3.1 2.0.0 2.0.1 All 36 releases
tag-groups / include / helpers / cache / class.transients.php

class.transients.php in Tag Groups is the Advanced Way to Display Your Taxonomy Terms trunk, at include/helpers/cache/class.transients.php

303 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Tag Groups
5 *
6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
9 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
11 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
12 * THE SOFTWARE.
13 * @package Tag Groups
14 *
15 * @author Christoph Amthor
16 * @copyright 2018 Christoph Amthor (@ Chatty Mango, chattymango.com)
17 * @license see official vendor website
18 *
19 * @since 1.24.0
20 */
21
22 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps, WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL
23 if (! class_exists('TagGroups_Transients')) {
24 /**
25 * The main purpose of this class is to keep all cron-related information in one place.
26 *
27 */
28 class TagGroups_Transients
29 {
30 /**
31 * option name used to save array of used transient names
32 */
33 public const TRANSIENT_NAMES = 'tag_group_used_transient_names';
34
35 /**
36 * Retrieves all transients created by Tag Groups Pro and deletes them
37 *
38 * @param void
39 * @return int
40 */
41 public static function delete_all_transients_and_log()
42 {
43
44 $count = 0;
45 TagGroups_Error::verbose_log('[Tag Groups Pro] Purging all transients.');
46 $count += self::delete_all_transients();
47 TagGroups_Error::verbose_log('[Tag Groups Pro] Purged %d transients.', $count);
48 return $count;
49 }
50
51 /**
52 * retrieve the array of all transient names that were used
53 *
54 * @return array
55 */
56 public static function get_used_names()
57 {
58
59 if (empty(self::TRANSIENT_NAMES)) {
60 TagGroups_Error::log('[Tag Groups] Error: TRANSIENT_NAMES is empty or undefined.');
61 return array();
62 }
63
64 $used_transient_names = get_option(self::TRANSIENT_NAMES, array());
65 if (!is_array($used_transient_names)) {
66 TagGroups_Error::log('[Tag Groups] Retrieved transient names are not an array.');
67 $used_transient_names = array();
68 }
69
70 return $used_transient_names;
71 }
72
73 /**
74 * set the array of all transient names that were used
75 *
76 * @param array $used_transient_names
77 * @return void
78 */
79 public static function set_used_names($used_transient_names)
80 {
81
82 if (self::TRANSIENT_NAMES) {
83 update_option(self::TRANSIENT_NAMES, $used_transient_names, true);
84 // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
85 // TagGroups_Error::verbose_log( '[Tag Groups] Saved array of %d transient name(s)', count( $used_transient_names ) );
86 } else {
87 TagGroups_Error::log('[Tag Groups] Error saving array of transient names');
88 }
89 }
90
91
92 /**
93 * Wrapper for delete_transient() that keeps track of the used transient names
94 *
95 * @param [type] $transient
96 * @return void
97 */
98 public static function delete_transient($transient)
99 {
100
101 if (delete_transient($transient)) {
102 $used_transient_names = self::get_used_names();
103 if (!is_array($used_transient_names)) {
104 $used_transient_names = array();
105 }
106
107 $key = array_search($transient, $used_transient_names);
108 if ($key !== false) {
109 unset($used_transient_names[ $key ]);
110 }
111
112 self::set_used_names($used_transient_names);
113 }
114 }
115
116
117 /**
118 * Wrapper for set_transient() that keeps track of the used transient names
119 *
120 * @param string $transient
121 * @param mixed $value
122 * @param integer $expiration
123 * @return void
124 */
125 public static function set_transient($transient, $value, $expiration = null)
126 {
127
128 if (strlen($transient) > 172) {
129 TagGroups_Error::log('[Tag Groups] Transient name %s is too long!', $transient);
130 }
131
132 $used_transient_names = self::get_used_names();
133 if (! in_array($transient, $used_transient_names)) {
134 $used_transient_names[] = $transient;
135 self::set_used_names($used_transient_names);
136 }
137
138 set_transient($transient, $value, $expiration);
139 }
140
141
142 /**
143 * Wrapper for get_transient() that keeps track of the used transients
144 *
145 * @param string $transient
146 * @return mixed
147 */
148 public static function get_transient($transient)
149 {
150
151 $value = get_transient($transient);
152 if (false === $value) {
153 $used_transient_names = self::get_used_names();
154 $key = array_search($transient, $used_transient_names);
155 if (false !== $key) {
156 unset($used_transient_names[ $key ]);
157 self::set_used_names($used_transient_names);
158 }
159 }
160
161 return $value;
162 }
163
164
165 /**
166 * Deletes all transients that we know about
167 *
168 * @param string $substring Optional substring to match transient identifiers against
169 * @return integer
170 */
171 public static function delete_all_transients($substring = '')
172 {
173
174 $count = 0;
175 $used_transient_names = self::get_used_names();
176 foreach ($used_transient_names as $key => $transient) {
177 if (! empty($substring) && strpos($transient, $substring) === false) {
178 continue;
179 }
180
181 if (delete_transient($transient)) {
182 $count++;
183 unset($used_transient_names[ $key ]);
184 } else {
185 /**
186 * delete_transient may have returned false because that entry didn't exist; if not, use a hack
187 */
188 if (false === get_transient($transient)) {
189 $count++;
190 } else {
191 if (set_transient($transient, false, 1)) {
192 $count++;
193 };
194 }
195
196 unset($used_transient_names[ $key ]);
197 }
198 }
199
200 if ($count) {
201 self::set_used_names($used_transient_names);
202 }
203
204 /**
205 * Make sure no Tag Groups transients are left in the database
206 */
207 global $wpdb;
208 if (! empty($substring)) {
209 $query_string = $wpdb->prepare("DELETE FROM `{$wpdb->options}` WHERE (`option_name` LIKE %s AND `option_name` LIKE %s)", '_transient_tag_groups_%', '%' . $substring . '%');
210 $query_string_timeout = $wpdb->prepare("DELETE FROM `{$wpdb->options}` WHERE (`option_name` LIKE %s AND `option_name` LIKE %s)", '_transient_timeout_tag_groups_%', '%' . $substring . '%');
211 } else {
212 $query_string = $wpdb->prepare("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE %s", '_transient_tag_groups_%');
213 $query_string_timeout = $wpdb->prepare("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE %s", '_transient_timeout_tag_groups_%');
214 }
215
216 $count += (int) $wpdb->query($query_string);
217 $wpdb->query($query_string_timeout);
218 return $count;
219 }
220
221
222 /**
223 * Deletes all expired transients that we know about
224 *
225 * @param string $substring
226 * @return integer
227 */
228 public static function delete_all_expired_transients($substring = '')
229 {
230
231 $count = 0;
232 $used_transient_names = self::get_used_names();
233 foreach ($used_transient_names as $key => $transient) {
234 if (! empty($substring) && strpos($transient, $substring) === false) {
235 continue;
236 }
237
238 if (false === get_transient($transient)) {
239 // we never save the value false to transients, so here we know that it was expired or non-existent
240
241 $count++;
242 unset($used_transient_names[ $key ]);
243 }
244 }
245
246 if ($count) {
247 self::set_used_names($used_transient_names);
248 }
249
250 return $count;
251 }
252
253
254 /**
255 * Since many front-end features depend on transients, we delete them here if needed
256 *
257 * @return void
258 */
259 public static function clear_transients_for_frontend_features()
260 {
261
262 self::delete_all_transients('tag_groups_group_terms');
263 // including variants for languages (appended strings)
264 }
265
266
267 /**
268 * Retrieves all transients for post listings and deletes them
269 *
270 * @param void
271 * @return int
272 */
273 public static function purge_post_list_transients($post_id, $post)
274 {
275
276 // omit autosaves
277 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
278 // apparently not equivalent to wp_is_post_autosave( $post_id )
279
280 return 0;
281 }
282
283 // omit revisions
284 if ('revision' == $post->post_type) {
285 return 0;
286 }
287
288 $count = 0;
289 TagGroups_Error::verbose_log('[Tag Groups Pro] Purging post transients.');
290 // Toggle Post Filter
291 $count += self::delete_all_transients('tpf_result_');
292 // Dynamic Post Filter
293 $count += self::delete_all_transients('dpf_result_');
294 // Post List
295 $count += self::delete_all_transients('post_list_result_');
296 TagGroups_Error::verbose_log('[Tag Groups Pro] Purged %d post transient(s).', $count);
297 return $count;
298 }
299 }
300
301
302 }
303