Skip to content

Commit 09a80b4

Browse files
authored
Merge pull request #18 from john-shaffer/17-add-cache-control-setting
Add Cache-Control Setting
2 parents 2f654db + 1f3ecdd commit 09a80b4

3 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/Controller.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ public static function seedOptions() : void {
175175
);
176176

177177
$wpdb->query( $query );
178+
179+
$query = $wpdb->prepare(
180+
$query_string,
181+
's3CacheControl',
182+
'public, max-age=900',
183+
'Cache-Control header value',
184+
''
185+
);
186+
187+
$wpdb->query( $query );
178188
}
179189

180190
/**
@@ -394,6 +404,12 @@ public static function saveOptionsFromUI() : void {
394404
[ 'name' => 's3RemotePath' ]
395405
);
396406

407+
$wpdb->update(
408+
$table_name,
409+
[ 'value' => sanitize_text_field( $_POST['s3CacheControl'] ) ],
410+
[ 'name' => 's3CacheControl' ]
411+
);
412+
397413
wp_safe_redirect( admin_url( 'admin.php?page=wp2static-s3' ) );
398414
exit;
399415
}

src/Deployer.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public function upload_files( string $processed_site_path ) : void {
3535
)
3636
);
3737

38+
$put_data = [
39+
'Bucket' => Controller::getValue( 's3Bucket' ),
40+
'ACL' => 'public-read',
41+
];
42+
43+
$cache_control = Controller::getValue( 's3CacheControl' );
44+
if ( $cache_control ) {
45+
$put_data['CacheControl'] = $cache_control;
46+
}
47+
3848
foreach ( $iterator as $filename => $file_object ) {
3949
$base_name = basename( $filename );
4050
if ( $base_name != '.' && $base_name != '..' ) {
@@ -69,15 +79,11 @@ public function upload_files( string $processed_site_path ) : void {
6979

7080
$mime_type = MimeTypes::GuessMimeType( $filename );
7181

72-
$result = $s3->putObject(
73-
[
74-
'Bucket' => Controller::getValue( 's3Bucket' ),
75-
'Key' => $s3_key,
76-
'Body' => file_get_contents( $filename ),
77-
'ACL' => 'public-read',
78-
'ContentType' => $mime_type,
79-
]
80-
);
82+
$put_data['Key'] = $s3_key;
83+
$put_data['Body'] = file_get_contents( $filename );
84+
$put_data['ContentType'] = $mime_type;
85+
86+
$result = $s3->putObject( $put_data );
8187

8288
if ( $result['@metadata']['statusCode'] === 200 ) {
8389
\WP2Static\DeployCache::addFile( $cache_key );

views/s3-page.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@
111111
</td>
112112
</tr>
113113

114+
<tr>
115+
<td style="width:50%;">
116+
<label
117+
for="<?php echo $view['options']['s3CacheControl']->name; ?>"
118+
><?php echo $view['options']['s3CacheControl']->label; ?></label>
119+
</td>
120+
<td>
121+
<input
122+
id="<?php echo $view['options']['s3CacheControl']->name; ?>"
123+
name="<?php echo $view['options']['s3CacheControl']->name; ?>"
124+
type="text"
125+
value="<?php echo $view['options']['s3CacheControl']->value !== '' ? $view['options']['s3CacheControl']->value : ''; ?>"
126+
/>
127+
</td>
128+
</tr>
129+
114130
</tbody>
115131
</table>
116132

0 commit comments

Comments
 (0)