Tagged: AWS CDN, W3 Total Cache
- AuthorPosts
Todd Baur
GuestHi,
I patched your plugin so our IAM profiles would work correctly with existing Cloudfront distributions and using IAM profiles. Here is the patch:
diff –git a/wordpress/wp-content/plugins/w3-total-cache/CdnEngine_Mirror_CloudFront.php b/wordpress/wp-content/plugins/w3-total-cache/CdnEngine_Mirror_CloudFront.php
index 5841e15..ea18b01 100644
— a/wordpress/wp-content/plugins/w3-total-cache/CdnEngine_Mirror_CloudFront.php
+++ b/wordpress/wp-content/plugins/w3-total-cache/CdnEngine_Mirror_CloudFront.php
@@ -50,13 +50,15 @@ class CdnEngine_Mirror_CloudFront extends CdnEngine_Mirror {
return;
}– if ( empty( $this->_config[‘key’] ) && empty( $this->_config[‘secret’] ) ) {
– $credentials = \Aws\Credentials\CredentialProvider::defaultProvider();
– } else {
+ // Only use static credentials if BOTH key and secret are set and non-empty
+ if ( !empty( $this->_config[‘key’] ) && !empty( $this->_config[‘secret’] ) ) {
$credentials = new \Aws\Credentials\Credentials(
$this->_config[‘key’],
$this->_config[‘secret’]
);
+ } else {
+ // Use the default provider chain (supports IAM roles, env vars, etc)
+ $credentials = \Aws\Credentials\CredentialProvider::defaultProvider();
}$this->api = new \Aws\CloudFront\CloudFrontClient(
@@ -172,7 +174,7 @@ class CdnEngine_Mirror_CloudFront extends CdnEngine_Mirror {
*/
public function test( &$error ) {
$this->_init();
–
+ $origin = $this->_get_origin();
/**
* Search active CF distribution
*/
@@ -385,10 +387,25 @@ class CdnEngine_Mirror_CloudFront extends CdnEngine_Mirror {
throw new \Exception( \esc_html__( ‘No distributions found.’, ‘w3-total-cache’ ) );
}– $dist = false;
+ $hosted_domain = isset( $_SERVER[‘HTTP_HOST’] ) ? strtolower( $_SERVER[‘HTTP_HOST’] ) : ”;
$origin = $this->_get_origin();$items = $dists[‘DistributionList’][‘Items’];
+ foreach ( $items as $dist ) {
+ // 1. Match on CNAME/Alias
+ if ( isset( $dist[‘Aliases’][‘Items’] ) ) {
+ foreach ( $dist[‘Aliases’][‘Items’] as $alias ) {
+ if ( strtolower( $alias ) === $hosted_domain ) {
+ return $dist;
+ }
+ }
+ }
+ // 2. Match on CloudFront domain
+ if ( isset( $dist[‘DomainName’] ) && strtolower( $dist[‘DomainName’] ) === $hosted_domain ) {
+ return $dist;
+ }
+ }
+ // 3. Fallback: match on origin (legacy logic)
foreach ( $items as $dist ) {
if ( isset( $dist[‘Origins’][‘Items’] ) ) {
foreach ( $dist[‘Origins’][‘Items’] as $o ) {
@@ -402,9 +419,9 @@ class CdnEngine_Mirror_CloudFront extends CdnEngine_Mirror {
throw new \Exception(
\esc_html(
sprintf(
– // Translators: 1 Origin name.
– \__( ‘Distribution for origin “%1$s” not found.’, ‘w3-total-cache’ ),
– $origin
+ // Translators: 1 Hosted domain name.
+ \__( ‘Distribution for hosted domain “%1$s” not found.’, ‘w3-total-cache’ ),
+ $hosted_domain
)
)
);Marko Vasiljevic
KeymasterHello Todd,
Thank you for reaching out, and I am happy to help.
If convenient, please create a pull request in the W3 Total Cache GitHub so the devs can check this and review this?Let me know so I can share this with the team
Thanks!
- AuthorPosts