Plugin Directory

Changeset 3366827


Ignore:
Timestamp:
09/24/2025 12:36:10 AM (6 months ago)
Author:
hideokamoto
Message:

Update to version 7.3.0 from GitHub

Location:
c3-cloudfront-clear-cache
Files:
2 added
16 edited
1 copied

Legend:

Unmodified
Added
Removed
  • c3-cloudfront-clear-cache/tags/7.3.0/c3-cloudfront-clear-cache.php

    r3346059 r3366827  
    22/**
    33 * Plugin Name: C3 Cloudfront Cache Controller
    4  * Version: 7.2.0
     4 * Version: 7.3.0
    55 * Plugin URI:https://github.com/amimoto-ami/c3-cloudfront-clear-cache
    66 * Description: Manage CloudFront Cache and provide some fixtures.
     
    2929    new C3_CloudFront_Cache_Controller\Settings_Service();
    3030    new C3_CloudFront_Cache_Controller\Views\Settings();
     31    new C3_CloudFront_Cache_Controller\Views\Debug_Settings();
    3132    new WP\Fixtures();
    3233}
  • c3-cloudfront-clear-cache/tags/7.3.0/classes/AWS/CloudFront_Service.php

    r3337531 r3366827  
    1414
    1515use C3_CloudFront_Cache_Controller\WP;
     16use C3_CloudFront_Cache_Controller\Constants;
    1617
    1718/**
     
    251252        $paths           = $params['InvalidationBatch']['Paths']['Items'];
    252253
    253         if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', false ) ) {
     254        if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) ) ) {
    254255            error_log( 'C3 CloudFront Invalidation Request - Distribution ID: ' . $distribution_id );
    255256            error_log( 'C3 CloudFront Invalidation Request - Paths: ' . print_r( $paths, true ) );
     
    316317            if ( isset( $result['Quantity'] ) && $result['Quantity'] > 0 && isset( $result['Items']['InvalidationSummary'] ) ) {
    317318                error_log( 'C3 CloudFront: Found ' . $result['Quantity'] . ' invalidations' );
    318                 return $result['Items']['InvalidationSummary'];
     319               
     320                // InvalidationSummaryが単一のオブジェクトの場合は配列に変換
     321                $invalidations = $result['Items']['InvalidationSummary'];
     322                if ( ! is_array( $invalidations ) || ( isset( $invalidations[0] ) === false && isset( $invalidations['Id'] ) ) ) {
     323                    // 単一のオブジェクトの場合は配列にラップ
     324                    $invalidations = array( $invalidations );
     325                }
     326               
     327                error_log( 'C3 CloudFront: Processed invalidations data: ' . print_r( $invalidations, true ) );
     328                return $invalidations;
    319329            }
    320330
     
    369379        }
    370380    }
     381
     382    /**
     383     * Get debug setting value
     384     *
     385     * @param string $setting_key Debug setting key.
     386     * @return boolean Debug setting value.
     387     */
     388    private function get_debug_setting( $setting_key ) {
     389        $debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
     390        $value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
     391        return $value;
     392    }
    371393}
  • c3-cloudfront-clear-cache/tags/7.3.0/classes/AWS/EC2_Metadata_Service.php

    r3336321 r3366827  
    174174     */
    175175    public function is_ec2_instance() {
     176        // First, get a token with IMDSv2
     177        $token = $this->get_imdsv2_token();
     178        if ( $token ) {
     179            // Accessing metadata using tokens
     180            $response = wp_remote_request(
     181                $this->metadata_endpoint . '/latest/meta-data/',
     182                array(
     183                    'method'  => 'GET',
     184                    'headers' => array( 'X-aws-ec2-metadata-token' => $token ),
     185                    'timeout' => 2,
     186                )
     187            );
     188            if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
     189                return true;
     190            }
     191        }
     192
     193        // If IMDSv2 fails, fall back to IMDSv1
    176194        $response = wp_remote_request(
    177195            $this->metadata_endpoint . '/latest/meta-data/',
     
    181199            )
    182200        );
    183 
    184201        return ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200;
    185202    }
  • c3-cloudfront-clear-cache/tags/7.3.0/classes/Constants.php

    r3309497 r3366827  
    3737    const SECRET_KEY      = 'secret_key';
    3838
     39    const DEBUG_OPTION_NAME = 'c3_debug_settings';
     40    const DEBUG_LOG_CRON_REGISTER_TASK = 'log_cron_register_task';
     41    const DEBUG_LOG_INVALIDATION_PARAMS = 'log_invalidation_params';
     42
    3943    /**
    4044     * Get Plugin text_domain
  • c3-cloudfront-clear-cache/tags/7.3.0/classes/Cron_Service.php

    r3336321 r3366827  
    99
    1010namespace C3_CloudFront_Cache_Controller;
     11use C3_CloudFront_Cache_Controller\Constants;
    1112if ( ! defined( 'ABSPATH' ) ) {
    1213    exit;
     
    3536
    3637    /**
    37      * Debug flag
     38     * Log cron register task flag
    3839     *
    3940     * @var boolean
    4041     */
    41     private $debug;
     42    private $log_cron_register_task;
    4243
    4344    /**
     
    7677            )
    7778        );
    78         $this->debug = $this->hook_service->apply_filters( 'c3_log_cron_invalidation_task', false );
     79        $this->log_cron_register_task = $this->hook_service->apply_filters( 'c3_log_cron_invalidation_task', $this->get_debug_setting( Constants::DEBUG_LOG_CRON_REGISTER_TASK ) );
    7980    }
    8081
     
    8586     */
    8687    public function run_schedule_invalidate() {
    87         if ( $this->debug ) {
     88        if ( $this->log_cron_register_task ) {
    8889            error_log( '===== C3 Invalidation cron is started ===' );
    8990        }
    9091        if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) {
    91             if ( $this->debug ) {
     92            if ( $this->log_cron_register_task ) {
    9293                error_log( '===== C3 Invalidation cron has been SKIPPED [Disabled] ===' );
    9394            }
     
    9596        }
    9697        $invalidation_batch = $this->transient_service->load_invalidation_query();
    97         if ( $this->debug ) {
     98        if ( $this->log_cron_register_task ) {
    9899            error_log( print_r( $invalidation_batch, true ) );
    99100        }
    100101        if ( ! $invalidation_batch || empty( $invalidation_batch ) ) {
    101             if ( $this->debug ) {
     102            if ( $this->log_cron_register_task ) {
    102103                error_log( '===== C3 Invalidation cron has been SKIPPED [No Target Item] ===' );
    103104            }
     
    109110            'InvalidationBatch' => $invalidation_batch,
    110111        );
    111         if ( $this->debug ) {
     112        if ( $this->log_cron_register_task ) {
    112113            error_log( print_r( $query, true ) );
    113114        }
     
    117118         */
    118119        $result = $this->cf_service->create_invalidation( $query );
    119         if ( $this->debug ) {
     120        if ( $this->log_cron_register_task ) {
    120121            if ( is_wp_error( $result ) ) {
    121122                error_log( 'C3 Cron: Invalidation failed: ' . $result->get_error_message() );
     
    125126        }
    126127        $this->transient_service->delete_invalidation_query();
    127         if ( $this->debug ) {
     128        if ( $this->log_cron_register_task ) {
    128129            error_log( '===== C3 Invalidation cron has been COMPLETED ===' );
    129130        }
    130131        return true;
    131132    }
     133
     134    /**
     135     * Get debug setting value
     136     *
     137     * @param string $setting_key Debug setting key.
     138     * @return boolean Debug setting value.
     139     */
     140    private function get_debug_setting( $setting_key ) {
     141        $debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
     142        $value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
     143        return $value;
     144    }
    132145}
  • c3-cloudfront-clear-cache/tags/7.3.0/classes/Invalidation_Service.php

    r3337531 r3366827  
    1010namespace C3_CloudFront_Cache_Controller;
    1111use C3_CloudFront_Cache_Controller\WP\Post_Service;
     12use C3_CloudFront_Cache_Controller\Constants;
    1213if ( ! defined( 'ABSPATH' ) ) {
    1314    exit;
     
    6465
    6566    /**
    66      * Debug flag
     67     * Log invalidation parameters flag
    6768     *
    6869     * @var boolean
    6970     */
    70     private $debug;
    71 
    72     /**
    73      * Inject a external services
    74      *
    75      * @param mixed ...$args Inject class.
     71    private $log_invalidation_params;
     72
     73    /**
     74     * Initialize the service, register WordPress hooks, and optionally inject dependencies.
     75     *
     76     * The constructor creates default implementations for hooks, options, invalidation batch,
     77     * transients, CloudFront, and admin notices, then registers action handlers used by the
     78     * invalidation workflow (post status transitions, attachment deletions, manual admin
     79     * invalidation, and AJAX detail requests). Any provided variadic arguments are treated
     80     * as dependency overrides and will replace the corresponding default instance when they
     81     * are an instance of one of the known service types (WP\Hooks, WP\Transient_Service,
     82     * WP\Options_Service, AWS\Invalidation_Batch_Service, AWS\CloudFront_Service,
     83     * WP\Admin_Notice). Finally, the constructor reads the `c3_log_cron_register_task`
     84     * filter to set the debug flag.
    7685     */
    7786    function __construct( ...$args ) {
     
    110119        );
    111120        $this->hook_service->add_action(
     121            'delete_attachment',
     122            array(
     123                $this,
     124                'invalidate_attachment_cache',
     125            ),
     126            10,
     127            1
     128        );
     129        $this->hook_service->add_action(
    112130            'admin_init',
    113131            array(
     
    123141            )
    124142        );
    125         $this->debug = $this->hook_service->apply_filters( 'c3_log_cron_register_task', false );
     143        $this->log_invalidation_params = $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) );
    126144    }
    127145
     
    179197     */
    180198    public function register_cron_event( $query ) {
    181         if ( $this->debug ) {
     199        if ( $this->log_invalidation_params ) {
    182200            error_log( '===== C3 CRON Job registration [START] ===' );
    183201        }
    184202        if ( ! isset( $query['Paths'] ) || ! isset( $query['Paths']['Items'] ) || $query['Paths']['Items'][0] === '/*' ) {
    185             if ( $this->debug ) {
     203            if ( $this->log_invalidation_params ) {
    186204                error_log( '===== C3 CRON Job registration [SKIP | NO ITEM] ===' );
    187205            }
     
    189207        }
    190208        if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) {
    191             if ( $this->debug ) {
     209            if ( $this->log_invalidation_params ) {
    192210                error_log( '===== C3 CRON Job registration [SKIP | DISABLED] ===' );
    193211            }
     
    198216        $interval_minutes = $this->hook_service->apply_filters( 'c3_invalidation_cron_interval', 1 );
    199217        $time             = time() + MINUTE_IN_SECONDS * $interval_minutes;
    200         if ( $this->debug ) {
     218        if ( $this->log_invalidation_params ) {
    201219            error_log( print_r( $query, true ) );
    202220        }
     
    204222        $result = wp_schedule_single_event( $time, 'c3_cron_invalidation' );
    205223
    206         if ( $this->debug ) {
     224        if ( $this->log_invalidation_params ) {
    207225            error_log( '===== C3 CRON Job registration [COMPLETE] ===' );
    208226        }
     
    255273        }
    256274
    257         if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', false ) ) {
     275        if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) ) ) {
    258276            error_log( 'C3 Invalidation Started - Query: ' . print_r( $query, true ) );
    259277            error_log( 'C3 Invalidation Started - Force: ' . ( $force ? 'true' : 'false' ) );
     
    276294        $result = $this->cf_service->create_invalidation( $query );
    277295       
    278         if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', false ) ) {
    279             if ( is_wp_error( $result ) ) {
    280                 error_log( 'C3 Invalidation Failed: ' . $result->get_error_message() );
    281             } else {
    282                 error_log( 'C3 Invalidation Completed Successfully: ' . print_r( $result, true ) );
    283             }
    284         }
    285        
    286296        if ( is_wp_error( $result ) ) {
     297            error_log( 'C3 Invalidation Failed: ' . $result->get_error_message() );
    287298            return $result;
    288299        }
     
    377388
    378389        // デバッグログを追加
    379         if ( $this->debug || $this->hook_service->apply_filters( 'c3_log_invalidation_list', false ) ) {
     390        if ( $this->hook_service->apply_filters( 'c3_log_invalidation_list', false ) ) {
    380391            error_log( 'C3 Invalidation Logs Result: ' . print_r( $histories, true ) );
    381392        }
     
    406417
    407418    /**
    408      * Handle AJAX request for invalidation details
     419     * Handle AJAX requests for fetching CloudFront invalidation details.
     420     *
     421     * Verifies the AJAX nonce ('c3_invalidation_details_nonce' via POST key 'nonce')
     422     * and the current user's 'cloudfront_clear_cache' capability. Reads and
     423     * sanitizes the POST parameter 'invalidation_id', then returns the invalidation
     424     * details as a JSON success response or a JSON error message on failure.
     425     *
     426     * Security and responses:
     427     * - Fails immediately with wp_die on nonce check or capability failure.
     428     * - If 'invalidation_id' is missing or empty, sends a JSON error.
     429     * - If get_invalidation_details() returns a WP_Error, sends its message as a JSON error.
     430     * - Otherwise sends the details with wp_send_json_success().
     431     *
     432     * Expected POST fields:
     433     * - nonce: string (AJAX nonce to validate request)
     434     * - invalidation_id: string (ID of the invalidation to fetch)
     435     *
     436     * @return void Sends a JSON response (and exits) or terminates via wp_die on security failures.
    409437     */
    410438    public function handle_invalidation_details_ajax() {
     
    430458        wp_send_json_success( $details );
    431459    }
     460
     461    /**
     462     * Trigger CloudFront invalidation for a deleted attachment.
     463     *
     464     * Builds a wildcard path from the deleted attachment's URL (dirname/filename*) and delegates
     465     * the invalidation request to invalidate_by_query().
     466     *
     467     * @param int $attachment_id ID of the attachment being deleted.
     468     * @return mixed|null WP_Error if plugin options are missing, the result returned by invalidate_by_query() on success, or null if the attachment URL/path cannot be determined.
     469     */
     470    public function invalidate_attachment_cache( $attachment_id ) {
     471        $attachment_url = wp_get_attachment_url( $attachment_id );
     472       
     473        if ( ! $attachment_url ) {
     474            return;
     475        }
     476       
     477        $parsed_url = parse_url( $attachment_url );
     478        if ( ! isset( $parsed_url['path'] ) ) {
     479            return;
     480        }
     481       
     482        $path_info = pathinfo( $parsed_url['path'] );
     483        $wildcard_path = $path_info['dirname'] . '/' . $path_info['filename'] . '*';
     484       
     485        $options = $this->get_plugin_option();
     486        if ( is_wp_error( $options ) ) {
     487            return $options;
     488        }
     489       
     490        $invalidation_batch = new AWS\Invalidation_Batch();
     491        $invalidation_batch->put_invalidation_path( $wildcard_path );
     492        $query = $invalidation_batch->get_invalidation_request_parameter( $options['distribution_id'] );
     493       
     494        return $this->invalidate_by_query( $query );
     495    }
     496
     497    /**
     498     * Get debug setting value
     499     *
     500     * @param string $setting_key Debug setting key.
     501     * @return boolean Debug setting value.
     502     */
     503    private function get_debug_setting( $setting_key ) {
     504        $debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
     505        $value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
     506        return $value;
     507    }
    432508}
  • c3-cloudfront-clear-cache/tags/7.3.0/classes/WP/Post_Service.php

    r3309497 r3366827  
    2121class Post_Service {
    2222    /**
    23      * Get the target post ids
     23     * Retrieve posts matching the provided post IDs.
    2424     *
    25      * @param array $post_ids The target post ids.
     25     * Queries WordPress for posts whose IDs are in $post_ids (searches only public post types)
     26     * and returns the resulting array of WP_Post objects. Resets global post data after the query.
     27     *
     28     * @param int[] $post_ids Array of post IDs to fetch.
     29     * @return \WP_Post[] Array of posts matching the given IDs (may be empty).
    2630     */
    2731    public function list_posts_by_ids( $post_ids ) {
     32        if ( empty( $post_ids ) || ! is_array( $post_ids ) ) {
     33            return array();
     34        }
     35
     36        $post_ids = array_filter( array_map( 'intval', $post_ids ), function( $id ) {
     37            return $id > 0;
     38        } );
     39        if ( empty( $post_ids ) ) {
     40            return array();
     41        }
     42
     43        $public_post_types = array_values( get_post_types( array( 'public' => true ), 'names' ) );
     44
    2845        $query = new \WP_Query(
    2946            array(
    3047                'post__in' => $post_ids,
     48                'post_type' => $public_post_types,
     49                'posts_per_page' => -1,
     50                'no_found_rows' => true,
     51                'update_post_meta_cache' => false,
     52                'update_post_term_cache' => false,
    3153            )
    3254        );
  • c3-cloudfront-clear-cache/tags/7.3.0/readme.txt

    r3346059 r3366827  
    55Requires at least: 4.9.0
    66Tested up to: 6.8.1
    7 Stable tag: 7.2.0
     7Stable tag: 7.3.0
    88License: GPLv3 or later
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
  • c3-cloudfront-clear-cache/trunk/c3-cloudfront-clear-cache.php

    r3346059 r3366827  
    22/**
    33 * Plugin Name: C3 Cloudfront Cache Controller
    4  * Version: 7.2.0
     4 * Version: 7.3.0
    55 * Plugin URI:https://github.com/amimoto-ami/c3-cloudfront-clear-cache
    66 * Description: Manage CloudFront Cache and provide some fixtures.
     
    2929    new C3_CloudFront_Cache_Controller\Settings_Service();
    3030    new C3_CloudFront_Cache_Controller\Views\Settings();
     31    new C3_CloudFront_Cache_Controller\Views\Debug_Settings();
    3132    new WP\Fixtures();
    3233}
  • c3-cloudfront-clear-cache/trunk/classes/AWS/CloudFront_Service.php

    r3337531 r3366827  
    1414
    1515use C3_CloudFront_Cache_Controller\WP;
     16use C3_CloudFront_Cache_Controller\Constants;
    1617
    1718/**
     
    251252        $paths           = $params['InvalidationBatch']['Paths']['Items'];
    252253
    253         if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', false ) ) {
     254        if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) ) ) {
    254255            error_log( 'C3 CloudFront Invalidation Request - Distribution ID: ' . $distribution_id );
    255256            error_log( 'C3 CloudFront Invalidation Request - Paths: ' . print_r( $paths, true ) );
     
    316317            if ( isset( $result['Quantity'] ) && $result['Quantity'] > 0 && isset( $result['Items']['InvalidationSummary'] ) ) {
    317318                error_log( 'C3 CloudFront: Found ' . $result['Quantity'] . ' invalidations' );
    318                 return $result['Items']['InvalidationSummary'];
     319               
     320                // InvalidationSummaryが単一のオブジェクトの場合は配列に変換
     321                $invalidations = $result['Items']['InvalidationSummary'];
     322                if ( ! is_array( $invalidations ) || ( isset( $invalidations[0] ) === false && isset( $invalidations['Id'] ) ) ) {
     323                    // 単一のオブジェクトの場合は配列にラップ
     324                    $invalidations = array( $invalidations );
     325                }
     326               
     327                error_log( 'C3 CloudFront: Processed invalidations data: ' . print_r( $invalidations, true ) );
     328                return $invalidations;
    319329            }
    320330
     
    369379        }
    370380    }
     381
     382    /**
     383     * Get debug setting value
     384     *
     385     * @param string $setting_key Debug setting key.
     386     * @return boolean Debug setting value.
     387     */
     388    private function get_debug_setting( $setting_key ) {
     389        $debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
     390        $value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
     391        return $value;
     392    }
    371393}
  • c3-cloudfront-clear-cache/trunk/classes/AWS/EC2_Metadata_Service.php

    r3336321 r3366827  
    174174     */
    175175    public function is_ec2_instance() {
     176        // First, get a token with IMDSv2
     177        $token = $this->get_imdsv2_token();
     178        if ( $token ) {
     179            // Accessing metadata using tokens
     180            $response = wp_remote_request(
     181                $this->metadata_endpoint . '/latest/meta-data/',
     182                array(
     183                    'method'  => 'GET',
     184                    'headers' => array( 'X-aws-ec2-metadata-token' => $token ),
     185                    'timeout' => 2,
     186                )
     187            );
     188            if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
     189                return true;
     190            }
     191        }
     192
     193        // If IMDSv2 fails, fall back to IMDSv1
    176194        $response = wp_remote_request(
    177195            $this->metadata_endpoint . '/latest/meta-data/',
     
    181199            )
    182200        );
    183 
    184201        return ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200;
    185202    }
  • c3-cloudfront-clear-cache/trunk/classes/Constants.php

    r3309497 r3366827  
    3737    const SECRET_KEY      = 'secret_key';
    3838
     39    const DEBUG_OPTION_NAME = 'c3_debug_settings';
     40    const DEBUG_LOG_CRON_REGISTER_TASK = 'log_cron_register_task';
     41    const DEBUG_LOG_INVALIDATION_PARAMS = 'log_invalidation_params';
     42
    3943    /**
    4044     * Get Plugin text_domain
  • c3-cloudfront-clear-cache/trunk/classes/Cron_Service.php

    r3336321 r3366827  
    99
    1010namespace C3_CloudFront_Cache_Controller;
     11use C3_CloudFront_Cache_Controller\Constants;
    1112if ( ! defined( 'ABSPATH' ) ) {
    1213    exit;
     
    3536
    3637    /**
    37      * Debug flag
     38     * Log cron register task flag
    3839     *
    3940     * @var boolean
    4041     */
    41     private $debug;
     42    private $log_cron_register_task;
    4243
    4344    /**
     
    7677            )
    7778        );
    78         $this->debug = $this->hook_service->apply_filters( 'c3_log_cron_invalidation_task', false );
     79        $this->log_cron_register_task = $this->hook_service->apply_filters( 'c3_log_cron_invalidation_task', $this->get_debug_setting( Constants::DEBUG_LOG_CRON_REGISTER_TASK ) );
    7980    }
    8081
     
    8586     */
    8687    public function run_schedule_invalidate() {
    87         if ( $this->debug ) {
     88        if ( $this->log_cron_register_task ) {
    8889            error_log( '===== C3 Invalidation cron is started ===' );
    8990        }
    9091        if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) {
    91             if ( $this->debug ) {
     92            if ( $this->log_cron_register_task ) {
    9293                error_log( '===== C3 Invalidation cron has been SKIPPED [Disabled] ===' );
    9394            }
     
    9596        }
    9697        $invalidation_batch = $this->transient_service->load_invalidation_query();
    97         if ( $this->debug ) {
     98        if ( $this->log_cron_register_task ) {
    9899            error_log( print_r( $invalidation_batch, true ) );
    99100        }
    100101        if ( ! $invalidation_batch || empty( $invalidation_batch ) ) {
    101             if ( $this->debug ) {
     102            if ( $this->log_cron_register_task ) {
    102103                error_log( '===== C3 Invalidation cron has been SKIPPED [No Target Item] ===' );
    103104            }
     
    109110            'InvalidationBatch' => $invalidation_batch,
    110111        );
    111         if ( $this->debug ) {
     112        if ( $this->log_cron_register_task ) {
    112113            error_log( print_r( $query, true ) );
    113114        }
     
    117118         */
    118119        $result = $this->cf_service->create_invalidation( $query );
    119         if ( $this->debug ) {
     120        if ( $this->log_cron_register_task ) {
    120121            if ( is_wp_error( $result ) ) {
    121122                error_log( 'C3 Cron: Invalidation failed: ' . $result->get_error_message() );
     
    125126        }
    126127        $this->transient_service->delete_invalidation_query();
    127         if ( $this->debug ) {
     128        if ( $this->log_cron_register_task ) {
    128129            error_log( '===== C3 Invalidation cron has been COMPLETED ===' );
    129130        }
    130131        return true;
    131132    }
     133
     134    /**
     135     * Get debug setting value
     136     *
     137     * @param string $setting_key Debug setting key.
     138     * @return boolean Debug setting value.
     139     */
     140    private function get_debug_setting( $setting_key ) {
     141        $debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
     142        $value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
     143        return $value;
     144    }
    132145}
  • c3-cloudfront-clear-cache/trunk/classes/Invalidation_Service.php

    r3337531 r3366827  
    1010namespace C3_CloudFront_Cache_Controller;
    1111use C3_CloudFront_Cache_Controller\WP\Post_Service;
     12use C3_CloudFront_Cache_Controller\Constants;
    1213if ( ! defined( 'ABSPATH' ) ) {
    1314    exit;
     
    6465
    6566    /**
    66      * Debug flag
     67     * Log invalidation parameters flag
    6768     *
    6869     * @var boolean
    6970     */
    70     private $debug;
    71 
    72     /**
    73      * Inject a external services
    74      *
    75      * @param mixed ...$args Inject class.
     71    private $log_invalidation_params;
     72
     73    /**
     74     * Initialize the service, register WordPress hooks, and optionally inject dependencies.
     75     *
     76     * The constructor creates default implementations for hooks, options, invalidation batch,
     77     * transients, CloudFront, and admin notices, then registers action handlers used by the
     78     * invalidation workflow (post status transitions, attachment deletions, manual admin
     79     * invalidation, and AJAX detail requests). Any provided variadic arguments are treated
     80     * as dependency overrides and will replace the corresponding default instance when they
     81     * are an instance of one of the known service types (WP\Hooks, WP\Transient_Service,
     82     * WP\Options_Service, AWS\Invalidation_Batch_Service, AWS\CloudFront_Service,
     83     * WP\Admin_Notice). Finally, the constructor reads the `c3_log_cron_register_task`
     84     * filter to set the debug flag.
    7685     */
    7786    function __construct( ...$args ) {
     
    110119        );
    111120        $this->hook_service->add_action(
     121            'delete_attachment',
     122            array(
     123                $this,
     124                'invalidate_attachment_cache',
     125            ),
     126            10,
     127            1
     128        );
     129        $this->hook_service->add_action(
    112130            'admin_init',
    113131            array(
     
    123141            )
    124142        );
    125         $this->debug = $this->hook_service->apply_filters( 'c3_log_cron_register_task', false );
     143        $this->log_invalidation_params = $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) );
    126144    }
    127145
     
    179197     */
    180198    public function register_cron_event( $query ) {
    181         if ( $this->debug ) {
     199        if ( $this->log_invalidation_params ) {
    182200            error_log( '===== C3 CRON Job registration [START] ===' );
    183201        }
    184202        if ( ! isset( $query['Paths'] ) || ! isset( $query['Paths']['Items'] ) || $query['Paths']['Items'][0] === '/*' ) {
    185             if ( $this->debug ) {
     203            if ( $this->log_invalidation_params ) {
    186204                error_log( '===== C3 CRON Job registration [SKIP | NO ITEM] ===' );
    187205            }
     
    189207        }
    190208        if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) {
    191             if ( $this->debug ) {
     209            if ( $this->log_invalidation_params ) {
    192210                error_log( '===== C3 CRON Job registration [SKIP | DISABLED] ===' );
    193211            }
     
    198216        $interval_minutes = $this->hook_service->apply_filters( 'c3_invalidation_cron_interval', 1 );
    199217        $time             = time() + MINUTE_IN_SECONDS * $interval_minutes;
    200         if ( $this->debug ) {
     218        if ( $this->log_invalidation_params ) {
    201219            error_log( print_r( $query, true ) );
    202220        }
     
    204222        $result = wp_schedule_single_event( $time, 'c3_cron_invalidation' );
    205223
    206         if ( $this->debug ) {
     224        if ( $this->log_invalidation_params ) {
    207225            error_log( '===== C3 CRON Job registration [COMPLETE] ===' );
    208226        }
     
    255273        }
    256274
    257         if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', false ) ) {
     275        if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', $this->get_debug_setting( Constants::DEBUG_LOG_INVALIDATION_PARAMS ) ) ) {
    258276            error_log( 'C3 Invalidation Started - Query: ' . print_r( $query, true ) );
    259277            error_log( 'C3 Invalidation Started - Force: ' . ( $force ? 'true' : 'false' ) );
     
    276294        $result = $this->cf_service->create_invalidation( $query );
    277295       
    278         if ( $this->hook_service->apply_filters( 'c3_log_invalidation_params', false ) ) {
    279             if ( is_wp_error( $result ) ) {
    280                 error_log( 'C3 Invalidation Failed: ' . $result->get_error_message() );
    281             } else {
    282                 error_log( 'C3 Invalidation Completed Successfully: ' . print_r( $result, true ) );
    283             }
    284         }
    285        
    286296        if ( is_wp_error( $result ) ) {
     297            error_log( 'C3 Invalidation Failed: ' . $result->get_error_message() );
    287298            return $result;
    288299        }
     
    377388
    378389        // デバッグログを追加
    379         if ( $this->debug || $this->hook_service->apply_filters( 'c3_log_invalidation_list', false ) ) {
     390        if ( $this->hook_service->apply_filters( 'c3_log_invalidation_list', false ) ) {
    380391            error_log( 'C3 Invalidation Logs Result: ' . print_r( $histories, true ) );
    381392        }
     
    406417
    407418    /**
    408      * Handle AJAX request for invalidation details
     419     * Handle AJAX requests for fetching CloudFront invalidation details.
     420     *
     421     * Verifies the AJAX nonce ('c3_invalidation_details_nonce' via POST key 'nonce')
     422     * and the current user's 'cloudfront_clear_cache' capability. Reads and
     423     * sanitizes the POST parameter 'invalidation_id', then returns the invalidation
     424     * details as a JSON success response or a JSON error message on failure.
     425     *
     426     * Security and responses:
     427     * - Fails immediately with wp_die on nonce check or capability failure.
     428     * - If 'invalidation_id' is missing or empty, sends a JSON error.
     429     * - If get_invalidation_details() returns a WP_Error, sends its message as a JSON error.
     430     * - Otherwise sends the details with wp_send_json_success().
     431     *
     432     * Expected POST fields:
     433     * - nonce: string (AJAX nonce to validate request)
     434     * - invalidation_id: string (ID of the invalidation to fetch)
     435     *
     436     * @return void Sends a JSON response (and exits) or terminates via wp_die on security failures.
    409437     */
    410438    public function handle_invalidation_details_ajax() {
     
    430458        wp_send_json_success( $details );
    431459    }
     460
     461    /**
     462     * Trigger CloudFront invalidation for a deleted attachment.
     463     *
     464     * Builds a wildcard path from the deleted attachment's URL (dirname/filename*) and delegates
     465     * the invalidation request to invalidate_by_query().
     466     *
     467     * @param int $attachment_id ID of the attachment being deleted.
     468     * @return mixed|null WP_Error if plugin options are missing, the result returned by invalidate_by_query() on success, or null if the attachment URL/path cannot be determined.
     469     */
     470    public function invalidate_attachment_cache( $attachment_id ) {
     471        $attachment_url = wp_get_attachment_url( $attachment_id );
     472       
     473        if ( ! $attachment_url ) {
     474            return;
     475        }
     476       
     477        $parsed_url = parse_url( $attachment_url );
     478        if ( ! isset( $parsed_url['path'] ) ) {
     479            return;
     480        }
     481       
     482        $path_info = pathinfo( $parsed_url['path'] );
     483        $wildcard_path = $path_info['dirname'] . '/' . $path_info['filename'] . '*';
     484       
     485        $options = $this->get_plugin_option();
     486        if ( is_wp_error( $options ) ) {
     487            return $options;
     488        }
     489       
     490        $invalidation_batch = new AWS\Invalidation_Batch();
     491        $invalidation_batch->put_invalidation_path( $wildcard_path );
     492        $query = $invalidation_batch->get_invalidation_request_parameter( $options['distribution_id'] );
     493       
     494        return $this->invalidate_by_query( $query );
     495    }
     496
     497    /**
     498     * Get debug setting value
     499     *
     500     * @param string $setting_key Debug setting key.
     501     * @return boolean Debug setting value.
     502     */
     503    private function get_debug_setting( $setting_key ) {
     504        $debug_options = get_option( Constants::DEBUG_OPTION_NAME, array() );
     505        $value = isset( $debug_options[ $setting_key ] ) ? $debug_options[ $setting_key ] : false;
     506        return $value;
     507    }
    432508}
  • c3-cloudfront-clear-cache/trunk/classes/WP/Post_Service.php

    r3309497 r3366827  
    2121class Post_Service {
    2222    /**
    23      * Get the target post ids
     23     * Retrieve posts matching the provided post IDs.
    2424     *
    25      * @param array $post_ids The target post ids.
     25     * Queries WordPress for posts whose IDs are in $post_ids (searches only public post types)
     26     * and returns the resulting array of WP_Post objects. Resets global post data after the query.
     27     *
     28     * @param int[] $post_ids Array of post IDs to fetch.
     29     * @return \WP_Post[] Array of posts matching the given IDs (may be empty).
    2630     */
    2731    public function list_posts_by_ids( $post_ids ) {
     32        if ( empty( $post_ids ) || ! is_array( $post_ids ) ) {
     33            return array();
     34        }
     35
     36        $post_ids = array_filter( array_map( 'intval', $post_ids ), function( $id ) {
     37            return $id > 0;
     38        } );
     39        if ( empty( $post_ids ) ) {
     40            return array();
     41        }
     42
     43        $public_post_types = array_values( get_post_types( array( 'public' => true ), 'names' ) );
     44
    2845        $query = new \WP_Query(
    2946            array(
    3047                'post__in' => $post_ids,
     48                'post_type' => $public_post_types,
     49                'posts_per_page' => -1,
     50                'no_found_rows' => true,
     51                'update_post_meta_cache' => false,
     52                'update_post_term_cache' => false,
    3153            )
    3254        );
  • c3-cloudfront-clear-cache/trunk/readme.txt

    r3346059 r3366827  
    55Requires at least: 4.9.0
    66Tested up to: 6.8.1
    7 Stable tag: 7.2.0
     7Stable tag: 7.3.0
    88License: GPLv3 or later
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
Note: See TracChangeset for help on using the changeset viewer.