Admin
4 years ago
Builder
4 years ago
Helpers
4 years ago
CFF_Autolink.php
4 years ago
CFF_Blocks.php
4 years ago
CFF_Cache.php
4 years ago
CFF_Education.php
4 years ago
CFF_Elementor_Base.php
4 years ago
CFF_Elementor_Widget.php
4 years ago
CFF_Error_Reporter.php
4 years ago
CFF_FB_Settings.php
4 years ago
CFF_Feed_Elementor_Control.php
4 years ago
CFF_Feed_Locator.php
4 years ago
CFF_Feed_Pro.php
4 years ago
CFF_GDPR_Integrations.php
4 years ago
CFF_Group_Posts.php
4 years ago
CFF_HTTP_Request.php
4 years ago
CFF_Oembed.php
4 years ago
CFF_Parse.php
4 years ago
CFF_Resizer.php
4 years ago
CFF_Response.php
4 years ago
CFF_Shortcode.php
4 years ago
CFF_Shortcode_Display.php
4 years ago
CFF_SiteHealth.php
4 years ago
CFF_Utils.php
4 years ago
CFF_View.php
4 years ago
Custom_Facebook_Feed.php
4 years ago
SB_Facebook_Data_Encryption.php
4 years ago
SB_Facebook_Data_Manager.php
4 years ago
CFF_Cache.php
600 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Custom Facebook Feed Cache |
| 4 | * |
| 5 | * For the new feed builder |
| 6 | * |
| 7 | * @since 4.0 |
| 8 | */ |
| 9 | |
| 10 | namespace CustomFacebookFeed; |
| 11 | use CustomFacebookFeed\SB_Facebook_Data_Encryption; |
| 12 | |
| 13 | class CFF_Cache { |
| 14 | |
| 15 | /** |
| 16 | * @var int |
| 17 | */ |
| 18 | private $feed_id; |
| 19 | |
| 20 | /** |
| 21 | * @var int |
| 22 | */ |
| 23 | private $page; |
| 24 | |
| 25 | /** |
| 26 | * @var string |
| 27 | */ |
| 28 | private $suffix; |
| 29 | |
| 30 | /** |
| 31 | * @var bool |
| 32 | */ |
| 33 | private $is_legacy; |
| 34 | |
| 35 | /** |
| 36 | * @var int |
| 37 | */ |
| 38 | private $cache_time; |
| 39 | |
| 40 | /** |
| 41 | * @var array |
| 42 | */ |
| 43 | private $posts; |
| 44 | |
| 45 | /** |
| 46 | * @var array |
| 47 | */ |
| 48 | private $posts_page; |
| 49 | |
| 50 | /** |
| 51 | * @var bool |
| 52 | */ |
| 53 | private $is_expired; |
| 54 | |
| 55 | /** |
| 56 | * @var array |
| 57 | */ |
| 58 | private $header; |
| 59 | |
| 60 | /** |
| 61 | * @var array |
| 62 | */ |
| 63 | private $resized_images; |
| 64 | |
| 65 | /** |
| 66 | * @var array |
| 67 | */ |
| 68 | private $meta; |
| 69 | |
| 70 | /** |
| 71 | * @var array |
| 72 | */ |
| 73 | private $posts_backup; |
| 74 | |
| 75 | /** |
| 76 | * @var array |
| 77 | */ |
| 78 | private $header_backup; |
| 79 | |
| 80 | /** |
| 81 | * @var object|SB_Facebook_Data_Encryption |
| 82 | */ |
| 83 | protected $encryption; |
| 84 | |
| 85 | /** |
| 86 | * CFF_Cache constructor. Set the feed id, cache key, legacy |
| 87 | * |
| 88 | * @param string $feed_id |
| 89 | * @param int $page |
| 90 | * @param int $cache_time |
| 91 | * @param bool $is_legacy |
| 92 | */ |
| 93 | public function __construct( $feed_id, $page = 1, $cache_time = 0, $is_legacy = false ) { |
| 94 | $this->cache_time = (int)$cache_time; |
| 95 | $this->is_legacy = $is_legacy; |
| 96 | $this->page = $page; |
| 97 | |
| 98 | if ( $this->page === 1 ) { |
| 99 | $this->suffix = ''; |
| 100 | } else { |
| 101 | $this->suffix = '_' . $this->page; |
| 102 | } |
| 103 | |
| 104 | $this->feed_id = $feed_id; |
| 105 | $this->encryption = new SB_Facebook_Data_Encryption(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Set all caches based on available data. |
| 110 | * |
| 111 | * @since 4.0 |
| 112 | */ |
| 113 | public function retrieve_and_set() { |
| 114 | |
| 115 | if ( ! $this->is_legacy ) { |
| 116 | $expired = true; |
| 117 | $existing_caches = $this->query_cff_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->posts_backup = $cache['cache_value']; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
| 153 | $this->is_expired = $expired; |
| 154 | |
| 155 | } else { |
| 156 | $transient_cache = get_transient( $this->feed_id ); |
| 157 | |
| 158 | if ( $transient_cache ) { |
| 159 | if ( strpos( $this->feed_id, 'cff_header_') !== false ) { |
| 160 | $this->header = $transient_cache; |
| 161 | $this->is_expired = false; |
| 162 | } else { |
| 163 | $this->posts = $transient_cache; |
| 164 | $this->is_expired = false; |
| 165 | } |
| 166 | |
| 167 | } else { |
| 168 | $this->posts = array(); |
| 169 | $this->is_expired = true; |
| 170 | } |
| 171 | $this->posts_backup = get_transient( '!cff_backup_' . $this->feed_id ); |
| 172 | } |
| 173 | |
| 174 | if ( $this->cache_time < 1 ) { |
| 175 | $this->is_expired = true; |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Whether or not the cache needs to be refreshed |
| 182 | * |
| 183 | * @param string $cache_type |
| 184 | * |
| 185 | * @return bool |
| 186 | * |
| 187 | * @since 4.0 |
| 188 | */ |
| 189 | public function is_expired( $cache_type = 'posts' ) { |
| 190 | |
| 191 | if ( $cache_type !== 'posts' ) { |
| 192 | $cache = $this->get( $cache_type ); |
| 193 | |
| 194 | return (empty( $cache ) || $this->is_expired); |
| 195 | } |
| 196 | if ( $this->page === 1 ) { |
| 197 | return $this->is_expired; |
| 198 | } else { |
| 199 | if ( $this->is_expired ) { |
| 200 | return true; |
| 201 | } |
| 202 | if ( empty( $this->posts_page ) ) { |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get data currently stored in the database for the type |
| 211 | * |
| 212 | * @param string $type |
| 213 | * |
| 214 | * @return array |
| 215 | * |
| 216 | * @since 4.0 |
| 217 | */ |
| 218 | public function get( $type ) { |
| 219 | $return = array(); |
| 220 | switch( $type ) { |
| 221 | case 'posts': |
| 222 | $return = $this->posts; |
| 223 | break; |
| 224 | case 'posts' . $this->suffix: |
| 225 | $return = $this->posts_page; |
| 226 | break; |
| 227 | case 'header': |
| 228 | $return = $this->header; |
| 229 | break; |
| 230 | case 'resized_images': |
| 231 | $return = $this->resized_images; |
| 232 | break; |
| 233 | case 'meta': |
| 234 | $return = $this->meta; |
| 235 | break; |
| 236 | case 'posts_backup': |
| 237 | $return = $this->posts_backup; |
| 238 | break; |
| 239 | case 'header_backup': |
| 240 | $return = $this->header_backup; |
| 241 | break; |
| 242 | } |
| 243 | return $this->maybe_decrypt( $return ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @param string $type |
| 248 | * @param array $cache_value |
| 249 | * |
| 250 | * @since 4.0 |
| 251 | */ |
| 252 | public function set( $type, $cache_value ) { |
| 253 | switch( $type ) { |
| 254 | case 'posts': |
| 255 | $this->posts = $cache_value; |
| 256 | break; |
| 257 | case 'posts' . $this->suffix: |
| 258 | $this->posts_page = $cache_value; |
| 259 | break; |
| 260 | case 'header': |
| 261 | $this->header = $cache_value; |
| 262 | break; |
| 263 | case 'resized_images': |
| 264 | $this->resized_images = $cache_value; |
| 265 | break; |
| 266 | case 'meta': |
| 267 | $this->meta = $cache_value; |
| 268 | break; |
| 269 | case 'posts_backup': |
| 270 | $this->posts_backup = $cache_value; |
| 271 | break; |
| 272 | case 'header_backup': |
| 273 | $this->header_backup = $cache_value; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Update a single cache with new data. Try to accept any data and convert it |
| 280 | * to JSON if needed |
| 281 | * |
| 282 | * @param string $cache_type |
| 283 | * @param array|object|string $cache_value |
| 284 | * @param bool $include_backup |
| 285 | * @param bool $cron_update |
| 286 | * |
| 287 | * @return int |
| 288 | * |
| 289 | * @since 4.0 |
| 290 | */ |
| 291 | public function update_or_insert( $cache_type, $cache_value, $include_backup = true, $cron_update = true ) { |
| 292 | $this->clear_wp_cache(); |
| 293 | |
| 294 | if ( $this->page > 1 || ($cache_type !== 'posts' && $cache_type !== 'header') ) { |
| 295 | $cron_update = false; |
| 296 | } |
| 297 | |
| 298 | $cache_key = $cache_type . $this->suffix; |
| 299 | |
| 300 | $this->set( $cache_key, $cache_value ); |
| 301 | |
| 302 | if ( is_array( $cache_value ) || is_object( $cache_value ) ) { |
| 303 | $cache_value = CFF_Utils::cff_json_encode( $cache_value ); |
| 304 | } |
| 305 | |
| 306 | $encrypted_cache_value = $this->maybe_encrypt( $cache_value ); |
| 307 | |
| 308 | if ( $this->is_legacy ) { |
| 309 | if ( $cache_key === 'posts' ) { |
| 310 | set_transient( $this->feed_id, $encrypted_cache_value, $this->cache_time ); |
| 311 | if ( $include_backup ) { |
| 312 | set_transient( '!cff_backup_' . $this->feed_id, $encrypted_cache_value, YEAR_IN_SECONDS ); |
| 313 | } |
| 314 | } elseif ( strpos( $cache_key, 'posts' ) !== false ) { |
| 315 | set_transient( $this->feed_id, $encrypted_cache_value, $this->cache_time ); |
| 316 | } elseif ( strpos( $cache_key, 'header' ) !== false ) { |
| 317 | set_transient( $this->feed_id, $encrypted_cache_value, $this->cache_time ); |
| 318 | } |
| 319 | |
| 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | global $wpdb; |
| 324 | $cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 325 | |
| 326 | $sql = $wpdb->prepare( " |
| 327 | SELECT * FROM $cache_table_name |
| 328 | WHERE feed_id = %d |
| 329 | AND cache_key = %s", $this->feed_id, $cache_key ); |
| 330 | |
| 331 | $existing = $wpdb->get_results( $sql, ARRAY_A ); |
| 332 | $data = array(); |
| 333 | $where = array(); |
| 334 | $format = array(); |
| 335 | |
| 336 | $data['cache_value'] = $this->maybe_encrypt( $cache_value ); |
| 337 | $format[] = '%s'; |
| 338 | |
| 339 | $data['last_updated'] = date( 'Y-m-d H:i:s' ); |
| 340 | $format[] = '%s'; |
| 341 | |
| 342 | if ( ! empty( $existing[0] ) ) { |
| 343 | $where['feed_id'] = $this->feed_id; |
| 344 | $where_format[] = '%d'; |
| 345 | |
| 346 | $where['cache_key'] = $cache_key; |
| 347 | $where_format[] = '%s'; |
| 348 | |
| 349 | $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format ); |
| 350 | } else { |
| 351 | $data['cache_key'] = $cache_key; |
| 352 | $format[] = '%s'; |
| 353 | |
| 354 | $data['cron_update'] = $cron_update === true ? 'yes' : ''; |
| 355 | $format[] = '%s'; |
| 356 | |
| 357 | $data['feed_id'] = $this->feed_id; |
| 358 | $format[] = '%d'; |
| 359 | |
| 360 | $affected = $wpdb->insert( $cache_table_name, $data, $format ); |
| 361 | } |
| 362 | |
| 363 | return $affected; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Tasks to do after a new set of posts are retrieved |
| 368 | * |
| 369 | * @since 4.0 |
| 370 | */ |
| 371 | public function after_new_posts_retrieved() { |
| 372 | if ( $this->page === 1 ) { |
| 373 | $this->clear( 'all' ); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Resets caches after they expire |
| 379 | * |
| 380 | * @param string $type |
| 381 | * |
| 382 | * @return bool|false|int |
| 383 | */ |
| 384 | public function clear( $type ) { |
| 385 | $this->clear_wp_cache(); |
| 386 | |
| 387 | global $wpdb; |
| 388 | $cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 389 | |
| 390 | if ( $type === 'all' ) { |
| 391 | $affected = $wpdb->query( $wpdb->prepare( |
| 392 | "UPDATE $cache_table_name |
| 393 | SET cache_value = '' |
| 394 | WHERE feed_id = %d |
| 395 | AND cache_key NOT IN ( 'posts', 'posts_backup', 'header_backup' );", |
| 396 | $this->feed_id ) ); |
| 397 | } else { |
| 398 | |
| 399 | $data = [ 'cache_value' => '' ]; |
| 400 | $format = [ '%s' ]; |
| 401 | |
| 402 | $where['feed_id'] = $this->feed_id; |
| 403 | $where_format[] = '%d'; |
| 404 | |
| 405 | $where['cache_key'] = $type . $this->suffix; |
| 406 | $where_format[] = '%s'; |
| 407 | |
| 408 | $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format ); |
| 409 | } |
| 410 | |
| 411 | return $affected; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Clears caches in the WP Options table used mostly by legacy feeds. |
| 416 | */ |
| 417 | public static function clear_legacy(){ |
| 418 | global $wpdb; |
| 419 | |
| 420 | $cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 421 | |
| 422 | $sql = " |
| 423 | UPDATE $cache_table_name |
| 424 | SET cache_value = '' |
| 425 | WHERE cache_key = 'posts';"; |
| 426 | $wpdb->query( $sql ); |
| 427 | |
| 428 | $table_name = $wpdb->prefix . "options"; |
| 429 | $wpdb->query( " |
| 430 | DELETE |
| 431 | FROM $table_name |
| 432 | WHERE `option_name` LIKE ('%\_transient\_cff\_%') |
| 433 | " ); |
| 434 | $wpdb->query( " |
| 435 | DELETE |
| 436 | FROM $table_name |
| 437 | WHERE `option_name` LIKE ('%\_transient\_cff\_ej\_%') |
| 438 | " ); |
| 439 | $wpdb->query( " |
| 440 | DELETE |
| 441 | FROM $table_name |
| 442 | WHERE `option_name` LIKE ('%\_transient\_cff\_tle\_%') |
| 443 | " ); |
| 444 | $wpdb->query( " |
| 445 | DELETE |
| 446 | FROM $table_name |
| 447 | WHERE `option_name` LIKE ('%\_transient\_cff\_album\_%') |
| 448 | " ); |
| 449 | $wpdb->query( " |
| 450 | DELETE |
| 451 | FROM $table_name |
| 452 | WHERE `option_name` LIKE ('%\_transient\_timeout\_cff\_%') |
| 453 | " ); |
| 454 | |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Clears caches in the CFF Feed Caches table. |
| 459 | */ |
| 460 | public static function clear_all_builder(){ |
| 461 | global $wpdb; |
| 462 | $cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 463 | |
| 464 | $affected = $wpdb->query( $wpdb->prepare( |
| 465 | "UPDATE $cache_table_name |
| 466 | SET cache_value = '', last_updated = '0000-00-00 00:00:00' |
| 467 | WHERE cron_update = %s;", |
| 468 | 'yes' ) ); |
| 469 | |
| 470 | return $affected; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Clears popular page caching solutions |
| 475 | */ |
| 476 | public static function clear_page_caches(){ |
| 477 | //Clear cache of major caching plugins |
| 478 | if(isset($GLOBALS['wp_fastest_cache']) && method_exists($GLOBALS['wp_fastest_cache'], 'deleteCache')){ |
| 479 | $GLOBALS['wp_fastest_cache']->deleteCache(); |
| 480 | } |
| 481 | //WP Super Cache |
| 482 | if (function_exists('wp_cache_clear_cache')) { |
| 483 | wp_cache_clear_cache(); |
| 484 | } |
| 485 | //W3 Total Cache |
| 486 | if (function_exists('w3tc_flush_all')) { |
| 487 | w3tc_flush_all(); |
| 488 | } |
| 489 | if (function_exists('sg_cachepress_purge_cache')) { |
| 490 | sg_cachepress_purge_cache(); |
| 491 | } |
| 492 | |
| 493 | // Litespeed Cache (older method) |
| 494 | if ( method_exists( 'LiteSpeed_Cache_API', 'purge' ) ) { |
| 495 | LiteSpeed_Cache_API::purge( 'esi.custom-facebook-feed' ); |
| 496 | } |
| 497 | |
| 498 | // Litespeed Cache (new method) |
| 499 | if(has_action('litespeed_purge')) { |
| 500 | do_action( 'litespeed_purge', 'esi.custom-facebook-feed' ); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Get all available caches from the cff_cache table. |
| 506 | * |
| 507 | * @return array |
| 508 | * |
| 509 | * @since 4.0 |
| 510 | */ |
| 511 | private function query_cff_feed_caches() { |
| 512 | $feed_cache = wp_cache_get( $this->get_wp_cache_key() ); |
| 513 | if ( false === $feed_cache ) { |
| 514 | global $wpdb; |
| 515 | $cache_table_name = $wpdb->prefix . 'cff_feed_caches'; |
| 516 | |
| 517 | if ( $this->page === 1 ) { |
| 518 | $sql = $wpdb->prepare( " |
| 519 | SELECT * FROM $cache_table_name |
| 520 | WHERE feed_id = %s", $this->feed_id ); |
| 521 | } else { |
| 522 | $sql = $wpdb->prepare( " |
| 523 | SELECT * FROM $cache_table_name |
| 524 | WHERE feed_id = %s |
| 525 | AND cache_key IN ( 'posts', %s, %s, %s )", |
| 526 | $this->feed_id, |
| 527 | 'posts_' . $this->page, |
| 528 | 'resized_images_' . $this->page, |
| 529 | 'meta_' . $this->page ); |
| 530 | } |
| 531 | |
| 532 | $feed_cache = $wpdb->get_results( $sql, ARRAY_A ); |
| 533 | |
| 534 | wp_cache_set( $this->get_wp_cache_key(), $feed_cache ); |
| 535 | } |
| 536 | |
| 537 | return $feed_cache; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Delete the wp_cache |
| 542 | * |
| 543 | * @since 4.0 |
| 544 | */ |
| 545 | private function clear_wp_cache() { |
| 546 | wp_cache_delete( $this->get_wp_cache_key() ); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Key used to get the wp cache key |
| 551 | * |
| 552 | * @return string |
| 553 | * |
| 554 | * @since 4.0 |
| 555 | */ |
| 556 | private function get_wp_cache_key() { |
| 557 | return 'cff_feed_'. $this->feed_id . '_' . $this->page; |
| 558 | } |
| 559 | /** |
| 560 | * Uses a raw value and attempts to encrypt it |
| 561 | * |
| 562 | * @param $value |
| 563 | * |
| 564 | * @return bool|string |
| 565 | */ |
| 566 | private function maybe_encrypt( $value ) { |
| 567 | if ( ! empty( $value ) && ! is_string( $value ) ) { |
| 568 | $value = cff_json_encode( $value ); |
| 569 | } |
| 570 | if ( empty( $value ) ) { |
| 571 | return $value; |
| 572 | } |
| 573 | |
| 574 | return $this->encryption->encrypt( $value ); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Uses a raw value and attempts to decrypt it |
| 579 | * |
| 580 | * @param $value |
| 581 | * |
| 582 | * @return bool|string |
| 583 | */ |
| 584 | private function maybe_decrypt( $value ) { |
| 585 | if ( ! is_string( $value ) ) { |
| 586 | return $value; |
| 587 | } |
| 588 | if ( strpos( $value, '{' ) === 0 ) { |
| 589 | return $value; |
| 590 | } |
| 591 | |
| 592 | $decrypted = $this->encryption->decrypt( $value ); |
| 593 | |
| 594 | if ( ! $decrypted ) { |
| 595 | return $value; |
| 596 | } |
| 597 | |
| 598 | return $decrypted; |
| 599 | } |
| 600 | } |