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 3 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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ notifications:
email: false

before_script:
- composer install
- composer install --ignore-platform-reqs

script:
- composer test
- composer test -- --coverage-clover=coverage.xml
- bash <(curl -s https://codecov.io/bash)
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
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"wordpress"
],
"license": "GPL-2.0+",
"autoload": {
"classmap": [ "./inc" ]
},
"authors": [
{
"name":"Human Made Limited",
Expand All @@ -23,7 +26,9 @@
"aws/aws-sdk-php": "~3.18"
},
"require-dev": {
"phpunit/phpunit": "7.5"
"phpunit/phpunit": "7.5",
"vimeo/psalm": "^3.11",
"pcov/clobber": "^2.0"
},
"scripts": {
"test": "./tests/run-tests.sh"
Expand Down
26 changes: 18 additions & 8 deletions inc/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function register_stream_wrapper() {
}

/**
* Overwrite the defaul wp_upload_dir.
* Overwrite the default wp_upload_dir.
*
* @param array{path: string, basedir: string, baseurl: string, url: string} $dirs
* @return array{path: string, basedir: string, baseurl: string, url: string}
Expand Down Expand Up @@ -190,6 +190,9 @@ public function filter_upload_dir( array $dirs ) : array {
public function delete_attachment_files( int $post_id ) {
$meta = wp_get_attachment_metadata( $post_id );
$file = get_attached_file( $post_id );
if ( ! $file ) {
return;
}

if ( ! empty( $meta['sizes'] ) ) {
foreach ( $meta['sizes'] as $sizeinfo ) {
Expand Down Expand Up @@ -257,15 +260,15 @@ public function get_original_upload_dir() : array {
* Reverse a file url in the uploads directory to the params needed for S3.
*
* @param string $url
* @return array
* @return array{bucket: string, key: string, query: string|null}|null
joehoyle marked this conversation as resolved.
Show resolved Hide resolved
*/
public function get_s3_location_for_url( string $url ) : ?array {
$s3_url = 'https://' . $this->get_s3_bucket() . '.s3.amazonaws.com/';
if ( strpos( $url, $s3_url ) === 0 ) {
$parsed = wp_parse_url( $url );
return [
'bucket' => $this->get_s3_bucket(),
'key' => ltrim( $parsed['path'], '/' ),
'key' => isset( $parsed['path'] ) ? ltrim( $parsed['path'], '/' ) : '',
'query' => $parsed['query'] ?? null,
];
}
Expand All @@ -277,6 +280,9 @@ public function get_s3_location_for_url( string $url ) : ?array {

$path = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], $url );
$parsed = wp_parse_url( $path );
if ( ! isset( $parsed['host'] ) || ! isset( $parsed['path'] ) ) {
return null;
}
return [
'bucket' => $parsed['host'],
'key' => ltrim( $parsed['path'], '/' ),
Expand All @@ -292,7 +298,7 @@ public function get_s3_location_for_url( string $url ) : ?array {
*/
public function get_s3_location_for_path( string $path ) : ?array {
$parsed = wp_parse_url( $path );
if ( $parsed['scheme'] !== 's3' ) {
if ( ! isset( $parsed['path'] ) || ! isset( $parsed['host'] ) || ! isset( $parsed['scheme'] ) || $parsed['scheme'] !== 's3' ) {
return null;
}
return [
Expand Down Expand Up @@ -320,6 +326,7 @@ public function s3() : Aws\S3\S3Client {
* @return Aws\Sdk
*/
public function get_aws_sdk() : Aws\Sdk {
/** @var null|Aws\Sdk */
$sdk = apply_filters( 's3_uploads_aws_sdk', null, $this );
if ( $sdk ) {
return $sdk;
Expand Down Expand Up @@ -370,8 +377,8 @@ public function filter_editors( array $editors ) : array {
* trying to do a rename() on the file cross streams. This is somewhat of a hack
* to work around the core issue https://core.trac.wordpress.org/ticket/29257
*
* @param array File array
* @return array
* @param array{tmp_name: string} $file File array
* @return array{tmp_name: string}
*/
public function filter_sideload_move_temp_file_to_s3( array $file ) {
$upload_dir = wp_upload_dir();
Expand All @@ -389,7 +396,7 @@ public function filter_sideload_move_temp_file_to_s3( array $file ) {
* file streams so we need to make a temporary local copy to extract
* exif data from.
*
* @param array $meta
* @param array $meta
* @param string $file
* @return array|bool
*/
Expand Down Expand Up @@ -428,7 +435,6 @@ public function copy_image_from_s3( string $file ) : string {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$temp_filename = wp_tempnam( $file );
echo $temp_filename;
copy( $file, $temp_filename );
return $temp_filename;
}
Expand Down Expand Up @@ -490,20 +496,24 @@ public function set_attachment_files_acl( int $attachment_id, string $acl ) : ?W
*/
public static function get_attachment_files( int $attachment_id ) : array {
$uploadpath = wp_get_upload_dir();
/** @var string */
$main_file = get_attached_file( $attachment_id );
$files = [ $main_file ];

$meta = wp_get_attachment_metadata( $attachment_id );
if ( isset( $meta['sizes'] ) ) {
foreach ( $meta['sizes'] as $size => $sizeinfo ) {
$files[] = $uploadpath['basedir'] . $sizeinfo['file'];
}
}

/** @var string|false */
$original_image = get_post_meta( $attachment_id, 'original_image', true );
if ( $original_image ) {
$files[] = $uploadpath['basedir'] . $original_image;
}

/** @var array<string,array{file: string}> */
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
if ( $backup_sizes ) {
foreach ( $backup_sizes as $size => $sizeinfo ) {
Expand Down
32 changes: 4 additions & 28 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function outdated_wp_version_notice() {

printf(
'<div class="error"><p>The S3 Uploads plugin requires WordPress version 5.3 or higher. Your server is running WordPress version %s.</p></div>',
$wp_version
esc_html( $wp_version )
);
}

Expand All @@ -106,37 +106,13 @@ function enabled() : bool {
// Make sure the plugin is enabled when autoenable is on
$constant_autoenable_off = ( defined( 'S3_UPLOADS_AUTOENABLE' ) && false === S3_UPLOADS_AUTOENABLE );

if ( $constant_autoenable_off && 'enabled' !== get_option( 's3_uploads_enabled' ) ) { // If the plugin is not enabled, skip
if ( $constant_autoenable_off && 'enabled' !== get_option( 's3_uploads_enabled' ) ) { // If the plugin is not enabled, skip
return false;
}

return true;
}

/**
* Autoload callback.
*
* @param $class_name Name of the class to load.
*/
function autoload( string $class_name ) {
/*
* Load plugin classes:
* - Class name: S3_Uploads\Image_Editor_Imagick.
* - File name: class-image-editor-imagick.php.
*/
if ( strpos( $class_name, 'S3_Uploads\\' ) !== 0 ) {
return;
}
$class_name = substr( $class_name, 11 );
$class_file = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
$class_path = dirname( __DIR__ ) . '/inc/' . $class_file;
if ( file_exists( $class_path ) ) {
require $class_path;

return;
}
}

/**
* Setup the filters for wp_privacy_exports_dir to use a temp folder location.
*/
Expand Down Expand Up @@ -169,15 +145,15 @@ function set_wp_privacy_exports_dir( string $dir ) {
$dir = sys_get_temp_dir() . '/wp_privacy_exports_dir/';
if ( ! is_dir( $dir ) ) {
mkdir( $dir );
file_put_contents( $dir . 'index.html', '' );
file_put_contents( $dir . 'index.html', '' ); // @codingStandardsIgnoreLine FS write is ok.
}
return $dir;
}

/**
* Move the tmp personal data file to the true uploads location
*
* Once a personal data file has been written, move it from the overriden "temp"
* Once a personal data file has been written, move it from the overridden "temp"
* location to the S3 location where it should have been stored all along, and where
* the "natural" Core URL is going to be pointing to.
*/
Expand Down
27 changes: 27 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<psalm requireVoidReturnType="false" totallyTyped="true" ensureArrayStringOffsetsExist="true">
<projectFiles>
<directory name="./" />
<ignoreFiles>
<directory name="vendor" />
<directory name="psalm" />
<directory name="tests" />
</ignoreFiles>
</projectFiles>
<stubs>
<file name="psalm/stubs/a-wordpress.php" />
<file name="psalm/stubs/a-wp-cli.php" />
<file name="psalm/stubs/overrides.php" />
<file name="psalm/stubs/constants.php" />
</stubs>
<globals>
<var name="wp_version" type="string" />
</globals>
<issueHandlers>
<MissingFile>
<errorLevel type="suppress">
<file name="inc/class-plugin.php" />
</errorLevel>
</MissingFile>
</issueHandlers>
</psalm>
1 change: 0 additions & 1 deletion s3-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
require_once __DIR__ . '/inc/namespace.php';

add_action( 'plugins_loaded', 'S3_Uploads\\init' );
spl_autoload_register( 'S3_Uploads\\autoload' );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 's3-uploads', __NAMESPACE__ . '\\WP_CLI_Command' );
Expand Down