Changeset 2380205
- Timestamp:
- 09/13/2020 01:04:19 AM (6 years ago)
- File:
-
- 1 edited
-
cynderhost/trunk/cynderhost.php (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cynderhost/trunk/cynderhost.php
r2380156 r2380205 10 10 * Plugin URI: https://cynderhost.com 11 11 * Description: Provides an easy interface to clear the CynderHost CDN cache, both automatically and programmatically. 12 * Version: 1.2. 212 * Version: 1.2.4 13 13 * Author: CynderHost 14 14 * Author URI: https://profiles.wordpress.org/cynderhost/ … … 24 24 25 25 26 27 28 define( 'CYNDERHOST_VERSION', '1.2.2' ); 29 30 31 26 //Define version 27 define( 'CYNDERHOST_VERSION', '1.2.4' ); 28 29 30 /** 31 * Purge CynderHost CDN + Server Cache 32 * @params (bool) resp - whether to wait for a response message or not 33 * @return string - returns success or failure message 34 */ 32 35 if ( ! function_exists( 'purge_cynderhost' ) ) { 33 /** 34 * Purge CynderHost CDN Cache 35 * 36 * @return string -- returns success or failure message 37 */ 38 function purge_cynderhost($resp = false) { 36 function purge_cynderhost($resp = false) { 39 37 $cynderhost_cdn_settings_options = get_option( 'cynderhost_cdn_settings_option_name' ); // Array of All Options 40 $api_key = $cynderhost_cdn_settings_options['api_key_0']; 41 $hostname = $cynderhost_cdn_settings_options['hostname_1']; 42 // cache purgeendpoint38 $api_key = $cynderhost_cdn_settings_options['api_key_0']; //API key from settings page 39 $hostname = $cynderhost_cdn_settings_options['hostname_1']; //Hostname from settings page 40 //CDN cache purge everything endpoint 43 41 $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/cdn/purge/", array( 44 42 'method' => 'POST', 45 43 'timeout' => 10, 46 44 'redirection' => 5, 47 'blocking' => $resp, 45 'blocking' => $resp, //true or false - whether to wait for response 48 46 'body' => array( 49 47 'API' => "$api_key" … … 51 49 ) 52 50 ); 53 //Non-standard Error, 5XX, 4XX 51 //Non-standard Error, 5XX, 4XX (internal server error) 54 52 if ( is_wp_error( $response ) ) { 55 $error_message = $response->get_error_message();56 $result = "Something went wrong: $error_message";53 $error_message = $response->get_error_message(); 54 $result = "Something went wrong: $error_message"; 57 55 } else { 56 //get purge status is response is return 58 57 $result = json_decode($response['body'], true)['status']; 59 58 } 59 //purge all local server cache 60 60 $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/local/purge/", array( 61 61 'method' => 'POST', … … 65 65 'body' => array( 66 66 'API' => "$api_key", 67 'HOST' => "$hostname"67 'HOST' => "$hostname" 68 68 ) 69 69 ) 70 70 ); 71 //Non-standard Error, 5XX, 4XX 71 //Non-standard Error, 5XX, 4XX again 72 72 if ( is_wp_error( $response ) ) { 73 $error_message = $response->get_error_message(); 74 $result = "Something went wrong: $error_message"; 73 //gets error message if it fails 74 $error_message = $response->get_error_message(); 75 $result = "Something went wrong: $error_message"; 75 76 } else { 77 //gets result of second api call 76 78 $result2 = json_decode($response['body'], true)['status']; 77 $result = ($result2 == "Success. Local server cache has been successfully purged.") ? $result : $result2; 78 } 79 //replace success message with error message if failed, otherwise do nothing 80 $result = ($result2 == "Success. Local server cache has been successfully purged.") ? $result : $result2; 81 } 82 //no response needed, return generic message 79 83 $result = $resp ? $result : "Cache purge request sent."; 80 84 return $result; 81 85 } 82 86 } 83 /** 84 * Purges a single url and refetches it (CDN + Local) 85 */ 86 function purge_cynderhost_single( $url) { 87 88 89 /** 90 * Purge CynderHost Server + CDN Cache (Multiple URL) 91 * @params (array) urls - array of urls to purge 92 * @return bool - returns true 93 */ 94 function purge_cynderhost_multiple($urls) { 95 //strip http added automatically 96 $stripped_urls = array(); 97 foreach($urls as $url){ 98 array_push($stripped_urls, preg_replace('#^https?://#', '', $url)); 99 } 100 $cynderhost_cdn_settings_options = get_option( 'cynderhost_cdn_settings_option_name' ); // Array of All Options 101 $api_key = $cynderhost_cdn_settings_options['api_key_0']; 102 $hostname = $cynderhost_cdn_settings_options['hostname_1']; 103 //cdn cache multiple urls 104 $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/cdn/purge-multiple/", array( 105 'method' => 'POST', 106 'timeout' => 10, 107 'redirection' => 5, 108 'blocking' => false, 109 'body' => array( 110 'API' => "$api_key", 111 'URLS' => json_encode($stripped_urls) 112 ) 113 ) 114 ); 115 //server cache single url 116 $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/local/purge-multiple/", array( 117 'method' => 'POST', 118 'timeout' => 10, 119 'redirection' => 5, 120 'blocking' => false, 121 'body' => array( 122 'API' => "$api_key", 123 "HOST" => $hostname, 124 'URLS' => json_encode($stripped_urls) 125 ) 126 ) 127 ); 128 //we don't know the response anyways 129 return true; 130 } 131 132 133 /** 134 * Purge CynderHost Server + CDN Cache (Single URL) 135 * @params (string) url - url of page to purge 136 * @return bool - returns true 137 */ 138 function purge_cynderhost_single($url) { 87 139 //strip http added automatically 88 140 $url = preg_replace('#^https?://#', '', $url); 89 141 $cynderhost_cdn_settings_options = get_option( 'cynderhost_cdn_settings_option_name' ); // Array of All Options 90 $api_key = $cynderhost_cdn_settings_options['api_key_0'];142 $api_key = $cynderhost_cdn_settings_options['api_key_0']; 91 143 $hostname = $cynderhost_cdn_settings_options['hostname_1']; 92 //c ache purge endpoint144 //cdn cache single url 93 145 $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/cdn/purge-some/", array( 94 'method' => 'POST',95 'timeout' => 10,96 'redirection' => 5,97 'blocking' => false,98 'body' => array(99 'API' => "$api_key",100 'URL' => "$url"101 )146 'method' => 'POST', 147 'timeout' => 10, 148 'redirection' => 5, 149 'blocking' => false, 150 'body' => array( 151 'API' => "$api_key", 152 'URL' => "$url" 153 ) 102 154 ) 103 155 ); 104 // Non-standard Error, 5XX, 4XX156 //server cache single url 105 157 $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/local/purge-some/", array( 106 158 'method' => 'POST', … … 108 160 'redirection' => 5, 109 161 'blocking' => false, 162 //WordPress's native encoding doesn't work, apparently 110 163 'body' => "API=".urlencode($api_key)."&HOST=$hostname&URL=" .urlencode($url) 111 164 ) 112 165 ); 166 //we don't know the response anyways 113 167 return true; 114 168 } 115 169 /** 116 * Hooks for Selective Purging 117 */ 170 * Purge CynderHost Server + CDN Cache Selective on Comment Publish or Deletion 171 * @params (null) _ - throwaway, not needed 172 * @params (null) _1 - '' 173 * @params (object) comment - WP Comment object 174 * @return null - nothing 175 */ 176 function cynderhost_comment_function($_, $_1, $comment) { 177 if ($comment->comment_parent > 0) { 178 //get parent comment 179 $comment_parent = get_comment( $comment->comment_parent ); 180 $comment_post_id = $comment_parent->comment_post_ID ; 181 } else { 182 $comment_post_id = $comment->comment_post_ID; 183 } 184 //purges a single url 185 purge_cynderhost_single( get_the_permalink($comment_post_id)); 186 } 187 //hook to comment status 118 188 add_action('transition_comment_status', 'cynderhost_comment_function', 10, 3); 119 function cynderhost_comment_function($new_status, $old_status, $comment) { 120 if ($comment->comment_parent > 0) { 121 $comment_parent = get_comment( $comment->comment_parent ); 122 $comment_post_id = $comment_parent->comment_post_ID ; 123 } else { 124 $comment_post_id = $comment->comment_post_ID; 125 } 126 purge_cynderhost_single( get_the_permalink($comment_post_id)); 127 } 128 /** 129 * Called to purge cache on updates and display status 189 190 /** 191 * Purges CynderHost Server + CDN Cache and sets an admin notice about message 192 * @params (bool) p - whether to get detailed response message 193 * @returns null 130 194 */ 131 195 function do_cache_cynderhost_purge($p = false){ … … 133 197 } 134 198 135 /* 136 * Sets the local cache status via api 199 200 /** 201 * Purges CynderHost Server + CDN Cache and sets an admin notice about message 202 * @params (bool) p - whether to get detailed response message 203 * @returns null 204 */ 205 function do_cache_cynderhost_selective_purge($postid, $post = ""){ 206 if ($post == ""){ 207 $post = get_post($postid); 208 } 209 //get the author url 210 $author_url = get_author_posts_url( $post->post_author ); 211 //get the categories and tags 212 $categories = get_the_category($postid); 213 $tags = get_the_tags($postid); 214 $category = array(); 215 $tag = array(); 216 //loop through and get each category and tag url 217 foreach ($categories as $c){ 218 array_push($category,get_category_link( $c )); 219 } 220 foreach ($tags as $t){ 221 array_push($tag,get_category_link( $t )); 222 } 223 //get archive link for the post type 224 $posts_archive = get_post_type_archive_link( $post->post_type ); 225 purge_cynderhost_multiple(array_merge($tag, $category, array(get_permalink($postid), $author_url, $posts_archive))); 226 } 227 228 /** 229 * Sets local cache status 230 * @params (string) boole - new status to set to 231 * @params (string) hostname - hostname of site server 232 * @params (string) api_key - api key of site 233 * @returns true 137 234 */ 138 235 function cynderhost_set_stat($boole, $hostname, $api_key){ … … 151 248 return true; 152 249 } 153 /** 154 * Displays a notice with cache purge status 250 251 /** 252 * Fires of admin pageview and displays notice, if exists 253 * @returns null 155 254 */ 156 255 function cynderhost_author_admin_notice(){ … … 164 263 165 264 /** 166 * Sets admin notice trasients 265 * Creates an admin notice as transint that loads on next pageview 266 * @returns null 167 267 */ 168 268 function cynderhost_set_admin_notice($message) { … … 171 271 ], 30); 172 272 } 273 173 274 /** 174 275 * Gets and removes admin notice trasients 276 * @returns (string) transient - message to display 175 277 */ 176 278 function cynderhost_get_admin_notice() { … … 184 286 /** 185 287 * Check if cache should be purged 288 * Fires on each pageview 289 * * probably should be optimized more 186 290 */ 187 291 function cynderhost_check_cache_purge(){ … … 192 296 } 193 297 /** 194 * Add cache purge action hooks: 195 * Post publish, update, or delete, Theme switch, Plugin activate or deactivate 196 */ 197 add_action('publish_post', 'do_cache_cynderhost_purge', 10, 0); 198 add_action('save_post', 'do_cache_cynderhost_purge', 10, 0); 199 add_action('wp_trash_post', 'do_cache_cynderhost_purge', 10, 0); 298 * Add full cache purge action hooks: 299 * Post publish, update, or delete, Theme switch, Plugin activate or deactivate, or theme customize 300 */ 200 301 add_action('switch_theme', 'do_cache_cynderhost_purge', 10, 0); 201 302 add_action('activated_plugin', 'do_cache_cynderhost_purge', 10, 0); 202 303 add_action('deactivated_plugin', 'do_cache_cynderhost_purge', 10, 0); 203 add_action('deactivated_plugin', 'do_cache_cynderhost_purge', 10, 0); 304 add_action('customize_save_after', 'do_cache_cynderhost_purge', 10, 0); 305 306 //hooks on each page to see if cache should be purged 307 add_action('wp_loaded', 'cynderhost_check_cache_purge'); 308 309 //add notice if applicable 204 310 add_action('admin_notices', 'cynderhost_author_admin_notice', 10, 0); 205 add_action('wp_loaded', 'cynderhost_check_cache_purge'); 206 311 312 //add selective purging if smart purge is enabled, else purge everything on some events 313 $cynderhost_cdn_settings_options = get_option( 'cynderhost_cdn_settings_option_name' ); 314 if ( $cynderhost_cdn_settings_options['smart_purge_4'] !== 'smart_purge_4' ){ 315 add_action('save_post', 'do_cache_cynderhost_purge', 10, 0); 316 add_action('wp_trash_post', 'do_cache_cynderhost_purge', 10, 0); 317 }else{ 318 add_action('save_post', 'do_cache_cynderhost_selective_purge', 10, 2); 319 add_action('wp_trash_post', 'do_cache_cynderhost_selective_purge', 10, 1); 320 } 207 321 208 322 /** … … 287 401 'cynderhost_cdn_settings_setting_section' // section 288 402 ); 403 add_settings_field( 404 'smart_purge_4', // id 405 'CynderHost Smart Cache', // title 406 array( $this, 'smart_purge_4_callback' ), // callback 407 'cynderhost-cdn-settings-admin', // page 408 'cynderhost_cdn_settings_setting_section' // section 409 ); 289 410 } 290 411 … … 304 425 $sanitary_values['server_page_cache_3'] = $input['server_page_cache_3']; 305 426 } 427 if ( isset( $input['smart_purge_4'] ) ) { 428 $sanitary_values['smart_purge_4'] = $input['smart_purge_4']; 429 } 306 430 do_cache_cynderhost_purge(true); 307 431 return $sanitary_values; … … 327 451 '<input type="checkbox" name="cynderhost_cdn_settings_option_name[server_page_cache_3]" id="server_page_cache_3" value="server_page_cache_3" %s> <label for="server_page_cache_3">Whether or not to enable the local, serverside page caching. </label>', 328 452 ( isset( $this->cynderhost_cdn_settings_options['server_page_cache_3'] ) && $this->cynderhost_cdn_settings_options['server_page_cache_3'] === 'server_page_cache_3' ) ? 'checked' : '' 453 ); 454 } 455 public function smart_purge_4_callback() { 456 printf( 457 '<input type="checkbox" name="cynderhost_cdn_settings_option_name[smart_purge_4]" id="smart_purge_4" value="smart_purge_4" %s> <label for="smart_purge_4">Smart cache sets a extremely long TTL on the server and edge caches, and selectively purges the archives, homepage, and author pages on various evsnts to increase the cache hit ratio. <br><br>This is highly recommended if you have a blog or business site.</label>', 458 ( isset( $this->cynderhost_cdn_settings_options['smart_purge_4'] ) && $this->cynderhost_cdn_settings_options['smart_purge_4'] === 'smart_purge_4' ) ? 'checked' : '' 329 459 ); 330 460 }
Note: See TracChangeset
for help on using the changeset viewer.