Unverified Commit 19f4b009 authored by Joe Hoyle's avatar Joe Hoyle Committed by GitHub
Browse files

Merge pull request #215 from washingtonstateuniversity/remove-original-file

Don't filter absolute paths in wp_delete_file()
parents 41373f7e 1e353ed1
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ class S3_Uploads {
	private        $secret;

	public $original_upload_dir;
	public $original_file;

	/**
	 *
@@ -46,6 +47,7 @@ class S3_Uploads {

		add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) );
		add_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 );
		add_action( 'delete_attachment', array( $this, 'set_original_file' ) );
		add_filter( 'wp_delete_file', array( $this, 'wp_filter_delete_file' ) );
		add_filter( 'wp_read_image_metadata', array( $this, 'wp_filter_read_image_metadata' ), 10, 2 );
		add_filter( 'wp_resource_hints', array( $this, 'wp_filter_resource_hints' ), 10, 2 );
@@ -103,6 +105,17 @@ class S3_Uploads {
		return $dirs;
	}

	/**
	 * Capture the full path to the original file being deleted. This
	 * is used when determining whether an absolute or relative path
	 * should be used when deleting the file.
	 *
	 * @param $post_id
	 */
	public function set_original_file( $post_id ) {
		$this->original_file = get_attached_file( $post_id );
	}

	/**
	 * When WordPress removes files, it's expecting to do so on
	 * absolute file paths, as such it breaks when using uris for
@@ -116,6 +129,11 @@ class S3_Uploads {
	public function wp_filter_delete_file( $file_path ) {
		$dir = wp_upload_dir();

		// When `wp_delete_file()` is called directly, it expects an absolute path.
		if ( ! $this->original_file || $file_path === $this->original_file ) {
			return $file_path;
		}

		return str_replace( trailingslashit( $dir['basedir'] ), '', $file_path );
	}