1111
1212class Deployer {
1313
14+ const DEFAULT_NAMESPACE = 'wp2static-addon-s3/default ' ;
15+
1416 // prepare deploy, if modifies URL structure, should be an action
1517 // $this->prepareDeploy();
1618
@@ -24,6 +26,8 @@ public function upload_files( string $processed_site_path ) : void {
2426 return ;
2527 }
2628
29+ $ namespace = self ::DEFAULT_NAMESPACE ;
30+
2731 // instantiate S3 client
2832 $ s3 = self ::s3_client ();
2933
@@ -45,6 +49,8 @@ public function upload_files( string $processed_site_path ) : void {
4549 $ put_data ['CacheControl ' ] = $ cache_control ;
4650 }
4751
52+ $ base_put_data = $ put_data ;
53+
4854 $ cf_max_paths = Controller::getValue ( 'cfMaxPathsToInvalidate ' );
4955 $ cf_max_paths = $ cf_max_paths ? intval ( $ cf_max_paths ) : 0 ;
5056 $ cf_stale_paths = [];
@@ -58,7 +64,7 @@ public function upload_files( string $processed_site_path ) : void {
5864
5965 $ cache_key = str_replace ( $ processed_site_path , '' , $ filename );
6066
61- if ( \WP2Static \DeployCache::fileisCached ( $ cache_key ) ) {
67+ if ( \WP2Static \DeployCache::fileisCached ( $ cache_key, $ namespace ) ) {
6268 continue ;
6369 }
6470
@@ -93,7 +99,7 @@ public function upload_files( string $processed_site_path ) : void {
9399 $ result = $ s3 ->putObject ( $ put_data );
94100
95101 if ( $ result ['@metadata ' ]['statusCode ' ] === 200 ) {
96- \WP2Static \DeployCache::addFile ( $ cache_key );
102+ \WP2Static \DeployCache::addFile ( $ cache_key, $ namespace );
97103
98104 if ( $ cf_max_paths >= count ( $ cf_stale_paths ) ) {
99105 $ cf_key = $ cache_key ;
@@ -106,6 +112,53 @@ public function upload_files( string $processed_site_path ) : void {
106112 }
107113 }
108114
115+ // Deploy 301 redirects.
116+
117+ $ put_data = $ base_put_data ;
118+ $ redirects = apply_filters ( 'wp2static_list_redirects ' , [] );
119+
120+ foreach ( $ redirects as $ redirect ) {
121+ $ file_hash = md5 ( '301 ' . $ redirect ['redirect_to ' ] );
122+ $ cache_key = $ redirect ['url ' ];
123+
124+ if ( mb_substr ( $ cache_key , -1 ) === '/ ' ) {
125+ $ cache_key = $ cache_key . 'index.html ' ;
126+ }
127+
128+ $ is_cached = \WP2Static \DeployCache::fileisCached (
129+ $ cache_key ,
130+ $ namespace ,
131+ $ file_hash
132+ );
133+
134+ if ( $ is_cached ) {
135+ continue ;
136+ }
137+
138+ $ s3_key =
139+ Controller::getValue ( 's3RemotePath ' ) ?
140+ Controller::getValue ( 's3RemotePath ' ) . '/ ' .
141+ ltrim ( $ cache_key , '/ ' ) :
142+ ltrim ( $ cache_key , '/ ' );
143+
144+ $ put_data ['Key ' ] = $ s3_key ;
145+ $ put_data ['WebsiteRedirectLocation ' ] = $ redirect ['redirect_to ' ];
146+
147+ $ result = $ s3 ->putObject ( $ put_data );
148+
149+ if ( $ result ['@metadata ' ]['statusCode ' ] === 200 ) {
150+ \WP2Static \DeployCache::addFile ( $ cache_key , $ namespace , $ file_hash );
151+
152+ if ( $ cf_max_paths >= count ( $ cf_stale_paths ) ) {
153+ $ cf_key = $ cache_key ;
154+ if ( 0 === substr_compare ( $ cf_key , '/index.html ' , -11 ) ) {
155+ $ cf_key = substr ( $ cf_key , 0 , -10 );
156+ }
157+ array_push ( $ cf_stale_paths , $ cf_key );
158+ }
159+ }
160+ }
161+
109162 $ distribution_id = Controller::getValue ( 'cfDistributionID ' );
110163 $ num_stale = count ( $ cf_stale_paths );
111164 if ( $ distribution_id && $ num_stale > 0 ) {
0 commit comments