PluginProbe ʕ •ᴥ•ʔ
Custom Twitter Feeds – A Tweets Widget or X Feed Widget / 2.7.0
Custom Twitter Feeds – A Tweets Widget or X Feed Widget v2.7.0
2.8.0 2.7.0 2.6.1 2.6.0 1.0 1.0.1 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.4 1.4.1 1.5 1.5.1 1.6 1.6.1 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1 2.1.1 2.1.2 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.5.4 2.5.5 trunk
custom-twitter-feeds / inc / Admin / SB_Twitter_Cache.php
custom-twitter-feeds / inc / Admin Last commit date
Traits 1 month ago CTF_About_Us.php 1 month ago CTF_Admin_Notices.php 1 month ago CTF_Cache_Handler.php 1 month ago CTF_Global_Settings.php 1 month ago CTF_HTTP_Request.php 1 month ago CTF_New_User.php 1 month ago CTF_Notifications.php 1 month ago CTF_Response.php 1 month ago CTF_Support.php 1 month ago CTF_Upgrader.php 1 month ago CTF_View.php 1 month ago PluginSilentUpgrader.php 1 month ago PluginSilentUpgraderSkin.php 1 month ago SB_Twitter_Cache.php 1 month ago addon-functions.php 1 month ago class-install-skin.php 1 month ago
SB_Twitter_Cache.php
593 lines
1 <?php
2
3 namespace TwitterFeed\Admin;
4
5 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
6
7 /**
8 * Custom Twitter Feed Cache.
9 *
10 * @since 2.0
11 */
12 class SB_Twitter_Cache {
13
14 /**
15 * @var int
16 */
17 private $feed_id;
18
19 /**
20 * @var int
21 */
22 private $page;
23
24 /**
25 * @var string
26 */
27 private $suffix;
28
29 /**
30 * @var bool
31 */
32 private $is_legacy;
33
34 /**
35 * @var int
36 */
37 private $cache_time;
38
39 /**
40 * @var array
41 */
42 private $posts;
43
44 /**
45 * @var array
46 */
47 private $posts_page;
48
49 /**
50 * @var bool
51 */
52 private $is_expired;
53
54 /**
55 * @var array
56 */
57 private $header;
58
59 /**
60 * @var array
61 */
62 private $resized_images;
63
64 /**
65 * @var array
66 */
67 private $meta;
68
69 /**
70 * @var array
71 */
72 private $posts_backup;
73
74 /**
75 * @var array
76 */
77 private $header_backup;
78
79
80 /**
81 * CTF_Cache constructor. Set the feed id, cache key, legacy
82 *
83 * @param string $feed_id
84 * @param int $page
85 * @param int $cache_time
86 *
87 * @since 2.0
88 */
89 public function __construct( $feed_id, $page = 1, $cache_time = 0 ) {
90 $this->cache_time = (int) $cache_time;
91 $this->is_legacy = strpos( $feed_id, '*' ) !== 0;
92 $this->page = $page;
93
94 if ( $this->page === 1 ) {
95 $this->suffix = '';
96 } else {
97 $this->suffix = '_' . $this->page;
98 }
99
100 $this->feed_id = str_replace( '*', '', $feed_id );
101
102 if ( is_admin() ) {
103 $this->feed_id .= $this->maybe_customizer_suffix();
104 }
105
106 }
107
108
109 /**
110 * Set all caches based on available data.
111 *
112 * @since 2.0
113 */
114 public function retrieve_and_set() {
115
116 $expired = true;
117 $existing_caches = $this->query_ctf_feed_caches();
118
119 foreach ( $existing_caches as $cache ) {
120 switch ( $cache['cache_key'] ) {
121 case 'posts':
122 $this->posts = $cache['cache_value'];
123 if ( strtotime( $cache['last_updated'] ) > time() - $this->cache_time ) {
124 $expired = false;
125 }
126
127 if ( empty( $cache['cache_value'] ) ) {
128 $expired = true;
129 }
130 break;
131 case 'posts' . $this->suffix:
132 $this->posts_page = $cache['cache_value'];
133 break;
134 case 'header':
135 $this->header = $cache['cache_value'];
136 break;
137 case 'resized_images' . $this->suffix:
138 $this->resized_images = $cache['cache_value'];
139 break;
140 case 'meta' . $this->suffix:
141 $this->meta = $cache['cache_value'];
142 break;
143 case 'posts_backup' . $this->suffix:
144 $this->posts_backup = $cache['cache_value'];
145 break;
146 case 'header_backup' . $this->suffix:
147 $this->header_backup = $cache['cache_value'];
148 break;
149 }
150 }
151
152 $this->is_expired = $expired;
153
154 if ( $this->cache_time < 1 ) {
155 $this->is_expired = true;
156 }
157
158 }
159
160 /**
161 * Whether or not the cache needs to be refreshed
162 *
163 * @param string $cache_type
164 *
165 * @return bool
166 *
167 * @since 2.0
168 */
169 public function is_expired( $cache_type = 'posts' ) {
170
171 if ( $cache_type !== 'posts' ) {
172 $cache = $this->get( $cache_type );
173
174 return ( empty( $cache ) || $this->is_expired );
175 }
176 if ( $this->page === 1 ) {
177 return $this->is_expired;
178 } else {
179 if ( $this->is_expired ) {
180 return true;
181 }
182 if ( empty( $this->posts_page ) ) {
183 return true;
184 }
185 }
186 return false;
187 }
188
189
190 /**
191 * Get data currently stored in the database for the type
192 *
193 * @param string $type
194 *
195 * @return string
196 *
197 * @since 2.0
198 */
199 public function get( $type ) {
200 $return = array();
201 switch ( $type ) {
202 case 'posts':
203 $return = $this->posts;
204 break;
205 case 'posts' . $this->suffix:
206 $return = $this->posts_page;
207 break;
208 case 'header':
209 $return = $this->header;
210 break;
211 case 'resized_images':
212 $return = $this->resized_images;
213 break;
214 case 'meta':
215 $return = $this->meta;
216 break;
217 case 'posts_backup':
218 $return = $this->posts_backup;
219 break;
220 case 'header_backup':
221 $return = $this->header_backup;
222 break;
223 }
224
225 return $return;
226 }
227
228 /**
229 * @param string $type
230 * @param array $cache_value
231 *
232 * @since 2.0
233 */
234 public function set( $type, $cache_value ) {
235 switch ( $type ) {
236 case 'posts':
237 $this->posts = $cache_value;
238 break;
239 case 'posts' . $this->suffix:
240 $this->posts_page = $cache_value;
241 break;
242 case 'header':
243 $this->header = $cache_value;
244 break;
245 case 'resized_images':
246 $this->resized_images = $cache_value;
247 break;
248 case 'meta':
249 $this->meta = $cache_value;
250 break;
251 case 'posts_backup':
252 $this->posts_backup = $cache_value;
253 break;
254 case 'header_backup':
255 $this->header_backup = $cache_value;
256 break;
257 }
258 }
259
260 /**
261 * Update a single cache with new data. Try to accept any data and convert it
262 * to JSON if needed
263 *
264 * @param string $cache_type
265 * @param array|object|string $cache_value
266 * @param bool $include_backup
267 * @param bool $cron_update
268 *
269 * @return int
270 *
271 * @since 2.0
272 */
273 public function update_or_insert( $cache_type, $cache_value, $include_backup = true, $cron_update = true ) {
274 $this->clear_wp_cache();
275
276 if ( $this->page > 1 || ( $cache_type !== 'posts' && $cache_type !== 'header' ) ) {
277 $cron_update = false;
278 }
279
280 if ( strpos( $this->feed_id, '_CUSTOMIZER' ) !== false ) {
281 $cron_update = false;
282 }
283
284 $cache_key = $cache_type . $this->suffix;
285
286 $this->set( $cache_key, $cache_value );
287
288 if ( is_array( $cache_value ) || is_object( $cache_value ) ) {
289 $cache_value = ctf_json_encode( $cache_value );
290 }
291
292 global $wpdb;
293 $cache_table_name = $wpdb->prefix . 'ctf_feed_caches';
294
295 $sql = $wpdb->prepare(
296 "
297 SELECT * FROM $cache_table_name
298 WHERE feed_id = %s
299 AND cache_key = %s",
300 $this->feed_id,
301 $cache_key
302 );
303
304 $existing = $wpdb->get_results( $sql, ARRAY_A );
305 $data = array();
306 $where = array();
307 $format = array();
308
309 $data['cache_value'] = $cache_value;
310 $format[] = '%s';
311
312 $data['last_updated'] = date( 'Y-m-d H:i:s' );
313 $format[] = '%s';
314
315 if ( ! empty( $existing[0] ) ) {
316 $where['feed_id'] = $this->feed_id;
317 $where_format[] = '%s';
318
319 $where['cache_key'] = $cache_key;
320 $where_format[] = '%s';
321
322 $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format );
323 } else {
324 $data['cache_key'] = $cache_key;
325 $format[] = '%s';
326
327 $data['cron_update'] = $cron_update === true ? 'yes' : '';
328 $format[] = '%s';
329
330 $data['feed_id'] = $this->feed_id;
331 $format[] = '%s';
332
333 $affected = $wpdb->insert( $cache_table_name, $data, $format );
334 }
335
336 return $affected;
337 }
338
339 /**
340 * Tasks to do after a new set of posts are retrieved
341 *
342 * @since 2.0
343 */
344 public function after_new_posts_retrieved() {
345 if ( $this->page === 1 ) {
346 $this->clear( 'all' );
347 }
348 }
349
350 /**
351 * Resets caches after they expire
352 *
353 * @param string $type
354 *
355 * @return bool|false|int
356 *
357 * @since 2.0
358 */
359 public function clear( $type ) {
360 $this->clear_wp_cache();
361
362 global $wpdb;
363 $cache_table_name = $wpdb->prefix . 'ctf_feed_caches';
364
365 $feed_id = str_replace( array( '_CUSTOMIZER', '_CUSTOMIZER_MODMODE' ), '', $this->feed_id );
366
367 if ( $type === 'all' ) {
368 $affected = $wpdb->query(
369 $wpdb->prepare(
370 "UPDATE $cache_table_name
371 SET cache_value = ''
372 WHERE feed_id = %s
373 AND cache_key NOT IN ( 'posts', 'posts_backup', 'header_backup' );",
374 $feed_id
375 )
376 );
377
378 $affected = $wpdb->query(
379 $wpdb->prepare(
380 "UPDATE $cache_table_name
381 SET cache_value = ''
382 WHERE feed_id = %s",
383 $feed_id . '_CUSTOMIZER'
384 )
385 );
386
387 $mod_mode_where = esc_sql( $feed_id ) . '_CUSTOMIZER_MODMODE%';
388 $affected = $wpdb->query(
389 $wpdb->prepare(
390 "UPDATE $cache_table_name
391 SET cache_value = ''
392 WHERE feed_id like %s",
393 $mod_mode_where
394 )
395 );
396 } else {
397
398 $data = array( 'cache_value' => '' );
399 $format = array( '%s' );
400
401 $where['feed_id'] = $feed_id;
402 $where_format[] = '%s';
403
404 $where['cache_key'] = $type . $this->suffix;
405 $where_format[] = '%s';
406
407 $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format );
408
409 $where['feed_id'] = $feed_id . '_CUSTOMIZER';
410
411 $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format );
412
413 $where['feed_id'] = $feed_id . '_CUSTOMIZER_MODMODE';
414
415 $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format );
416 }
417
418 return $affected;
419 }
420
421 public function get_customizer_cache() {
422 if ( strpos( $this->feed_id, '_CUSTOMIZER' ) === false ) {
423 $feed_id = $this->feed_id . '_CUSTOMIZER';
424 } else {
425 $feed_id = $this->feed_id;
426 }
427 global $wpdb;
428 $cache_table_name = $wpdb->prefix . 'ctf_feed_caches';
429
430 $sql = $wpdb->prepare(
431 "
432 SELECT * FROM $cache_table_name
433 WHERE feed_id = %s
434 AND cache_key = 'posts'",
435 $feed_id
436 );
437 $results = $wpdb->get_results( $sql, ARRAY_A );
438
439 $return = array();
440 if ( ! empty( $results[0] ) ) {
441 $return = $results[0]['cache_value'];
442 $return = json_decode( $return, true );
443
444 $return = isset( $return['data'] ) ? $return['data'] : array();
445 }
446
447 return $return;
448 }
449
450 /**
451 * Clears caches in the WP Options table used mostly by legacy feeds.
452 * Also resets caches created by common page caching plugins
453 *
454 * @param false $hard_clear
455 * @since 2.0
456 */
457 public static function clear_legacy( $hard_clear = false ) {
458 global $wpdb;
459 $cache_table_name = $wpdb->prefix . 'ctf_feed_caches';
460
461 if ( $hard_clear ) {
462 $affected = $wpdb->query(
463 "DELETE FROM $cache_table_name
464 WHERE feed_id LIKE ('ctf\_%')
465 AND cache_key NOT IN ( 'posts_backup', 'header_backup' );"
466 );
467 } else {
468 $affected = $wpdb->query(
469 "UPDATE $cache_table_name
470 SET cache_value = ''
471 WHERE feed_id LIKE ('ctf\_%')
472 AND cache_key NOT IN ( 'posts_backup', 'header_backup' );"
473 );
474 }
475
476
477
478 }
479
480 /**
481 * Get all available caches from the ctf_cache table.
482 *
483 * @return array
484 *
485 * @since 2.0
486 */
487 public function query_ctf_feed_caches() {
488 $feed_cache = wp_cache_get( $this->get_wp_cache_key() );
489
490 if ( false === $feed_cache ) {
491 global $wpdb;
492 $cache_table_name = $wpdb->prefix . 'ctf_feed_caches';
493
494 if ( $this->page === 1 ) {
495 $sql = $wpdb->prepare(
496 "
497 SELECT * FROM $cache_table_name
498 WHERE feed_id = %s",
499 $this->feed_id
500 );
501 } else {
502 $sql = $wpdb->prepare(
503 "
504 SELECT * FROM $cache_table_name
505 WHERE feed_id = %s
506 AND cache_key IN ( 'posts', %s, %s, %s )",
507 $this->feed_id,
508 'posts_' . $this->page,
509 'resized_images_' . $this->page,
510 'meta_' . $this->page
511 );
512 }
513
514 $feed_cache = $wpdb->get_results( $sql, ARRAY_A );
515 wp_cache_set( $this->get_wp_cache_key(), $feed_cache );
516 }
517 return $feed_cache;
518 }
519
520 /**
521 * Delete the wp_cache
522 *
523 * @since 2.0
524 */
525 private function clear_wp_cache() {
526 wp_cache_delete( $this->get_wp_cache_key() );
527 }
528
529 /**
530 * Key used to get the wp cache key
531 *
532 * @return string
533 *
534 * @since 2.0
535 */
536 private function get_wp_cache_key() {
537 return 'ctf_feed_' . $this->feed_id . '_' . $this->page;
538 }
539 /**
540 * Add suffix to cache keys used in the customizer
541 *
542 * @return string
543 *
544 * @since 2.0
545 */
546 private function maybe_customizer_suffix() {
547 $additional_suffix = '';
548 $in_customizer = ! empty( $_POST['previewSettings'] ) || ( isset( $_GET['page'] ) && $_GET['page'] === 'ctf-feed-builder' );
549 if ( $in_customizer ) {
550 $additional_suffix .= '_CUSTOMIZER';
551 }
552
553 return $additional_suffix;
554 }
555
556 public function update_last_updated() {
557 global $wpdb;
558 $cache_table_name = $wpdb->prefix . 'ctf_feed_caches';
559
560 $data = array();
561 $format = array();
562 $where_format = array();
563
564 $data['last_updated'] = date( 'Y-m-d H:i:s' );
565 $format[] = '%s';
566
567 $where['feed_id'] = $this->feed_id;
568 $where_format[] = '%s';
569
570 $where['cache_key'] = 'posts';
571 $where_format[] = '%s';
572
573 $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format );
574
575 $data = array();
576 $format = array();
577 $where_format = array();
578
579 $data['last_updated'] = date( 'Y-m-d H:i:s' );
580 $format[] = '%s';
581
582 $where['feed_id'] = $this->feed_id;
583 $where_format[] = '%s';
584
585 $where['cache_key'] = 'header';
586 $where_format[] = '%s';
587
588 $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format );
589
590 return $affected;
591 }
592
593 }