@@ -49,6 +49,8 @@ public function upload_files( string $processed_site_path ) : void {
4949 $ put_data ['CacheControl ' ] = $ cache_control ;
5050 }
5151
52+ $ base_put_data = $ put_data ;
53+
5254 $ cf_max_paths = Controller::getValue ( 'cfMaxPathsToInvalidate ' );
5355 $ cf_max_paths = $ cf_max_paths ? intval ( $ cf_max_paths ) : 0 ;
5456 $ cf_stale_paths = [];
@@ -110,6 +112,53 @@ public function upload_files( string $processed_site_path ) : void {
110112 }
111113 }
112114
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+
113162 $ distribution_id = Controller::getValue ( 'cfDistributionID ' );
114163 $ num_stale = count ( $ cf_stale_paths );
115164 if ( $ distribution_id && $ num_stale > 0 ) {
0 commit comments