Commit 844b2202 authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Simplify!

parent ae0a6acc
Loading
Loading
Loading
Loading

inc/class-s3-uploads-uploader.php

deleted100644 → 0
+0 −42
Original line number Diff line number Diff line
<?php

class S3_Uploads_Uploader {

	protected $bucket;
	protected $key;
	protected $secret;

	/**
	 * @param string $bucket 
	 * @param string $key
	 * @param string $secret
	 */
	function __construct( $bucket, $key, $secret ) {
		$this->bucket = $bucket;
		$this->key = $key;
		$this->secret = $secret;

		$this->s3()->registerStreamWrapper();
		stream_context_set_option( stream_context_get_default(), 's3', 'ACL', Aws\S3\Enum\CannedAcl::PUBLIC_READ );
	}

	public function get_s3_url() {
		return 'https://' . $this->bucket . '.s3.amazonaws.com';
	}

	/**
	 * @return Aws\S3\S3Client
	 */
	private function s3() {

		require_once dirname( __FILE__ ) . '/aws-sdk/aws-autoloader.php';

		if ( ! empty( $this->s3 ) )
			return $this->s3;

		$this->s3 = Aws\Common\Aws::factory( array( 'key' => $this->key, 'secret' => $this->secret ) )->get( 's3' );

		return $this->s3;
	}

}
 No newline at end of file
+0 −77
Original line number Diff line number Diff line
<?php

/**
 * Upload all images to S3
 * Adapted from Neeki Patel's "Uploads S3" plugin
 */
class S3_Uploads_WordPress_Uploads_Uploader extends S3_Uploads_Uploader {

	public function __construct( $bucket, $key, $secret ) {

		parent::__construct( $bucket, $key, $secret );
		add_filter( 'wp_get_attachment_url', array( $this, 'filter_get_attachment_url' ), 9, 2 );
		add_filter( 'get_attached_file', array( $this, 'filter_get_attached_file' ), 10, 2 );

		add_filter( 'wp_handle_upload', array( $this, 'filter_wp_handle_upload' ) );
		add_filter( 'load_image_to_edit_path', array( $this, 'filter_load_image_to_edit_path' ) );

	}

	public function filter_wp_handle_upload( $file ) {

		$path = str_replace( WP_CONTENT_DIR . '/', '', $file['file'] );
		$new_file = 's3://' . $this->bucket . '/' . $path;
		copy( $file['file'], $new_file );
		unlink( $file['file'] );

		$file['file'] = $new_file;
		$file['url'] = $this->get_s3_url() . '/' . $path;

		return $file;
	}

	public function filter_load_image_to_edit_path( $url ) {

		return $this->get_s3_path_for_public_url( $url );
	}

	/**
	 * Replace the saved attachment URL with the bucket URL
	 */
	public function filter_get_attachment_url( $url, $attachment_id ) {

		$path = get_post_meta( $attachment_id, '_wp_attached_file', true );

		if ( strpos( $path, 's3://' ) !== 0 )
			return $url;

		preg_match( '#s3://.+?/(.+)#', $path, $matches );

		return $this->get_s3_url() . '/' . $matches[1];
	}

	public function filter_get_attached_file( $path, $id ) {

		if ( strpos( $path, 's3://' ) ) {
			return get_post_meta( $id, '_wp_attached_file', true );
		}

		return $path;
	}

	public function is_attachment_uploaded( $attachment_id ) {
		return (bool) get_post_meta( $attachment_id, 's3_path', true );
	}

	private function get_public_url_for_s3_path( $s3_path ) {

		preg_match( '#s3://.+?/(.+)#', $path, $matches );

		return $this->get_s3_url() . '/' . $matches[1];
	}

	private function get_s3_path_for_public_url( $url ) {

		return str_replace( $this->get_s3_url(), 's3://' . $this->bucket, $url );
	}
}
+41 −6
Original line number Diff line number Diff line
@@ -3,11 +3,9 @@
class S3_Uploads {

	private static $instance;

	/**
	 * @var S3_Uploads_WordPress_Uploads_Uploader
	 */
	public $wordpress_uploads_uploader;
	private $bucket;
	private $key;
	private $secret;

	/**
	 * 
@@ -22,6 +20,43 @@ class S3_Uploads {
	}

	public function __construct( $bucket, $key, $secret ) {
		$this->wordpress_uploads_uploader = new S3_Uploads_WordPress_Uploads_Uploader( $bucket, $key, $secret );
		
		add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) );

		$this->bucket = $bucket;
		$this->key = $key;
		$this->secret = $secret;

		$this->s3()->registerStreamWrapper();
		stream_context_set_option( stream_context_get_default(), 's3', 'ACL', Aws\S3\Enum\CannedAcl::PUBLIC_READ );

	}

	public function filter_upload_dir( $dirs ) {
		$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['url'] = str_replace( WP_CONTENT_URL, $this->get_s3_url(), $dirs['url'] );
		$dirs['baseurl'] = str_replace( WP_CONTENT_URL, $this->get_s3_url(), $dirs['baseurl'] );

		return $dirs;
	}

	public function get_s3_url() {
		return 'https://' . $this->bucket . '.s3.amazonaws.com';
	}
	
	/**
	 * @return Aws\S3\S3Client
	 */
	private function s3() {

		require_once dirname( __FILE__ ) . '/aws-sdk/aws-autoloader.php';

		if ( ! empty( $this->s3 ) )
			return $this->s3;

		$this->s3 = Aws\Common\Aws::factory( array( 'key' => $this->key, 'secret' => $this->secret ) )->get( 's3' );

		return $this->s3;
	}
}
 No newline at end of file
+10 −3
Original line number Diff line number Diff line
@@ -9,9 +9,16 @@ Author URI: http://hmn.md
*/

require_once dirname( __FILE__ ) . '/inc/class-s3-uploads.php';
require_once dirname( __FILE__ ) . '/inc/class-s3-uploads-uploader.php';
require_once dirname( __FILE__ ) . '/inc/class-s3-uploads-wordpress-uploads-uploader.php';

add_action( 'plugins_loaded', function() {
	S3_Uploads::get_instance();
});

add_filter( 'wp_image_editors', function( $editors ) {

	if ( ( $position = array_search( 'WP_Image_Editor_Imagick', $editors ) ) !== false ) {
		unset($editors[$position]);
	}

	return $editors;
}, 9 );
 No newline at end of file