Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Cdn_TotalCdn_Auto_Configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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' ),
);
}

/**
Expand Down
55 changes: 52 additions & 3 deletions Generic_Plugin_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,44 @@ 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'] ) ) {
$data['response_errors'][] = 'cdn_totalcdn_fsd_custom_hostname_failed';
}
}

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'] ) ) {
Expand All @@ -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
*
Expand Down Expand Up @@ -1160,6 +1208,7 @@ public function admin_notices() {
'<acronym title="' . esc_attr__( 'Content Delivery Network', 'w3-total-cache' ) . '">' . esc_html__( 'CDN', 'w3-total-cache' ) . '</acronym>'
),
'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(),
Expand Down