Plugin Directory

Changeset 2373503


Ignore:
Timestamp:
09/02/2020 03:07:31 AM (6 years ago)
Author:
cynderhost
Message:

v1.1, added local cache api

Location:
cynderhost/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cynderhost/trunk/README.txt

    r2353317 r2373503  
    33Donate link: https://cynderhost.com
    44Tags: cache, performance, cynderhost
    5 Requires at least: 3.0.1
    6 Tested up to: 5.4.2
    7 Stable tag: 5.4.2
     5Requires at least: 4.0.1
     6Tested up to: 5.5.0
     7Stable tag: 5.5.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    1010
    11 Provides an easy interface to clear the CynderHost CDN cache automatically and manually.
     11Provides an easy interface to clear the CynderHost CDN & local cache automatically and manually.
    1212
    1313== Description ==
     
    1515This plugin is only compatible with functional sites hosted on [CynderHost High Performance](https://cynderhost.com/high-performance-hosting).
    1616
    17 This plugin will use the [CynderHost HP API](https://api.cynderhost.com/high-performance/cache/cdn/purge/) to purge the cache automatically when a post is published, updated, or delete, and when a plugin is installed. This will also add a button to the admin interface allowing you to purge the cache manually.
     17This plugin will use the CynderHost High Performance API to purge the CDN and local caches automatically when a post is published, updated, or delete, and when a plugin is installed. This will also add a button to the admin interface allowing you to purge the cache manually.
    1818
    19 An API key is required for this plugin to work, which you can obtain from the hosting panel for your site under "CDN"
     19An API key is required for this plugin to work, which you can obtain from the hosting panel for your site under "CDN" as well as the hostname of your hosting server.
    2020
    21 Use of this plugin requires acceptance of CynderHost's [terms of service](https://cynderhost.com/terms-of-service)
    22 As well as [privacy policy](https://cynderhost.com/privacy-policy)
     21Use of this plugin requires acceptance of CynderHost's [terms of service](https://cynderhost.com/terms-of-service) as well as [privacy policy](https://cynderhost.com/privacy-policy)
    2322
    2423
     
    3029Upload/activate the Plugin
    3130
    32 Go to the settings page titled "CynderHost CDN" and insert your api key for the site.
     31Go to the settings page titled "CynderHost CDN" and insert your api key & hostname for the site.
  • cynderhost/trunk/cynderhost.php

    r2354247 r2373503  
    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.0.5
     12 * Version:           1.1.1
    1313 * Author:            CynderHost
    1414 * Author URI:        https://profiles.wordpress.org/cynderhost/
     
    3939    $cynderhost_cdn_settings_options = get_option( 'cynderhost_cdn_settings_option_name' ); // Array of All Options
    4040  $api_key = $cynderhost_cdn_settings_options['api_key_0'];
     41    $hostname = $cynderhost_cdn_settings_options['hostname_1'];
    4142    //cache purge endpoint
    4243    $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/cdn/purge/", array(
     
    5758        $result = json_decode($response['body'], true)['status'];
    5859    }
     60    $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/local/purge/", array(
     61        'method'      => 'POST',
     62        'timeout'     => 10,
     63        'redirection' => 5,
     64        'blocking'    => $resp,
     65        'body'        => array(
     66            'API' => "$api_key",
     67                    'HOST' => "$hostname"
     68         )
     69        )
     70    );
     71    //Non-standard Error, 5XX, 4XX
     72    if ( is_wp_error( $response ) ) {
     73     $error_message = $response->get_error_message();
     74     $result = "Something went wrong: $error_message";
     75    } else {
     76        $result2 = json_decode($response['body'], true)['status'];
     77            $result = ($result2 == "Success. Local server cache has been successfully purged.") ? $result : $result2;
     78    }
    5979    $result = $resp ? $result : "Cache purge request sent.";
    6080    return $result;
     
    6989}
    7090
     91/*
     92 * Sets the local cache status via api
     93 */
     94function cynderhost_set_stat($boole, $hostname, $api_key){
     95    $response = wp_remote_post( "https://api.cynderhost.com/high-performance/cache/local/status/", array(
     96        'method'      => 'POST',
     97        'timeout'     => 10,
     98        'redirection' => 5,
     99        'blocking'    => false,
     100        'body'        => array(
     101            'API' => "$api_key",
     102                    'HOST' => "$hostname",
     103                    "STATUS" => "$boole"
     104         )
     105        )
     106    );
     107    return true;
     108}
    71109/**
    72110 * Displays a notice with cache purge status
     
    169207        register_setting(
    170208            'cynderhost_cdn_settings_option_group', // option_group
    171             'cynderhost_cdn_settings_option_name', // option_name
     209            'cynderhost_cdn_settings_option_name',
     210            // option_name
    172211            array( $this, 'cynderhost_cdn_settings_sanitize' ) // sanitize_callback
    173212        );
     213
    174214
    175215        add_settings_section(
     
    187227            'cynderhost_cdn_settings_setting_section' // section
    188228        );
     229
     230        add_settings_field(
     231            'hostname_1', // id
     232            'Hostname', // title
     233            array( $this, 'hostname_1_callback' ), // callback
     234            'cynderhost-cdn-settings-admin', // page
     235            'cynderhost_cdn_settings_setting_section' // section
     236        );
     237
     238        add_settings_field(
     239            'server_page_cache_3', // id
     240            'Server Page Cache', // title
     241            array( $this, 'server_page_cache_3_callback' ), // callback
     242            'cynderhost-cdn-settings-admin', // page
     243            'cynderhost_cdn_settings_setting_section' // section
     244        );
    189245    }
    190246
     
    194250            $sanitary_values['api_key_0'] = sanitize_text_field( $input['api_key_0'] );
    195251        }
     252
     253        if ( isset( $input['hostname_1'] ) ) {
     254            $sanitary_values['hostname_1'] = sanitize_text_field( $input['hostname_1'] );
     255        }
     256        if ( get_option( 'cynderhost_cdn_settings_option_name')['server_page_cache_3']  !=  $input['server_page_cache_3']){
     257            cynderhost_set_stat(( isset( $input['server_page_cache_3']) && $input['server_page_cache_3'] === 'server_page_cache_3' ) ? 'active' : "inactive", $sanitary_values['hostname_1'] ,$sanitary_values['api_key_0']  );
     258        }
     259        if ( isset( $input['server_page_cache_3'] ) ) {
     260            $sanitary_values['server_page_cache_3'] = $input['server_page_cache_3'];
     261        }
    196262        do_cache_cynderhost_purge(true);
    197263        return $sanitary_values;
     
    203269            '<input class="regular-text" type="text" name="cynderhost_cdn_settings_option_name[api_key_0]" id="api_key_0" value="%s">',
    204270            isset( $this->cynderhost_cdn_settings_options['api_key_0'] ) ? esc_attr( $this->cynderhost_cdn_settings_options['api_key_0']) : ''
     271        );
     272    }
     273
     274    public function hostname_1_callback() {
     275        printf(
     276            '<input class="regular-text" type="text" name="cynderhost_cdn_settings_option_name[hostname_1]" id="hostname_1" value="%s"><p>The hostname of your server, derived from the panel url (ie, for proton.cynderhost.com the hostname proton)',
     277            isset( $this->cynderhost_cdn_settings_options['hostname_1'] ) ? esc_attr( $this->cynderhost_cdn_settings_options['hostname_1']) : ''
     278        );
     279    }
     280
     281    public function server_page_cache_3_callback() {
     282        printf(
     283            '<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>',
     284            ( isset( $this->cynderhost_cdn_settings_options['server_page_cache_3'] ) && $this->cynderhost_cdn_settings_options['server_page_cache_3'] === 'server_page_cache_3' ) ? 'checked' : ''
    205285        );
    206286    }
Note: See TracChangeset for help on using the changeset viewer.