Commit 0f0f56c1 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Replicate legacy path replacement, add new control

parent 813042d6
Loading
Loading
Loading
Loading
+28 −4
Original line number Diff line number Diff line
@@ -84,20 +84,44 @@ class S3_Uploads {
	public function filter_upload_dir( $dirs ) {
		$this->original_upload_dir = $dirs;

		$wp_content_len = strlen(WP_CONTENT_DIR);
		$orig_basedir = $dirs['basedir'];
		$orig_baseurl = $dirs['baseurl'];
		$new_basedir = 's3://' . $this->bucket;

		if ( defined( 'S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL' ) && S3_UPLOADS_DISABLE_REPLACE_UPLOAD_URL ) {
			$new_baseurl = $dirs['baseurl'];
			$new_baseurl = $orig_baseurl;
		} else if ( defined( 'S3_UPLOADS_USE_LOCAL' ) && S3_UPLOADS_USE_LOCAL ) {
			$new_baseurl = $dirs['baseurl'] . '/s3/' . $this->bucket;
			$new_baseurl = $orig_baseurl . '/s3/' . $this->bucket;
		} else {
			$new_baseurl = $this->get_s3_url();
		}

		if ( defined( 'S3_UPLOADS_BASEPATH' ) ) {
			/* If S3_UPLOADS_BASEPATH is set to a non-empty string, use it as a base
			 * path in the bucket.  It also acts as a toggle to disable legacy
			 * behaviour: set to null or false to avoid using the relative path under
			 * WP_CONTENT_DIR as a base path.
			 */
			if ( S3_UPLOADS_BASEPATH ) {
				$new_basedir .= '/' . S3_UPLOADS_BASEPATH;
				$new_baseurl .= '/' . S3_UPLOADS_BASEPATH;
			}
		} else if ( substr( $orig_basedir, 0, $wp_content_len ) === WP_CONTENT_DIR ) {
			/* For compatibility with legacy behaviour, if the basedir is in
			 * WP_CONTENT_DIR, append the relative path between the two to the new
			 * basedir.
			 */
			$new_basedir .= substr($orig_basedir, $wp_content_len);

			$orig_path = parse_url($orig_baseurl, PHP_URL_PATH);
			$new_baseurl .= substr($orig_path, $wp_content_len - strlen(ABSPATH) + 1);
		}

		return array(
			'subdir' => $dirs['subdir'],
			'path' => str_replace( $dirs['basedir'], $new_basedir, $dirs['path'] ),
			'url' => str_replace( $dirs['baseurl'], $new_baseurl, $dirs['url'] ),
			'path' => str_replace( $orig_basedir, $new_basedir, $dirs['path'] ),
			'url' => str_replace( $orig_baseurl, $new_baseurl, $dirs['url'] ),
			'basedir' => $new_basedir,
			'baseurl' => $new_baseurl,
			'error' => false,