diff --git a/Cdn_TotalCdn_Auto_Configure.php b/Cdn_TotalCdn_Auto_Configure.php index 19547417d..b19d74824 100644 --- a/Cdn_TotalCdn_Auto_Configure.php +++ b/Cdn_TotalCdn_Auto_Configure.php @@ -151,9 +151,11 @@ public function w3tc_totalcdn_auto_configured( $applied ): bool { * * @since x.x.x * + * @param bool $enable Whether to enable the CDN after configuration. + * * @return array */ - public function run(): array { + public function run( bool $enable = true ): array { // Check and verify that the account API key is set. $api_key_result = $this->check_api_key(); @@ -169,7 +171,14 @@ public function run(): array { } // Enable the CDN and return the result. - return $this->enable_cdn(); + if ( $enable ) { + return $this->enable_cdn(); + } + + return array( + 'success' => true, + 'message' => \__( 'Pull zone created successfully.', 'w3-total-cache' ), + ); } /** diff --git a/Generic_Plugin_Admin.php b/Generic_Plugin_Admin.php index b5a197466..79c9e2973 100644 --- a/Generic_Plugin_Admin.php +++ b/Generic_Plugin_Admin.php @@ -196,13 +196,36 @@ public function w3tc_save_options( $data ) { wp_clear_scheduled_hook( 'w3tc_purge_all_wpcron' ); } - if ( Cdn_TotalCdn_CustomHostname::should_remove_on_save( $new_config, $old_config ) ) { + $skip_totalcdn_fsd_operations = false; + + if ( $this->should_auto_configure_totalcdn_fsd( $new_config ) ) { + $auto_configurator = new Cdn_TotalCdn_Auto_Configure( $new_config ); + $auto_result = $auto_configurator->run( false ); + + if ( false === $auto_result['success'] ) { + $data['response_errors'][] = 'cdn_totalcdn_fsd_auto_config_failed'; + $skip_totalcdn_fsd_operations = true; + + if ( $old_config ) { + $new_config->set( 'cdnfsd.enabled', $old_config->get_boolean( 'cdnfsd.enabled' ) ); + $new_config->set( 'cdnfsd.engine', $old_config->get_string( 'cdnfsd.engine' ) ); + } else { + $new_config->set( 'cdnfsd.enabled', false ); + + if ( 'totalcdn' === $new_config->get_string( 'cdnfsd.engine' ) ) { + $new_config->set( 'cdnfsd.engine', '' ); + } + } + } + } + + if ( ! $skip_totalcdn_fsd_operations && Cdn_TotalCdn_CustomHostname::should_remove_on_save( $new_config, $old_config ) ) { $result = Cdn_TotalCdn_CustomHostname::remove( $new_config, $old_config ); if ( empty( $result['success'] ) && empty( $result['skipped'] ) ) { $data['response_errors'][] = 'cdn_totalcdn_fsd_custom_hostname_remove_failed'; } - } elseif ( Cdn_TotalCdn_CustomHostname::should_attempt_on_save( $new_config, $old_config ) ) { + } elseif ( ! $skip_totalcdn_fsd_operations && Cdn_TotalCdn_CustomHostname::should_attempt_on_save( $new_config, $old_config ) ) { $result = Cdn_TotalCdn_CustomHostname::ensure( $new_config ); if ( empty( $result['success'] ) && empty( $result['skipped'] ) ) { @@ -210,7 +233,7 @@ public function w3tc_save_options( $data ) { } } - if ( Cdn_TotalCdn_Fsd_Origin::should_update_on_save( $new_config, $old_config ) ) { + if ( ! $skip_totalcdn_fsd_operations && Cdn_TotalCdn_Fsd_Origin::should_update_on_save( $new_config, $old_config ) ) { $result = Cdn_TotalCdn_Fsd_Origin::ensure( $new_config ); if ( empty( $result['success'] ) && empty( $result['skipped'] ) ) { @@ -221,6 +244,31 @@ public function w3tc_save_options( $data ) { return $data; } + /** + * Determines if Total CDN FSD should trigger auto-configuration. + * + * @param Config $new_config New configuration values. + * + * @since X.X.X + * + * @return bool True when auto-configuration should be attempted. + */ + private function should_auto_configure_totalcdn_fsd( Config $new_config ): bool { + if ( ! $new_config->get_boolean( 'cdnfsd.enabled' ) ) { + return false; + } + + if ( 'totalcdn' !== $new_config->get_string( 'cdnfsd.engine' ) ) { + return false; + } + + if ( $new_config->get_integer( 'cdn.totalcdn.pull_zone_id' ) > 0 ) { + return false; + } + + return true; + } + /** * Load action * @@ -1160,6 +1208,7 @@ public function admin_notices() { '' . esc_html__( 'CDN', 'w3-total-cache' ) . '' ), 'updated_pullzone_url' => __( 'Pull Zone URL could not be automatically updated. Please contact support for assistance.', 'w3-total-cache' ), + 'cdn_totalcdn_fsd_auto_config_failed' => __( 'Unable to provision a Total CDN pull zone for Full Site Delivery. Please verify your API key or contact support.', 'w3-total-cache' ), 'cdn_totalcdn_fsd_origin_update_failed' => __( 'Unable to update the Total CDN origin for Full Site Delivery. Please contact support for assistance.', 'w3-total-cache' ), 'cdn_totalcdn_fsd_custom_hostname_remove_failed' => Cdn_TotalCdn_CustomHostname::removal_failure_message(), 'cdn_totalcdn_fsd_custom_hostname_failed' => Cdn_TotalCdn_CustomHostname::failure_message(),