Skip to content

Commit

Permalink
Merge pull request #401 from humanmade/namespaces
Browse files Browse the repository at this point in the history
Switch to using PHP namespaces
  • Loading branch information
joehoyle authored Dec 8, 2020
2 parents 539d0c1 + 00f9a47 commit 9621685
Show file tree
Hide file tree
Showing 24 changed files with 1,948 additions and 1,592 deletions.
1 change: 1 addition & 0 deletions .phpcsignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib/
psalm/
30 changes: 9 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
# Travis CI Configuration File

# Tell Travis CI we're using PHP
language: php
services:
- docker

cache:
timeout: 1000
directories:
- vendor

notifications:
email: false

php:
- 7.0

env:
- WP_VERSION=latest WP_MULTISITE=0

matrix:
include:
- php: 7.0
env: WP_VERSION=latest WP_MULTISITE=1

# Clones WordPress and configures our testing environment.
before_script:
- bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 $WP_VERSION
- printf "\n" | pecl install imagick
- composer install
- docker run --rm -v $PWD:/code --entrypoint='' humanmade/plugin-tester composer install

script:
- phpunit --coverage-clover clover.xml

after_script:
# Push coverage off to Codecov
- ./tests/run-tests.sh --coverage-clover=coverage.xml
- bash <(curl -s https://codecov.io/bash)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
Lightweight "drop-in" for storing WordPress uploads on Amazon S3 instead of the local filesystem.
</td>
<td align="right" width="20%">
<a href="https://shepherd.dev/github/humanmade/S3-Uploads/">
<img src="https://shepherd.dev/github/humanmade/S3-Uploads/coverage.svg" alt="Psalm coverage">
</a>
<a href="https://travis-ci.com/humanmade/S3-Uploads">
<img src="https://travis-ci.com/humanmade/S3-Uploads.svg?branch=master" alt="Build status">
</a>
Expand Down
78 changes: 0 additions & 78 deletions bin/install-wp-tests.sh

This file was deleted.

1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comment: false
16 changes: 14 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,32 @@
"wordpress"
],
"license": "GPL-2.0+",
"autoload": {
"classmap": [ "./inc" ]
},
"authors": [
{
"name":"Human Made Limited",
"email":"support@humanmade.co.uk",
"homepage":"http://hmn.md/"
}
],
"support" : {
"support": {
"issues": "https://github.com/humanmade/s3-uploads/issues",
"source": "https://github.com/humanmade/s3-uploads"
},
"type": "wordpress-plugin",
"require": {
"composer/installers": "~1.0",
"aws/aws-sdk-php": "~3.18"
}
},
"require-dev": {
"phpunit/phpunit": "7.5",
"pcov/clobber": "^2.0",
"humanmade/psalm-plugin-wordpress": "dev-master"
},
"scripts": {
"test": "./tests/run-tests.sh",
"check-types": "./vendor/bin/psalm"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
<?php

class S3_Uploads_Image_Editor_Imagick extends WP_Image_Editor_Imagick {
namespace S3_Uploads;

protected $temp_file_to_cleanup = null;
use Imagick;
use WP_Error;
use WP_Image_Editor_Imagick;

class Image_Editor_Imagick extends WP_Image_Editor_Imagick {

/**
* @var ?Imagick
*/
protected $image;

/**
* @var ?string
*/
protected $file;

/**
* @var ?array{width: int, height: int}
*/
protected $size;

/**
* @var ?string
*/
protected $remote_filename = null;

/**
* Hold on to a reference of all temp local files.
Expand All @@ -11,7 +35,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 All @@ -23,13 +47,13 @@ public function load() {
return true;
}

if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
if ( $this->file && ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
return new WP_Error( 'error_loading_image', __( 'File doesn&#8217;t exist?' ), $this->file );
}

$upload_dir = wp_upload_dir();

if ( strpos( $this->file, $upload_dir['basedir'] ) !== 0 ) {
if ( ! $this->file || strpos( $this->file, $upload_dir['basedir'] ) !== 0 ) {
return parent::load();
}

Expand All @@ -50,8 +74,18 @@ public function load() {
* Imagick by default can't handle s3:// paths
* for saving images. We have instead save it to a file file,
* then copy it to the s3:// path as a workaround.
*
* @param Imagick $image
* @param ?string $filename
* @param ?string $mime_type
* @return WP_Error|array{path: string, file: string, width: int, height: int, mime-type: string}
*/
protected function _save( $image, $filename = null, $mime_type = null ) {
/**
* @var ?string $filename
* @var string $extension
* @var string $mime_type
*/
list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );

if ( ! $filename ) {
Expand All @@ -61,32 +95,47 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
$upload_dir = wp_upload_dir();

if ( strpos( $filename, $upload_dir['basedir'] ) === 0 ) {
/** @var false|string */
$temp_filename = tempnam( get_temp_dir(), 's3-uploads' );
} else {
$temp_filename = false;
}

$save = parent::_save( $image, $temp_filename, $mime_type );
/**
* @var WP_Error|array{path: string, file: string, width: int, height: int, mime-type: string}
*/
$parent_call = parent::_save( $image, $temp_filename ?: $filename, $mime_type );

if ( is_wp_error( $save ) ) {
if ( is_wp_error( $parent_call ) && $temp_filename ) {
unlink( $temp_filename );
return $save;
return $parent_call;
} else {
/**
* @var array{path: string, file: string, width: int, height: int, mime-type: string} $save
*/
$save = $parent_call;
}

$copy_result = copy( $save['path'], $filename );

unlink( $save['path'] );
unlink( $temp_filename );
if ( $temp_filename ) {
unlink( $temp_filename );
}

if ( ! $copy_result ) {
return new WP_Error( 'unable-to-copy-to-s3', 'Unable to copy the temp image to S3' );
}

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

return $response;
}

public function __destruct() {
Expand Down
Loading

0 comments on commit 9621685

Please sign in to comment.