Thanks to Martin Lindkvist for S3::invalidateDistribution()
/** * Invalidates all cloudfront paths stored in `$pathsToInvalidate`. Needs to * invalidate everything in a single batch request otherwise cloudfront limits * are hit (3 active invalidation requests per distribution) * * Called on each wp_redirect since that's the only way to capture multiple * delete calls in a single request (i.e., through the bulk actions) * * @param integer $postId The original post ID */ function invalidateCloudfrontPosts($url) { if (empty($pathsToInvalidate)) { return $url; } $s3Key = get_option('s3_access_key'); $s3KeySecret = get_option('s3_access_secret'); $S3 = new S3($s3Key, $s3KeySecret); // invalidate cloudfront cache (if applicable) $downloadDomain = get_option('s3_download'); if (!empty($downloadDomain)) { $distributions = $S3->listDistributions(); // get matching distribution id foreach ($distributions as $distributionId => $distributionDetails) { if ($distributionDetails['domain'] === $downloadDomain) { $S3->invalidateDistribution($distributionId, $this->pathsToInvalidate); break; } } } return $url; }
/** * Invalidates paths in a cloudfront distribution * * @param $paths * @return bool */ public function invalidateCloudfrontPaths($paths) { $result = \S3::invalidateDistribution(craft()->imager->getSetting('cloudfrontDistributionId'), $paths); return $result; }