Пример #1
0
 /**
  * Flush All CloudFront Cache
  *
  * ## OPTIONS
  * <post_id>
  * post_id
  *
  * [--force]
  * Activate Force Clear Mode
  *
  * ## EXAMPLES
  *
  *     wp c3 flush <post_id>       : Flush <post_id>'s CloudFront Cache.
  *     wp c3 flush all             : Flush All CloudFront Cache.
  *     wp c3 flush all --force     : Flush All CloudFront Cache.( Force )
  *
  * @param string $args: WP-CLI Command Name
  * @param string $assoc_args: WP-CLI Command Option
  * @since 2.3.0
  */
 function flush($args, $assoc_args)
 {
     WP_CLI::line('Start to Clear CloudFront Cache...');
     if (empty($args)) {
         WP_CLI::error('Please input parameter:post_id(numeric) or all');
         exit;
     }
     list($type) = $args;
     $c3 = CloudFront_Clear_Cache::get_instance();
     if (array_search('force', $assoc_args)) {
         WP_CLI::line('Force Clear Mode');
         add_filter('c3_invalidation_flag', '__return_false');
     }
     if ('all' == $type) {
         WP_CLI::line('Clear Item = All');
         $result = $c3->c3_invalidation();
     } elseif (is_numeric($type)) {
         WP_CLI::line("Clear Item = (post_id={$type})");
         $result = $c3->c3_invalidation($type);
     } else {
         WP_CLI::error('Please input parameter:post_id(numeric) or all');
         exit;
     }
     if (!is_wp_error($result)) {
         WP_CLI::success("Create Invalidation Request. Please wait few minutes to finished clear CloudFront Cache.");
     }
 }
 public function c3_admin_init()
 {
     $option_name = CloudFront_Clear_Cache::OPTION_NAME;
     $nonce_key = CloudFront_Clear_Cache::OPTION_NAME;
     if (isset($_POST[self::MENU_ID]) && $_POST[self::MENU_ID]) {
         if (check_admin_referer($nonce_key, self::MENU_ID)) {
             $e = new WP_Error();
             update_option(CloudFront_Clear_Cache::OPTION_NAME, $_POST[$option_name]);
         } else {
             update_option(CloudFront_Clear_Cache::OPTION_NAME, '');
         }
         wp_safe_redirect(menu_page_url(self::MENU_ID, false));
     }
     if (isset($_POST[self::FLUSH_CACHE]) && $_POST[self::FLUSH_CACHE]) {
         $c3 = CloudFront_Clear_Cache::get_instance();
         $c3->c3_invalidation();
     }
 }
<?php

/*
Plugin Name: C3 Cloudfront Clear Cache
Version: 2.1.1
Plugin URI:https://github.com/megumiteam/C3-Cloudfront-Clear-Cache
Description:This is simple plugin that clear all cloudfront cache if you publish posts.
Author: hideokamoto
Author URI: http://wp-kyoto.net/
Text Domain: c3_cloudfront_clear_cache
*/
require_once dirname(__FILE__) . '/aws.phar';
require_once dirname(__FILE__) . '/lib/c3-admin.php';
use Aws\CloudFront\CloudFrontClient;
use Aws\Common\Credentials\Credentials;
$c3 = CloudFront_Clear_Cache::get_instance();
$c3->add_hook();
class CloudFront_Clear_Cache
{
    private static $instance;
    const OPTION_NAME = 'c3_settings';
    private function __construct()
    {
    }
    public static function get_instance()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c();
        }
        return self::$instance;
 function __construct()
 {
     $this->C3 = CloudFront_Clear_Cache::get_instance();
     $this->C3->add_hook();
 }