@@ -45,6 +45,10 @@ public function upload_files( string $processed_site_path ) : void {
4545 $ put_data ['CacheControl ' ] = $ cache_control ;
4646 }
4747
48+ $ cf_max_paths = Controller::getValue ( 'cfMaxPathsToInvalidate ' );
49+ $ cf_max_paths = $ cf_max_paths ? intval ( $ cf_max_paths ) : 0 ;
50+ $ cf_stale_paths = [];
51+
4852 foreach ( $ iterator as $ filename => $ file_object ) {
4953 $ base_name = basename ( $ filename );
5054 if ( $ base_name != '. ' && $ base_name != '.. ' ) {
@@ -87,12 +91,32 @@ public function upload_files( string $processed_site_path ) : void {
8791
8892 if ( $ result ['@metadata ' ]['statusCode ' ] === 200 ) {
8993 \WP2Static \DeployCache::addFile ( $ cache_key );
94+
95+ if ( $ cf_max_paths >= count ( $ cf_stale_paths ) ) {
96+ $ cf_key = $ cache_key ;
97+ if ( 0 === substr_compare ( $ cf_key , '/index.html ' , -11 ) ) {
98+ $ cf_key = substr ( $ cf_key , 0 , -10 );
99+ }
100+ array_push ( $ cf_stale_paths , $ cf_key );
101+ }
90102 }
91103 }
92104 }
105+
106+ $ distribution_id = Controller::getValue ( 'cfDistributionID ' );
107+ $ num_stale = count ( $ cf_stale_paths );
108+ if ( $ distribution_id && $ num_stale > 0 ) {
109+ if ( $ num_stale > $ cf_max_paths ) {
110+ self ::cloudfront_invalidate_all_items ();
111+ } else {
112+ $ path_text = ( $ num_stale === 1 ) ? 'path ' : 'paths ' ;
113+ \WP2Static \WsLog::l ( "Invalidating $ num_stale CloudFront $ path_text " );
114+ self ::create_invalidation ( $ distribution_id , $ cf_stale_paths );
115+ }
116+ }
93117 }
94118
95- public function s3_client () : \Aws \S3 \S3Client {
119+ public static function s3_client () : \Aws \S3 \S3Client {
96120 $ client_options = [
97121 'version ' => 'latest ' ,
98122 'region ' => Controller::getValue ( 's3Region ' ),
@@ -124,7 +148,7 @@ public function s3_client() : \Aws\S3\S3Client {
124148 return new \Aws \S3 \S3Client ( $ client_options );
125149 }
126150
127- public function cloudfront_client () : \Aws \CloudFront \CloudFrontClient {
151+ public static function cloudfront_client () : \Aws \CloudFront \CloudFrontClient {
128152 /*
129153 If no credentials option, SDK attempts to load credentials from
130154 your environment in the following order:
@@ -173,29 +197,34 @@ public function cloudfront_client() : \Aws\CloudFront\CloudFrontClient {
173197 return $ client ;
174198 }
175199
176- public function cloudfront_invalidate_all_items () : void {
200+ public static function create_invalidation ( string $ distribution_id , array $ items )
201+ : string {
202+ $ client = self ::cloudfront_client ();
203+
204+ return $ client ->createInvalidation (
205+ [
206+ 'DistributionId ' => $ distribution_id ,
207+ 'InvalidationBatch ' => [
208+ 'CallerReference ' => 'WP2Static S3 Add-on ' . time (),
209+ 'Paths ' => [
210+ 'Items ' => $ items ,
211+ 'Quantity ' => count ( $ items ),
212+ ],
213+ ],
214+ ]
215+ );
216+ }
217+
218+ public static function cloudfront_invalidate_all_items () : void {
177219 if ( ! Controller::getValue ( 'cfDistributionID ' ) ) {
178220 return ;
179221 }
180222
181- \WP2Static \WsLog::l ( 'Invalidating all CloudFront items ' );
182-
183- $ client = self ::cloudfront_client ();
223+ \WP2Static \WsLog::l ( 'Invalidating all CloudFront paths ' );
184224
185225 try {
186- $ result = $ client ->createInvalidation (
187- [
188- 'DistributionId ' => Controller::getValue ( 'cfDistributionID ' ),
189- 'InvalidationBatch ' => [
190- 'CallerReference ' => 'WP2Static S3 Add-on ' . time (),
191- 'Paths ' => [
192- 'Items ' => [ '/* ' ],
193- 'Quantity ' => 1 ,
194- ],
195- ],
196- ]
197- );
198-
226+ self ::create_invalidation ( Controller::getValue ( 'cfDistributionID ' ),
227+ [ '/* ' ]);
199228 } catch ( AwsException $ e ) {
200229 // output error message if fails
201230 error_log ( $ e ->getMessage () );
0 commit comments