PluginProbe ʕ •ᴥ•ʔ
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress / 4.7.5
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress v4.7.5
4.8.1 trunk 1.0 1.1 1.12.1 1.2.3 1.2.4 1.2.5 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5 1.5.1 1.5.2 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.5.1 1.6.6 1.6.6.1 1.6.6.2 1.6.6.3 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.9.0 1.9.1 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.8.1 1.9.9 1.9.9.1 1.9.9.2 1.9.9.3 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.10 2.11 2.11.1 2.12 2.12.1 2.12.2 2.12.3 2.12.4 2.13 2.14 2.14.1 2.15 2.15.1 2.16 2.16.1 2.17 2.17.1 2.18 2.18.1 2.18.2 2.18.3 2.19 2.19.1 2.19.2 2.19.3 2.2 2.2.1 2.3 2.3.1 2.3.10 2.3.2 2.3.3 2.3.4 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.1.1 2.4.1.2 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5 2.5.1 2.5.2 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.7 2.7.1 2.7.2 2.8 2.9 2.9.1 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.1 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.7.5 4.7.6 4.7.7
custom-facebook-feed / inc / CFF_Cache.php
custom-facebook-feed / inc Last commit date
Admin 4 months ago Builder 4 months ago Helpers 4 months ago Integrations 4 months ago CFF_Autolink.php 4 months ago CFF_Blocks.php 4 months ago CFF_Cache.php 4 months ago CFF_Education.php 4 months ago CFF_Elementor_Base.php 4 months ago CFF_Elementor_Widget.php 4 months ago CFF_Error_Reporter.php 4 months ago CFF_FB_Settings.php 4 months ago CFF_Feed_Elementor_Control.php 4 months ago CFF_Feed_Locator.php 4 months ago CFF_Feed_Pro.php 4 months ago CFF_GDPR_Integrations.php 4 months ago CFF_Group_Posts.php 4 months ago CFF_HTTP_Request.php 4 months ago CFF_Oembed.php 4 months ago CFF_Parse.php 4 months ago CFF_Resizer.php 4 months ago CFF_Response.php 4 months ago CFF_Shortcode.php 4 months ago CFF_Shortcode_Display.php 4 months ago CFF_SiteHealth.php 4 months ago CFF_Utils.php 4 months ago CFF_View.php 4 months ago Custom_Facebook_Feed.php 4 months ago Email_Notification.php 4 months ago Platform_Data.php 4 months ago SB_Facebook_Data_Encryption.php 4 months ago SB_Facebook_Data_Manager.php 4 months ago index.php 4 months ago
CFF_Cache.php
620 lines
1 <?php
2
3 /**
4 * Custom Facebook Feed Cache
5 *
6 * For the new feed builder
7 *
8 * @since 4.0
9 */
10
11 namespace CustomFacebookFeed;
12
13 use CustomFacebookFeed\SB_Facebook_Data_Encryption;
14
15 if (!defined('ABSPATH')) {
16 exit; // Exit if accessed directly
17 }
18
19 class CFF_Cache
20 {
21 /**
22 * @var int
23 */
24 private $feed_id;
25
26 /**
27 * @var int
28 */
29 private $page;
30
31 /**
32 * @var string
33 */
34 private $suffix;
35
36 /**
37 * @var bool
38 */
39 private $is_legacy;
40
41 /**
42 * @var int
43 */
44 private $cache_time;
45
46 /**
47 * @var array
48 */
49 private $posts;
50
51 /**
52 * @var array
53 */
54 private $posts_page;
55
56 /**
57 * @var bool
58 */
59 private $is_expired;
60
61 /**
62 * @var array
63 */
64 private $header;
65
66 /**
67 * @var array
68 */
69 private $resized_images;
70
71 /**
72 * @var array
73 */
74 private $meta;
75
76 /**
77 * @var array
78 */
79 private $posts_backup;
80
81 /**
82 * @var array
83 */
84 private $header_backup;
85
86 /**
87 * @var object|SB_Facebook_Data_Encryption
88 */
89 protected $encryption;
90
91 /**
92 * CFF_Cache constructor. Set the feed id, cache key, legacy
93 *
94 * @param string $feed_id
95 * @param int $page
96 * @param int $cache_time
97 * @param bool $is_legacy
98 */
99 public function __construct($feed_id, $page = 1, $cache_time = 0, $is_legacy = false)
100 {
101 $this->cache_time = (int)$cache_time;
102 $this->is_legacy = $is_legacy;
103 $this->page = $page;
104
105 if ($this->page === 1) {
106 $this->suffix = '';
107 } else {
108 $this->suffix = '_' . $this->page;
109 }
110 $this->feed_id = $feed_id;
111 $this->encryption = new SB_Facebook_Data_Encryption();
112 }
113
114 /**
115 * Set all caches based on available data.
116 *
117 * @since 4.0
118 */
119 public function retrieve_and_set()
120 {
121
122 if (! $this->is_legacy) {
123 $expired = true;
124 $existing_caches = $this->query_cff_feed_caches();
125
126 foreach ($existing_caches as $cache) {
127 switch ($cache['cache_key']) {
128 case 'posts':
129 $this->posts = $cache['cache_value'];
130 if (strtotime($cache['last_updated']) > time() - $this->cache_time) {
131 $expired = false;
132 }
133
134 if (empty($cache['cache_value'])) {
135 $expired = true;
136 }
137 break;
138 case 'posts' . $this->suffix:
139 $this->posts_page = $cache['cache_value'];
140 break;
141 case 'header':
142 $this->header = $cache['cache_value'];
143 break;
144 case 'resized_images' . $this->suffix:
145 $this->resized_images = $cache['cache_value'];
146 break;
147 case 'meta' . $this->suffix:
148 $this->meta = $cache['cache_value'];
149 break;
150 case 'posts_backup' . $this->suffix:
151 $this->posts_backup = $cache['cache_value'];
152 break;
153 case 'header_backup' . $this->suffix:
154 $this->posts_backup = $cache['cache_value'];
155 break;
156 }
157 }
158
159 $this->is_expired = $expired;
160 } else {
161 $transient_cache = get_transient($this->feed_id);
162
163 if ($transient_cache) {
164 if (strpos($this->feed_id, 'cff_header_') !== false) {
165 $this->header = $transient_cache;
166 $this->is_expired = false;
167 } else {
168 $this->posts = $transient_cache;
169 $this->is_expired = false;
170 }
171 } else {
172 $this->posts = array();
173 $this->is_expired = true;
174 }
175 $this->posts_backup = get_transient('!cff_backup_' . $this->feed_id);
176 }
177
178 if ($this->cache_time < 1) {
179 $this->is_expired = true;
180 }
181 }
182
183 /**
184 * Whether or not the cache needs to be refreshed
185 *
186 * @param string $cache_type
187 *
188 * @return bool
189 *
190 * @since 4.0
191 */
192 public function is_expired($cache_type = 'posts')
193 {
194
195 if ($cache_type !== 'posts') {
196 $cache = $this->get($cache_type);
197
198 return (empty($cache) || $this->is_expired);
199 }
200 if ($this->page === 1) {
201 return $this->is_expired;
202 } else {
203 if ($this->is_expired) {
204 return true;
205 }
206 if (empty($this->posts_page)) {
207 return true;
208 }
209 }
210 return false;
211 }
212
213 /**
214 * Get data currently stored in the database for the type
215 *
216 * @param string $type
217 *
218 * @return array
219 *
220 * @since 4.0
221 */
222 public function get($type)
223 {
224 $return = array();
225 switch ($type) {
226 case 'posts':
227 $return = $this->posts;
228 break;
229 case 'posts' . $this->suffix:
230 $return = $this->posts_page;
231 break;
232 case 'header':
233 $return = $this->header;
234 break;
235 case 'resized_images':
236 $return = $this->resized_images;
237 break;
238 case 'meta':
239 $return = $this->meta;
240 break;
241 case 'posts_backup':
242 $return = $this->posts_backup;
243 break;
244 case 'header_backup':
245 $return = $this->header_backup;
246 break;
247 }
248 return $this->maybe_decrypt($return);
249 }
250
251 /**
252 * @param string $type
253 * @param array $cache_value
254 *
255 * @since 4.0
256 */
257 public function set($type, $cache_value)
258 {
259 switch ($type) {
260 case 'posts':
261 $this->posts = $cache_value;
262 break;
263 case 'posts' . $this->suffix:
264 $this->posts_page = $cache_value;
265 break;
266 case 'header':
267 $this->header = $cache_value;
268 break;
269 case 'resized_images':
270 $this->resized_images = $cache_value;
271 break;
272 case 'meta':
273 $this->meta = $cache_value;
274 break;
275 case 'posts_backup':
276 $this->posts_backup = $cache_value;
277 break;
278 case 'header_backup':
279 $this->header_backup = $cache_value;
280 break;
281 }
282 }
283
284 /**
285 * Update a single cache with new data. Try to accept any data and convert it
286 * to JSON if needed
287 *
288 * @param string $cache_type
289 * @param array|object|string $cache_value
290 * @param bool $include_backup
291 * @param bool $cron_update
292 *
293 * @return int
294 *
295 * @since 4.0
296 */
297 public function update_or_insert($cache_type, $cache_value, $include_backup = true, $cron_update = true)
298 {
299 $this->clear_wp_cache();
300
301 if ($this->page > 1 || ($cache_type !== 'posts' && $cache_type !== 'header')) {
302 $cron_update = false;
303 }
304
305 $cache_key = $cache_type . $this->suffix;
306
307 $this->set($cache_key, $cache_value);
308
309 if (is_array($cache_value) || is_object($cache_value)) {
310 $cache_value = CFF_Utils::cff_json_encode($cache_value);
311 }
312
313 $encrypted_cache_value = $this->maybe_encrypt($cache_value);
314
315 if ($this->is_legacy) {
316 if ($cache_key === 'posts') {
317 set_transient($this->feed_id, $encrypted_cache_value, $this->cache_time);
318 if ($include_backup) {
319 set_transient('!cff_backup_' . $this->feed_id, $encrypted_cache_value, YEAR_IN_SECONDS);
320 }
321 } elseif (strpos($cache_key, 'posts') !== false) {
322 set_transient($this->feed_id, $encrypted_cache_value, $this->cache_time);
323 } elseif (strpos($cache_key, 'header') !== false) {
324 set_transient($this->feed_id, $encrypted_cache_value, $this->cache_time);
325 }
326
327 return 1;
328 }
329
330 global $wpdb;
331 $cache_table_name = $wpdb->prefix . 'cff_feed_caches';
332
333 $sql = $wpdb->prepare("
334 SELECT * FROM $cache_table_name
335 WHERE feed_id = %d
336 AND cache_key = %s", $this->feed_id, $cache_key);
337
338 $existing = $wpdb->get_results($sql, ARRAY_A);
339 $data = array();
340 $where = array();
341 $format = array();
342
343 $data['cache_value'] = $this->maybe_encrypt($cache_value);
344 $format[] = '%s';
345
346 $data['last_updated'] = date('Y-m-d H:i:s');
347 $format[] = '%s';
348
349 if (! empty($existing[0])) {
350 $where['feed_id'] = $this->feed_id;
351 $where_format[] = '%d';
352
353 $where['cache_key'] = $cache_key;
354 $where_format[] = '%s';
355
356 $affected = $wpdb->update($cache_table_name, $data, $where, $format, $where_format);
357 } else {
358 $data['cache_key'] = $cache_key;
359 $format[] = '%s';
360
361 $data['cron_update'] = $cron_update === true ? 'yes' : '';
362 $format[] = '%s';
363
364 $data['feed_id'] = $this->feed_id;
365 $format[] = '%d';
366
367 $affected = $wpdb->insert($cache_table_name, $data, $format);
368 }
369
370 return $affected;
371 }
372
373 /**
374 * Tasks to do after a new set of posts are retrieved
375 *
376 * @since 4.0
377 */
378 public function after_new_posts_retrieved()
379 {
380 if ($this->page === 1) {
381 $this->clear('all');
382 }
383 }
384
385 /**
386 * Resets caches after they expire
387 *
388 * @param string $type
389 *
390 * @return bool|false|int
391 */
392 public function clear($type)
393 {
394 $this->clear_wp_cache();
395
396 global $wpdb;
397 $cache_table_name = $wpdb->prefix . 'cff_feed_caches';
398
399 if ($type === 'all') {
400 $affected = $wpdb->query($wpdb->prepare(
401 "UPDATE $cache_table_name
402 SET cache_value = ''
403 WHERE feed_id = %d
404 AND cache_key NOT IN ( 'posts', 'posts_backup', 'header_backup' );",
405 $this->feed_id
406 ));
407 } else {
408 $data = [ 'cache_value' => '' ];
409 $format = [ '%s' ];
410
411 $where['feed_id'] = $this->feed_id;
412 $where_format[] = '%d';
413
414 $where['cache_key'] = $type . $this->suffix;
415 $where_format[] = '%s';
416
417 $affected = $wpdb->update($cache_table_name, $data, $where, $format, $where_format);
418 }
419
420 return $affected;
421 }
422
423 /**
424 * Clears caches in the WP Options table used mostly by legacy feeds.
425 */
426 public static function clear_legacy()
427 {
428 global $wpdb;
429
430 $cache_table_name = $wpdb->prefix . 'cff_feed_caches';
431
432 $sql = "
433 UPDATE $cache_table_name
434 SET cache_value = ''
435 WHERE cache_key = 'posts';";
436 $wpdb->query($sql);
437
438 $table_name = $wpdb->prefix . "options";
439 $wpdb->query("
440 DELETE
441 FROM $table_name
442 WHERE `option_name` LIKE ('%\_transient\_cff\_%')
443 ");
444 $wpdb->query("
445 DELETE
446 FROM $table_name
447 WHERE `option_name` LIKE ('%\_transient\_cff\_ej\_%')
448 ");
449 $wpdb->query("
450 DELETE
451 FROM $table_name
452 WHERE `option_name` LIKE ('%\_transient\_cff\_tle\_%')
453 ");
454 $wpdb->query("
455 DELETE
456 FROM $table_name
457 WHERE `option_name` LIKE ('%\_transient\_cff\_album\_%')
458 ");
459 $wpdb->query("
460 DELETE
461 FROM $table_name
462 WHERE `option_name` LIKE ('%\_transient\_timeout\_cff\_%')
463 ");
464 }
465
466 /**
467 * Clears caches in the CFF Feed Caches table.
468 */
469 public static function clear_all_builder()
470 {
471 global $wpdb;
472 $cache_table_name = $wpdb->prefix . 'cff_feed_caches';
473
474 $affected = $wpdb->query($wpdb->prepare(
475 "UPDATE $cache_table_name
476 SET cache_value = '', last_updated = '0000-00-00 00:00:00'
477 WHERE cron_update = %s;",
478 'yes'
479 ));
480
481 return $affected;
482 }
483
484 /**
485 * Clears popular page caching solutions
486 */
487 public static function clear_page_caches()
488 {
489 // Clear cache of major caching plugins
490 if (isset($GLOBALS['wp_fastest_cache']) && method_exists($GLOBALS['wp_fastest_cache'], 'deleteCache')) {
491 $GLOBALS['wp_fastest_cache']->deleteCache();
492 }
493 // WP Super Cache
494 if (function_exists('wp_cache_clear_cache')) {
495 wp_cache_clear_cache();
496 }
497 // W3 Total Cache
498 if (function_exists('w3tc_flush_all')) {
499 w3tc_flush_all();
500 }
501 if (function_exists('sg_cachepress_purge_cache')) {
502 sg_cachepress_purge_cache();
503 }
504
505 // Litespeed Cache (older method)
506 if (method_exists('LiteSpeed_Cache_API', 'purge')) {
507 LiteSpeed_Cache_API::purge('esi.custom-facebook-feed');
508 }
509
510 // Litespeed Cache (new method)
511 if (has_action('litespeed_purge')) {
512 do_action('litespeed_purge', 'esi.custom-facebook-feed');
513 }
514 }
515
516 /**
517 * Get all available caches from the cff_cache table.
518 *
519 * @return array
520 *
521 * @since 4.0
522 */
523 private function query_cff_feed_caches()
524 {
525 $feed_cache = wp_cache_get($this->get_wp_cache_key());
526 if (false === $feed_cache) {
527 global $wpdb;
528 $cache_table_name = $wpdb->prefix . 'cff_feed_caches';
529
530 if ($this->page === 1) {
531 $sql = $wpdb->prepare("
532 SELECT * FROM $cache_table_name
533 WHERE feed_id = %s", $this->feed_id);
534 } else {
535 $sql = $wpdb->prepare(
536 "
537 SELECT * FROM $cache_table_name
538 WHERE feed_id = %s
539 AND cache_key IN ( 'posts', %s, %s, %s )",
540 $this->feed_id,
541 'posts_' . $this->page,
542 'resized_images_' . $this->page,
543 'meta_' . $this->page
544 );
545 }
546
547 $feed_cache = $wpdb->get_results($sql, ARRAY_A);
548
549 wp_cache_set($this->get_wp_cache_key(), $feed_cache);
550 }
551
552 return $feed_cache;
553 }
554
555 /**
556 * Delete the wp_cache
557 *
558 * @since 4.0
559 */
560 private function clear_wp_cache()
561 {
562 wp_cache_delete($this->get_wp_cache_key());
563 }
564
565 /**
566 * Key used to get the wp cache key
567 *
568 * @return string
569 *
570 * @since 4.0
571 */
572 private function get_wp_cache_key()
573 {
574 return 'cff_feed_' . $this->feed_id . '_' . $this->page;
575 }
576 /**
577 * Uses a raw value and attempts to encrypt it
578 *
579 * @param $value
580 *
581 * @return bool|string
582 */
583 private function maybe_encrypt($value)
584 {
585 if (! empty($value) && ! is_string($value)) {
586 $value = cff_json_encode($value);
587 }
588 if (empty($value)) {
589 return $value;
590 }
591
592 return $this->encryption->encrypt($value);
593 }
594
595 /**
596 * Uses a raw value and attempts to decrypt it
597 *
598 * @param $value
599 *
600 * @return bool|string
601 */
602 private function maybe_decrypt($value)
603 {
604 if (! is_string($value)) {
605 return $value;
606 }
607 if (strpos($value, '{') === 0) {
608 return $value;
609 }
610
611 $decrypted = $this->encryption->decrypt($value);
612
613 if (! $decrypted) {
614 return $value;
615 }
616
617 return $decrypted;
618 }
619 }
620