Skip to content

Commit

Permalink
Switch to using PHP namespaces
Browse files Browse the repository at this point in the history
To keep inline with the HM coding standards and consistency with other Human Made code, I'd like to swich S3 Uploads to namespaces, and fix up any outstanding PHP formatting issues with the HM PHPCS.
  • Loading branch information
joehoyle committed Apr 29, 2020
1 parent 42385e6 commit c89b5c6
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 711 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

class S3_Uploads_Image_Editor_Imagick extends WP_Image_Editor_Imagick {
namespace S3_Uploads;

use WP_Error;
use WP_Image_Editor_Imagick;

class Image_Editor_Imagick extends WP_Image_Editor_Imagick {

protected $temp_file_to_cleanup = null;

Expand All @@ -11,7 +16,7 @@ class S3_Uploads_Image_Editor_Imagick extends WP_Image_Editor_Imagick {
*
* @var array
*/
protected $temp_files_to_cleanup = array();
protected $temp_files_to_cleanup = [];

/**
* Loads image from $this->file into new Imagick Object.
Expand Down Expand Up @@ -80,13 +85,13 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
return new WP_Error( 'unable-to-copy-to-s3', 'Unable to copy the temp image to S3' );
}

return array(
return [
'path' => $filename,
'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
'width' => $this->size['width'],
'height' => $this->size['height'],
'mime-type' => $mime_type,
);
];
}

public function __destruct() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

namespace S3_Uploads;

/**
* Local streamwrapper that writes files to the upload dir
*
* This is for the most part taken from Drupal, with some modifications.
*/
class S3_Uploads_Local_Stream_Wrapper {
class Local_Stream_Wrapper {
/**
* Stream context resource.
*
Expand Down Expand Up @@ -36,7 +38,7 @@ class S3_Uploads_Local_Stream_Wrapper {
* String specifying the path.
*/
static function getDirectoryPath() {
$upload_dir = S3_Uploads::get_instance()->get_original_upload_dir();
$upload_dir = Plugin::get_instance()->get_original_upload_dir();
return $upload_dir['basedir'] . '/s3';
}

Expand Down
128 changes: 78 additions & 50 deletions inc/class-s3-uploads.php → inc/class-plugin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

class S3_Uploads {
namespace S3_Uploads;

use Aws;

class Plugin {

private static $instance;
private $bucket;
private $bucket_url;
private $key;
Expand All @@ -11,14 +14,29 @@ class S3_Uploads {
public $original_upload_dir;
public $original_file;

/**
* @var string
*/
private $region = null;

/**
* @var ?Aws\S3\S3Client
*/
private $s3 = null;

/**
* @var ?static
*/
private static $instance = null;

/**
*
* @return S3_Uploads
* @return static
*/
public static function get_instance() {

if ( ! self::$instance ) {
self::$instance = new S3_Uploads(
self::$instance = new static(
S3_UPLOADS_BUCKET,
defined( 'S3_UPLOADS_KEY' ) ? S3_UPLOADS_KEY : null,
defined( 'S3_UPLOADS_SECRET' ) ? S3_UPLOADS_SECRET : null,
Expand All @@ -45,20 +63,20 @@ public function __construct( string $bucket, string $key, string $secret, string
public function setup() {
$this->register_stream_wrapper();

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, 'delete_attachment_files' ) );
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 );
add_filter( 'upload_dir', [ $this, 'filter_upload_dir' ] );
add_filter( 'wp_image_editors', [ $this, 'filter_editors' ], 9 );
add_action( 'delete_attachment', [ $this, 'delete_attachment_files' ] );
add_filter( 'wp_read_image_metadata', [ $this, 'wp_filter_read_image_metadata' ], 10, 2 );
add_filter( 'wp_resource_hints', [ $this, 'wp_filter_resource_hints' ], 10, 2 );
remove_filter( 'admin_notices', 'wpthumb_errors' );

add_action( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) );
add_action( 'wp_handle_sideload_prefilter', [ $this, 'filter_sideload_move_temp_file_to_s3' ] );

add_action( 'wp_get_attachment_url', array( $this, 'add_s3_signed_params_to_attachment_url' ), 10, 2 );
add_action( 'wp_get_attachment_image_src', array( $this, 'add_s3_signed_params_to_attachment_image_src' ), 10, 2 );
add_action( 'wp_calculate_image_srcset', array( $this, 'add_s3_signed_params_to_attachment_image_srcset' ), 10, 5 );
add_action( 'wp_get_attachment_url', [ $this, 'add_s3_signed_params_to_attachment_url' ], 10, 2 );
add_action( 'wp_get_attachment_image_src', [ $this, 'add_s3_signed_params_to_attachment_image_src' ], 10, 2 );
add_action( 'wp_calculate_image_srcset', [ $this, 'add_s3_signed_params_to_attachment_image_srcset' ], 10, 5 );

add_filter( 'wp_generate_attachment_metadata', array( $this, 'set_attachment_private_on_generate_attachment_metadata' ), 10, 2 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'set_attachment_private_on_generate_attachment_metadata' ], 10, 2 );
}

/**
Expand All @@ -67,15 +85,15 @@ public function setup() {
public function tear_down() {

stream_wrapper_unregister( 's3' );
remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) );
remove_filter( 'wp_image_editors', array( $this, 'filter_editors' ), 9 );
remove_filter( 'wp_handle_sideload_prefilter', array( $this, 'filter_sideload_move_temp_file_to_s3' ) );
remove_filter( 'upload_dir', [ $this, 'filter_upload_dir' ] );
remove_filter( 'wp_image_editors', [ $this, 'filter_editors' ], 9 );
remove_filter( 'wp_handle_sideload_prefilter', [ $this, 'filter_sideload_move_temp_file_to_s3' ] );

remove_action( 'wp_get_attachment_url', array( $this, 'add_s3_signed_params_to_attachment_url' ) );
remove_action( 'wp_get_attachment_image_src', array( $this, 'add_s3_signed_params_to_attachment_image_src' ) );
remove_action( 'wp_calculate_image_srcset', array( $this, 'add_s3_signed_params_to_attachment_image_srcset' ) );
remove_action( 'wp_get_attachment_url', [ $this, 'add_s3_signed_params_to_attachment_url' ] );
remove_action( 'wp_get_attachment_image_src', [ $this, 'add_s3_signed_params_to_attachment_image_src' ] );
remove_action( 'wp_calculate_image_srcset', [ $this, 'add_s3_signed_params_to_attachment_image_srcset' ] );

remove_filter( 'wp_generate_attachment_metadata', array( $this, 'set_attachment_private_on_generate_attachment_metadata' ) );
remove_filter( 'wp_generate_attachment_metadata', [ $this, 'set_attachment_private_on_generate_attachment_metadata' ] );
}

/**
Expand All @@ -85,15 +103,15 @@ public function register_stream_wrapper() {
if ( defined( 'S3_UPLOADS_USE_LOCAL' ) && S3_UPLOADS_USE_LOCAL ) {
stream_wrapper_register( 's3', 'S3_Uploads_Local_Stream_Wrapper', STREAM_IS_URL );
} else {
S3_Uploads_Stream_Wrapper::register( $this->s3() );
Stream_Wrapper::register( $this->s3() );
$acl = defined( 'S3_UPLOADS_OBJECT_ACL' ) ? S3_UPLOADS_OBJECT_ACL : 'public-read';
stream_context_set_option( stream_context_get_default(), 's3', 'ACL', $acl );
}

stream_context_set_option( stream_context_get_default(), 's3', 'seekable', true );
}

public function filter_upload_dir( $dirs ) {
public function filter_upload_dir( array $dirs ) : array {

$this->original_upload_dir = $dirs;

Expand Down Expand Up @@ -125,9 +143,9 @@ public function filter_upload_dir( $dirs ) {
* up the s3 files when an attachment is removed, and leave WordPress to try
* a failed attempt at mangling the s3:// urls.
*
* @param $post_id
* @param int $post_id
*/
public function delete_attachment_files( $post_id ) {
public function delete_attachment_files( int $post_id ) {
$meta = wp_get_attachment_metadata( $post_id );
$file = get_attached_file( $post_id );

Expand All @@ -141,7 +159,12 @@ public function delete_attachment_files( $post_id ) {
wp_delete_file( $file );
}

public function get_s3_url() {
/**
* Get the S3 URL base for uploads.
*
* @return string
*/
public function get_s3_url() : string {
if ( $this->bucket_url ) {
return $this->bucket_url;
}
Expand All @@ -157,15 +180,15 @@ public function get_s3_url() {
*
* @return string
*/
public function get_s3_bucket() {
public function get_s3_bucket() : string {
return $bucket = strtok( $this->bucket, '/' );
}

public function get_s3_bucket_region() {
public function get_s3_bucket_region() : string {
return $this->region;
}

public function get_original_upload_dir() {
public function get_original_upload_dir() : array {

if ( empty( $this->original_upload_dir ) ) {
wp_upload_dir();
Expand Down Expand Up @@ -225,7 +248,7 @@ public function get_s3_location_for_path( string $path ) : ?array {
/**
* @return Aws\S3\S3Client
*/
public function s3() {
public function s3() : Aws\S3\S3Client {

if ( ! empty( $this->s3 ) ) {
return $this->s3;
Expand All @@ -238,15 +261,15 @@ public function s3() {
/**
* Get the AWS Sdk.
*
* @return AWS\Sdk
* @return Aws\Sdk
*/
public function get_aws_sdk() : AWS\Sdk {
public function get_aws_sdk() : Aws\Sdk {
$sdk = apply_filters( 's3_uploads_aws_sdk', null, $this );
if ( $sdk ) {
return $sdk;
}

$params = array( 'version' => 'latest' );
$params = [ 'version' => 'latest' ];

if ( $this->key && $this->secret ) {
$params['credentials']['key'] = $this->key;
Expand Down Expand Up @@ -275,13 +298,13 @@ public function get_aws_sdk() : AWS\Sdk {
return $sdk;
}

public function filter_editors( $editors ) {
public function filter_editors( array $editors ) : array {

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

array_unshift( $editors, 'S3_Uploads_Image_Editor_Imagick' );
array_unshift( $editors, __NAMESPACE__ . '\\Image_Editor_Imagick' );

return $editors;
}
Expand Down Expand Up @@ -314,11 +337,11 @@ public function filter_sideload_move_temp_file_to_s3( array $file ) {
* @param string $file
* @return array|bool
*/
public function wp_filter_read_image_metadata( $meta, $file ) {
remove_filter( 'wp_read_image_metadata', array( $this, 'wp_filter_read_image_metadata' ), 10 );
public function wp_filter_read_image_metadata( array $meta, string $file ) {
remove_filter( 'wp_read_image_metadata', [ $this, 'wp_filter_read_image_metadata' ], 10 );
$temp_file = $this->copy_image_from_s3( $file );
$meta = wp_read_image_metadata( $temp_file );
add_filter( 'wp_read_image_metadata', array( $this, 'wp_filter_read_image_metadata' ), 10, 2 );
add_filter( 'wp_read_image_metadata', [ $this, 'wp_filter_read_image_metadata' ], 10, 2 );
unlink( $temp_file );
return $meta;
}
Expand All @@ -330,7 +353,7 @@ public function wp_filter_read_image_metadata( $meta, $file ) {
* @param $relation_type
* @return array
*/
function wp_filter_resource_hints( $hints, $relation_type ) {
function wp_filter_resource_hints( array $hints, string $relation_type ) : array {
if ( 'dns-prefetch' === $relation_type ) {
$hints[] = $this->get_s3_url();
}
Expand All @@ -344,7 +367,7 @@ function wp_filter_resource_hints( $hints, $relation_type ) {
* @param string $file
* @return string
*/
public function copy_image_from_s3( $file ) {
public function copy_image_from_s3( string $file ) {
if ( ! function_exists( 'wp_tempnam' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
Expand All @@ -366,7 +389,8 @@ public function is_private_attachment( int $attachment_id ) : bool {
* @param bool Whether the attachment is private.
* @param int The attachment ID.
*/
return apply_filters( 's3_uploads_is_attachment_private', false, $attachment_id );
$private = apply_filters( 's3_uploads_is_attachment_private', false, $attachment_id );
return $private;
}

/**
Expand All @@ -382,11 +406,13 @@ public function set_attachment_files_acl( int $attachment_id, string $acl ) : ?W
$s3 = $this->s3();
$commands = [];
foreach ( $locations as $location ) {
$commands[] = $s3->getCommand( 'putObjectAcl', [
'Bucket' => $location['bucket'],
'Key' => $location['key'],
'ACL' => $acl,
] );
$commands[] = $s3->getCommand(
'putObjectAcl', [
'Bucket' => $location['bucket'],
'Key' => $location['key'],
'ACL' => $acl,
]
);
try {
Aws\CommandPool::batch( $s3, $commands );
} catch ( Exception $e ) {
Expand Down Expand Up @@ -451,10 +477,12 @@ public function add_s3_signed_params_to_attachment_url( string $url, int $post_i
if ( ! $path ) {
return $url;
}
$cmd = $this->s3()->getCommand('GetObject', [
'Bucket' => $path['bucket'],
'Key' => $path['key'],
]);
$cmd = $this->s3()->getCommand(
'GetObject', [
'Bucket' => $path['bucket'],
'Key' => $path['key'],
]
);

$presigned_url_expires = apply_filters( 's3_uploads_private_attachment_url_expiry', '+24 hours', $post_id );
$query = $this->s3()->createPresignedRequest( $cmd, $presigned_url_expires )->getUri()->getQuery();
Expand Down
Loading

0 comments on commit c89b5c6

Please sign in to comment.