Skip to content

Commit b65f2c7

Browse files
authored
Merge pull request #632 from humanmade/backport-564-to-v3-branch
[Backport v3-branch] Added `get_s3_path` function instead of hard coding s3 path.
2 parents 1f3a7a2 + fc88fbd commit b65f2c7

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

inc/class-plugin.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ public function register_stream_wrapper() {
154154
stream_context_set_option( stream_context_get_default(), 's3', 'seekable', true );
155155
}
156156

157+
/**
158+
* Get the s3:// path for the bucket.
159+
*/
160+
public function get_s3_path() {
161+
return 's3://' . $this->bucket;
162+
}
163+
157164
/**
158165
* Overwrite the default wp_upload_dir.
159166
*
@@ -163,19 +170,20 @@ public function register_stream_wrapper() {
163170
public function filter_upload_dir( array $dirs ) : array {
164171

165172
$this->original_upload_dir = $dirs;
173+
$s3_path = $this->get_s3_path();
166174

167-
$dirs['path'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['path'] );
168-
$dirs['basedir'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['basedir'] );
175+
$dirs['path'] = str_replace( WP_CONTENT_DIR, $s3_path, $dirs['path'] );
176+
$dirs['basedir'] = str_replace( WP_CONTENT_DIR, $s3_path, $dirs['basedir'] );
169177

170178
if ( ! defined( 'S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL' ) || ! S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL ) {
171179

172180
if ( defined( 'S3_UPLOADS_USE_LOCAL' ) && S3_UPLOADS_USE_LOCAL ) {
173-
$dirs['url'] = str_replace( 's3://' . $this->bucket, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['path'] );
174-
$dirs['baseurl'] = str_replace( 's3://' . $this->bucket, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['basedir'] );
181+
$dirs['url'] = str_replace( $s3_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['path'] );
182+
$dirs['baseurl'] = str_replace( $s3_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['basedir'] );
175183

176184
} else {
177-
$dirs['url'] = str_replace( 's3://' . $this->bucket, $this->get_s3_url(), $dirs['path'] );
178-
$dirs['baseurl'] = str_replace( 's3://' . $this->bucket, $this->get_s3_url(), $dirs['basedir'] );
185+
$dirs['url'] = str_replace( $s3_path, $this->get_s3_url(), $dirs['path'] );
186+
$dirs['baseurl'] = str_replace( $s3_path, $this->get_s3_url(), $dirs['basedir'] );
179187
}
180188
}
181189

0 commit comments

Comments
 (0)