diff --git a/inc/class-plugin.php b/inc/class-plugin.php index 30639e17..2a27be64 100644 --- a/inc/class-plugin.php +++ b/inc/class-plugin.php @@ -154,6 +154,13 @@ public function register_stream_wrapper() { stream_context_set_option( stream_context_get_default(), 's3', 'seekable', true ); } + /** + * Get the s3:// path for the bucket. + */ + public function get_s3_path() { + return 's3://' . $this->bucket; + } + /** * Overwrite the default wp_upload_dir. * @@ -163,19 +170,20 @@ public function register_stream_wrapper() { public function filter_upload_dir( array $dirs ) : array { $this->original_upload_dir = $dirs; + $s3_path = $this->get_s3_path(); - $dirs['path'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['path'] ); - $dirs['basedir'] = str_replace( WP_CONTENT_DIR, 's3://' . $this->bucket, $dirs['basedir'] ); + $dirs['path'] = str_replace( WP_CONTENT_DIR, $s3_path, $dirs['path'] ); + $dirs['basedir'] = str_replace( WP_CONTENT_DIR, $s3_path, $dirs['basedir'] ); if ( ! defined( 'S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL' ) || ! S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL ) { if ( defined( 'S3_UPLOADS_USE_LOCAL' ) && S3_UPLOADS_USE_LOCAL ) { - $dirs['url'] = str_replace( 's3://' . $this->bucket, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['path'] ); - $dirs['baseurl'] = str_replace( 's3://' . $this->bucket, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['basedir'] ); + $dirs['url'] = str_replace( $s3_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['path'] ); + $dirs['baseurl'] = str_replace( $s3_path, $dirs['baseurl'] . '/s3/' . $this->bucket, $dirs['basedir'] ); } else { - $dirs['url'] = str_replace( 's3://' . $this->bucket, $this->get_s3_url(), $dirs['path'] ); - $dirs['baseurl'] = str_replace( 's3://' . $this->bucket, $this->get_s3_url(), $dirs['basedir'] ); + $dirs['url'] = str_replace( $s3_path, $this->get_s3_url(), $dirs['path'] ); + $dirs['baseurl'] = str_replace( $s3_path, $this->get_s3_url(), $dirs['basedir'] ); } }