Plugin Directory

Changeset 2380205


Ignore:
Timestamp:
09/13/2020 01:04:19 AM (6 years ago)
Author:
cynderhost
Message:

Added SmartPurge V1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cynderhost/trunk/cynderhost.php

    r2380156 r2380205  
    1010 * Plugin URI:        https://cynderhost.com
    1111 * Description:       Provides an easy interface to clear the CynderHost CDN cache, both automatically and programmatically.
    12  * Version:           1.2.2
     12 * Version:           1.2.4
    1313 * Author:            CynderHost
    1414 * Author URI:        https://profiles.wordpress.org/cynderhost/
     
    2424
    2525
    26 
    27 
    28 define( 'CYNDERHOST_VERSION', '1.2.2' );
    29 
    30 
    31 
     26//Define version
     27define( '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 */
    3235if ( ! 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) {
    3937    $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 purge endpoint
     38  $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
    4341    $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/cdn/purge/", array(
    4442        'method'      => 'POST',
    4543        'timeout'     => 10,
    4644        'redirection' => 5,
    47         'blocking'    => $resp,
     45        'blocking'    => $resp, //true or false - whether to wait for response
    4846        'body'        => array(
    4947            'API' => "$api_key"
     
    5149        )
    5250    );
    53     //Non-standard Error, 5XX, 4XX
     51    //Non-standard Error, 5XX, 4XX (internal server error)
    5452    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";
    5755    } else {
     56        //get purge status is response is return
    5857        $result = json_decode($response['body'], true)['status'];
    5958    }
     59    //purge all local server cache
    6060    $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/local/purge/", array(
    6161        'method'      => 'POST',
     
    6565        'body'        => array(
    6666            'API' => "$api_key",
    67                     'HOST' => "$hostname"
     67            'HOST' => "$hostname"
    6868         )
    6969        )
    7070    );
    71     //Non-standard Error, 5XX, 4XX
     71    //Non-standard Error, 5XX, 4XX again
    7272    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";
    7576    } else {
     77        //gets result of second api call
    7678        $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
    7983    $result = $resp ? $result : "Cache purge request sent.";
    8084    return $result;
    8185  }
    8286}
    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 */
     94function 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 */
     138function purge_cynderhost_single($url) {
    87139    //strip http added automatically
    88140    $url = preg_replace('#^https?://#', '', $url);
    89141    $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'];
    91143    $hostname = $cynderhost_cdn_settings_options['hostname_1'];
    92     //cache purge endpoint
     144    //cdn cache single url
    93145    $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            )
    102154        )
    103155    );
    104     //Non-standard Error, 5XX, 4XX
     156    //server cache single url
    105157    $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/local/purge-some/", array(
    106158        'method'      => 'POST',
     
    108160        'redirection' => 5,
    109161        'blocking'    => false,
     162        //WordPress's native encoding doesn't work, apparently
    110163        'body' => "API=".urlencode($api_key)."&HOST=$hostname&URL=" .urlencode($url)
    111164       )
    112165    );
     166    //we don't know the response anyways
    113167    return true;
    114168 }
    115169/**
    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 */
     176function 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
    118188add_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
    130194 */
    131195function do_cache_cynderhost_purge($p = false){
     
    133197}
    134198
    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 */
     205function 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
    137234 */
    138235function cynderhost_set_stat($boole, $hostname, $api_key){
     
    151248    return true;
    152249}
    153 /**
    154  * Displays a notice with cache purge status
     250
     251/**
     252 * Fires of admin pageview and displays notice, if exists
     253 * @returns null
    155254 */
    156255function cynderhost_author_admin_notice(){
     
    164263
    165264/**
    166  * Sets admin notice trasients
     265 * Creates an admin notice as transint that loads on next pageview
     266 * @returns null
    167267 */
    168268function cynderhost_set_admin_notice($message) {
     
    171271    ], 30);
    172272}
     273
    173274/**
    174275 * Gets and removes admin notice trasients
     276 * @returns (string) transient - message to display
    175277 */
    176278function cynderhost_get_admin_notice() {
     
    184286/**
    185287 * Check if cache should be purged
     288 * Fires on each pageview
     289 *  * probably should be optimized more
    186290 */
    187291function cynderhost_check_cache_purge(){
     
    192296}
    193297/**
    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 */
    200301add_action('switch_theme', 'do_cache_cynderhost_purge', 10, 0);
    201302add_action('activated_plugin', 'do_cache_cynderhost_purge', 10, 0);
    202303add_action('deactivated_plugin', 'do_cache_cynderhost_purge', 10, 0);
    203 add_action('deactivated_plugin', 'do_cache_cynderhost_purge', 10, 0);
     304add_action('customize_save_after', 'do_cache_cynderhost_purge', 10, 0);
     305
     306//hooks on each page to see if cache should be purged
     307add_action('wp_loaded', 'cynderhost_check_cache_purge');
     308
     309//add notice if applicable
    204310add_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' );
     314if ( $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}
    207321
    208322/**
     
    287401            'cynderhost_cdn_settings_setting_section' // section
    288402        );
     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        );
    289410    }
    290411
     
    304425            $sanitary_values['server_page_cache_3'] = $input['server_page_cache_3'];
    305426        }
     427        if ( isset( $input['smart_purge_4'] ) ) {
     428            $sanitary_values['smart_purge_4'] = $input['smart_purge_4'];
     429        }
    306430        do_cache_cynderhost_purge(true);
    307431        return $sanitary_values;
     
    327451            '<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>',
    328452            ( 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' : ''
    329459        );
    330460    }
Note: See TracChangeset for help on using the changeset viewer.