• Resolved Mike Wiegand

    (@mikeg_de)


    Hi there,

    I have put a CDN in front of WordPress to leverage AWS WAF, Origin Shield and a few other features. To invalidate the CDN I used this function, credits to https://gist.github.com/claylo/1009169, which does its job but causes unwanted side effects during save process.

    function albCdnFlush($post_ID, $new_status, $old_status)  {
    	if ( $new_status === "publish" || $old_status === "publish" ) {
    		$access_key = 'MYKACCESSEY';
    		$secret_key = 'MYSECRETKEY';
    		$distribution = 'DISTRIBUTION-ID';
    		$epoch = date('U');
    
    		$xml = <<<EOD
    			<InvalidationBatch>
    			<Path>/*</Path>
    			<CallerReference>{$distribution}{$epoch}</CallerReference>
    			</InvalidationBatch>
    			EOD;
    
    		/**
    		 * You probably don't need to change anything below here.
    		 */
    		$len = strlen($xml);
    		$date = gmdate('D, d M Y G:i:s T');
    		$sig = base64_encode(
    			hash_hmac('sha1', $date, $secret_key, true)
    		);
    
    		$msg = "POST /2010-11-01/distribution/{$distribution}/invalidation HTTP/1.0\r\n";
    		$msg .= "Host: cloudfront.amazonaws.com\r\n";
    		$msg .= "Date: {$date}\r\n";
    		$msg .= "Content-Type: text/xml; charset=UTF-8\r\n";
    		$msg .= "Authorization: AWS {$access_key}:{$sig}\r\n";
    		$msg .= "Content-Length: {$len}\r\n\r\n";
    		$msg .= $xml;
    
    		$fp = fsockopen('ssl://cloudfront.amazonaws.com', 443, 
    		 $errno, $errstr, 30
    		);
    		if (!$fp) {
    			die("Connection failed: {$errno} {$errstr}\n");
    		}
    		fwrite($fp, $msg);
    		$resp = '';
    		while(! feof($fp)) {
    			$resp .= fgets($fp, 1024);
    		}
    		fclose($fp);
    		echo $resp;
    	}
    }
    add_action(  'transition_post_status',  'albCdnFlush', 10, 3 );

    I wonder, though, if I the existing W3Total Cache process(es) can be used but with different credentials, respectively another CDN ID?

    Many thanks in advance
    Mike

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @mikeg_de

    Thank you for your inquiry and I am happy to assist youj with this.
    Just to confirm, you are asking if you can set up the AWS CDN with W3 Total Cache and use this function for invalidations?
    Thanks!

    Thread Starter Mike Wiegand

    (@mikeg_de)

    Hi Marco,

    sort of and thanks for your fast reply. I already have a CDN for asset delivery which is configured with W3TC. In addition to the asset CDN I use another CDN in front of the server and I wondered if there is a way to hook into the W3TC CDN flush processes to automatically invalidate the additional CDN too.

    Best
    Mike

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @mikeg_de

    Thank you for the clarification.
    If you want to purge an additional CDN, then you can use an action, if there is one fired on purge.
    wp-cotent/plugins/w3-total-cache/CacheFlush_Locally.php#L116
    do_action( 'w3tc_cdn_purge_all_after' );
    or for a set of files
    wp-cotent/plugins/w3-total-cache/CacheFlush_Locally.php#L134
    do_action( 'w3tc_cdn_purge_files_after', $purgefiles );
    I hope this helps!
    Thanks!

    Thread Starter Mike Wiegand

    (@mikeg_de)

    Hi @vmarko,

    Thanks for the suggestion which sounds feasible. Do you know if / how I can overwrite the AWS CloudFront ID with the one of the additional CDN?

    Best
    Mike

    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @mikeg_de

    You are welcome!
    Not possible via W3 Total Cache.
    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Trigger purge of an additional CDN’ is closed to new replies.