admin-toolbar.php
10 months ago
admin.php
7 months ago
cache.php
1 year ago
cdn.php
1 year ago
clearing-specific-pages.php
2 years ago
cli.php
4 years ago
column.php
6 years ago
css-utilities.php
2 years ago
index.html
11 years ago
js-utilities.php
1 year ago
preload.php
1 year ago
single-preload.php
2 years ago
varnish.php
2 years ago
wp-polls.php
9 years ago
varnish.php
178 lines
| 1 | <?php |
| 2 | class VarnishWPFC{ |
| 3 | |
| 4 | public static function purge_cache($data = false) { |
| 5 | if(isset($GLOBALS["wpfc_varnish_purge_cache_executed"])){ |
| 6 | return; |
| 7 | }else{ |
| 8 | $GLOBALS["wpfc_varnish_purge_cache_executed"] = true; |
| 9 | } |
| 10 | |
| 11 | |
| 12 | |
| 13 | if(gettype($data) == "array"){ |
| 14 | // Clearing page cache action sends the data as Array |
| 15 | if(isset($data["status"]) && $data["status"] == "pause"){ |
| 16 | return array("success" => true); |
| 17 | } |
| 18 | |
| 19 | $server = $data["server"]; |
| 20 | }else{ |
| 21 | // Ajax request and save() function send the data as string |
| 22 | $server = $data; |
| 23 | } |
| 24 | |
| 25 | $home = get_option('home'); |
| 26 | $host = preg_replace("/(https?\:\/\/)(.+)/", "$2", $home); |
| 27 | $host = preg_replace("/\/.*/", "", $host); |
| 28 | |
| 29 | $schema = preg_replace("/(https?\:\/\/).+/", "$1", $home); |
| 30 | $schema = strtolower($schema); |
| 31 | |
| 32 | $ssl_verification = $schema == 'https://' ? true : false; |
| 33 | |
| 34 | $request_url = $schema.$server."/.*"; |
| 35 | |
| 36 | $request_args = array( |
| 37 | 'method' => "PURGE", |
| 38 | 'headers' => array( |
| 39 | 'Host' => $host, |
| 40 | 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', |
| 41 | ), |
| 42 | 'sslverify' => $ssl_verification, |
| 43 | ); |
| 44 | |
| 45 | |
| 46 | $response = wp_remote_request($request_url, $request_args ); |
| 47 | |
| 48 | |
| 49 | if(is_wp_error( $response ) || $response['response']['code'] != '200'){ |
| 50 | |
| 51 | if($schema === 'https://'){ |
| 52 | $request_url = str_replace("https://", "http://", $request_url); |
| 53 | }else{ |
| 54 | $request_url = str_replace("http://", "https://", $request_url); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | $response = wp_remote_request($request_url, $request_args ); |
| 59 | |
| 60 | |
| 61 | if(is_wp_error( $response ) || $response['response']['code'] != '200'){ |
| 62 | |
| 63 | if($response->get_error_messages()){ |
| 64 | return array("success" => "", "message" => $response->get_error_message()); |
| 65 | |
| 66 | } |
| 67 | |
| 68 | if($response['response']['code'] == "501"){ |
| 69 | $text = '"Purge" method is not allowed'; |
| 70 | |
| 71 | if(isset($response['headers']['allow'])){ |
| 72 | $text = $text.". The allowed methods are ".$response['headers']['allow']; |
| 73 | $text = $text.". Please contact your hosting provider"; |
| 74 | } |
| 75 | |
| 76 | return array("success" => "", "message" => $text); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return array("success" => true); |
| 84 | } |
| 85 | |
| 86 | public static function save(){ |
| 87 | if(!wp_verify_nonce($_POST["security"], 'wpfc-varnish-ajax-nonce')){ |
| 88 | die( 'Security check' ); |
| 89 | } |
| 90 | |
| 91 | $_POST["server"] = sanitize_text_field($_POST["server"]); |
| 92 | |
| 93 | $purce_res = self::purge_cache($_POST["server"]); |
| 94 | |
| 95 | if(!$purce_res["success"]){ |
| 96 | wp_send_json($purce_res); |
| 97 | } |
| 98 | |
| 99 | $datas = get_option("WpFastestCacheVarnish"); |
| 100 | |
| 101 | if(!is_array($datas)){ |
| 102 | $datas = array(); |
| 103 | $datas["server"] = $_POST["server"]; |
| 104 | |
| 105 | add_option("WpFastestCacheVarnish", $datas, 1, "yes"); |
| 106 | }else{ |
| 107 | $datas["server"] = $_POST["server"]; |
| 108 | |
| 109 | update_option("WpFastestCacheVarnish", $datas, 1, "yes"); |
| 110 | } |
| 111 | |
| 112 | wp_send_json_success(); |
| 113 | |
| 114 | |
| 115 | } |
| 116 | |
| 117 | public static function status(){ |
| 118 | $datas = get_option("WpFastestCacheVarnish"); |
| 119 | |
| 120 | if(is_array($datas)){ |
| 121 | if(isset($datas["status"]) && $datas["status"] == "pause"){ |
| 122 | echo "isConnected pause"; |
| 123 | }else{ |
| 124 | echo "isConnected"; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public static function start(){ |
| 130 | if(!wp_verify_nonce($_POST["security"], 'wpfc-varnish-ajax-nonce')){ |
| 131 | die( 'Security check' ); |
| 132 | } |
| 133 | |
| 134 | $datas = get_option("WpFastestCacheVarnish"); |
| 135 | |
| 136 | if(is_array($datas)){ |
| 137 | unset($datas["status"]); |
| 138 | |
| 139 | $purce_res = self::purge_cache($datas); |
| 140 | |
| 141 | if(!$purce_res["success"]){ |
| 142 | wp_send_json($purce_res); |
| 143 | } |
| 144 | |
| 145 | update_option("WpFastestCacheVarnish", $datas, 1, "yes"); |
| 146 | } |
| 147 | |
| 148 | wp_send_json_success(); |
| 149 | } |
| 150 | |
| 151 | public static function pause(){ |
| 152 | if(!wp_verify_nonce($_POST["security"], 'wpfc-varnish-ajax-nonce')){ |
| 153 | die( 'Security check' ); |
| 154 | } |
| 155 | |
| 156 | $datas = get_option("WpFastestCacheVarnish"); |
| 157 | |
| 158 | if(is_array($datas)){ |
| 159 | $datas["status"] = "pause"; |
| 160 | update_option("WpFastestCacheVarnish", $datas, 1, "yes"); |
| 161 | } |
| 162 | |
| 163 | wp_send_json_success(); |
| 164 | } |
| 165 | |
| 166 | public static function remove(){ |
| 167 | if(!wp_verify_nonce($_POST["security"], 'wpfc-varnish-ajax-nonce')){ |
| 168 | die( 'Security check' ); |
| 169 | } |
| 170 | |
| 171 | delete_option("WpFastestCacheVarnish"); |
| 172 | |
| 173 | wp_send_json_success(); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | ?> |