Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using PHP namespaces #401

Merged
merged 56 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
c89b5c6
Switch to using PHP namespaces
joehoyle Apr 29, 2020
8055887
Run tests via docker image
joehoyle Apr 29, 2020
7a46c4d
Switch to using resuable container
joehoyle May 4, 2020
788c30a
Bring composer scripts and travis config from https://github.com/huma…
joehoyle May 4, 2020
4a33f16
Don't use a custom docker iamge
joehoyle May 4, 2020
ae8dd98
Remove unneeded files
joehoyle May 4, 2020
573035b
Merge pull request #402 from humanmade/docker-tests
joehoyle May 4, 2020
2dbeb3f
Merge branch 'private-attachments' into namespaces
joehoyle May 7, 2020
f07d65a
Code formatting
joehoyle May 7, 2020
5467ac5
Use parse_url
joehoyle May 7, 2020
4fdfb0a
PHPCS
joehoyle May 7, 2020
b641dd3
Fix typo
joehoyle May 7, 2020
f61f909
No need for alignment
joehoyle May 7, 2020
2cccc1d
Add type checking
joehoyle May 8, 2020
d798d60
Typing info
joehoyle May 11, 2020
a2316df
Upload to codecov
joehoyle May 11, 2020
98f5d7f
Add shepherd image
joehoyle May 11, 2020
9a188e4
PHPCS and Psalm fixes
joehoyle May 11, 2020
597ce34
Add types in image editor
joehoyle May 11, 2020
f193f19
Fix typing
joehoyle May 11, 2020
eca7605
PHPCS fixes
joehoyle May 11, 2020
b2f882e
Switch to dev-master of psalm
joehoyle May 11, 2020
82be4d1
Add more types
joehoyle May 15, 2020
639bee1
Run Psalm on testing
joehoyle May 15, 2020
539237b
Add GH workflow
joehoyle May 15, 2020
dafc932
Update main.yml
joehoyle May 15, 2020
97a3b73
Update main.yml
joehoyle May 15, 2020
12ee992
Fix : @
joehoyle May 15, 2020
3ffa375
Update main.yml
joehoyle May 15, 2020
f25b6e2
Update main.yml
joehoyle May 15, 2020
e7ea14e
Update main.yml
joehoyle May 15, 2020
40f2159
Update main.yml
joehoyle May 15, 2020
1f36fbc
Update main.yml
joehoyle May 15, 2020
ea41711
Update main.yml
joehoyle May 15, 2020
7c4d679
Fix remaining type errors
joehoyle May 18, 2020
07c4737
Merge branch 'namespaces' of github.com:humanmade/S3-Uploads into nam…
joehoyle May 18, 2020
bf3b533
Install deps with plugin-tester
joehoyle May 18, 2020
5fa2b28
Fix compat with parent class
joehoyle May 18, 2020
d180e77
Add clobber
joehoyle May 18, 2020
e6fbfef
Use set -e
joehoyle May 18, 2020
bc3fb23
Switch to dev-master version of psalm wp plugin
joehoyle May 18, 2020
6493193
Merge branch 'master' into namespaces
joehoyle May 20, 2020
62d2a48
Remove GH workflows
joehoyle May 20, 2020
f2a21ad
Ignore psalm dir from phpcs
joehoyle May 20, 2020
1e3b3d6
Fix slow call to scandir() when uploading files
joehoyle Jul 3, 2020
8a6911d
Add checks
joehoyle Jul 3, 2020
0f42215
Add support for pre_wp_unique_filename_file_list filter
joehoyle Jul 7, 2020
923a5ec
Fix Psalm passing
joehoyle Jul 8, 2020
a025465
Merge pull request #422 from humanmade/fix-slow-scandir
joehoyle Jul 8, 2020
730d057
Merge branch 'master' into namespaces
joehoyle Jul 8, 2020
dce05fb
Merge branch 'namespaces' of github.com:humanmade/S3-Uploads into nam…
joehoyle Jul 8, 2020
b2187f0
Update inc/class-plugin.php
joehoyle Dec 8, 2020
dc83833
phpcs fixes
joehoyle Dec 8, 2020
81a90cf
Fix up Psalm errors
joehoyle Dec 8, 2020
eb123b7
Update inc/class-stream-wrapper.php
joehoyle Dec 8, 2020
00f9a47
Update inc/class-stream-wrapper.php
joehoyle Dec 8, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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