Skip to content

Commit

Permalink
Deploying version 2.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmjones committed Mar 21, 2022
1 parent 4e27924 commit 084825e
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 140 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
**Requires at least:** 4.9
**Tested up to:** 5.9
**Requires PHP:** 5.6
**Stable tag:** 2.6.0
**Stable tag:** 2.6.1
**License:** GPLv3

Copies files to Amazon S3, DigitalOcean Spaces or Google Cloud Storage as they are uploaded to the Media Library. Optionally configure Amazon CloudFront or another CDN for faster delivery.
Expand Down Expand Up @@ -93,6 +93,9 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### WP Offload Media Lite 2.6.1 - 2022-03-21 ###
* Bug fix: Local files are no longer removed if as3cf_pre_upload_attachment filter is used to abort upload

### WP Offload Media Lite 2.6 - 2022-03-09 ###
* [Release Summary Blog Post](https://deliciousbrains.com/wp-offload-media-2-6-released/?utm_campaign=changelogs&utm_source=wordpress.org&utm_medium=free%2Bplugin%2Blisting)
* New: WP Offload Media is now compatible with WordPress 5.9 and Full Site Editing
Expand Down
4 changes: 3 additions & 1 deletion classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use DeliciousBrains\WP_Offload_Media\Providers\Storage\GCP_Provider;
use DeliciousBrains\WP_Offload_Media\Providers\Storage\Null_Provider;
use DeliciousBrains\WP_Offload_Media\Providers\Storage\Storage_Provider;
use DeliciousBrains\WP_Offload_Media\Upgrades\Clear_Postmeta_Cache;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Content_Replace_URLs;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_EDD_Replace_URLs;
Expand Down Expand Up @@ -166,7 +167,7 @@ class Amazon_S3_And_CloudFront extends AS3CF_Plugin_Base {
*/
protected $integration_manager;

const LATEST_UPGRADE_ROUTINE = 10;
const LATEST_UPGRADE_ROUTINE = 11;

/**
* @param string $plugin_file_path
Expand Down Expand Up @@ -231,6 +232,7 @@ public function init( $plugin_file_path ) {
new Upgrade_Items_Table( $this );
new Upgrade_Tools_Errors( $this );
new Upgrade_Item_Extra_Data( $this );
new Clear_Postmeta_Cache( $this );

// Plugin setup
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
Expand Down
4 changes: 2 additions & 2 deletions classes/as3cf-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ protected function is_failure( $value ) {
* @return bool
*/
public function item_matches_src( $item_source, $url ) {
if ( empty( $item_source['id'] ) || empty( $item_source['source_type'] ) || Media_Library_Item::source_type() !== $item_source['source_type'] ) {
if ( Item::is_empty_item_source( $item_source ) || Media_Library_Item::source_type() !== $item_source['source_type'] ) {
return false;
}
$meta = get_post_meta( $item_source['id'], '_wp_attachment_metadata', true );
Expand Down Expand Up @@ -533,7 +533,7 @@ protected function push_to_url_pairs( &$url_pairs, $item_source, $find, &$to_cac
* @return null|string
*/
public function get_size_string_from_url( $item_source, $url ) {
if ( empty( $item_source['id'] ) || empty( $item_source['source_type'] ) ) {
if ( Item::is_empty_item_source( $item_source ) ) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions classes/filters/as3cf-local-to-s3.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function url_needs_replacing( $url ) {
* @return bool|string
*/
protected function get_url( $item_source, $object_key = null ) {
if ( empty( $item_source['id'] ) || empty( $item_source['source_type'] ) ) {
if ( Item::is_empty_item_source( $item_source ) ) {
return false;
}

Expand All @@ -207,7 +207,7 @@ protected function get_url( $item_source, $object_key = null ) {
* @return string|false
*/
protected function get_base_url( $item_source ) {
if ( empty( $item_source['id'] ) || empty( $item_source['source_type'] ) ) {
if ( Item::is_empty_item_source( $item_source ) ) {
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions classes/filters/as3cf-s3-to-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function url_needs_replacing( $url ) {
* @return bool|string
*/
protected function get_url( $item_source, $object_key = null ) {
if ( empty( $item_source['id'] ) || empty( $item_source['source_type'] ) ) {
if ( Item::is_empty_item_source( $item_source ) ) {
return false;
}

Expand All @@ -134,7 +134,7 @@ protected function get_url( $item_source, $object_key = null ) {
* @return string|false
*/
protected function get_base_url( $item_source ) {
if ( empty( $item_source['id'] ) || empty( $item_source['source_type'] ) ) {
if ( Item::is_empty_item_source( $item_source ) ) {
return false;
}

Expand All @@ -155,7 +155,7 @@ protected function get_base_url( $item_source ) {
*
* @param string $url
*
* @return bool|int
* @return bool|array
*/
public function get_item_source_from_url( $url ) {
// Result for sized URL already cached in request, return it.
Expand All @@ -165,7 +165,7 @@ public function get_item_source_from_url( $url ) {

$item_source = Item::get_item_source_by_remote_url( $url );

if ( ! empty( $item_source['id'] ) ) {
if ( ! Item::is_empty_item_source( $item_source ) ) {
$this->query_cache[ $url ] = $item_source;

return $item_source;
Expand All @@ -187,7 +187,7 @@ public function get_item_source_from_url( $url ) {

$item_source = Item::get_item_source_by_remote_url( $full_url );

$this->query_cache[ $full_url ] = ! empty( $item_source['id'] ) ? $item_source : false;
$this->query_cache[ $full_url ] = ! Item::is_empty_item_source( $item_source ) ? $item_source : false;

return $this->query_cache[ $full_url ];
}
Expand Down
2 changes: 1 addition & 1 deletion classes/integrations/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function init() {
* @param array $options Handler dependent options that may have been set for the action.
*/
public function maybe_remove_local_files( $result, Item $as3cf_item, array $options ) {
if ( ! is_wp_error( $result ) && $this->as3cf->get_setting( 'remove-local-file', false ) && $as3cf_item->exists_locally() ) {
if ( ! is_wp_error( $result ) && $as3cf_item->id() && $this->as3cf->get_setting( 'remove-local-file', false ) && $as3cf_item->exists_locally() ) {
$remove_local_handler = $this->as3cf->get_item_handler( Remove_Local_Handler::get_item_handler_key_name() );

$remove_local_handler->handle( $as3cf_item );
Expand Down
12 changes: 3 additions & 9 deletions classes/integrations/media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,7 @@ public function wp_update_attachment_metadata( $data, $post_id ) {
*/
protected function upload_item( Media_Library_Item $as3cf_item, array $offloaded_files ) {
$upload_handler = $this->as3cf->get_item_handler( Upload_Handler::get_item_handler_key_name() );
$upload_result = $upload_handler->handle( $as3cf_item, array( 'offloaded_files' => $offloaded_files ) );

if ( is_wp_error( $upload_result ) ) {
foreach ( $upload_result->get_error_messages() as $error_message ) {
AS3CF_Error::Log( $error_message );
}
}
$upload_handler->handle( $as3cf_item, array( 'offloaded_files' => $offloaded_files ) );
}

/**
Expand Down Expand Up @@ -1124,7 +1118,7 @@ public function get_size_string_from_url_for_item_source( $size, $url, $item_sou
public function get_attachment_id_from_provider_url( $url ) {
$item_source = $this->as3cf->filter_provider->get_item_source_from_url( $url );

if ( ! empty( $item_source['id'] ) && ! empty( $item_source['source_type'] ) && Media_Library_Item::source_type() === $item_source['source_type'] ) {
if ( ! Item::is_empty_item_source( $item_source ) && Media_Library_Item::source_type() === $item_source['source_type'] ) {
return $item_source['id'];
}

Expand All @@ -1141,7 +1135,7 @@ public function get_attachment_id_from_provider_url( $url ) {
public function get_attachment_id_from_local_url( $url ) {
$item_source = $this->as3cf->filter_local->get_item_source_from_url( $url );

if ( ! empty( $item_source['id'] ) && ! empty( $item_source['source_type'] ) && Media_Library_Item::source_type() === $item_source['source_type'] ) {
if ( ! Item::is_empty_item_source( $item_source ) && Media_Library_Item::source_type() === $item_source['source_type'] ) {
return $item_source['id'];
}

Expand Down
18 changes: 9 additions & 9 deletions classes/items/download-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace DeliciousBrains\WP_Offload_Media\Items;

use AS3CF_Error;
use DeliciousBrains\WP_Offload_Media\Providers\Storage\Storage_Provider;
use Exception;
use WP_Error;
Expand Down Expand Up @@ -80,13 +79,13 @@ protected function handle_item( Item $as3cf_item, Manifest $manifest, array $opt
// we'll return an error.
$current_provider = $this->as3cf->get_storage_provider();
if ( ! empty( $current_provider ) && $current_provider::get_provider_key_name() !== $as3cf_item->provider() ) {
$message = sprintf(
$error_msg = sprintf(
__( '%1$s with ID %d is offloaded to a different provider than currently configured', 'amazon-s3-and-cloudfront' ),
$this->as3cf->get_source_type_name( $as3cf_item->source_type() ),
$as3cf_item->source_id()
);

return new WP_Error( 'exception', $message );
return $this->return_handler_error( $error_msg );
} else {
$provider_client = $this->as3cf->get_provider_client( $as3cf_item->region() );

Expand Down Expand Up @@ -148,20 +147,21 @@ private function download_object( $provider_client, $object ) {
// Make sure the local directory exists.
$dir = dirname( $object['SaveAs'] );
if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) {
$error_message = sprintf( __( 'The local directory %s does not exist and could not be created.', 'amazon-s3-and-cloudfront' ), $dir );
AS3CF_Error::log( sprintf( __( 'There was an error attempting to download the file %s from the bucket: %s', 'amazon-s3-and-cloudfront' ), $object['Key'], $error_message ) );
$error_msg = sprintf( __( 'The local directory %s does not exist and could not be created.', 'amazon-s3-and-cloudfront' ), $dir );
$error_msg = sprintf( __( 'There was an error attempting to download the file %1$s from the bucket: %2$s', 'amazon-s3-and-cloudfront' ), $object['Key'], $error_msg );

return $this->return_handler_error( $error_msg );
}

try {
$provider_client->get_object( $object );
} catch ( Exception $e ) {
$error_msg = sprintf( __( 'Error downloading %1$s from bucket: %2$s', 'amazon-s3-and-cloudfront' ), $object['Key'], $e->getMessage() );
AS3CF_Error::log( $error_msg );

// If storage provider file doesn't exist, an empty local file will be created, clean it up.
@unlink( $object['SaveAs'] );

return new WP_Error( 'download_object', $error_msg );
$error_msg = sprintf( __( 'Error downloading %1$s from bucket: %2$s', 'amazon-s3-and-cloudfront' ), $object['Key'], $e->getMessage() );

return $this->return_handler_error( $error_msg );
}

return true;
Expand Down
23 changes: 17 additions & 6 deletions classes/items/item-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public function handle( Item $as3cf_item, array $options = array() ) {

// Cancelled, let caller know that request was not handled.
if ( false !== $cancel ) {
// If something unexpected happened, let the caller know.
if ( is_wp_error( $cancel ) ) {
return $this->return_result( $cancel, $as3cf_item, $options );
}

return $this->return_result( false, $as3cf_item, $options );
}

Expand Down Expand Up @@ -177,18 +182,24 @@ abstract protected function handle_item( Item $as3cf_item, Manifest $manifest, a
abstract protected function post_handle( Item $as3cf_item, Manifest $manifest, array $options );

/**
* Helper to record errors and return meta data on handler error.
* Helper to record errors and return them or optional supplied value.
*
* @param string $error_msg
* @param array|null $return
* @param string|WP_Error $error_msg An error message or already constructed WP_Error.
* @param mixed|null $return Optional return value instead of WP_Error.
*
* @return array|WP_Error
* @return mixed|WP_Error
*/
protected function return_handler_error( $error_msg, $return = null ) {
AS3CF_Error::log( $error_msg );
if ( is_wp_error( $error_msg ) ) {
foreach ( $error_msg->get_error_messages() as $msg ) {
AS3CF_Error::Log( $msg );
}
} else {
AS3CF_Error::log( $error_msg );
}

if ( is_null( $return ) ) {
return new WP_Error( 'exception', $error_msg );
return is_wp_error( $error_msg ) ? $error_msg : new WP_Error( 'exception', $error_msg );
}

return $return;
Expand Down
31 changes: 25 additions & 6 deletions classes/items/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ public static function get_by_source_id( $source_id ) {

$source_id = (int) $source_id;

if ( empty( $source_id ) ) {
if ( $source_id < 0 ) {
return false;
}

Expand Down Expand Up @@ -1410,7 +1410,7 @@ public static function get_item_source_by_remote_url( $url ) {
*
* While source id isn't strictly unique, it is by source type, which is always used in queries based on called class.
*
* @param int $upper_bound Returned source_ids should be lower than this, use null/0 for no upper bound.
* @param int $upper_bound Returned source_ids should be lower than this, use null for no upper bound.
* @param int $limit Maximum number of source_ids to return. Required if not counting.
* @param bool $count Just return a count of matching source_ids? Negates $limit, default false.
* @param int $originator Optionally restrict to only records with given originator type from ORIGINATORS const.
Expand All @@ -1421,17 +1421,16 @@ public static function get_item_source_by_remote_url( $url ) {
public static function get_source_ids( $upper_bound, $limit, $count = false, $originator = null, $is_verified = null ) {
global $wpdb;

$args = array( static::$source_type );

if ( $count ) {
$sql = 'SELECT COUNT(DISTINCT source_id)';
} else {
$sql = 'SELECT DISTINCT source_id';
}

$sql .= ' FROM ' . static::items_table() . ' WHERE source_type = %s';
$sql .= ' FROM ' . static::items_table() . ' WHERE source_type = %s';
$args = array( static::$source_type );

if ( ! empty( $upper_bound ) ) {
if ( is_numeric( $upper_bound ) ) {
$sql .= ' AND source_id < %d';
$args[] = $upper_bound;
}
Expand Down Expand Up @@ -1967,4 +1966,24 @@ public function offloaded_files() {

return $offloaded_files;
}

/**
* Is the supplied item_source considered to be empty?
*
* @param array $item_source
*
* @return bool
*/
public static function is_empty_item_source( $item_source ) {
if (
empty( $item_source['source_type'] ) ||
! isset( $item_source['id'] ) ||
! is_numeric( $item_source['id'] ) ||
$item_source['id'] < 0
) {
return true;
}

return false;
}
}
7 changes: 3 additions & 4 deletions classes/items/remove-local-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace DeliciousBrains\WP_Offload_Media\Items;

use AS3CF_Error;
use WP_Error;

class Remove_Local_Handler extends Item_Handler {
/**
Expand Down Expand Up @@ -51,7 +50,7 @@ public static function default_options() {
* @param Item $as3cf_item
* @param array $options
*
* @return Manifest|WP_Error
* @return Manifest
*/
protected function pre_handle( Item $as3cf_item, array $options ) {
$manifest = new Manifest();
Expand Down Expand Up @@ -143,7 +142,7 @@ protected function pre_handle( Item $as3cf_item, array $options ) {
* @param Manifest $manifest
* @param array $options
*
* @return bool|WP_Error
* @return bool
*/
protected function handle_item( Item $as3cf_item, Manifest $manifest, array $options ) {
foreach ( $manifest->objects as &$file_to_remove ) {
Expand Down Expand Up @@ -175,7 +174,7 @@ protected function handle_item( Item $as3cf_item, Manifest $manifest, array $opt
* @param Manifest $manifest
* @param array $options
*
* @return bool|WP_Error
* @return bool
*/
protected function post_handle( Item $as3cf_item, Manifest $manifest, array $options ) {
if ( empty( $manifest->objects ) ) {
Expand Down
Loading

0 comments on commit 084825e

Please sign in to comment.