Changeset 3366827
- Timestamp:
- 09/24/2025 12:36:10 AM (6 months ago)
- Location:
- c3-cloudfront-clear-cache
- Files:
-
- 2 added
- 16 edited
- 1 copied
-
tags/7.3.0 (copied) (copied from c3-cloudfront-clear-cache/trunk)
-
tags/7.3.0/c3-cloudfront-clear-cache.php (modified) (2 diffs)
-
tags/7.3.0/classes/AWS/CloudFront_Service.php (modified) (4 diffs)
-
tags/7.3.0/classes/AWS/EC2_Metadata_Service.php (modified) (2 diffs)
-
tags/7.3.0/classes/Constants.php (modified) (1 diff)
-
tags/7.3.0/classes/Cron_Service.php (modified) (8 diffs)
-
tags/7.3.0/classes/Invalidation_Service.php (modified) (13 diffs)
-
tags/7.3.0/classes/Views/Debug_Settings.php (added)
-
tags/7.3.0/classes/WP/Post_Service.php (modified) (1 diff)
-
tags/7.3.0/readme.txt (modified) (1 diff)
-
trunk/c3-cloudfront-clear-cache.php (modified) (2 diffs)
-
trunk/classes/AWS/CloudFront_Service.php (modified) (4 diffs)
-
trunk/classes/AWS/EC2_Metadata_Service.php (modified) (2 diffs)
-
trunk/classes/Constants.php (modified) (1 diff)
-
trunk/classes/Cron_Service.php (modified) (8 diffs)
-
trunk/classes/Invalidation_Service.php (modified) (13 diffs)
-
trunk/classes/Views/Debug_Settings.php (added)
-
trunk/classes/WP/Post_Service.php (modified) (1 diff)
-
trunk/readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
c3-cloudfront-clear-cache/tags/7.3.0/c3-cloudfront-clear-cache.php
r3346059 r3366827 2 2 /** 3 3 * Plugin Name: C3 Cloudfront Cache Controller 4 * Version: 7. 2.04 * Version: 7.3.0 5 5 * Plugin URI:https://github.com/amimoto-ami/c3-cloudfront-clear-cache 6 6 * Description: Manage CloudFront Cache and provide some fixtures. … … 29 29 new C3_CloudFront_Cache_Controller\Settings_Service(); 30 30 new C3_CloudFront_Cache_Controller\Views\Settings(); 31 new C3_CloudFront_Cache_Controller\Views\Debug_Settings(); 31 32 new WP\Fixtures(); 32 33 } -
c3-cloudfront-clear-cache/tags/7.3.0/classes/AWS/CloudFront_Service.php
r3337531 r3366827 14 14 15 15 use C3_CloudFront_Cache_Controller\WP; 16 use C3_CloudFront_Cache_Controller\Constants; 16 17 17 18 /** … … 251 252 $paths = $params['InvalidationBatch']['Paths']['Items']; 252 253 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 ) ) ) { 254 255 error_log( 'C3 CloudFront Invalidation Request - Distribution ID: ' . $distribution_id ); 255 256 error_log( 'C3 CloudFront Invalidation Request - Paths: ' . print_r( $paths, true ) ); … … 316 317 if ( isset( $result['Quantity'] ) && $result['Quantity'] > 0 && isset( $result['Items']['InvalidationSummary'] ) ) { 317 318 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; 319 329 } 320 330 … … 369 379 } 370 380 } 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 } 371 393 } -
c3-cloudfront-clear-cache/tags/7.3.0/classes/AWS/EC2_Metadata_Service.php
r3336321 r3366827 174 174 */ 175 175 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 176 194 $response = wp_remote_request( 177 195 $this->metadata_endpoint . '/latest/meta-data/', … … 181 199 ) 182 200 ); 183 184 201 return ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200; 185 202 } -
c3-cloudfront-clear-cache/tags/7.3.0/classes/Constants.php
r3309497 r3366827 37 37 const SECRET_KEY = 'secret_key'; 38 38 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 39 43 /** 40 44 * Get Plugin text_domain -
c3-cloudfront-clear-cache/tags/7.3.0/classes/Cron_Service.php
r3336321 r3366827 9 9 10 10 namespace C3_CloudFront_Cache_Controller; 11 use C3_CloudFront_Cache_Controller\Constants; 11 12 if ( ! defined( 'ABSPATH' ) ) { 12 13 exit; … … 35 36 36 37 /** 37 * Debugflag38 * Log cron register task flag 38 39 * 39 40 * @var boolean 40 41 */ 41 private $ debug;42 private $log_cron_register_task; 42 43 43 44 /** … … 76 77 ) 77 78 ); 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 ) ); 79 80 } 80 81 … … 85 86 */ 86 87 public function run_schedule_invalidate() { 87 if ( $this-> debug) {88 if ( $this->log_cron_register_task ) { 88 89 error_log( '===== C3 Invalidation cron is started ===' ); 89 90 } 90 91 if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) { 91 if ( $this-> debug) {92 if ( $this->log_cron_register_task ) { 92 93 error_log( '===== C3 Invalidation cron has been SKIPPED [Disabled] ===' ); 93 94 } … … 95 96 } 96 97 $invalidation_batch = $this->transient_service->load_invalidation_query(); 97 if ( $this-> debug) {98 if ( $this->log_cron_register_task ) { 98 99 error_log( print_r( $invalidation_batch, true ) ); 99 100 } 100 101 if ( ! $invalidation_batch || empty( $invalidation_batch ) ) { 101 if ( $this-> debug) {102 if ( $this->log_cron_register_task ) { 102 103 error_log( '===== C3 Invalidation cron has been SKIPPED [No Target Item] ===' ); 103 104 } … … 109 110 'InvalidationBatch' => $invalidation_batch, 110 111 ); 111 if ( $this-> debug) {112 if ( $this->log_cron_register_task ) { 112 113 error_log( print_r( $query, true ) ); 113 114 } … … 117 118 */ 118 119 $result = $this->cf_service->create_invalidation( $query ); 119 if ( $this-> debug) {120 if ( $this->log_cron_register_task ) { 120 121 if ( is_wp_error( $result ) ) { 121 122 error_log( 'C3 Cron: Invalidation failed: ' . $result->get_error_message() ); … … 125 126 } 126 127 $this->transient_service->delete_invalidation_query(); 127 if ( $this-> debug) {128 if ( $this->log_cron_register_task ) { 128 129 error_log( '===== C3 Invalidation cron has been COMPLETED ===' ); 129 130 } 130 131 return true; 131 132 } 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 } 132 145 } -
c3-cloudfront-clear-cache/tags/7.3.0/classes/Invalidation_Service.php
r3337531 r3366827 10 10 namespace C3_CloudFront_Cache_Controller; 11 11 use C3_CloudFront_Cache_Controller\WP\Post_Service; 12 use C3_CloudFront_Cache_Controller\Constants; 12 13 if ( ! defined( 'ABSPATH' ) ) { 13 14 exit; … … 64 65 65 66 /** 66 * Debugflag67 * Log invalidation parameters flag 67 68 * 68 69 * @var boolean 69 70 */ 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. 76 85 */ 77 86 function __construct( ...$args ) { … … 110 119 ); 111 120 $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( 112 130 'admin_init', 113 131 array( … … 123 141 ) 124 142 ); 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 ) ); 126 144 } 127 145 … … 179 197 */ 180 198 public function register_cron_event( $query ) { 181 if ( $this-> debug) {199 if ( $this->log_invalidation_params ) { 182 200 error_log( '===== C3 CRON Job registration [START] ===' ); 183 201 } 184 202 if ( ! isset( $query['Paths'] ) || ! isset( $query['Paths']['Items'] ) || $query['Paths']['Items'][0] === '/*' ) { 185 if ( $this-> debug) {203 if ( $this->log_invalidation_params ) { 186 204 error_log( '===== C3 CRON Job registration [SKIP | NO ITEM] ===' ); 187 205 } … … 189 207 } 190 208 if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) { 191 if ( $this-> debug) {209 if ( $this->log_invalidation_params ) { 192 210 error_log( '===== C3 CRON Job registration [SKIP | DISABLED] ===' ); 193 211 } … … 198 216 $interval_minutes = $this->hook_service->apply_filters( 'c3_invalidation_cron_interval', 1 ); 199 217 $time = time() + MINUTE_IN_SECONDS * $interval_minutes; 200 if ( $this-> debug) {218 if ( $this->log_invalidation_params ) { 201 219 error_log( print_r( $query, true ) ); 202 220 } … … 204 222 $result = wp_schedule_single_event( $time, 'c3_cron_invalidation' ); 205 223 206 if ( $this-> debug) {224 if ( $this->log_invalidation_params ) { 207 225 error_log( '===== C3 CRON Job registration [COMPLETE] ===' ); 208 226 } … … 255 273 } 256 274 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 ) ) ) { 258 276 error_log( 'C3 Invalidation Started - Query: ' . print_r( $query, true ) ); 259 277 error_log( 'C3 Invalidation Started - Force: ' . ( $force ? 'true' : 'false' ) ); … … 276 294 $result = $this->cf_service->create_invalidation( $query ); 277 295 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 286 296 if ( is_wp_error( $result ) ) { 297 error_log( 'C3 Invalidation Failed: ' . $result->get_error_message() ); 287 298 return $result; 288 299 } … … 377 388 378 389 // デバッグログを追加 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 ) ) { 380 391 error_log( 'C3 Invalidation Logs Result: ' . print_r( $histories, true ) ); 381 392 } … … 406 417 407 418 /** 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. 409 437 */ 410 438 public function handle_invalidation_details_ajax() { … … 430 458 wp_send_json_success( $details ); 431 459 } 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 } 432 508 } -
c3-cloudfront-clear-cache/tags/7.3.0/classes/WP/Post_Service.php
r3309497 r3366827 21 21 class Post_Service { 22 22 /** 23 * Get the target post ids23 * Retrieve posts matching the provided post IDs. 24 24 * 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). 26 30 */ 27 31 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 28 45 $query = new \WP_Query( 29 46 array( 30 47 '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, 31 53 ) 32 54 ); -
c3-cloudfront-clear-cache/tags/7.3.0/readme.txt
r3346059 r3366827 5 5 Requires at least: 4.9.0 6 6 Tested up to: 6.8.1 7 Stable tag: 7. 2.07 Stable tag: 7.3.0 8 8 License: GPLv3 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-3.0.html -
c3-cloudfront-clear-cache/trunk/c3-cloudfront-clear-cache.php
r3346059 r3366827 2 2 /** 3 3 * Plugin Name: C3 Cloudfront Cache Controller 4 * Version: 7. 2.04 * Version: 7.3.0 5 5 * Plugin URI:https://github.com/amimoto-ami/c3-cloudfront-clear-cache 6 6 * Description: Manage CloudFront Cache and provide some fixtures. … … 29 29 new C3_CloudFront_Cache_Controller\Settings_Service(); 30 30 new C3_CloudFront_Cache_Controller\Views\Settings(); 31 new C3_CloudFront_Cache_Controller\Views\Debug_Settings(); 31 32 new WP\Fixtures(); 32 33 } -
c3-cloudfront-clear-cache/trunk/classes/AWS/CloudFront_Service.php
r3337531 r3366827 14 14 15 15 use C3_CloudFront_Cache_Controller\WP; 16 use C3_CloudFront_Cache_Controller\Constants; 16 17 17 18 /** … … 251 252 $paths = $params['InvalidationBatch']['Paths']['Items']; 252 253 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 ) ) ) { 254 255 error_log( 'C3 CloudFront Invalidation Request - Distribution ID: ' . $distribution_id ); 255 256 error_log( 'C3 CloudFront Invalidation Request - Paths: ' . print_r( $paths, true ) ); … … 316 317 if ( isset( $result['Quantity'] ) && $result['Quantity'] > 0 && isset( $result['Items']['InvalidationSummary'] ) ) { 317 318 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; 319 329 } 320 330 … … 369 379 } 370 380 } 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 } 371 393 } -
c3-cloudfront-clear-cache/trunk/classes/AWS/EC2_Metadata_Service.php
r3336321 r3366827 174 174 */ 175 175 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 176 194 $response = wp_remote_request( 177 195 $this->metadata_endpoint . '/latest/meta-data/', … … 181 199 ) 182 200 ); 183 184 201 return ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200; 185 202 } -
c3-cloudfront-clear-cache/trunk/classes/Constants.php
r3309497 r3366827 37 37 const SECRET_KEY = 'secret_key'; 38 38 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 39 43 /** 40 44 * Get Plugin text_domain -
c3-cloudfront-clear-cache/trunk/classes/Cron_Service.php
r3336321 r3366827 9 9 10 10 namespace C3_CloudFront_Cache_Controller; 11 use C3_CloudFront_Cache_Controller\Constants; 11 12 if ( ! defined( 'ABSPATH' ) ) { 12 13 exit; … … 35 36 36 37 /** 37 * Debugflag38 * Log cron register task flag 38 39 * 39 40 * @var boolean 40 41 */ 41 private $ debug;42 private $log_cron_register_task; 42 43 43 44 /** … … 76 77 ) 77 78 ); 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 ) ); 79 80 } 80 81 … … 85 86 */ 86 87 public function run_schedule_invalidate() { 87 if ( $this-> debug) {88 if ( $this->log_cron_register_task ) { 88 89 error_log( '===== C3 Invalidation cron is started ===' ); 89 90 } 90 91 if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) { 91 if ( $this-> debug) {92 if ( $this->log_cron_register_task ) { 92 93 error_log( '===== C3 Invalidation cron has been SKIPPED [Disabled] ===' ); 93 94 } … … 95 96 } 96 97 $invalidation_batch = $this->transient_service->load_invalidation_query(); 97 if ( $this-> debug) {98 if ( $this->log_cron_register_task ) { 98 99 error_log( print_r( $invalidation_batch, true ) ); 99 100 } 100 101 if ( ! $invalidation_batch || empty( $invalidation_batch ) ) { 101 if ( $this-> debug) {102 if ( $this->log_cron_register_task ) { 102 103 error_log( '===== C3 Invalidation cron has been SKIPPED [No Target Item] ===' ); 103 104 } … … 109 110 'InvalidationBatch' => $invalidation_batch, 110 111 ); 111 if ( $this-> debug) {112 if ( $this->log_cron_register_task ) { 112 113 error_log( print_r( $query, true ) ); 113 114 } … … 117 118 */ 118 119 $result = $this->cf_service->create_invalidation( $query ); 119 if ( $this-> debug) {120 if ( $this->log_cron_register_task ) { 120 121 if ( is_wp_error( $result ) ) { 121 122 error_log( 'C3 Cron: Invalidation failed: ' . $result->get_error_message() ); … … 125 126 } 126 127 $this->transient_service->delete_invalidation_query(); 127 if ( $this-> debug) {128 if ( $this->log_cron_register_task ) { 128 129 error_log( '===== C3 Invalidation cron has been COMPLETED ===' ); 129 130 } 130 131 return true; 131 132 } 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 } 132 145 } -
c3-cloudfront-clear-cache/trunk/classes/Invalidation_Service.php
r3337531 r3366827 10 10 namespace C3_CloudFront_Cache_Controller; 11 11 use C3_CloudFront_Cache_Controller\WP\Post_Service; 12 use C3_CloudFront_Cache_Controller\Constants; 12 13 if ( ! defined( 'ABSPATH' ) ) { 13 14 exit; … … 64 65 65 66 /** 66 * Debugflag67 * Log invalidation parameters flag 67 68 * 68 69 * @var boolean 69 70 */ 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. 76 85 */ 77 86 function __construct( ...$args ) { … … 110 119 ); 111 120 $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( 112 130 'admin_init', 113 131 array( … … 123 141 ) 124 142 ); 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 ) ); 126 144 } 127 145 … … 179 197 */ 180 198 public function register_cron_event( $query ) { 181 if ( $this-> debug) {199 if ( $this->log_invalidation_params ) { 182 200 error_log( '===== C3 CRON Job registration [START] ===' ); 183 201 } 184 202 if ( ! isset( $query['Paths'] ) || ! isset( $query['Paths']['Items'] ) || $query['Paths']['Items'][0] === '/*' ) { 185 if ( $this-> debug) {203 if ( $this->log_invalidation_params ) { 186 204 error_log( '===== C3 CRON Job registration [SKIP | NO ITEM] ===' ); 187 205 } … … 189 207 } 190 208 if ( $this->hook_service->apply_filters( 'c3_disabled_cron_retry', false ) ) { 191 if ( $this-> debug) {209 if ( $this->log_invalidation_params ) { 192 210 error_log( '===== C3 CRON Job registration [SKIP | DISABLED] ===' ); 193 211 } … … 198 216 $interval_minutes = $this->hook_service->apply_filters( 'c3_invalidation_cron_interval', 1 ); 199 217 $time = time() + MINUTE_IN_SECONDS * $interval_minutes; 200 if ( $this-> debug) {218 if ( $this->log_invalidation_params ) { 201 219 error_log( print_r( $query, true ) ); 202 220 } … … 204 222 $result = wp_schedule_single_event( $time, 'c3_cron_invalidation' ); 205 223 206 if ( $this-> debug) {224 if ( $this->log_invalidation_params ) { 207 225 error_log( '===== C3 CRON Job registration [COMPLETE] ===' ); 208 226 } … … 255 273 } 256 274 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 ) ) ) { 258 276 error_log( 'C3 Invalidation Started - Query: ' . print_r( $query, true ) ); 259 277 error_log( 'C3 Invalidation Started - Force: ' . ( $force ? 'true' : 'false' ) ); … … 276 294 $result = $this->cf_service->create_invalidation( $query ); 277 295 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 286 296 if ( is_wp_error( $result ) ) { 297 error_log( 'C3 Invalidation Failed: ' . $result->get_error_message() ); 287 298 return $result; 288 299 } … … 377 388 378 389 // デバッグログを追加 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 ) ) { 380 391 error_log( 'C3 Invalidation Logs Result: ' . print_r( $histories, true ) ); 381 392 } … … 406 417 407 418 /** 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. 409 437 */ 410 438 public function handle_invalidation_details_ajax() { … … 430 458 wp_send_json_success( $details ); 431 459 } 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 } 432 508 } -
c3-cloudfront-clear-cache/trunk/classes/WP/Post_Service.php
r3309497 r3366827 21 21 class Post_Service { 22 22 /** 23 * Get the target post ids23 * Retrieve posts matching the provided post IDs. 24 24 * 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). 26 30 */ 27 31 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 28 45 $query = new \WP_Query( 29 46 array( 30 47 '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, 31 53 ) 32 54 ); -
c3-cloudfront-clear-cache/trunk/readme.txt
r3346059 r3366827 5 5 Requires at least: 4.9.0 6 6 Tested up to: 6.8.1 7 Stable tag: 7. 2.07 Stable tag: 7.3.0 8 8 License: GPLv3 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-3.0.html
Note: See TracChangeset
for help on using the changeset viewer.