Skip to content

Commit

Permalink
Deploying version 2.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
eriktorsner committed Apr 4, 2022
1 parent 084825e commit 8f641c0
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 86 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.1
**Stable tag:** 2.6.2
**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.2 - 2022-04-04 ###
* Bug fix: Upgrade routine no longer risks breaking items when external object cache is in use

### 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

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 @@ -27,6 +27,7 @@
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\Fix_Broken_Item_Extra_Data;
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 @@ -167,7 +168,7 @@ class Amazon_S3_And_CloudFront extends AS3CF_Plugin_Base {
*/
protected $integration_manager;

const LATEST_UPGRADE_ROUTINE = 11;
const LATEST_UPGRADE_ROUTINE = 12;

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

// Plugin setup
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
Expand Down
21 changes: 13 additions & 8 deletions classes/items/download-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,23 @@ protected function handle_item( Item $as3cf_item, Manifest $manifest, array $opt
* @return bool|WP_Error
*/
protected function post_handle( Item $as3cf_item, Manifest $manifest, array $options ) {
// Look for errors
$errors = new WP_Error;
$i = 1;
$error_count = 0;

foreach ( $manifest->objects as $manifest_object ) {
if ( $manifest_object['download_result']['status'] !== self::STATUS_OK ) {
$errors->add( 'download-error-' . $i++, $manifest_object['download_result']['message'] );
if ( self::STATUS_OK !== $manifest_object['download_result']['status'] ) {
$error_count++;
}
}

if ( count( $errors->get_error_codes() ) ) {
return $errors;
if ( $error_count > 0 ) {
$error_message = sprintf(
__( 'There were %1$d errors downloading files for %2$s ID %3$d from bucket', 'amazon-s3-and-cloudfront' ),
$error_count,
$this->as3cf->get_source_type_name( $as3cf_item->source_type() ),
$as3cf_item->source_id()
);

return new WP_Error( 'download-error', $error_message );
}

$as3cf_item->update_filesize_after_download_local();
Expand Down Expand Up @@ -166,4 +171,4 @@ private function download_object( $provider_client, $object ) {

return true;
}
}
}
41 changes: 39 additions & 2 deletions classes/items/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class Item {
);

private static $checked_table_exists = array();
private static $enable_cache = true;

private $id;
private $provider;
Expand Down Expand Up @@ -179,6 +180,20 @@ public static function primary_object_key() {
return '__as3cf_primary';
}

/**
* Enable the built-in Item cache.
*/
public static function enable_cache() {
self::$enable_cache = true;
}

/**
* Disable the built-in Item cache.
*/
public static function disable_cache() {
self::$enable_cache = false;
}

/**
* Returns the string used to group all keys in the object cache by.
*
Expand Down Expand Up @@ -389,6 +404,10 @@ protected static function remove_from_items_cache( $item ) {
* @return bool|Item
*/
private static function get_from_items_cache_by_id( $id ) {
if ( false === self::$enable_cache ) {
return false;
}

$blog_id = get_current_blog_id();

if ( ! empty( static::$items_cache_by_id[ $blog_id ][ $id ] ) ) {
Expand All @@ -414,6 +433,10 @@ private static function get_from_items_cache_by_id( $id ) {
* @return bool|Item
*/
private static function get_from_items_cache_by_source_id( $source_id ) {
if ( false === self::$enable_cache ) {
return false;
}

$blog_id = get_current_blog_id();

if ( ! empty( static::$items_cache_by_source_id[ $blog_id ][ static::$source_type ][ $source_id ] ) ) {
Expand All @@ -440,6 +463,10 @@ private static function get_from_items_cache_by_source_id( $source_id ) {
* @return bool|Item
*/
private static function get_from_items_cache_by_bucket_and_path( $bucket, $path ) {
if ( false === self::$enable_cache ) {
return false;
}

$blog_id = get_current_blog_id();

if ( ! empty( static::$items_cache_by_path[ $blog_id ][ static::$source_type ][ $path ] ) ) {
Expand Down Expand Up @@ -1896,7 +1923,7 @@ public function remove_duplicate_paths( Item $as3cf_item, $paths ) {
*/
protected static function maybe_update_extra_info( &$extra_info, $source_id, $is_private ) {
if ( ! is_array( $extra_info ) ) {
return;
$extra_info = array();
}

// Compatibility fallback for if just an array of private sizes is supplied.
Expand All @@ -1905,6 +1932,16 @@ protected static function maybe_update_extra_info( &$extra_info, $source_id, $is
$private_sizes = $extra_info;
}

// Compatibility fallback for old broken format.
if ( isset( $extra_info['private_sizes'] ) && isset( $extra_info['private_sizes']['private_sizes'] ) ) {
$extra_info['private_sizes'] = $extra_info['private_sizes']['private_sizes'];
}

// Extra info must have at least one element, if not it's broken.
if ( isset( $extra_info['objects'] ) && 0 === count( $extra_info['objects'] ) ) {
unset( $extra_info['objects'] );
}

if ( ! isset( $extra_info['objects'] ) ) {
$private_sizes = isset( $extra_info['private_sizes'] ) && is_array( $extra_info['private_sizes'] ) ? $extra_info['private_sizes'] : $private_sizes;
$extra_info['objects'] = array();
Expand Down Expand Up @@ -1986,4 +2023,4 @@ public static function is_empty_item_source( $item_source ) {

return false;
}
}
}
74 changes: 74 additions & 0 deletions classes/upgrades/fix-broken-item-extra-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Upgrade extra info in custom objects table.
*
* @package amazon-s3-and-cloudfront
* @subpackage Classes/Upgrades/Upgrade_Item_Extra_Data
* @copyright Copyright (c) 2022, Delicious Brains
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 2.6.2
*/

namespace DeliciousBrains\WP_Offload_Media\Upgrades;

use DeliciousBrains\WP_Offload_Media\Items\Item;

/**
* Fix_Broken_Item_Extra_Data Class
*
* This class handles updating extra info in the custom objects table.
*
* @since 2.6.2
*/
class Fix_Broken_Item_Extra_Data extends Upgrade_Item_Extra_Data {

/**
* @var int
*/
protected $upgrade_id = 12;

/**
* @var string
*/
protected $upgrade_name = 'fix_broken_item_extra_data';

/**
* Wrapper for database call to get items with legacy extra info.
*
* @param string $prefix Table prefix for blog.
* @param bool $count return count of attachments
* @param null|int $limit
*
* @return mixed
*/
protected function get_items_with_old_extra_info( $prefix, $count = false, $limit = null ) {
global $wpdb;

$table = Item::ITEMS_TABLE;

/**
* Find items with empty object array or that still have the private_sizes array element in
* extra_info
*/
$sql = "
FROM {$prefix}{$table}
WHERE (extra_info LIKE '%s:7:\"objects\";a:0%' OR extra_info LIKE '%s:13:\"private_sizes\";a%' OR extra_info LIKE 's:%')
AND source_type='media-library'
";

if ( $count ) {
$sql = 'SELECT COUNT(source_id)' . $sql;

return $wpdb->get_var( $sql );
}

$sql = 'SELECT source_id' . $sql;
$sql .= ' ORDER BY id';

if ( $limit && $limit > 0 ) {
$sql .= sprintf( ' LIMIT %d', (int) $limit );
}

return $wpdb->get_results( $sql, OBJECT );
}
}
7 changes: 5 additions & 2 deletions classes/upgrades/upgrade-item-extra-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use AS3CF_Error;
use DeliciousBrains\WP_Offload_Media\Items\Item;
use DeliciousBrains\WP_Offload_Media\Items\Media_Library_Item;
use stdClass;

/**
* Upgrade_Item_Extra_Data Class
Expand Down Expand Up @@ -51,12 +52,14 @@ protected function get_running_update_text() {
/**
* Update extra_info in items table
*
* @param $item
* @param stdClass $item
*
* @return bool
*/
protected function upgrade_item( $item ) {
Item::disable_cache();
$as3cf_item = Media_Library_Item::get_by_source_id( $item->source_id );
Item::enable_cache();

if ( ! $as3cf_item ) {
AS3CF_Error::log( 'Could not construct item for attachment with ID ' . $item->source_id . '.' );
Expand Down Expand Up @@ -149,4 +152,4 @@ protected function get_items_with_old_extra_info( $prefix, $count = false, $limi

return $wpdb->get_results( $sql, OBJECT );
}
}
}
Loading

0 comments on commit 8f641c0

Please sign in to comment.