PluginProbe ʕ •ᴥ•ʔ
WP Fastest Cache – WordPress Cache Plugin / 1.4.9
WP Fastest Cache – WordPress Cache Plugin v1.4.9
1.4.9 1.4.8 trunk 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7.0 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.7.7 0.8.7.8 0.8.7.9 0.8.8.0 0.8.8.1 0.8.8.2 0.8.8.3 0.8.8.4 0.8.8.5 0.8.8.6 0.8.8.7 0.8.8.8 0.8.8.9 0.8.9.0 0.8.9.1 0.8.9.2 0.8.9.3 0.8.9.4 0.8.9.5 0.8.9.6 0.8.9.7 0.8.9.8 0.8.9.9 0.9.0.0 0.9.0.1 0.9.0.2 0.9.0.3 0.9.0.4 0.9.0.5 0.9.0.6 0.9.0.7 0.9.0.8 0.9.0.9 0.9.1.0 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 0.9.1.5 0.9.1.6 0.9.1.7 0.9.1.8 0.9.1.9 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 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.1.9 1.2.0 1.2.1 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.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7
wp-fastest-cache / wpFastestCache.php
wp-fastest-cache Last commit date
css 1 year ago images 2 weeks ago inc 2 weeks ago js 2 weeks ago languages 1 year ago templates 2 weeks ago index.html 11 years ago readme.txt 2 weeks ago uninstall.php 2 weeks ago wpFastestCache.php 2 weeks ago
wpFastestCache.php
2747 lines
1 <?php
2 /*
3 Plugin Name: WP Fastest Cache
4 Plugin URI: http://wordpress.org/plugins/wp-fastest-cache/
5 Description: The simplest and fastest WP Cache system
6 Version: 1.4.9
7 Author: Emre Vona
8 Author URI: https://www.wpfastestcache.com/
9 Text Domain: wp-fastest-cache
10 Domain Path: /languages/
11
12 Copyright (C)2013 Emre Vona
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 */
24
25 if (!defined('WPFC_WP_CONTENT_BASENAME')) {
26 if (!defined('WPFC_WP_PLUGIN_DIR')) {
27 if(preg_match("/(\/trunk\/|\/wp-fastest-cache\/)$/", plugin_dir_path( __FILE__ ))){
28 define("WPFC_WP_PLUGIN_DIR", preg_replace("/(\/trunk\/|\/wp-fastest-cache\/)$/", "", plugin_dir_path( __FILE__ )));
29 }else if(preg_match("/\\\wp-fastest-cache\/$/", plugin_dir_path( __FILE__ ))){
30 //D:\hosting\LINEapp\public_html\wp-content\plugins\wp-fastest-cache/
31 define("WPFC_WP_PLUGIN_DIR", preg_replace("/\\\wp-fastest-cache\/$/", "", plugin_dir_path( __FILE__ )));
32 }
33 }
34 define("WPFC_WP_CONTENT_DIR", dirname(WPFC_WP_PLUGIN_DIR));
35 define("WPFC_WP_CONTENT_BASENAME", basename(WPFC_WP_CONTENT_DIR));
36 }
37
38 if (!defined('WPFC_MAIN_PATH')) {
39 define("WPFC_MAIN_PATH", plugin_dir_path( __FILE__ ));
40 }
41
42 if(!isset($GLOBALS["wp_fastest_cache_options"])){
43 if($wp_fastest_cache_options = get_option("WpFastestCache")){
44 $GLOBALS["wp_fastest_cache_options"] = json_decode($wp_fastest_cache_options);
45 }else{
46 $GLOBALS["wp_fastest_cache_options"] = array();
47 }
48 }
49
50 function wpfastestcache_activate(){
51 if($options = get_option("WpFastestCache")){
52 $post = json_decode($options, true);
53
54 include_once('inc/admin.php');
55 $wpfc = new WpFastestCacheAdmin();
56 $wpfc->modifyHtaccess($post);
57 }
58 }
59
60 function wpfastestcache_deactivate(){
61 $wpfc = new WpFastestCache();
62
63 $path = ABSPATH;
64
65 if($wpfc->is_subdirectory_install()){
66 $path = $wpfc->getABSPATH();
67 }
68
69 if(is_file($path.".htaccess") && is_writable($path.".htaccess")){
70 $htaccess = file_get_contents($path.".htaccess");
71 $htaccess = preg_replace("/#\s?BEGIN\s?WpFastestCache.*?#\s?END\s?WpFastestCache/s", "", $htaccess);
72 $htaccess = preg_replace("/#\s?BEGIN\s?GzipWpFastestCache.*?#\s?END\s?GzipWpFastestCache/s", "", $htaccess);
73 $htaccess = preg_replace("/#\s?BEGIN\s?LBCWpFastestCache.*?#\s?END\s?LBCWpFastestCache/s", "", $htaccess);
74 $htaccess = preg_replace("/#\s?BEGIN\s?WEBPWpFastestCache.*?#\s?END\s?WEBPWpFastestCache/s", "", $htaccess);
75 @file_put_contents($path.".htaccess", $htaccess);
76 }
77
78 $wpfc->deleteCache();
79 }
80
81 register_activation_hook( __FILE__, "wpfastestcache_activate");
82 register_deactivation_hook( __FILE__, "wpfastestcache_deactivate");
83
84 class WpFastestCache{
85 private $systemMessage = "";
86 private $options = array();
87 public $noscript = "";
88 public $content_url = "";
89 public $deleted_before = false;
90
91 public function __construct(){
92 $this->set_content_url();
93
94 $optimize_image_ajax_requests = array("wpfc_revert_image_ajax_request",
95 "wpfc_statics_ajax_request",
96 "wpfc_optimize_image_ajax_request",
97 "wpfc_update_image_list_ajax_request"
98 );
99
100 add_action('wp_ajax_wpfc_delete_cache', array($this, "deleteCacheToolbar"));
101 add_action('wp_ajax_wpfc_delete_cache_and_minified', array($this, "deleteCssAndJsCacheToolbar"));
102 add_action('wp_ajax_wpfc_delete_current_page_cache', array($this, "delete_current_page_cache"));
103
104 add_action('wp_ajax_wpfc_clear_cache_of_allsites', array($this, "wpfc_clear_cache_of_allsites_callback"));
105
106 add_action('wp_ajax_wpfc_toolbar_save_settings', array($this, "wpfc_toolbar_save_settings_callback"));
107 add_action('wp_ajax_wpfc_toolbar_get_settings', array($this, "wpfc_toolbar_get_settings_callback"));
108
109 //add_action('wp_ajax_wpfc_cache_path_save_settings', array($this, "wpfc_cache_path_save_settings_callback"));
110
111 add_action( 'wp_ajax_wpfc_save_timeout_pages', array($this, 'wpfc_save_timeout_pages_callback'));
112 add_action( 'wp_ajax_wpfc_save_exclude_pages', array($this, 'wpfc_save_exclude_pages_callback'));
113 add_action( 'wp_ajax_wpfc_cdn_options', array($this, 'wpfc_cdn_options_ajax_request_callback'));
114 add_action( 'wp_ajax_wpfc_remove_cdn_integration', array($this, 'wpfc_remove_cdn_integration_ajax_request_callback'));
115 add_action( 'wp_ajax_wpfc_pause_cdn_integration', array($this, 'wpfc_pause_cdn_integration_ajax_request_callback'));
116 add_action( 'wp_ajax_wpfc_start_cdn_integration', array($this, 'wpfc_start_cdn_integration_ajax_request_callback'));
117 add_action( 'wp_ajax_wpfc_save_cdn_integration', array($this, 'wpfc_save_cdn_integration_ajax_request_callback'));
118 add_action( 'wp_ajax_wpfc_cdn_template', array($this, 'wpfc_cdn_template_ajax_request_callback'));
119 add_action( 'wp_ajax_wpfc_check_url', array($this, 'wpfc_check_url_ajax_request_callback'));
120 add_action( 'wp_ajax_wpfc_cache_statics_get', array($this, 'wpfc_cache_statics_get_callback'));
121 add_action( 'wp_ajax_wpfc_db_statics', array($this, 'wpfc_db_statics_callback'));
122 add_action( 'wp_ajax_wpfc_db_fix', array($this, 'wpfc_db_fix_callback'));
123 add_action( 'rate_post', array($this, 'wp_postratings_clear_fastest_cache'), 10, 2);
124 add_action( 'user_register', array($this, 'modify_htaccess_for_new_user'), 10, 1);
125 add_action( 'profile_update', array($this, 'modify_htaccess_for_new_user'), 10, 1);
126 add_action( 'edit_terms', array($this, 'delete_cache_of_term'), 10, 1);
127
128 add_action( 'wp_ajax_wpfc_save_csp', array($this, 'wpfc_save_csp_callback'));
129 add_action( 'wp_ajax_wpfc_remove_csp', array($this, 'wpfc_remove_csp_callback'));
130 add_action( 'wp_ajax_wpfc_get_list_csp', array($this, 'wpfc_get_list_csp_callback'));
131
132
133 add_action( 'wp_ajax_wpfc_save_varnish', array($this, 'wpfc_save_varnish_callback'));
134 add_action( 'wp_ajax_wpfc_remove_varnish', array($this, 'wpfc_remove_varnish_callback'));
135 add_action( 'wp_ajax_wpfc_pause_varnish', array($this, 'wpfc_pause_varnish_callback'));
136 add_action( 'wp_ajax_wpfc_start_varnish', array($this, 'wpfc_start_varnish_callback'));
137 add_action( 'wp_ajax_wpfc_purgecache_varnish', array($this, 'wpfc_purgecache_varnish_callback'));
138
139
140 if(defined("WPFC_CLEAR_CACHE_AFTER_SWITCH_THEME") && WPFC_CLEAR_CACHE_AFTER_SWITCH_THEME){
141 add_action('after_switch_theme', array($this, 'clear_cache_after_switch_theme'));
142 }
143
144 if(defined("WPFC_CLEAR_CACHE_AFTER_ACTIVATE_DEACTIVATE_PLUGIN") && WPFC_CLEAR_CACHE_AFTER_ACTIVATE_DEACTIVATE_PLUGIN){
145 add_action('activate_plugin', array($this, 'clear_cache_after_activate_plugin'));
146 add_action('deactivate_plugin', array($this, 'clear_cache_after_deactivate_plugin'));
147 }
148
149
150 add_action('upgrader_process_complete', array($this, 'clear_cache_after_update_plugin'), 10, 2);
151 add_action('upgrader_process_complete', array($this, 'clear_cache_after_update_theme'), 10, 2);
152
153
154 if(defined("WPFC_DISABLE_CLEARING_CACHE_AFTER_WOOCOMMERCE_CHECKOUT_ORDER_PROCESSED") && WPFC_DISABLE_CLEARING_CACHE_AFTER_WOOCOMMERCE_CHECKOUT_ORDER_PROCESSED){
155 }else if(defined("WPFC_DISABLE_CLEARING_CACHE_AFTER_WOOCOMMERCE_ORDER_STATUS_CHANGED") && WPFC_DISABLE_CLEARING_CACHE_AFTER_WOOCOMMERCE_ORDER_STATUS_CHANGED){
156 }else{
157 // to clear cache after new Woocommerce orders
158 add_action('woocommerce_order_status_changed', array($this, 'clear_cache_after_woocommerce_order_status_changed'), 1, 1);
159 }
160
161
162 // kk Star Ratings: to clear the cache of the post after voting
163 add_action('kksr_rate', array($this, 'clear_cache_on_kksr_rate'));
164
165 // Elementor: to clear cache after Maintenance Mode activation/deactivation
166 add_action('elementor/maintenance_mode/mode_changed', array($this, 'deleteCache'));
167
168 // clearing cache hooks
169 add_action("wpfc_clear_all_cache", array($this, 'deleteCache'), 10, 1);
170 add_action("wpfc_clear_all_site_cache", array($this, 'wpfc_clear_cache_of_allsites_callback'));
171 add_action("wpfc_clear_post_cache_by_id", array($this, 'singleDeleteCache'), 10, 2);
172
173 // create cache by id hook
174 add_action("wpfc_create_post_cache_by_id", array($this, 'create_post_cache_by_id'), 10, 1);
175
176 // to enable Auto Cache Panel for the classic editor
177 add_action( 'admin_init', array($this, 'enable_auto_cache_settings_panel'));
178
179 // to add settings link
180 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array($this, 'action_links'));
181
182 // to clear cache after ajax request by other plugins
183 if(isset($_POST["action"])){
184 // All In One Schema.org Rich Snippets
185 if(preg_match("/bsf_(update|submit)_rating/i", $_POST["action"])){
186 if(isset($_POST["post_id"])){
187 $this->singleDeleteCache(false, $_POST["post_id"]);
188 }
189 }
190
191 // Yet Another Stars Rating
192 if($_POST["action"] == "yasr_send_visitor_rating"){
193 if(isset($_POST["post_id"])){
194 // to need call like that because get_permalink() does not work if we call singleDeleteCache() directly
195 add_action('init', array($this, "singleDeleteCache"));
196 }
197 }
198 }
199
200 // to clear /tmpWpfc folder
201 if(is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
202 $this->rm_folder_recursively($this->getWpContentDir("/cache/tmpWpfc"));
203 }
204
205
206 if($this->isPluginActive('wp-polls/wp-polls.php')){
207 //for WP-Polls
208 require_once "inc/wp-polls.php";
209 $wp_polls = new WpPollsForWpFc();
210 $wp_polls->hook();
211 }
212
213 if(isset($_GET) && isset($_GET["action"]) && in_array($_GET["action"], $optimize_image_ajax_requests)){
214 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
215 include_once $this->get_premium_path("image.php");
216 $img = new WpFastestCacheImageOptimisation();
217 $img->hook();
218 }
219 }else if(isset($_GET) && isset($_GET["action"]) && $_GET["action"] == "wpfastestcache"){
220 if(isset($_GET) && isset($_GET["type"]) && $_GET["type"] == "preload"){
221 // /?action=wpfastestcache&type=preload
222
223 add_action('init', array($this, "create_preload_cache"), 11);
224 }
225
226 if(isset($_GET) && isset($_GET["type"]) && preg_match("/^clearcache(andminified|allsites)*$/i", $_GET["type"])){
227 // /?action=wpfastestcache&type=clearcache&token=123
228 // /?action=wpfastestcache&type=clearcacheandminified&token=123
229
230 add_action('wp_loaded', array($this, "handle_custom_delete_cache_request"));
231 }
232 }else{
233 $this->setCustomInterval();
234
235 $this->options = $this->getOptions();
236
237 add_action('transition_post_status', array($this, 'on_all_status_transitions'), 10, 3 );
238
239 // when the regular price is updated, the "transition_post_status" action cannot catch it
240 add_action('woocommerce_update_product', array($this, 'clear_cache_after_woocommerce_update_product'), 10, 1);
241
242 $this->commentHooks();
243
244 $this->checkCronTime();
245
246 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
247 include_once $this->get_premium_path("mobile-cache.php");
248
249 if(file_exists(WPFC_WP_PLUGIN_DIR."/wp-fastest-cache-premium/pro/library/statics.php")){
250 include_once $this->get_premium_path("statics.php");
251 }
252
253 if(!defined('DOING_AJAX')){
254 include_once $this->get_premium_path("powerful-html.php");
255 }
256 }
257
258 if(is_admin()){
259 add_action('wp_loaded', array($this, "load_column"));
260
261 if(defined('DOING_AJAX') && DOING_AJAX){
262 //do nothing
263 }else{
264 // to avoid loading menu and optionPage() twice
265 if(!class_exists("WpFastestCacheAdmin")){
266 //for wp-panel
267
268 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
269 include_once $this->get_premium_path("image.php");
270 }
271
272 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
273 include_once $this->get_premium_path("logs.php");
274 }
275
276 add_action('plugins_loaded', array($this, 'wpfc_load_plugin_textdomain'));
277 add_action('wp_loaded', array($this, "load_admin_toolbar"));
278
279 $this->admin();
280 }
281 }
282 }else{
283 if(preg_match("/\/([^\/]+)\/([^\/]+(\.css|\.js))(\?.+)?$/", $this->current_url(), $path)){
284 // for security
285 if(preg_match("/\.{2,}/", $this->current_url())){
286 die("May be Directory Traversal Attack");
287 }
288
289 if(is_dir($this->getWpContentDir("/cache/wpfc-minified/").$path[1])){
290 if($sources = @scandir($this->getWpContentDir("/cache/wpfc-minified/").$path[1], 1)){
291 if(isset($sources[0])){
292 // $exist_url = str_replace($path[2], $sources[0], $this->current_url());
293 // header('Location: ' . $exist_url, true, 301);
294 // exit;
295
296 if(preg_match("/\.css/", $this->current_url())){
297 header('Content-type: text/css');
298 }else if(preg_match("/\.js/", $this->current_url())){
299 header('Content-type: text/js');
300 }
301
302 echo file_get_contents($this->getWpContentDir("/cache/wpfc-minified/").$path[1]."/".$sources[0]);
303 exit;
304 }
305 }
306 }
307
308
309 if(preg_match("/".basename($this->getWpContentDir("/cache/wpfc-minified/"))."/i", $this->current_url())){
310 //for non-exists minified files
311 if(preg_match("/\.css/", $this->current_url())){
312 header('Content-type: text/css');
313 die("/* File not found */");
314 }else if(preg_match("/\.js/", $this->current_url())){
315 header('Content-type: text/js');
316 die("//File not found");
317 }
318 }
319
320 }else{
321 // to show if the user is logged-in
322 add_action('wp_loaded', array($this, "load_admin_toolbar"));
323
324 //for cache
325 $this->cache();
326 }
327 }
328 }
329 }
330
331 public function handle_custom_delete_cache_request(){
332 $action = false;
333 $wpfc_token = false;
334
335 if(defined("WPFC_CLEAR_CACHE_URL_TOKEN") && WPFC_CLEAR_CACHE_URL_TOKEN){
336 $wpfc_token = WPFC_CLEAR_CACHE_URL_TOKEN;
337 }else{
338 $wpfc_token = apply_filters( 'wpfc_clear_cache_url_token', false );
339 }
340
341 if(isset($_GET["token"]) && $_GET["token"]){
342 if($wpfc_token){
343 if($wpfc_token == $_GET["token"]){
344 $action = true;
345 }else{
346 die("Wrong token");
347 }
348 }else{
349 die("WPFC_CLEAR_CACHE_URL_TOKEN must be defined");
350 }
351 }else{
352 die("Security token must be set.");
353 }
354
355 if($action){
356 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
357 include_once $this->get_premium_path("mobile-cache.php");
358 }
359
360 if($_GET["type"] == "clearcache"){
361
362 if(isset($_GET["post_id"])){
363 $this->singleDeleteCache(false, $_GET["post_id"]);
364 }else{
365 $this->deleteCache();
366 }
367
368 }
369
370 if($_GET["type"] == "clearcacheandminified"){
371 $this->deleteCache(true);
372 }
373
374 if($_GET["type"] == "clearcacheallsites"){
375 $this->wpfc_clear_cache_of_allsites_callback();
376 }
377
378 die("Done");
379 }
380
381 exit;
382 }
383
384 public function enable_auto_cache_settings_panel(){
385 if($this->isPluginActive('classic-editor/classic-editor.php') || $this->isPluginActive('disable-gutenberg/disable-gutenberg.php') || has_filter("use_block_editor_for_post", "__return_false")){
386 add_action("add_meta_boxes", array($this, "add_meta_box"), 10, 2);
387 add_action('admin_notices', array($this, 'single_preload_inline_js'));
388 add_action('wp_ajax_wpfc_preload_single_save_settings', array($this, "wpfc_preload_single_save_settings_callback"));
389 add_action('wp_ajax_wpfc_preload_single', array($this, "wpfc_preload_single_callback"));
390 }
391 }
392
393 public function clear_cache_after_activate_plugin(){
394 $this->deleteCache(true);
395 }
396
397 public function clear_cache_after_deactivate_plugin(){
398 $this->deleteCache(true);
399 }
400
401 public function clear_cache_after_switch_theme(){
402 $this->deleteCache(true);
403 }
404
405 public function clear_cache_after_update_plugin($upgrader_object, $options){
406 if(isset($options['action']) && isset($options['type'])){
407 if($options['action'] == 'update'){
408 if($options['type'] == 'plugin' && (isset($options['plugins']) || isset($options['plugin']))){
409
410 $options_json = json_encode($options);
411
412 if(preg_match("/elementor\\\\\/elementor\.php/i", $options_json)){
413 $this->deleteCache(true);
414 }
415
416 if(defined("WPFC_CLEAR_CACHE_AFTER_PLUGIN_UPDATE") && WPFC_CLEAR_CACHE_AFTER_PLUGIN_UPDATE){
417 $this->deleteCache(true);
418 }
419 }
420 }
421 }
422 }
423
424 public function clear_cache_after_update_theme($upgrader_object, $options){
425 if(isset($options['action']) && isset($options['type'])){
426 if($options['action'] == 'update'){
427 if($options['type'] == 'theme' && isset($options['themes'])){
428
429 if(defined("WPFC_CLEAR_CACHE_AFTER_THEME_UPDATE") && WPFC_CLEAR_CACHE_AFTER_THEME_UPDATE){
430 $this->deleteCache(true);
431 }
432
433 }
434 }
435 }
436 }
437
438 public function action_links($actions){
439 $actions['powered_settings'] = sprintf(__( '<a href="%s">Settings</a>', 'wp-fastest-cache'), esc_url( admin_url( 'admin.php?page=wpfastestcacheoptions')));
440 return array_reverse($actions);
441 }
442
443 public function wpfc_preload_single_callback(){
444 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
445 die( 'Security check' );
446 }
447
448 include_once('inc/single-preload.php');
449 SinglePreloadWPFC::create_cache();
450 }
451
452 public function create_post_cache_by_id($id){
453 include_once('inc/single-preload.php');
454 SinglePreloadWPFC::init($id);
455 $res = SinglePreloadWPFC::create_cache_for_all_urls();
456
457 return $res;
458 }
459
460 public function single_preload_inline_js(){
461 include_once('inc/single-preload.php');
462 SinglePreloadWPFC::init();
463 SinglePreloadWPFC::put_inline_js();
464 }
465 public function add_meta_box(){
466 include_once('inc/single-preload.php');
467 SinglePreloadWPFC::add_meta_box();
468 }
469
470 public function wpfc_preload_single_save_settings_callback(){
471 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
472 die( 'Security check' );
473 }
474
475 include_once('inc/single-preload.php');
476 SinglePreloadWPFC::save_settings();
477 }
478
479 public function notify($message = array()){
480 if(isset($message[0]) && $message[0]){
481 if(function_exists("add_settings_error")){
482 add_settings_error('wpfc-notice', esc_attr( 'settings_updated' ), $message[0], $message[1]);
483 }
484 }
485 }
486
487 public function set_content_url(){
488 $content_url = content_url();
489
490 // Hide My WP
491 if($this->isPluginActive('hide_my_wp/hide-my-wp.php')){
492 $hide_my_wp = get_option("hide_my_wp");
493
494 if(isset($hide_my_wp["new_content_path"]) && $hide_my_wp["new_content_path"]){
495 $hide_my_wp["new_content_path"] = trim($hide_my_wp["new_content_path"], "/");
496 $content_url = str_replace(basename(WPFC_WP_CONTENT_DIR), $hide_my_wp["new_content_path"], $content_url);
497 }
498 }
499
500 // to change content url if a different url is used for other langs
501 if($this->isPluginActive('polylang/polylang.php') || $this->isPluginActive('polylang-pro/polylang.php')){
502 $url = parse_url($content_url);
503
504 if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']){
505 if($url["host"] != $_SERVER['HTTP_HOST']){
506 $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
507 $content_url = $protocol.$_SERVER['HTTP_HOST'].$url['path'];
508 }
509 }
510 }
511
512 if (!defined('WPFC_WP_CONTENT_URL')) {
513 define("WPFC_WP_CONTENT_URL", $content_url);
514 }
515
516 $this->content_url = $content_url;
517 }
518
519 public function clear_cache_on_kksr_rate($id){
520 $this->singleDeleteCache(false, $id);
521 }
522
523 public function clear_cache_after_woocommerce_order_status_changed($order_id = false){
524 if(function_exists("wc_get_order")){
525 if($order_id){
526 $order = wc_get_order($order_id);
527
528 if($order){
529 foreach($order->get_items() as $item_key => $item_values ){
530 if(method_exists($item_values, 'get_product_id')){
531 $this->singleDeleteCache(false, $item_values->get_product_id());
532 }
533 }
534 }
535 }
536 }
537 }
538
539 public function wpfc_db_fix_callback(){
540 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
541 die( 'Security check' );
542 }
543
544 if(current_user_can('manage_options')){
545 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
546 include_once $this->get_premium_path("db.php");
547
548 if(class_exists("WpFastestCacheDatabaseCleanup")){
549 WpFastestCacheDatabaseCleanup::clean($_GET["type"]);
550 }else{
551 die(json_encode(array("success" => false, "showupdatewarning" => true, "message" => "Only available in Premium version")));
552 }
553
554 }else{
555 die(json_encode(array("success" => false, "message" => "Only available in Premium version")));
556 }
557 }else{
558 wp_die("Must be admin");
559 }
560
561 }
562
563 public function wpfc_db_statics_callback(){
564 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
565 die( 'Security check' );
566 }
567
568 global $wpdb;
569
570 $statics = array("all_warnings" => 0,
571 "post_revisions" => 0,
572 "trashed_contents" => 0,
573 "trashed_spam_comments" => 0,
574 "trackback_pingback" => 0,
575 "transient_options" => 0,
576 "orphaned_post_meta" => 0,
577 "orphaned_comment_meta" => 0,
578 "orphaned_user_meta" => 0,
579 "orphaned_term_meta" => 0,
580 "orphaned_term_relationships" => 0
581 );
582
583
584 $statics["post_revisions"] = $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->posts` WHERE post_type = 'revision';");
585 $statics["all_warnings"] = $statics["all_warnings"] + $statics["post_revisions"];
586
587 $statics["trashed_contents"] = $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->posts` WHERE post_status = 'trash';");
588 $statics["all_warnings"] = $statics["all_warnings"] + $statics["trashed_contents"];
589
590 $statics["trashed_spam_comments"] = $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_approved = 'spam' OR comment_approved = 'trash' ;");
591 $statics["all_warnings"] = $statics["all_warnings"] + $statics["trashed_spam_comments"];
592
593 $statics["trackback_pingback"] = $wpdb->get_var("SELECT COUNT(*) FROM `$wpdb->comments` WHERE comment_type = 'trackback' OR comment_type = 'pingback' ;");
594 $statics["all_warnings"] = $statics["all_warnings"] + $statics["trackback_pingback"];
595
596 $element = "SELECT COUNT(*) FROM `$wpdb->options` WHERE option_name LIKE '%\_transient\_%' ;";
597 $statics["transient_options"] = $wpdb->get_var( $element ) > 100 ? $wpdb->get_var( $element ) : 0;
598 $statics["all_warnings"] = $statics["all_warnings"] + $statics["transient_options"];
599
600
601
602 $statics["orphaned_post_meta"] = $wpdb->get_var("SELECT COUNT(pm.meta_id) FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.ID IS NULL");
603 $statics["all_warnings"] += $statics["orphaned_post_meta"];
604
605 $statics["orphaned_comment_meta"] = $wpdb->get_var("SELECT COUNT(cm.meta_id) FROM {$wpdb->commentmeta} cm LEFT JOIN {$wpdb->comments} c ON c.comment_ID = cm.comment_id WHERE c.comment_ID IS NULL");
606 $statics["all_warnings"] += $statics["orphaned_comment_meta"];
607
608 $statics["orphaned_user_meta"] = $wpdb->get_var("SELECT COUNT(um.umeta_id) FROM {$wpdb->usermeta} um LEFT JOIN {$wpdb->users} u ON u.ID = um.user_id WHERE u.ID IS NULL");
609 $statics["all_warnings"] += $statics["orphaned_user_meta"];
610
611 $statics["orphaned_term_meta"] = $wpdb->get_var("SELECT COUNT(tm.meta_id) FROM {$wpdb->termmeta} tm LEFT JOIN {$wpdb->terms} t ON t.term_id = tm.term_id WHERE t.term_id IS NULL");
612 $statics["all_warnings"] += $statics["orphaned_term_meta"];
613
614 $statics["orphaned_term_relationships"] = $wpdb->get_var("SELECT COUNT(tr.object_id) FROM {$wpdb->term_relationships} tr LEFT JOIN {$wpdb->posts} p ON p.ID = tr.object_id WHERE p.ID IS NULL");
615 $statics["all_warnings"] += $statics["orphaned_term_relationships"];
616
617
618
619
620 die(json_encode($statics));
621 }
622
623 public function is_trailing_slash(){
624 // no need to check if Custom Permalinks plugin is active (https://tr.wordpress.org/plugins/custom-permalinks/)
625 if($this->isPluginActive("custom-permalinks/custom-permalinks.php")){
626 return false;
627 }
628
629 if($permalink_structure = get_option('permalink_structure')){
630 if(preg_match("/\/$/", $permalink_structure)){
631 return true;
632 }
633 }
634
635 return false;
636 }
637
638 public function wpfc_cache_statics_get_callback(){
639 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
640 die( 'Security check' );
641 }
642
643 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
644 if(file_exists(WPFC_WP_PLUGIN_DIR."/wp-fastest-cache-premium/pro/library/statics.php")){
645 include_once $this->get_premium_path("statics.php");
646
647 $cache_statics = new WpFastestCacheStatics();
648 $res = $cache_statics->get();
649 echo json_encode($res);
650 exit;
651 }
652 }
653 }
654
655 public function wpfc_check_url_ajax_request_callback(){
656 if(!wp_verify_nonce($_REQUEST["nonce"], 'cdn-nonce')){
657 die( 'Security check' );
658 }
659
660 include_once('inc/cdn.php');
661 CdnWPFC::check_url();
662 }
663
664 public function wpfc_cdn_template_ajax_request_callback(){
665 include_once('inc/cdn.php');
666 CdnWPFC::cdn_template();
667 }
668
669 public function wpfc_save_cdn_integration_ajax_request_callback(){
670 if(!wp_verify_nonce($_REQUEST["nonce"], 'cdn-nonce')){
671 die( 'Security check' );
672 }
673
674 include_once('inc/cdn.php');
675 CdnWPFC::save_cdn_integration();
676 }
677
678 public function wpfc_start_cdn_integration_ajax_request_callback(){
679 if(!wp_verify_nonce($_REQUEST["nonce"], 'cdn-nonce')){
680 die( 'Security check' );
681 }
682
683 include_once('inc/cdn.php');
684 CdnWPFC::start_cdn_integration();
685 }
686
687 public function wpfc_pause_cdn_integration_ajax_request_callback(){
688 if(!wp_verify_nonce($_REQUEST["nonce"], 'cdn-nonce')){
689 die( 'Security check' );
690 }
691
692 include_once('inc/cdn.php');
693 CdnWPFC::pause_cdn_integration();
694 }
695
696 public function wpfc_remove_cdn_integration_ajax_request_callback(){
697 if(!wp_verify_nonce($_REQUEST["nonce"], 'cdn-nonce')){
698 die( 'Security check' );
699 }
700
701 include_once('inc/cdn.php');
702 CdnWPFC::remove_cdn_integration();
703 }
704
705 public function wpfc_cdn_options_ajax_request_callback(){
706 if(!wp_verify_nonce($_REQUEST["nonce"], 'cdn-nonce')){
707 die( 'Security check' );
708 }
709
710 include_once('inc/cdn.php');
711 CdnWPFC::cdn_options();
712 }
713
714 public function wpfc_save_exclude_pages_callback(){
715 if(!wp_verify_nonce($_POST["security"], 'wpfc-save-exclude-ajax-nonce')){
716 die( 'Security check' );
717 }
718
719 if(current_user_can('manage_options')){
720 if(isset($_POST["rules"])){
721 foreach ($_POST["rules"] as $key => &$value) {
722 $value["prefix"] = strip_tags($value["prefix"]);
723 $value["content"] = strip_tags($value["content"]);
724
725 $value["prefix"] = preg_replace("/\'|\"/", "", $value["prefix"]);
726
727 if($value["prefix"] == "regex"){
728 $value["content"] = stripslashes($value["content"]);
729
730 $value["content"] = esc_attr($value["content"]);
731 }else{
732 $value["content"] = preg_replace("/\'|\"/", "", $value["content"]);
733 $value["content"] = preg_replace("/(\#|\s|\(|\)|\*)/", "", $value["content"]);
734 }
735
736 if($value["prefix"] == "homepage"){
737 $this->deleteHomePageCache(false);
738 }
739 }
740
741 $data = json_encode($_POST["rules"]);
742
743 if(get_option("WpFastestCacheExclude")){
744 update_option("WpFastestCacheExclude", $data);
745 }else{
746 add_option("WpFastestCacheExclude", $data, null, "yes");
747 }
748 }else{
749 delete_option("WpFastestCacheExclude");
750 }
751
752 $this->modify_htaccess_for_exclude();
753
754 echo json_encode(array("success" => true));
755 exit;
756 }else{
757 wp_die("Must be admin");
758 }
759 }
760
761 public function modify_htaccess_for_exclude(){
762 $path = ABSPATH;
763
764 if($this->is_subdirectory_install()){
765 $path = $this->getABSPATH();
766 }
767
768 $htaccess = @file_get_contents($path.".htaccess");
769
770 if(preg_match("/\#\s?Start\sWPFC\sExclude/", $htaccess)){
771 $exclude_rules = $this->excludeRules();
772
773 $htaccess = preg_replace("/\#\s?Start\sWPFC\sExclude[^\#]*\#\s?End\sWPFC\sExclude\s+/", $exclude_rules, $htaccess);
774 }
775
776 @file_put_contents($path.".htaccess", $htaccess);
777 }
778
779 public function wpfc_purgecache_varnish_callback(){
780 if(!wp_verify_nonce($_REQUEST["security"], 'wpfc-varnish-ajax-nonce')){
781 die( 'Security check' );
782 }
783
784 if($varnish_datas = get_option("WpFastestCacheVarnish")){
785 include_once('inc/varnish.php');
786 $res_arr = VarnishWPFC::purge_cache($varnish_datas["server"]);
787
788 wp_send_json($res_arr);
789 }
790 }
791
792 public function wpfc_save_varnish_callback(){
793 include_once('inc/varnish.php');
794 VarnishWPFC::save();
795 }
796
797 public function wpfc_remove_varnish_callback(){
798 include_once('inc/varnish.php');
799 VarnishWPFC::remove();
800 }
801
802 public function wpfc_start_varnish_callback(){
803 include_once('inc/varnish.php');
804 VarnishWPFC::start();
805 }
806
807 public function wpfc_pause_varnish_callback(){
808 include_once('inc/varnish.php');
809 VarnishWPFC::pause();
810 }
811
812 public function wpfc_status_varnish(){
813 include_once('inc/varnish.php');
814 VarnishWPFC::status();
815 }
816
817 public function wpfc_get_list_csp_callback(){
818 include_once('inc/clearing-specific-pages.php');
819 ClearingSpecificPagesWPFC::get_list();
820 }
821
822 public function wpfc_save_csp_callback(){
823 include_once('inc/clearing-specific-pages.php');
824 ClearingSpecificPagesWPFC::save();
825 }
826
827 public function wpfc_remove_csp_callback(){
828 include_once('inc/clearing-specific-pages.php');
829 ClearingSpecificPagesWPFC::remove();
830 }
831
832 public function wpfc_save_timeout_pages_callback(){
833 if(!wp_verify_nonce($_POST["security"], 'wpfc-save-timeout-ajax-nonce')){
834 die( 'Security check' );
835 }
836
837 if(current_user_can('manage_options')){
838 $this->setCustomInterval();
839
840 $crons = _get_cron_array();
841
842 foreach ($crons as $cron_key => $cron_value) {
843 foreach ( (array) $cron_value as $hook => $events ) {
844 if(preg_match("/^wp\_fastest\_cache(.*)/", $hook, $id)){
845 if(!$id[1] || preg_match("/^\_(\d+)$/", $id[1])){
846 foreach ( (array) $events as $event_key => $event ) {
847 if($id[1]){
848 wp_clear_scheduled_hook("wp_fastest_cache".$id[1], $event["args"]);
849 }else{
850 wp_clear_scheduled_hook("wp_fastest_cache", $event["args"]);
851 }
852 }
853 }
854 }
855 }
856 }
857
858 if(isset($_POST["rules"]) && count($_POST["rules"]) > 0){
859 $i = 0;
860
861 foreach ($_POST["rules"] as $key => $value) {
862 if(preg_match("/^(daily|onceaday)$/i", $value["schedule"]) && isset($value["hour"]) && isset($value["minute"]) && strlen($value["hour"]) > 0 && strlen($value["minute"]) > 0){
863 $args = array("prefix" => $value["prefix"], "content" => $value["content"], "hour" => $value["hour"], "minute" => $value["minute"]);
864
865 $timestamp = mktime($value["hour"],$value["minute"],0,date("m"),date("d"),date("Y"));
866
867 $timestamp = $timestamp > time() ? $timestamp : $timestamp + 60*60*24;
868 }else{
869 $args = array("prefix" => $value["prefix"], "content" => $value["content"]);
870 $timestamp = time();
871 }
872
873 wp_schedule_event($timestamp, $value["schedule"], "wp_fastest_cache_".$i, array(json_encode($args)));
874 $i = $i + 1;
875 }
876 }
877
878 echo json_encode(array("success" => true));
879 exit;
880 }else{
881 wp_die("Must be admin");
882 }
883 }
884
885 public function wp_postratings_clear_fastest_cache($rate_userid, $post_id){
886 // to remove cache if vote is from homepage or category page or tag
887 if(isset($_SERVER["HTTP_REFERER"]) && $_SERVER["HTTP_REFERER"]){
888 $url = parse_url($_SERVER["HTTP_REFERER"]);
889
890 $url["path"] = isset($url["path"]) ? $url["path"] : "/index.html";
891
892 if(isset($url["path"])){
893 if($url["path"] == "/"){
894 $this->rm_folder_recursively($this->getWpContentDir("/cache/all/index.html"));
895 }else{
896 // to prevent changing path with ../ or with another method
897 if($url["path"] == realpath(".".$url["path"])){
898 $this->rm_folder_recursively($this->getWpContentDir("/cache/all").$url["path"]);
899 }
900 }
901 }
902 }
903
904 if($post_id){
905 $this->singleDeleteCache(false, $post_id);
906 }
907 }
908
909 private function admin(){
910 if(isset($_GET["page"]) && $_GET["page"] == "wpfastestcacheoptions"){
911 include_once('inc/admin.php');
912 $wpfc = new WpFastestCacheAdmin();
913 $wpfc->addMenuPage();
914 }else{
915 add_action('admin_menu', array($this, 'register_my_custom_menu_page'));
916 }
917 }
918
919 public function load_column(){
920 if(!defined('WPFC_HIDE_CLEAR_CACHE_BUTTON') || (defined('WPFC_HIDE_CLEAR_CACHE_BUTTON') && !WPFC_HIDE_CLEAR_CACHE_BUTTON)){
921 include_once plugin_dir_path(__FILE__)."inc/column.php";
922
923 $column = new WpFastestCacheColumn();
924 $column->add();
925 }
926 }
927
928 public function load_admin_toolbar(){
929 $display = true;
930
931 if(apply_filters('wpfc_hide_toolbar', false )){
932 $display = "";
933 }
934
935 if(defined('WPFC_HIDE_TOOLBAR') && WPFC_HIDE_TOOLBAR){
936 $display = "";
937 }
938
939
940 if($display){
941 $user = wp_get_current_user();
942 $allowed_roles = array('administrator');
943
944 $wpfc_role_status = get_option("WpFastestCacheToolbarSettings");
945 if(is_array($wpfc_role_status) && !empty($wpfc_role_status)){
946 foreach ($wpfc_role_status as $key => $value){
947 $key = str_replace("wpfc_toolbar_", "", $key);
948 array_push($allowed_roles, strtolower($key));
949 }
950 }
951
952
953 if(array_intersect($allowed_roles, $user->roles)){
954 include_once plugin_dir_path(__FILE__)."inc/admin-toolbar.php";
955
956 if(preg_match("/\/cache\/all/", $this->getWpContentDir("/cache/all"))){
957 $is_multi = false;
958 }else{
959 $is_multi = true;
960 }
961
962 $toolbar = new WpFastestCacheAdminToolbar($is_multi);
963 $toolbar->add();
964 }
965 }
966 }
967
968 public function tmp_saveOption(){
969 if(!empty($_POST)){
970 if(isset($_POST["wpFastestCachePage"])){
971 include_once('inc/admin.php');
972 $wpfc = new WpFastestCacheAdmin();
973 $wpfc->optionsPageRequest();
974 }
975 }
976 }
977
978 public function register_mysettings(){
979 register_setting('wpfc-group', 'wpfc-group', array($this, 'tmp_saveOption'));
980 }
981
982 public function register_my_custom_menu_page(){
983 if(function_exists('add_menu_page')){
984
985 if(defined("WPFC_MOVE_MENU_ITEM_UNDER_SETTINGS") && WPFC_MOVE_MENU_ITEM_UNDER_SETTINGS){
986 add_options_page("WP Fastest Cache Settings", "WP Fastest Cache", 'manage_options', 'wpfastestcacheoptions', array($this, 'optionsPage'));
987 }else{
988 add_menu_page("WP Fastest Cache Settings", "WP Fastest Cache", 'manage_options', "wpfastestcacheoptions", array($this, 'optionsPage'), plugins_url("wp-fastest-cache/images/icon.svg"));
989 }
990
991 add_action('admin_init', array($this, 'register_mysettings'));
992
993 wp_enqueue_style("wp-fastest-cache", plugins_url("wp-fastest-cache/css/style.css"), array(), time(), "all");
994 }
995
996 if(isset($_GET["page"]) && $_GET["page"] == "wpfastestcacheoptions"){
997 wp_enqueue_style("wp-fastest-cache-buycredit", plugins_url("wp-fastest-cache/css/buycredit.css"), array(), time(), "all");
998 wp_enqueue_style("wp-fastest-cache-flaticon", plugins_url("wp-fastest-cache/css/flaticon.css"), array(), time(), "all");
999 wp_enqueue_style("wp-fastest-cache-dialog", plugins_url("wp-fastest-cache/css/dialog.css"), array(), time(), "all");
1000 }
1001 }
1002
1003 public function deleteCacheToolbar(){
1004 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
1005 die( 'Security check' );
1006 }
1007
1008 $this->deleteCache();
1009 }
1010
1011 public function deleteCssAndJsCacheToolbar(){
1012 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
1013 die( 'Security check' );
1014 }
1015
1016 $this->deleteCache(true);
1017 }
1018
1019 public function wpfc_toolbar_get_settings_callback(){
1020 if(current_user_can('manage_options')){
1021 $result = array("success" => true, "roles" => false);
1022
1023 $wpfc_role_status = get_option("WpFastestCacheToolbarSettings");
1024 if(is_array($wpfc_role_status) && !empty($wpfc_role_status)){
1025 $result["roles"] = $wpfc_role_status;
1026 }
1027
1028 die(json_encode($result));
1029 }else{
1030 wp_die("Must be admin");
1031 }
1032 }
1033
1034 public function wpfc_cache_path_save_settings_callback(){
1035 if(current_user_can('manage_options')){
1036 foreach($_POST as $key => &$value){
1037 $value = esc_html(esc_sql($value));
1038 }
1039
1040 $path_arr = array(
1041 "cachepath" => sanitize_text_field($_POST["cachepath"]),
1042 "optimizedpath" => sanitize_text_field($_POST["optimizedpath"])
1043 );
1044
1045 if(get_option("WpFastestCachePathSettings") === false){
1046 add_option("WpFastestCachePathSettings", $path_arr, 1, "no");
1047 }else{
1048 update_option("WpFastestCachePathSettings", $path_arr);
1049 }
1050
1051 die(json_encode(array("success" => true)));
1052 }else{
1053 wp_die("Must be admin");
1054 }
1055 }
1056
1057 public function wpfc_toolbar_save_settings_callback(){
1058 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
1059 die( 'Security check' );
1060 }
1061
1062 if(current_user_can('manage_options')){
1063 if(isset($_GET["roles"]) && is_array($_GET["roles"]) && !empty($_GET["roles"])){
1064 $roles_arr = array();
1065
1066 foreach($_GET["roles"] as $key => $value){
1067 $value = esc_html(esc_sql($value));
1068 $key = esc_html(esc_sql($key));
1069
1070 $roles_arr[$key] = $value;
1071 }
1072
1073 if(get_option("WpFastestCacheToolbarSettings") === false){
1074 add_option("WpFastestCacheToolbarSettings", $roles_arr, 1, "no");
1075 }else{
1076 update_option("WpFastestCacheToolbarSettings", $roles_arr);
1077 }
1078 }else{
1079 delete_option("WpFastestCacheToolbarSettings");
1080 }
1081
1082
1083 die(json_encode(array("Saved","success")));
1084 }else{
1085 wp_die("Must be admin");
1086 }
1087 }
1088
1089 public function wpfc_clear_cache_of_allsites_callback(){
1090
1091 if(defined('DOING_AJAX') && DOING_AJAX){
1092 if(!wp_verify_nonce($_REQUEST["nonce"], 'wpfc')){
1093 die( 'Security check' );
1094 }
1095 }
1096
1097 include_once('inc/cdn.php');
1098 CdnWPFC::cloudflare_clear_cache();
1099
1100 $path = $this->getWpContentDir("/cache/*");
1101
1102 $files = glob($this->getWpContentDir("/cache/*"));
1103
1104 if(!is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
1105 if(@mkdir($this->getWpContentDir("/cache/tmpWpfc"), 0755, true)){
1106 //tmpWpfc has been created
1107 }
1108 }
1109
1110 foreach ((array)$files as $file){
1111 @rename($file, $this->getWpContentDir("/cache/tmpWpfc/").basename($file)."-".time());
1112 }
1113
1114 if (is_admin() && defined('DOING_AJAX') && DOING_AJAX){
1115 die(json_encode(array("The cache of page has been cleared","success")));
1116 }
1117 }
1118
1119 public function delete_current_page_cache(){
1120 if(!wp_verify_nonce($_GET["nonce"], "wpfc")){
1121 die(json_encode(array("Security Error!", "error", "alert")));
1122 }
1123
1124 if($varnish_datas = get_option("WpFastestCacheVarnish")){
1125 include_once('inc/varnish.php');
1126 VarnishWPFC::purge_cache($varnish_datas);
1127 }
1128
1129 include_once('inc/cdn.php');
1130 CdnWPFC::cloudflare_clear_cache();
1131
1132 if(isset($_GET["path"])){
1133 if($_GET["path"]){
1134 if($_GET["path"] == "/"){
1135 $_GET["path"] = $_GET["path"]."index.html";
1136 }
1137 }else{
1138 $_GET["path"] = "/index.html";
1139 }
1140
1141 $_GET["path"] = urldecode(esc_url_raw($_GET["path"]));
1142
1143 // for security
1144 if(preg_match("/\.{2,}/", $_GET["path"])){
1145 die("May be Directory Traversal Attack");
1146 }
1147
1148 $paths = array();
1149
1150 array_push($paths, $this->getWpContentDir("/cache/all").$_GET["path"]);
1151
1152 if(class_exists("WpFcMobileCache")){
1153 $wpfc_mobile = new WpFcMobileCache();
1154 array_push($paths, $this->getWpContentDir("/cache/wpfc-mobile-cache").$_GET["path"]);
1155 }
1156
1157 foreach ($paths as $key => $value){
1158 if(file_exists($value)){
1159 if(preg_match("/\/(all|wpfc-mobile-cache)\/index\.html$/i", $value)){
1160 @unlink($value);
1161 }else{
1162 $this->rm_folder_recursively($value);
1163 }
1164 }
1165 }
1166
1167 $this->delete_multiple_domain_mapping_cache();
1168
1169 die(json_encode(array("The cache of page has been cleared","success")));
1170 }else{
1171 die(json_encode(array("Path has NOT been defined", "error", "alert")));
1172 }
1173
1174 exit;
1175 }
1176
1177 private function cache(){
1178 if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']){
1179 include_once('inc/cache.php');
1180 $wpfc = new WpFastestCacheCreateCache();
1181 $wpfc->createCache();
1182 }
1183 }
1184
1185 protected function slug(){
1186 return "wp_fastest_cache";
1187 }
1188
1189 public function getWpContentDir($path = false){
1190 /*
1191 Sample Paths;
1192
1193 /cache/
1194
1195 /cache/all/
1196 /cache/all
1197 /cache/all/page
1198 /cache/all/index.html
1199
1200 /cache/wpfc-minified
1201
1202 /cache/wpfc-widget-cache
1203
1204 /cache/wpfc-mobile-cache/
1205 /cache/wpfc-mobile-cache/page
1206 /cache/wpfc-mobile-cache/index.html
1207
1208 /cache/tmpWpfc
1209 /cache/tmpWpfc/
1210 /cache/tmpWpfc/mobile_
1211 /cache/tmpWpfc/m
1212 /cache/tmpWpfc/w
1213
1214
1215 /cache/testWpFc/
1216
1217 /cache/all/testWpFc/
1218
1219 /cache/wpfc-widget-cache/
1220 /cache/wpfc-widget-cache
1221 /cache/wpfc-widget-cache/".$args["widget_id"].".html
1222 */
1223
1224 if($path){
1225 if(preg_match("/\/cache\/(all|wpfc-minified|wpfc-widget-cache|wpfc-mobile-cache)/", $path)){
1226 //WPML language switch
1227 //https://wpml.org/forums/topic/wpml-language-switch-wp-fastest-cache-issue/
1228 $language_negotiation_type = apply_filters('wpml_setting', false, 'language_negotiation_type');
1229 if(($language_negotiation_type == 2) && $this->isPluginActive('sitepress-multilingual-cms/sitepress.php')){
1230 $my_home_url = apply_filters('wpml_home_url', get_option('home'));
1231 $my_home_url = preg_replace("/https?\:\/\//i", "", $my_home_url);
1232 $my_home_url = trim($my_home_url, "/");
1233
1234 $path = preg_replace("/\/cache\/(all|wpfc-minified|wpfc-widget-cache|wpfc-mobile-cache)/", "/cache/".$my_home_url."/$1", $path);
1235 }else if(($language_negotiation_type == 1) && $this->isPluginActive('sitepress-multilingual-cms/sitepress.php')){
1236 $my_current_lang = apply_filters('wpml_current_language', NULL);
1237
1238 if($my_current_lang){
1239 $path = preg_replace("/\/cache\/wpfc-widget-cache\/(.+)/", "/cache/wpfc-widget-cache/".$my_current_lang."-"."$1", $path);
1240 }
1241 }
1242
1243 if($this->isPluginActive('multiple-domain-mapping-on-single-site/multidomainmapping.php')){
1244 $path = preg_replace("/\/cache\/(all|wpfc-minified|wpfc-widget-cache|wpfc-mobile-cache)/", "/cache/".$_SERVER['HTTP_HOST']."/$1", $path);
1245 }
1246
1247 if($this->isPluginActive('polylang/polylang.php') || $this->isPluginActive('polylang-pro/polylang.php')){
1248 $polylang_settings = get_option("polylang");
1249
1250 if(isset($polylang_settings["force_lang"])){
1251 if($polylang_settings["force_lang"] == 2 || $polylang_settings["force_lang"] == 3){
1252 // The language is set from the subdomain name in pretty permalinks
1253 // The language is set from different domains
1254 $path = preg_replace("/\/cache\/(all|wpfc-minified|wpfc-widget-cache|wpfc-mobile-cache)/", "/cache/".$_SERVER['HTTP_HOST']."/$1", $path);
1255 }
1256 }
1257 }
1258
1259 if($this->isPluginActive('multiple-domain/multiple-domain.php')){
1260 $path = preg_replace("/\/cache\/(all|wpfc-minified|wpfc-widget-cache|wpfc-mobile-cache)/", "/cache/".$_SERVER['HTTP_HOST']."/$1", $path);
1261 }
1262
1263 if(is_multisite()){
1264 $path = preg_replace("/\/cache\/(all|wpfc-minified|wpfc-widget-cache|wpfc-mobile-cache)/", "/cache/".$_SERVER['HTTP_HOST']."/$1", $path);
1265 }
1266 }
1267
1268 return WPFC_WP_CONTENT_DIR.$path;
1269 }else{
1270 return WPFC_WP_CONTENT_DIR;
1271 }
1272 }
1273
1274 protected function getOptions(){
1275 return $GLOBALS["wp_fastest_cache_options"];
1276 }
1277
1278 protected function getSystemMessage(){
1279 return $this->systemMessage;
1280 }
1281
1282 protected function get_excluded_useragent(){
1283 return "facebookexternalhit|WP_FASTEST_CACHE_CSS_VALIDATOR|Twitterbot|LinkedInBot|WhatsApp|Mediatoolkitbot";
1284 }
1285
1286 // protected function detectNewPost(){
1287 // if(isset($this->options->wpFastestCacheNewPost) && isset($this->options->wpFastestCacheStatus)){
1288 // add_filter ('save_post', array($this, 'deleteCache'));
1289 // }
1290 // }
1291
1292 public function deleteWidgetCache(){
1293 $widget_cache_path = $this->getWpContentDir("/cache/wpfc-widget-cache");
1294
1295 if(is_dir($widget_cache_path)){
1296 if(!is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
1297 if(@mkdir($this->getWpContentDir("/cache/tmpWpfc"), 0755, true)){
1298 //tmpWpfc has been created
1299 }
1300 }
1301
1302 if(@rename($widget_cache_path, $this->getWpContentDir("/cache/tmpWpfc/w").time())){
1303 //DONE
1304 }
1305 }
1306 }
1307
1308 public function clear_cache_after_woocommerce_update_product($product_id){
1309 if(!$this->deleted_before){
1310 $this->singleDeleteCache(false, $product_id);
1311 }
1312 }
1313
1314 public function on_all_status_transitions($new_status, $old_status, $post){
1315 $this->deleted_before = true;
1316
1317 if(!wp_is_post_revision($post->ID)){
1318 if(isset($post->post_type)){
1319 if($post->post_type == "nf_sub"){
1320 return 0;
1321 }
1322 }
1323
1324 if($new_status == "publish" && $old_status != "publish"){
1325
1326 $this->specificDeleteCache();
1327
1328 if(isset($this->options->wpFastestCacheNewPost) && isset($this->options->wpFastestCacheStatus)){
1329 if(isset($this->options->wpFastestCacheNewPost_type) && $this->options->wpFastestCacheNewPost_type){
1330 if($this->options->wpFastestCacheNewPost_type == "all"){
1331 $this->deleteCache();
1332 }else if($this->options->wpFastestCacheNewPost_type == "homepage"){
1333 $this->deleteHomePageCache();
1334
1335 //to clear category cache and tag cache
1336 $this->singleDeleteCache(false, $post->ID);
1337
1338 //to clear widget cache
1339 $this->deleteWidgetCache();
1340 }
1341 }else{
1342 $this->deleteCache();
1343 }
1344 }
1345 }
1346
1347 if(isset($this->options->wpFastestCacheStatus) && $new_status == "publish" && $old_status == "publish"){
1348
1349 // if(isset($this->options->wpFastestCacheUpdatePost) && isset($this->options->wpFastestCacheStatus)){
1350
1351 // if($this->options->wpFastestCacheUpdatePost_type == "post"){
1352 // $this->singleDeleteCache(false, $post->ID);
1353 // }else if($this->options->wpFastestCacheUpdatePost_type == "all"){
1354 // $this->deleteCache();
1355 // }
1356
1357 // }
1358
1359 $this->specificDeleteCache();
1360
1361 if(isset($this->options->wpFastestCacheUpdatePost)){
1362
1363 if($this->options->wpFastestCacheUpdatePost_type == "post"){
1364 $this->singleDeleteCache(false, $post->ID);
1365 }else if($this->options->wpFastestCacheUpdatePost_type == "all"){
1366 $this->deleteCache();
1367 }
1368
1369 }else{
1370 // to clear only cache of content even though the "update post" option is disabled
1371 $this->singleDeleteCache(false, $post->ID, false);
1372 }
1373 }
1374
1375 if($new_status == "trash" && $old_status == "publish"){
1376
1377 if($post->post_type == "shop_coupon"){
1378 // YITH WooCommerce Coupon Email System Premium
1379 return;
1380 }
1381
1382 $this->singleDeleteCache(false, $post->ID);
1383 }else if(($new_status == "draft" || $new_status == "pending" || $new_status == "private") && $old_status == "publish"){
1384 $this->deleteCache();
1385 }
1386 }
1387 }
1388
1389 protected function commentHooks(){
1390 //it works when the status of a comment changes
1391 add_action('wp_set_comment_status', array($this, 'detect_comment_status_change'), 10, 2);
1392
1393 //it works when a comment is saved in the database
1394 add_action('comment_post', array($this, 'detectNewComment'), 10, 2);
1395
1396 // it work when a commens is updated
1397 add_action('edit_comment', array($this, 'detectEditComment'), 10, 2);
1398 }
1399
1400 public function detect_comment_status_change($comment_id, $new_status) {
1401 $comment = get_comment($comment_id);
1402
1403 if (!$comment) {
1404 return; // Exit if the comment doesn't exist
1405 }
1406
1407 // Check if the comment was pending and is now marked as spam
1408 if($comment->comment_status == 'open' && $new_status === 'spam'){
1409 return;
1410 }
1411
1412 // Check if the comment was pending and is now marked as trash
1413 if($comment->comment_status == 'open' && $new_status === 'trash'){
1414 return;
1415 }
1416
1417 $this->singleDeleteCache($comment_id);
1418 }
1419
1420 public function detectEditComment($comment_id, $comment_data){
1421 if($comment_data["comment_approved"] == 1){
1422 $this->singleDeleteCache($comment_id);
1423 }
1424 }
1425
1426 public function detectNewComment($comment_id, $comment_approved){
1427 // if(current_user_can( 'manage_options') || !get_option('comment_moderation')){
1428 if($comment_approved === 1){
1429 $this->singleDeleteCache($comment_id);
1430 }
1431 }
1432
1433 public function specificDeleteCache(){
1434 $urls = get_option("WpFastestCacheCSP");
1435 $files = array();
1436
1437 if(!empty($urls)){
1438 foreach ($urls as $key => $value) {
1439
1440 if(preg_match("/https?:\/\/[^\/]+\/(.+)/", $value->url, $out)){
1441
1442 if(preg_match("/\.{2,}/", $out[1])){
1443 // to prevent Directory Traversal Attack
1444 continue;
1445 }
1446
1447 if(preg_match("/\/\(\.\*\)/", $out[1])){
1448 $out[1] = str_replace("(.*)", "", $out[1]);
1449
1450 $path = $this->getWpContentDir("/cache/all/").$out[1];
1451 $mobile_path = $this->getWpContentDir("/cache/wpfc-mobile-cache/").$out[1];
1452 }else{
1453 $out[1] = $out[1]."index.html";
1454
1455 $path = $this->getWpContentDir("/cache/all/").$out[1];
1456 $mobile_path = $this->getWpContentDir("/cache/wpfc-mobile-cache/").$out[1];
1457 }
1458
1459 if(!is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
1460 @mkdir($this->getWpContentDir("/cache/tmpWpfc"), 0755, true);
1461 }
1462
1463 if(is_dir($path)){
1464 rename($path, $this->getWpContentDir("/cache/tmpWpfc/").time());
1465 }else if(is_file($path)){
1466 @unlink($path);
1467 }
1468
1469 if(is_dir($mobile_path)){
1470 rename($mobile_path, $this->getWpContentDir("/cache/tmpWpfc/mobile_").time());
1471 }else if(is_file($mobile_path)){
1472 @unlink($mobile_path);
1473 }
1474
1475 }
1476
1477 }
1478 }
1479 }
1480
1481 public function singleDeleteCache($comment_id = false, $post_id = false, $to_clear_parents = true){
1482 if($varnish_datas = get_option("WpFastestCacheVarnish")){
1483 include_once('inc/varnish.php');
1484 VarnishWPFC::purge_cache($varnish_datas);
1485 }
1486
1487 include_once('inc/cdn.php');
1488 CdnWPFC::cloudflare_clear_cache();
1489
1490 $to_clear_feed = true;
1491
1492 // not to clear cache of homepage/cats/tags after ajax request by other plugins
1493 if(isset($_POST) && isset($_POST["action"])){
1494 // kk Star Rating
1495 if($_POST["action"] == "kksr_ajax"){
1496 $to_clear_parents = false;
1497 }
1498
1499 // All In One Schema.org Rich Snippets
1500 if(preg_match("/bsf_(update|submit)_rating/i", $_POST["action"])){
1501 $to_clear_parents = false;
1502 }
1503
1504 // Yet Another Stars Rating
1505 if($_POST["action"] == "yasr_send_visitor_rating"){
1506 $to_clear_parents = false;
1507 $post_id = sanitize_text_field($_POST["post_id"]);
1508 }
1509
1510 // All In One Schema.org Rich Snippets
1511 if(preg_match("/bsf_(update|submit)_rating/i", $_POST["action"])){
1512 $to_clear_feed = false;
1513 }
1514 }
1515
1516 if($comment_id){
1517 $comment_id = intval($comment_id);
1518
1519 $comment = get_comment($comment_id);
1520
1521 if($comment && $comment->comment_post_ID){
1522 $post_id = $comment->comment_post_ID;
1523 }
1524 }
1525
1526 if($post_id){
1527 $post_id = intval($post_id);
1528
1529 $permalink = get_permalink($post_id);
1530
1531 $permalink = urldecode(get_permalink($post_id));
1532
1533
1534
1535 //for trash contents
1536 if(preg_match("/\/\?(p|page_id)\=\d+/i", $permalink)){
1537 $post = get_post($post_id);
1538
1539 $clone_post = clone $post;
1540 $clone_post->post_status = 'publish';
1541
1542 $permalink = get_permalink($clone_post);
1543 $permalink = rtrim($permalink, "/");
1544
1545 $permalink = preg_replace("/__trashed$/", "", $permalink);
1546 //for /%postname%/%post_id% : sample-url__trashed/57595
1547 $permalink = preg_replace("/__trashed\/(\d+)$/", "/$1", $permalink);
1548 }
1549
1550
1551
1552 if(preg_match("/https?:\/\/[^\/]+\/(.+)/", $permalink, $out)){
1553 $path = $this->getWpContentDir("/cache/all/").$out[1];
1554 $mobile_path = $this->getWpContentDir("/cache/wpfc-mobile-cache/").$out[1];
1555
1556 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
1557 include_once $this->get_premium_path("logs.php");
1558 $log = new WpFastestCacheLogs("delete");
1559 $log->action();
1560 }
1561
1562 $files = array();
1563
1564 if(is_dir($path)){
1565 array_push($files, $path);
1566 }
1567
1568 if(is_dir($mobile_path)){
1569 array_push($files, $mobile_path);
1570 }
1571
1572 if(defined('WPFC_CACHE_QUERYSTRING') && WPFC_CACHE_QUERYSTRING){
1573 $files_with_query_string = glob($path."\?*");
1574 $mobile_files_with_query_string = glob($mobile_path."\?*");
1575
1576 if(is_array($files_with_query_string) && (count($files_with_query_string) > 0)){
1577 $files = array_merge($files, $files_with_query_string);
1578 }
1579
1580 if(is_array($mobile_files_with_query_string) && (count($mobile_files_with_query_string) > 0)){
1581 $files = array_merge($files, $mobile_files_with_query_string);
1582 }
1583 }
1584
1585 if($to_clear_feed){
1586 // to clear cache of /feed
1587 if(preg_match("/https?:\/\/[^\/]+\/(.+)/", get_feed_link(), $feed_out)){
1588 array_push($files, $this->getWpContentDir("/cache/all/").$feed_out[1]);
1589 }
1590
1591 // to clear cache of /comments/feed/
1592 if(preg_match("/https?:\/\/[^\/]+\/(.+)/", get_feed_link("comments_"), $comment_feed_out)){
1593 array_push($files, $this->getWpContentDir("/cache/all/").$comment_feed_out[1]);
1594 }
1595 }
1596
1597 foreach((array)$files as $file){
1598 $this->rm_folder_recursively($file);
1599 }
1600 }
1601
1602 if($to_clear_parents){
1603 // to clear cache of homepage
1604 $this->deleteHomePageCache();
1605
1606 // to clear cache of author page
1607 $this->delete_author_page_cache($post_id);
1608
1609 // to clear sitemap cache
1610 $this->delete_sitemap_cache();
1611
1612 // to clear cache of next post and cache of prev post
1613 $this->delete_next_prev_cache($post_id);
1614
1615 // to clear cache of cats and tags which contains the post (only first page)
1616 global $wpdb;
1617 $terms = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."term_relationships` WHERE `object_id`=".$post_id, ARRAY_A);
1618
1619 foreach ($terms as $term_key => $term_val){
1620 $this->delete_cache_of_term($term_val["term_taxonomy_id"]);
1621 }
1622 }
1623
1624 $this->delete_multiple_domain_mapping_cache();
1625 }
1626 }
1627
1628 public function delete_next_prev_cache($post_id){
1629 global $wpdb;
1630
1631 $post = get_post($post_id);
1632
1633 $next_post_query = "SELECT * FROM `".$wpdb->prefix."posts` WHERE ID = (SELECT min(ID) FROM `".$wpdb->prefix."posts` where ID > ".$post_id." AND `post_type` = '".$post->post_type."' AND `post_status` = 'publish')";
1634 $prev_post_query = "SELECT * FROM `".$wpdb->prefix."posts` WHERE ID = (SELECT max(ID) FROM `".$wpdb->prefix."posts` where ID < ".$post_id." AND `post_type` = '".$post->post_type."' AND `post_status` = 'publish')";
1635
1636 $res = $wpdb->get_results($next_post_query." UNION ".$prev_post_query);
1637
1638 if(isset($res[0])){
1639
1640 foreach ($res as $key => $value) {
1641 $permalink = urldecode(get_permalink($value->ID));
1642
1643 if(preg_match("/https?:\/\/[^\/]+\/(.+)/", $permalink, $out)){
1644 $path = $this->getWpContentDir("/cache/all/").$out[1];
1645 $mobile_path = $this->getWpContentDir("/cache/wpfc-mobile-cache/").$out[1];
1646
1647 if(is_dir($path)){
1648 $this->rm_folder_recursively($path);
1649 }
1650
1651 if(is_dir($mobile_path)){
1652 $this->rm_folder_recursively($mobile_path);
1653 }
1654 }
1655 }
1656
1657 }
1658
1659
1660 }
1661
1662 public function delete_sitemap_cache(){
1663 //to clear sitemap.xml and sitemap-(.+).xml
1664 $files = array_merge(glob($this->getWpContentDir("/cache/all/")."sitemap*.xml"), glob($this->getWpContentDir("/cache/wpfc-mobile-cache/")."sitemap*.xml"));
1665
1666 foreach((array)$files as $file){
1667 $this->rm_folder_recursively($file);
1668 }
1669 }
1670
1671 public function delete_multiple_domain_mapping_cache($minified = false){
1672 //https://wordpress.org/plugins/multiple-domain-mapping-on-single-site/
1673 if($this->isPluginActive("multiple-domain-mapping-on-single-site/multidomainmapping.php")){
1674 $multiple_arr = get_option('falke_mdm_mappings');
1675
1676 if(isset($multiple_arr) && isset($multiple_arr["mappings"]) && isset($multiple_arr["mappings"][0])){
1677 foreach($multiple_arr["mappings"] as $mapping_key => $mapping_value){
1678 if($minified){
1679 $mapping_domain_path = preg_replace("/(\/cache\/[^\/]+\/all\/)/", "/cache/".$mapping_value["domain"]."/", $this->getWpContentDir("/cache/all/"));
1680
1681 if(is_dir($mapping_domain_path)){
1682 if(@rename($mapping_domain_path, $this->getWpContentDir("/cache/tmpWpfc/").$mapping_value["domain"]."_".time())){
1683
1684 }
1685 }
1686 }else{
1687 $mapping_domain_path = preg_replace("/(\/cache\/[^\/]+\/all)/", "/cache/".$mapping_value["domain"]."/all", $this->getWpContentDir("/cache/all/index.html"));
1688
1689 @unlink($mapping_domain_path);
1690 }
1691 }
1692 }
1693 }
1694 }
1695
1696 public function delete_author_page_cache($post_id){
1697 $author_id = get_post_field ('post_author', $post_id);
1698 $permalink = get_author_posts_url($author_id);
1699
1700 if(preg_match("/https?:\/\/[^\/]+\/(.+)/", $permalink, $out)){
1701 $path = $this->getWpContentDir("/cache/all/").$out[1];
1702 $mobile_path = $this->getWpContentDir("/cache/wpfc-mobile-cache/").$out[1];
1703
1704 $this->move_folder_to_tmpWpfc($path);
1705 $this->move_folder_to_tmpWpfc($mobile_path);
1706 }
1707 }
1708
1709 public function move_folder_to_tmpWpfc($path = false){
1710 if(!is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
1711 @mkdir($this->getWpContentDir("/cache/tmpWpfc"), 0755, true);
1712 }
1713
1714 if(!$path){
1715 return false;
1716 }
1717
1718 if(!is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
1719 return false;
1720 }
1721
1722 if(is_dir($path)){
1723 preg_match("/\/([^\/]+)\/?$/", $path, $type);
1724
1725 if(isset($type[1])){
1726 if(preg_match("/\/cache\/wpfc-mobile-cache\//", $path)){
1727 $type[1] = "m_".$type[1];
1728 }
1729
1730 $move_to = $this->getWpContentDir("/cache/tmpWpfc/").$type[1]."_".time();
1731
1732 rename($path, $move_to);
1733 }
1734 }
1735 }
1736
1737 public function delete_cache_of_term($term_taxonomy_id){
1738 $term = get_term_by("term_taxonomy_id", $term_taxonomy_id);
1739
1740 if(!$term || is_wp_error($term)){
1741 return false;
1742 }
1743
1744 //if(preg_match("/cat|tag|store|listing/", $term->taxonomy)){}
1745
1746 $url = get_term_link($term->term_id, $term->taxonomy);
1747
1748 if(preg_match("/^http/", $url)){
1749 $path = preg_replace("/https?\:\/\/[^\/]+/i", "", $url);
1750 $path = trim($path, "/");
1751 $path = urldecode($path);
1752
1753 // to remove the cache of tag/cat
1754 if(file_exists($this->getWpContentDir("/cache/all/").$path."/index.html")){
1755 @unlink($this->getWpContentDir("/cache/all/").$path."/index.html");
1756 }
1757
1758 if(file_exists($this->getWpContentDir("/cache/wpfc-mobile-cache/").$path."/index.html")){
1759 @unlink($this->getWpContentDir("/cache/wpfc-mobile-cache/").$path."/index.html");
1760 }
1761
1762
1763 // to remove the cache of the pages
1764 $this->rm_folder_recursively($this->getWpContentDir("/cache/all/").$path."/page");
1765 $this->rm_folder_recursively($this->getWpContentDir("/cache/wpfc-mobile-cache/").$path."/page");
1766
1767 // to remove the cache of the feeds
1768 $this->rm_folder_recursively($this->getWpContentDir("/cache/all/").$path."/feed");
1769 $this->rm_folder_recursively($this->getWpContentDir("/cache/wpfc-mobile-cache/").$path."/feed");
1770 }
1771
1772 if($term->parent > 0){
1773 $parent = get_term_by("id", $term->parent, $term->taxonomy);
1774
1775 if(isset($parent->term_taxonomy_id)){
1776 $this->delete_cache_of_term($parent->term_taxonomy_id);
1777 }
1778 }
1779 }
1780
1781 public function unlink($path){
1782 if(file_exists($path)){
1783 unlink($path);
1784 }
1785 }
1786
1787 public function deleteHomePageCache($log = true){
1788 if($varnish_datas = get_option("WpFastestCacheVarnish")){
1789 include_once('inc/varnish.php');
1790 VarnishWPFC::purge_cache($varnish_datas);
1791 }
1792
1793 include_once('inc/cdn.php');
1794 CdnWPFC::cloudflare_clear_cache();
1795
1796 $site_url_path = preg_replace("/https?\:\/\/[^\/]+/i", "", site_url());
1797 $home_url_path = preg_replace("/https?\:\/\/[^\/]+/i", "", home_url());
1798
1799 if($site_url_path){
1800 $site_url_path = trim($site_url_path, "/");
1801
1802 if($site_url_path){
1803 $this->unlink($this->getWpContentDir("/cache/all/").$site_url_path."/index.html");
1804 $this->unlink($this->getWpContentDir("/cache/wpfc-mobile-cache/").$site_url_path."/index.html");
1805
1806 //to clear pagination of homepage cache
1807 $this->rm_folder_recursively($this->getWpContentDir("/cache/all/").$site_url_path."/page");
1808 $this->rm_folder_recursively($this->getWpContentDir("/cache/wpfc-mobile-cache/").$site_url_path."/page");
1809 }
1810 }
1811
1812 if($home_url_path){
1813 $home_url_path = trim($home_url_path, "/");
1814
1815 if($home_url_path){
1816 $this->unlink($this->getWpContentDir("/cache/all/").$home_url_path."/index.html");
1817 $this->unlink($this->getWpContentDir("/cache/wpfc-mobile-cache/").$home_url_path."/index.html");
1818
1819 //to clear pagination of homepage cache
1820 $this->rm_folder_recursively($this->getWpContentDir("/cache/all/").$home_url_path."/page");
1821 $this->rm_folder_recursively($this->getWpContentDir("/cache/wpfc-mobile-cache/").$home_url_path."/page");
1822 }
1823 }
1824
1825 if(function_exists("wc_get_page_id")){
1826 if($shop_id = wc_get_page_id('shop')){
1827 $store_url_path = preg_replace("/https?\:\/\/[^\/]+/i", "", get_permalink($shop_id));
1828
1829 if($store_url_path){
1830 $store_url_path = trim($store_url_path, "/");
1831
1832 if($store_url_path){
1833 if(file_exists($this->getWpContentDir("/cache/all/").$store_url_path."/index.html")){
1834 @unlink($this->getWpContentDir("/cache/all/").$store_url_path."/index.html");
1835 }
1836
1837 if(file_exists($this->getWpContentDir("/cache/wpfc-mobile-cache/").$store_url_path."/index.html")){
1838 @unlink($this->getWpContentDir("/cache/wpfc-mobile-cache/").$store_url_path."/index.html");
1839 }
1840
1841 //to clear pagination of store homepage cache
1842 $this->rm_folder_recursively($this->getWpContentDir("/cache/all/").$store_url_path."/page");
1843 $this->rm_folder_recursively($this->getWpContentDir("/cache/wpfc-mobile-cache/").$store_url_path."/page");
1844 }
1845 }
1846 }
1847 }
1848
1849 $this->unlink($this->getWpContentDir("/cache/all/index.html"));
1850 $this->unlink($this->getWpContentDir("/cache/wpfc-mobile-cache/index.html"));
1851
1852 //to clear pagination of homepage cache
1853 $this->rm_folder_recursively($this->getWpContentDir("/cache/all/page"));
1854 $this->rm_folder_recursively($this->getWpContentDir("/cache/wpfc-mobile-cache/page"));
1855
1856 // options-reading.php - static posts page
1857 if($page_for_posts_id = get_option('page_for_posts')){
1858 $page_for_posts_permalink = urldecode(get_permalink($page_for_posts_id));
1859
1860 $page_for_posts_permalink = rtrim($page_for_posts_permalink, "/");
1861 $page_for_posts_permalink = preg_replace("/__trashed$/", "", $page_for_posts_permalink);
1862 //for /%postname%/%post_id% : sample-url__trashed/57595
1863 $page_for_posts_permalink = preg_replace("/__trashed\/(\d+)$/", "/$1", $page_for_posts_permalink);
1864
1865 if(preg_match("/https?:\/\/[^\/]+\/(.+)/", $page_for_posts_permalink, $out)){
1866 $page_for_posts_path = $this->getWpContentDir("/cache/all/").$out[1];
1867 $page_for_posts_mobile_path = $this->getWpContentDir("/cache/wpfc-mobile-cache/").$out[1];
1868
1869 $this->rm_folder_recursively($page_for_posts_path);
1870 $this->rm_folder_recursively($page_for_posts_mobile_path);
1871 }
1872 }
1873
1874
1875 if($log){
1876 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
1877 include_once $this->get_premium_path("logs.php");
1878
1879 $log = new WpFastestCacheLogs("delete");
1880 $log->action();
1881 }
1882 }
1883 }
1884
1885 public function deleteCache($minified = false){
1886 if($varnish_datas = get_option("WpFastestCacheVarnish")){
1887 include_once('inc/varnish.php');
1888 VarnishWPFC::purge_cache($varnish_datas);
1889 }
1890
1891 include_once('inc/cdn.php');
1892 CdnWPFC::cloudflare_clear_cache();
1893
1894 $this->set_preload();
1895
1896 $created_tmpWpfc = false;
1897 $cache_deleted = false;
1898 $minifed_deleted = false;
1899
1900 $cache_path = $this->getWpContentDir("/cache/all");
1901 $minified_cache_path = $this->getWpContentDir("/cache/wpfc-minified");
1902
1903 if(class_exists("WpFcMobileCache")){
1904
1905
1906
1907
1908 if(is_dir($this->getWpContentDir("/cache/wpfc-mobile-cache"))){
1909 if(is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
1910 rename($this->getWpContentDir("/cache/wpfc-mobile-cache"), $this->getWpContentDir("/cache/tmpWpfc/mobile_").time());
1911 }else if(@mkdir($this->getWpContentDir("/cache/tmpWpfc"), 0755, true)){
1912 rename($this->getWpContentDir("/cache/wpfc-mobile-cache"), $this->getWpContentDir("/cache/tmpWpfc/mobile_").time());
1913 }
1914 }
1915
1916
1917 }
1918
1919 if(!is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
1920 if(@mkdir($this->getWpContentDir("/cache/tmpWpfc"), 0755, true)){
1921 $created_tmpWpfc = true;
1922 }else{
1923 $created_tmpWpfc = false;
1924 //$this->systemMessage = array("Permission of <strong>/wp-content/cache</strong> must be <strong>755</strong>", "error");
1925 }
1926 }else{
1927 $created_tmpWpfc = true;
1928 }
1929
1930 //to clear widget cache path
1931 $this->deleteWidgetCache();
1932
1933 $this->delete_multiple_domain_mapping_cache($minified);
1934
1935 if(is_dir($cache_path)){
1936 if(@rename($cache_path, $this->getWpContentDir("/cache/tmpWpfc/").time())){
1937 delete_option("WpFastestCacheHTML");
1938 delete_option("WpFastestCacheHTMLSIZE");
1939 delete_option("WpFastestCacheMOBILE");
1940 delete_option("WpFastestCacheMOBILESIZE");
1941
1942 $cache_deleted = true;
1943 }
1944 }else{
1945 $cache_deleted = true;
1946 }
1947
1948 if($minified){
1949 if(is_dir($minified_cache_path)){
1950 if(@rename($minified_cache_path, $this->getWpContentDir("/cache/tmpWpfc/m").time())){
1951 delete_option("WpFastestCacheCSS");
1952 delete_option("WpFastestCacheCSSSIZE");
1953 delete_option("WpFastestCacheJS");
1954 delete_option("WpFastestCacheJSSIZE");
1955
1956 $minifed_deleted = true;
1957 }
1958 }else{
1959 $minifed_deleted = true;
1960 }
1961 }else{
1962 $minifed_deleted = true;
1963 }
1964
1965 if($created_tmpWpfc && $cache_deleted && $minifed_deleted){
1966 do_action('wpfc_delete_cache');
1967
1968 $this->notify(array("All cache files have been deleted", "updated"));
1969
1970 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
1971 include_once $this->get_premium_path("logs.php");
1972
1973 $log = new WpFastestCacheLogs("delete");
1974 $log->action();
1975 }
1976 }else{
1977 $this->notify(array("Permissions Problem: <a href='http://www.wpfastestcache.com/warnings/delete-cache-problem-related-to-permission/' target='_blank'>Read More</a>", "error"));
1978 }
1979
1980 // for ajax request
1981 if(isset($_GET["action"]) && in_array($_GET["action"], array("wpfc_delete_cache", "wpfc_delete_cache_and_minified"))){
1982 die(json_encode($this->systemMessage));
1983 }
1984 }
1985
1986 public function checkCronTime(){
1987 $crons = _get_cron_array();
1988
1989 foreach ((array)$crons as $cron_key => $cron_value) {
1990 foreach ( (array) $cron_value as $hook => $events ) {
1991 if(preg_match("/^wp\_fastest\_cache(.*)/", $hook, $id)){
1992 if(!$id[1] || preg_match("/^\_(\d+)$/", $id[1])){
1993 foreach ( (array) $events as $event_key => $event ) {
1994 add_action("wp_fastest_cache".$id[1], array($this, 'setSchedule'));
1995 }
1996 }
1997 }
1998 }
1999 }
2000
2001 add_action($this->slug()."_Preload", array($this, 'create_preload_cache'), 11);
2002 }
2003
2004 public function set_preload(){
2005 include_once('inc/preload.php');
2006 PreloadWPFC::set_preload($this->slug());
2007 }
2008
2009 public function create_preload_cache(){
2010 $this->options = $this->getOptions();
2011
2012 include_once('inc/preload.php');
2013 PreloadWPFC::create_preload_cache($this->options);
2014 }
2015
2016 public function wpfc_remote_get($url, $user_agent, $return_content = false){
2017 //$response = wp_remote_get($url, array('timeout' => 10, 'sslverify' => false, 'headers' => array("cache-control" => array("no-store, no-cache, must-revalidate", "post-check=0, pre-check=0"),'user-agent' => $user_agent)));
2018 $response = wp_remote_get($url, array('user-agent' => $user_agent, 'timeout' => 10, 'sslverify' => false, 'headers' => array("cache-control" => "no-store, no-cache, must-revalidate, post-check=0, pre-check=0")));
2019
2020 if (!$response || is_wp_error($response)){
2021 echo $response->get_error_message()." - ";
2022
2023 return false;
2024 }else{
2025 if(wp_remote_retrieve_response_code($response) != 200){
2026 return false;
2027 }
2028
2029 if($return_content){
2030 if(wp_remote_retrieve_response_code($response) == 200){
2031 $data = wp_remote_retrieve_body($response);
2032
2033 return $data;
2034 }
2035 }
2036 }
2037
2038 return true;
2039 }
2040
2041 public function setSchedule($args = ""){
2042 if($args){
2043 $rule = json_decode($args);
2044
2045 if($rule->prefix == "all"){
2046 $this->deleteCache();
2047 }else if($rule->prefix == "homepage"){
2048 $this->deleteHomePageCache();
2049
2050 if(isset($this->options->wpFastestCachePreload_homepage) && $this->options->wpFastestCachePreload_homepage){
2051 $this->wpfc_remote_get(get_option("home"), "WP Fastest Cache Preload Bot - After Cache Timeout");
2052 $this->wpfc_remote_get(get_option("home"), "WP Fastest Cache Preload iPhone Mobile Bot - After Cache Timeout");
2053 }
2054 }else if($rule->prefix == "startwith"){
2055 if(!is_dir($this->getWpContentDir("/cache/tmpWpfc"))){
2056 if(@mkdir($this->getWpContentDir("/cache/tmpWpfc"), 0755, true)){}
2057 }
2058
2059 $rule->content = trim($rule->content, "/");
2060
2061 $files = glob($this->getWpContentDir("/cache/all/").$rule->content."*");
2062
2063 foreach ((array)$files as $file) {
2064 $mobile_file = str_replace("/cache/all/", "/cache/wpfc-mobile-cache/", $file);
2065
2066 @rename($file, $this->getWpContentDir("/cache/tmpWpfc/").time());
2067 @rename($mobile_file, $this->getWpContentDir("/cache/tmpWpfc/mobile_").time());
2068 }
2069 }else if($rule->prefix == "exact"){
2070 $rule->content = trim($rule->content, "/");
2071
2072 @unlink($this->getWpContentDir("/cache/all/").$rule->content."/index.html");
2073 @unlink($this->getWpContentDir("/cache/wpfc-mobile-cache/").$rule->content."/index.html");
2074 }
2075
2076 if($rule->prefix != "all"){
2077 if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
2078 include_once $this->get_premium_path("logs.php");
2079 $log = new WpFastestCacheLogs("delete");
2080 $log->action($rule);
2081 }
2082 }
2083 }else{
2084 //for old cron job
2085 $this->deleteCache();
2086 }
2087 }
2088
2089 public function modify_htaccess_for_new_user($user_id){
2090 $path = ABSPATH;
2091
2092 if($this->is_subdirectory_install()){
2093 $path = $this->getABSPATH();
2094 }
2095
2096 $htaccess = @file_get_contents($path.".htaccess");
2097
2098 if(preg_match("/\#\s?Start_WPFC_Exclude_Admin_Cookie/", $htaccess)){
2099 $rules = $this->excludeAdminCookie();
2100
2101 $htaccess = preg_replace("/\#\s?Start_WPFC_Exclude_Admin_Cookie[^\#]*\#\s?End_WPFC_Exclude_Admin_Cookie\s+/", $rules, $htaccess);
2102 }
2103
2104 @file_put_contents($path.".htaccess", $htaccess);
2105 }
2106
2107 public function excludeAdminCookie(){
2108 $usernames = array();
2109 $users_groups = array_chunk(get_users(array("role" => "administrator", "fields" => array("user_login"))), 5);
2110
2111 foreach ($users_groups as $group_key => $group) {
2112 $tmp_user = "";
2113 $tmp_rule = "";
2114
2115 foreach ($group as $key => $value) {
2116 $tmp_user = sanitize_user(wp_unslash($value->user_login), true);
2117
2118 /*
2119 to replace spaces with %20
2120
2121 1. Empty space character causes 500 internal server error
2122 2. "\s" is not detected by htaccess so we added "%20"
2123 */
2124
2125 $tmp_user = preg_replace("/\s/", "%20", $tmp_user);
2126
2127 array_push($usernames, $tmp_user);
2128 }
2129 }
2130
2131 $rule = "# Start_WPFC_Exclude_Admin_Cookie\n";
2132 $rule = $rule."RewriteCond %{HTTP:Cookie} !wordpress_logged_in_[^\=]+\=".implode("|", $usernames);
2133 $rule = $rule."\n# End_WPFC_Exclude_Admin_Cookie\n";
2134
2135 return $rule;
2136 }
2137
2138
2139
2140
2141
2142 public function excludeRules(){
2143 $htaccess_page_rules = "";
2144 $htaccess_page_useragent = "";
2145 $htaccess_page_cookie = "";
2146
2147 $rules_json = get_option("WpFastestCacheExclude");
2148
2149 if(!empty($rules_json) && $rules_json !== "null"){
2150 $rules_std = json_decode($rules_json);
2151
2152 // JSON hatalıysa çık
2153 if (json_last_error() !== JSON_ERROR_NONE || !is_array($rules_std)) {
2154 return "";
2155 }
2156
2157 foreach ($rules_std as $value) {
2158
2159 // default type
2160 $value->type = isset($value->type) ? $value->type : "page";
2161
2162 // content yoksa skip
2163 if(empty($value->content)){
2164 continue;
2165 }
2166
2167 // HTML entity decode (örn: &lt; &gt;)
2168 $content = html_entity_decode($value->content, ENT_QUOTES, 'UTF-8');
2169
2170 // ---------- PAGE RULES ----------
2171 if($value->type == "page"){
2172
2173 // REGEX (escape yok!)
2174 if(isset($value->prefix) && $value->prefix == "regex"){
2175
2176 // basit regex validation (çok kırılmayı önler)
2177 if(@preg_match("/".$content."/", null) === false){
2178 continue; // hatalı regex skip
2179 }
2180
2181 $htaccess_page_rules .= "RewriteCond %{REQUEST_URI} !".$content." [NC]\n";
2182 }
2183
2184 // NORMAL RULES (escape var)
2185 else{
2186
2187 // regex injection önleme
2188 $safe_content = preg_quote($content, "/");
2189
2190 if($value->prefix == "startwith"){
2191 $safe_content = ltrim($safe_content, "/");
2192 $htaccess_page_rules .= "RewriteCond %{REQUEST_URI} !^/".$safe_content." [NC]\n";
2193 }
2194
2195 if($value->prefix == "contain"){
2196 $htaccess_page_rules .= "RewriteCond %{REQUEST_URI} !".$safe_content." [NC]\n";
2197 }
2198
2199 if($value->prefix == "exact"){
2200 $safe_content = trim($safe_content, "/");
2201 $htaccess_page_rules .= "RewriteCond %{REQUEST_URI} !^/".$safe_content."$ [NC]\n";
2202 }
2203 }
2204 }
2205
2206 // ---------- USER AGENT ----------
2207 else if($value->type == "useragent"){
2208 $safe_content = preg_quote($content, "/");
2209 $htaccess_page_useragent .= "RewriteCond %{HTTP_USER_AGENT} !".$safe_content." [NC]\n";
2210 }
2211
2212 // ---------- COOKIE ----------
2213 else if($value->type == "cookie"){
2214
2215
2216 if($value->prefix == "contain"){
2217
2218 $safe_content = preg_quote($content, "/");
2219 $htaccess_page_cookie .= "RewriteCond %{HTTP:Cookie} !".$safe_content." [NC]\n";
2220
2221 }else if($value->prefix == "regex"){
2222
2223 // basit regex validation (çok kırılmayı önler)
2224 if(@preg_match("/".$content."/", null) === false){
2225 continue; // hatalı regex skip
2226 }
2227
2228 $htaccess_page_cookie .= "RewriteCond %{HTTP:Cookie} !".$content." [NC]\n";
2229
2230 }
2231
2232
2233 }
2234 }
2235 }
2236
2237 return "# Start WPFC Exclude\n"
2238 .$htaccess_page_rules
2239 .$htaccess_page_useragent
2240 .$htaccess_page_cookie
2241 ."# End WPFC Exclude\n";
2242 }
2243
2244
2245
2246
2247
2248
2249 public function getABSPATH(){
2250
2251 if(function_exists("get_home_path")){
2252 return get_home_path();
2253 }else{
2254
2255 $path = ABSPATH;
2256 $siteUrl = site_url();
2257 $homeUrl = home_url();
2258 $diff = str_replace($homeUrl, "", $siteUrl);
2259 $diff = trim($diff,"/");
2260
2261 $pos = strrpos($path, $diff);
2262
2263 if($pos !== false){
2264 $path = substr_replace($path, "", $pos, strlen($diff));
2265 $path = trim($path,"/");
2266 $path = "/".$path."/";
2267 }
2268 return $path;
2269
2270 }
2271
2272 }
2273
2274 public function rm_folder_recursively($dir, &$i = 1) {
2275
2276 if (!is_dir($dir)) {
2277 return true;
2278 }
2279
2280 $files = @scandir($dir);
2281
2282 if ($files === false) {
2283 return true;
2284 }
2285
2286 foreach ($files as $file) {
2287
2288 if ($file === '.' || $file === '..') {
2289 continue;
2290 }
2291
2292 if ($i > 100 && !preg_match("/wp-fastest-cache-premium/i", $dir)) {
2293 return true;
2294 }
2295
2296 $path = $dir . '/' . $file;
2297
2298 if (is_dir($path)) {
2299 $this->rm_folder_recursively($path, $i);
2300 } else {
2301 if (file_exists($path)) {
2302 @unlink($path);
2303
2304 $i++;
2305 }
2306 }
2307 }
2308
2309 if (is_dir($dir)) {
2310 $files_tmp = @scandir($dir);
2311
2312 if ($files_tmp !== false && count($files_tmp) <= 2) {
2313 @rmdir($dir);
2314 }
2315 }
2316
2317 return true;
2318 }
2319
2320 public function is_subdirectory_install(){
2321 if(strlen(site_url()) > strlen(home_url())){
2322 return true;
2323 }
2324 return false;
2325 }
2326
2327 protected function getMobileUserAgents(){
2328 return implode("|", $this->get_mobile_browsers())."|".implode("|", $this->get_operating_systems());
2329 }
2330
2331 public function get_premium_path($name){
2332 return WPFC_WP_PLUGIN_DIR."/wp-fastest-cache-premium/pro/library/".$name;
2333 }
2334
2335 public function cron_add_minute( $schedules ) {
2336 $schedules['everyminute'] = array(
2337 'interval' => 60*1,
2338 'display' => __( 'Once Every 1 Minute' ),
2339 'wpfc' => true
2340 );
2341
2342 $schedules['everyfiveminute'] = array(
2343 'interval' => 60*5,
2344 'display' => __( 'Once Every 5 Minutes' ),
2345 'wpfc' => true
2346 );
2347
2348 $schedules['everyfifteenminute'] = array(
2349 'interval' => 60*15,
2350 'display' => __( 'Once Every 15 Minutes' ),
2351 'wpfc' => true
2352 );
2353
2354 $schedules['twiceanhour'] = array(
2355 'interval' => 60*30,
2356 'display' => __( 'Twice an Hour' ),
2357 'wpfc' => true
2358 );
2359
2360 $schedules['onceanhour'] = array(
2361 'interval' => 60*60,
2362 'display' => __( 'Once an Hour' ),
2363 'wpfc' => true
2364 );
2365
2366 $schedules['everytwohours'] = array(
2367 'interval' => 60*60*2,
2368 'display' => __( 'Once Every 2 Hours' ),
2369 'wpfc' => true
2370 );
2371
2372 $schedules['everythreehours'] = array(
2373 'interval' => 60*60*3,
2374 'display' => __( 'Once Every 3 Hours' ),
2375 'wpfc' => true
2376 );
2377
2378 $schedules['everyfourhours'] = array(
2379 'interval' => 60*60*4,
2380 'display' => __( 'Once Every 4 Hours' ),
2381 'wpfc' => true
2382 );
2383
2384 $schedules['everyfivehours'] = array(
2385 'interval' => 60*60*5,
2386 'display' => __( 'Once Every 5 Hours' ),
2387 'wpfc' => true
2388 );
2389
2390 $schedules['everysixhours'] = array(
2391 'interval' => 60*60*6,
2392 'display' => __( 'Once Every 6 Hours' ),
2393 'wpfc' => true
2394 );
2395
2396 $schedules['everysevenhours'] = array(
2397 'interval' => 60*60*7,
2398 'display' => __( 'Once Every 7 Hours' ),
2399 'wpfc' => true
2400 );
2401
2402 $schedules['everyeighthours'] = array(
2403 'interval' => 60*60*8,
2404 'display' => __( 'Once Every 8 Hours' ),
2405 'wpfc' => true
2406 );
2407
2408 $schedules['everyninehours'] = array(
2409 'interval' => 60*60*9,
2410 'display' => __( 'Once Every 9 Hours' ),
2411 'wpfc' => true
2412 );
2413
2414 $schedules['everytenhours'] = array(
2415 'interval' => 60*60*10,
2416 'display' => __( 'Once Every 10 Hours' ),
2417 'wpfc' => true
2418 );
2419
2420 $schedules['onceaday'] = array(
2421 'interval' => 60*60*24,
2422 'display' => __( 'Once a Day' ),
2423 'wpfc' => true
2424 );
2425
2426 $schedules['everythreedays'] = array(
2427 'interval' => 60*60*24*3,
2428 'display' => __( 'Once Every 3 Days' ),
2429 'wpfc' => true
2430 );
2431
2432 $schedules['everysevendays'] = array(
2433 'interval' => 60*60*24*7,
2434 'display' => __( 'Once Every 7 Days' ),
2435 'wpfc' => true
2436 );
2437
2438 $schedules['everytendays'] = array(
2439 'interval' => 60*60*24*10,
2440 'display' => __( 'Once Every 10 Days' ),
2441 'wpfc' => true
2442 );
2443
2444 $schedules['everyfifteendays'] = array(
2445 'interval' => 60*60*24*15,
2446 'display' => __( 'Once Every 15 Days' ),
2447 'wpfc' => true
2448 );
2449
2450 $schedules['montly'] = array(
2451 'interval' => 60*60*24*30,
2452 'display' => __( 'Once a Month' ),
2453 'wpfc' => true
2454 );
2455
2456 $schedules['yearly'] = array(
2457 'interval' => 60*60*24*30*12,
2458 'display' => __( 'Once a Year' ),
2459 'wpfc' => true
2460 );
2461
2462 return $schedules;
2463 }
2464
2465 public function setCustomInterval(){
2466 add_filter( 'cron_schedules', array($this, 'cron_add_minute'));
2467 }
2468
2469 public function isPluginActive( $plugin ) {
2470 return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || $this->isPluginActiveForNetwork( $plugin );
2471 }
2472
2473 public function isPluginActiveForNetwork( $plugin ) {
2474 if ( !is_multisite() )
2475 return false;
2476
2477 $plugins = get_site_option( 'active_sitewide_plugins');
2478 if ( isset($plugins[$plugin]) )
2479 return true;
2480
2481 return false;
2482 }
2483
2484 public function current_url(){
2485 global $wp;
2486 $current_url = home_url($_SERVER['REQUEST_URI']);
2487
2488 return $current_url;
2489
2490
2491 // if(defined('WP_CLI')){
2492 // $_SERVER["SERVER_NAME"] = isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : "";
2493 // $_SERVER["SERVER_PORT"] = isset($_SERVER["SERVER_PORT"]) ? $_SERVER["SERVER_PORT"] : 80;
2494 // }
2495
2496 // $pageURL = 'http';
2497
2498 // if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){
2499 // $pageURL .= 's';
2500 // }
2501
2502 // $pageURL .= '://';
2503
2504 // if($_SERVER['SERVER_PORT'] != '80'){
2505 // $pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
2506 // }else{
2507 // $pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
2508 // }
2509
2510 // return $pageURL;
2511 }
2512
2513 public function wpfc_load_plugin_textdomain(){
2514 load_plugin_textdomain('wp-fastest-cache', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
2515 }
2516
2517 public function cdn_replace_urls($matches){
2518 if(count($this->cdn) > 0){
2519 foreach ($this->cdn as $key => $cdn) {
2520 if($cdn->id == "cloudflare"){
2521 continue;
2522 }
2523
2524 if(isset($cdn->status) && $cdn->status == "pause"){
2525 continue;
2526 }
2527
2528 if(preg_match("/manifest\.json\.php/i", $matches[0])){
2529 return $matches[0];
2530 }
2531
2532 //https://site.com?brizy_media=AttachmentName.jpg&brizy_crop=CropSizes&brizy_post=TheCurrentPost
2533 if(preg_match("/brizy_media\=/i", $matches[0])){
2534 return $matches[0];
2535 }
2536
2537 //https://cdn.shortpixel.ai/client/q_glossy,ret_img,w_736/http://wpfc.com/stories.png
2538 if(preg_match("/cdn\.shortpixel\.ai\/client/i", $matches[0])){
2539 return $matches[0];
2540 }
2541
2542
2543 if(preg_match("/^\/\/random/", $cdn->cdnurl) || preg_match("/\/\/i\d\.wp\.com/", $cdn->cdnurl)){
2544 // Photon will no longer be supported
2545 continue;
2546 }
2547
2548 $cdnurl = $cdn->cdnurl;
2549
2550 $cdn->file_types = str_replace(",", "|", $cdn->file_types);
2551
2552 if(preg_match("/\.(".$cdn->file_types.")(\"|\'|\?|\)|\s|\&quot\;)/i", $matches[0])){
2553 //nothing
2554 }else{
2555 if(preg_match("/js/", $cdn->file_types)){
2556 if(!preg_match("/\/revslider\/public\/assets\/js/", $matches[0])){
2557 continue;
2558 }
2559 }else{
2560 continue;
2561 }
2562 }
2563
2564 if($cdn->keywords){
2565 $cdn->keywords = str_replace(",", "|", $cdn->keywords);
2566
2567 if(!preg_match("/".preg_quote($cdn->keywords, "/")."/i", $matches[0])){
2568 continue;
2569 }
2570 }
2571
2572 if(isset($cdn->excludekeywords) && $cdn->excludekeywords){
2573 $cdn->excludekeywords = str_replace(",", "|", $cdn->excludekeywords);
2574
2575 if(preg_match("/".preg_quote($cdn->excludekeywords, "/")."/i", $matches[0])){
2576 continue;
2577 }
2578 }
2579
2580 if(preg_match("/(data-product_variations|data-siteorigin-parallax)\=[\"\'][^\"\']+[\"\']/i", $matches[0])){
2581 $cdnurl = preg_replace("/(https?\:)?(\/\/)(www\.)?/", "", $cdnurl);
2582
2583 // if(preg_match("/i\d\.wp\.com/i", $cdnurl)){
2584 // $matches[0] = preg_replace("/(quot\;|\s)(https?\:)?(\\\\\/\\\\\/|\/\/)(www\.)?".$cdn->originurl."/i", "$1$2$3".$cdnurl, $matches[0]);
2585 // }else{
2586 // $matches[0] = preg_replace("/(quot\;|\s)(https?\:)?(\\\\\/\\\\\/|\/\/)(www\.)?".$cdn->originurl."/i", "$1$2$3$4".$cdnurl, $matches[0]);
2587 // }
2588
2589 $matches[0] = preg_replace("/(quot\;|\s)(https?\:)?(\\\\\/\\\\\/|\/\/)(www\.)?".$cdn->originurl."/i", '${1}${2}${3}'.$cdnurl, $matches[0]);
2590
2591
2592 }else if(preg_match("/\{\"concatemoji\"\:\"[^\"]+\"\}/i", $matches[0])){
2593 $matches[0] = preg_replace("/(http(s?)\:)?".preg_quote("\/\/", "/")."(www\.)?/i", "", $matches[0]);
2594 $matches[0] = preg_replace("/".preg_quote($cdn->originurl, "/")."/i", $cdnurl, $matches[0]);
2595 }else if(isset($matches[2]) && preg_match("/".preg_quote($cdn->originurl, "/")."/", $matches[2])){
2596 $matches[0] = preg_replace("/(http(s?)\:)?\/\/(www\.)?".preg_quote($cdn->originurl, "/")."/i", $cdnurl, $matches[0]);
2597 }else if(isset($matches[2]) && preg_match("/^(\/?)(wp-includes|wp-content)/", $matches[2])){
2598 $matches[0] = preg_replace("/(\/?)(wp-includes|wp-content)/i", $cdnurl."/"."$2", $matches[0]);
2599 }else if(preg_match("/[\"\']https?\:\\\\\/\\\\\/[^\"\']+[\"\']/i", $matches[0])){
2600 if(preg_match("/^(logo|url|image)$/i", $matches[1])){
2601 //If the url is called with "//", it causes an error on https://search.google.com/structured-data/testing-tool/u/0/
2602 //<script type="application/ld+json">"logo":{"@type":"ImageObject","url":"\/\/cdn.site.com\/image.png"}</script>
2603 //<script type="application/ld+json">{"logo":"\/\/cdn.site.com\/image.png"}</script>
2604 //<script type="application/ld+json">{"image":"\/\/cdn.site.com\/image.jpg"}</script>
2605 }else{
2606 //<script>var loaderRandomImages=["https:\/\/www.site.com\/wp-content\/uploads\/2016\/12\/image.jpg"];</script>
2607 $matches[0] = preg_replace("/\\\\\//", "/", $matches[0]);
2608
2609 if(preg_match("/".preg_quote($cdn->originurl, "/")."/", $matches[0])){
2610 $matches[0] = preg_replace("/(http(s?)\:)?\/\/(www\.)?".preg_quote($cdn->originurl, "/")."/i", $cdnurl, $matches[0]);
2611 $matches[0] = preg_replace("/\//", "\/", $matches[0]);
2612 }
2613 }
2614 }
2615 }
2616 }
2617
2618 return $matches[0];
2619 }
2620
2621 public function read_file($url){
2622 if(!preg_match("/\.php/", $url)){
2623 $url = preg_replace("/\?.*/", "", $url);
2624
2625 if(preg_match("/wp-content/", $url)){
2626 $path = preg_replace("/.+\/wp-content\/(.+)/", WPFC_WP_CONTENT_DIR."/"."$1", $url);
2627 }else if(preg_match("/wp-includes/", $url)){
2628 $path = preg_replace("/.+\/wp-includes\/(.+)/", ABSPATH."wp-includes/"."$1", $url);
2629 }
2630
2631 if(isset($path)){
2632 if(@file_exists($path)){
2633 $filesize = filesize($path);
2634
2635 if($filesize > 0){
2636 $myfile = fopen($path, "r") or die("Unable to open file!");
2637 $data = fread($myfile, $filesize);
2638 fclose($myfile);
2639
2640 return $data;
2641 }else{
2642 return false;
2643 }
2644 }
2645 }
2646
2647
2648 }
2649
2650 return false;
2651 }
2652
2653 public function get_operating_systems(){
2654 $operating_systems = array(
2655 'Android',
2656 'blackberry|\bBB10\b|rim\stablet\sos',
2657 'PalmOS|avantgo|blazer|elaine|hiptop|palm|plucker|xiino',
2658 'Symbian|SymbOS|Series60|Series40|SYB-[0-9]+|\bS60\b',
2659 'Windows\sCE.*(PPC|Smartphone|Mobile|[0-9]{3}x[0-9]{3})|Window\sMobile|Windows\sPhone\s[0-9.]+|WCE;',
2660 'Windows\sPhone\s10.0|Windows\sPhone\s8.1|Windows\sPhone\s8.0|Windows\sPhone\sOS|XBLWP7|ZuneWP7|Windows\sNT\s6\.[23]\;\sARM\;',
2661 '\biPhone.*Mobile|\biPod|\biPad',
2662 'Apple-iPhone7C2',
2663 'MeeGo',
2664 'Maemo',
2665 'J2ME\/|\bMIDP\b|\bCLDC\b', // '|Java/' produces bug #135
2666 'webOS|hpwOS',
2667 '\bBada\b',
2668 'BREW'
2669 );
2670 return $operating_systems;
2671 }
2672
2673 public function get_mobile_browsers(){
2674 $mobile_browsers = array(
2675 '\bCrMo\b|CriOS|Android.*Chrome\/[.0-9]*\s(Mobile)?',
2676 '\bDolfin\b',
2677 'Opera.*Mini|Opera.*Mobi|Android.*Opera|Mobile.*OPR\/[0-9.]+|Coast\/[0-9.]+',
2678 'Skyfire',
2679 'Mobile\sSafari\/[.0-9]*\sEdge',
2680 'IEMobile|MSIEMobile', // |Trident/[.0-9]+
2681 'fennec|firefox.*maemo|(Mobile|Tablet).*Firefox|Firefox.*Mobile|FxiOS',
2682 'bolt',
2683 'teashark',
2684 'Blazer',
2685 'Version.*Mobile.*Safari|Safari.*Mobile|MobileSafari',
2686 'Tizen',
2687 'UC.*Browser|UCWEB',
2688 'baiduboxapp',
2689 'baidubrowser',
2690 'DiigoBrowser',
2691 'Puffin',
2692 '\bMercury\b',
2693 'Obigo',
2694 'NF-Browser',
2695 'NokiaBrowser|OviBrowser|OneBrowser|TwonkyBeamBrowser|SEMC.*Browser|FlyFlow|Minimo|NetFront|Novarra-Vision|MQQBrowser|MicroMessenger',
2696 'Android.*PaleMoon|Mobile.*PaleMoon'
2697 );
2698 return $mobile_browsers;
2699 }
2700
2701
2702 }
2703
2704 // Load WP CLI command(s) on demand.
2705 if ( defined( 'WP_CLI' ) && WP_CLI ) {
2706 require_once "inc/cli.php";
2707 }
2708
2709 function wpfc_clear_all_site_cache(){
2710 if(defined('WPFC_DISABLE_HOOK_CLEAR_ALL_CACHE') && WPFC_DISABLE_HOOK_CLEAR_ALL_CACHE){
2711 return array("success" => false, "message" => "Clearing Cache Hook system has been disabled");
2712 }
2713
2714 do_action("wpfc_clear_all_cache");
2715 }
2716
2717 function wpfc_clear_all_cache($minified = false){
2718 if(defined('WPFC_DISABLE_HOOK_CLEAR_ALL_CACHE') && WPFC_DISABLE_HOOK_CLEAR_ALL_CACHE){
2719 return array("success" => false, "message" => "Clearing Cache Hook system has been disabled");
2720 }
2721
2722 do_action("wpfc_clear_all_cache", $minified);
2723 }
2724
2725 function wpfc_exclude_current_page(){
2726 do_action("wpfc_exclude_current_page");
2727 }
2728
2729 function wpfc_clear_post_cache_by_id($post_id = false){
2730 if($post_id){
2731 do_action("wpfc_clear_post_cache_by_id", false, $post_id);
2732 }
2733 }
2734
2735 function wpfc_create_post_cache_by_id($post_id = false){
2736 if($post_id){
2737 do_action("wpfc_create_post_cache_by_id", $post_id);
2738 }
2739 }
2740
2741
2742
2743 $GLOBALS["wp_fastest_cache"] = new WpFastestCache();
2744
2745
2746
2747 ?>