Skip to content

Commit

Permalink
Deploying version 2.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmjones committed Dec 9, 2019
1 parent ac0998d commit 0142a3b
Show file tree
Hide file tree
Showing 196 changed files with 9,723 additions and 5,175 deletions.
8 changes: 7 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.3
**Requires PHP:** 5.5
**Stable tag:** 2.3.1
**Stable tag:** 2.3.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 @@ -89,6 +89,12 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### WP Offload Media Lite 2.3.2 - 2019-12-09 ###
* Improvement: Reduced database queries when external object cache available
* Bug fix: Uncaught Error: Call to undefined function DeliciousBrains\WP_Offload_Media\Gcp\GuzzleHttp\choose_handler()
* Bug fix: SVG files not automatically offloaded
* Tested: PHP 7.4

### WP Offload Media Lite 2.3.1 - 2019-11-19 ###
* Bug fix: Uncaught Error: Cannot use object of type Media_Library_Item as array in wp-includes/media.php:217
* Bug fix: Image not automatically offloaded if subsizes not expected
Expand Down
2 changes: 1 addition & 1 deletion classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ function wp_update_attachment_metadata( $data, $post_id ) {
// Protect against updates of partially formed metadata since WordPress 5.3.
// Checks whether new upload currently has no subsizes recorded but is expected to have subsizes during upload,
// and if so, are any of its currently missing sizes part of the set.
if ( function_exists( 'wp_get_registered_image_subsizes' ) && function_exists( 'wp_get_missing_image_subsizes' ) ) {
if ( ! empty( $data ) && function_exists( 'wp_get_registered_image_subsizes' ) && function_exists( 'wp_get_missing_image_subsizes' ) ) {
if ( empty( $data['sizes'] ) && wp_attachment_is_image( $post_id ) ) {

// There is no unified way of checking whether subsizes are expected, so we have to duplicate WordPress code here.
Expand Down
188 changes: 180 additions & 8 deletions classes/items/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ abstract class Item {
protected static $items_cache_by_path = array();
protected static $items_cache_by_source_path = array();

/**
* @var array Keys with array of fields that can be used for cache lookups.
*/
protected static $cache_keys = array(
'id' => array( 'id' ),
'source_id' => array( 'source_id' ),
'path' => array( 'path', 'original_path' ),
'source_path' => array( 'source_path', 'original_source_path' ),
);

private $id;
private $provider;
private $region;
Expand Down Expand Up @@ -71,6 +81,136 @@ public function __construct( $provider, $region, $bucket, $path, $is_private, $s
static::add_to_items_cache( $this );
}

/**
* Returns the string used to group all keys in the object cache by.
*
* @return string
*/
protected static function get_object_cache_group() {
static $group;

if ( empty( $group ) ) {
/** @var Amazon_S3_And_CloudFront $as3cf */
global $as3cf;
$group = trim( '' . apply_filters( 'as3cf_object_cache_group', $as3cf->get_plugin_prefix() ) );
}

return $group;
}

/**
* Get base string for all of current blog's object cache keys.
*
* @return string
*/
protected static function get_object_cache_base_key() {
$blog_id = get_current_blog_id();

return static::items_table() . '-' . $blog_id . '-' . static::$source_type;
}

/**
* Get full object cache key.
*
* @param string $base_key
* @param string $key
* @param string $field
*
* @return string
*/
protected static function get_object_cache_full_key( $base_key, $key, $field ) {
return sanitize_text_field( $base_key . '-' . $key . '-' . $field );
}

/**
* Add the given item to the object cache.
*
* @param Item $item
*/
protected static function add_to_object_cache( $item ) {
if ( empty( $item ) || empty( static::$cache_keys ) ) {
return;
}

$base_key = static::get_object_cache_base_key();
$group = static::get_object_cache_group();

$keys = array();

foreach ( static::$cache_keys as $key => $fields ) {
foreach ( $fields as $field ) {
$full_key = static::get_object_cache_full_key( $base_key, $key, $item->{$field}() );

if ( in_array( $full_key, $keys ) ) {
continue;
}

wp_cache_set( $full_key, $item, $group );

$keys[] = $full_key;
}
}
}

/**
* Delete the given item from the object cache.
*
* @param Item $item
*/
protected static function remove_from_object_cache( $item ) {
if ( empty( $item ) || empty( static::$cache_keys ) ) {
return;
}

$base_key = static::get_object_cache_base_key();
$group = static::get_object_cache_group();

$keys = array();

foreach ( static::$cache_keys as $key => $fields ) {
foreach ( $fields as $field ) {
$full_key = static::get_object_cache_full_key( $base_key, $key, $item->{$field}() );

if ( in_array( $full_key, $keys ) ) {
continue;
}

wp_cache_delete( $full_key, $group );

$keys[] = $full_key;
}
}
}

/**
* Try and get Item from object cache by known key and value.
*
* Note: Actual lookup is scoped by blog and item's source_type, so example key may be 'source_id'.
*
* @param string $key The base of the key that makes up the lookup, e.g. field for given value.
* @param mixed $value Will be coerced to string for lookup.
*
* @return bool|Item
*/
protected static function get_from_object_cache( $key, $value ) {
if ( ! array_key_exists( $key, static::$cache_keys ) ) {
return false;
}

$base_key = static::get_object_cache_base_key();
$full_key = static::get_object_cache_full_key( $base_key, $key, $value );
$group = static::get_object_cache_group();
$force = false;
$found = false;
$result = wp_cache_get( $full_key, $group, $force, $found );

if ( $found ) {
return $result;
}

return false;
}

/**
* (Re)initialize the static cache used for speeding up queries.
*/
Expand Down Expand Up @@ -111,7 +251,7 @@ protected static function add_to_items_cache( $item ) {
}

/**
* Add an item to the static cache to allow fast retrieval via get_from_items_cache_by_* functions.
* Remove an item from the static cache that allows fast retrieval via get_from_items_cache_by_* functions.
*
* @param Item $item
*/
Expand Down Expand Up @@ -151,6 +291,14 @@ private static function get_from_items_cache_by_id( $id ) {
return static::$items_cache_by_id[ $blog_id ][ $id ];
}

$item = static::get_from_object_cache( 'id', $id );

if ( $item ) {
static::add_to_items_cache( $item );

return $item;
}

return false;
}

Expand All @@ -168,6 +316,14 @@ private static function get_from_items_cache_by_source_id( $source_id ) {
return static::$items_cache_by_source_id[ $blog_id ][ static::$source_type ][ $source_id ];
}

$item = static::get_from_object_cache( 'source_id', $source_id );

if ( $item ) {
static::add_to_items_cache( $item );

return $item;
}

return false;
}

Expand Down Expand Up @@ -372,14 +528,22 @@ public function save() {
$this->id = $wpdb->insert_id;

// Now that the item has an ID it should be (re)cached.
self::add_to_items_cache( $this );
static::add_to_items_cache( $this );
}
} else {
// Make sure object cache does not have stale items.
$old_item = static::get_from_object_cache( 'id', $this->id() );
static::remove_from_object_cache( $old_item );
unset( $old_item );

$result = $wpdb->update( static::items_table(), $this->key_values(), array( 'id' => $this->id ), $this->formats(), array( '%d' ) );
}

if ( false === $result ) {
self::remove_from_items_cache( $this );
if ( $result ) {
// Now that the item has an ID it should be (re)cached.
static::add_to_object_cache( $this );
} else {
static::remove_from_items_cache( $this );

return new WP_Error( 'item_save', 'Error saving item:- ' . $wpdb->last_error );
}
Expand All @@ -396,6 +560,7 @@ public function delete() {
global $wpdb;

static::remove_from_items_cache( $this );
static::remove_from_object_cache( $this );

if ( empty( $this->id ) ) {
return new WP_Error( 'item_delete', 'Error trying to delete item with no id.' );
Expand All @@ -414,17 +579,18 @@ public function delete() {
* Creates an item based on object from database.
*
* @param object $object
* @param bool $add_to_object_cache Should this object be added to the object cache too?
*
* @return Item
*/
protected static function create( $object ) {
protected static function create( $object, $add_to_object_cache = false ) {
$extra_info = array();

if ( ! empty( $object->extra_info ) ) {
$extra_info = unserialize( $object->extra_info );
}

return new static(
$item = new static(
$object->provider,
$object->region,
$object->bucket,
Expand All @@ -436,6 +602,12 @@ protected static function create( $object ) {
$extra_info,
$object->id
);

if ( $add_to_object_cache ) {
static::add_to_object_cache( $item );
}

return $item;
}

/**
Expand Down Expand Up @@ -466,7 +638,7 @@ public static function get_by_id( $id ) {
return false;
}

return static::create( $object );
return static::create( $object, true );
}

/**
Expand Down Expand Up @@ -505,7 +677,7 @@ public static function get_by_source_id( $source_id ) {
return false;
}

return static::create( $object );
return static::create( $object, true );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion languages/amazon-s3-and-cloudfront-en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: amazon-s3-and-cloudfront\n"
"Report-Msgid-Bugs-To: nom@deliciousbrains.com\n"
"POT-Creation-Date: 2019-11-19 10:16+0000\n"
"POT-Creation-Date: 2019-12-09 11:47+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: uploads, amazon, s3, amazon s3, digitalocean, digitalocean spaces, google
Requires at least: 4.9
Tested up to: 5.3
Requires PHP: 5.5
Stable tag: 2.3.1
Stable tag: 2.3.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 @@ -81,6 +81,12 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

== Changelog ==

= WP Offload Media Lite 2.3.2 - 2019-12-09 =
* Improvement: Reduced database queries when external object cache available
* Bug fix: Uncaught Error: Call to undefined function DeliciousBrains\WP_Offload_Media\Gcp\GuzzleHttp\choose_handler()
* Bug fix: SVG files not automatically offloaded
* Tested: PHP 7.4

= WP Offload Media Lite 2.3.1 - 2019-11-19 =
* Bug fix: Uncaught Error: Cannot use object of type Media_Library_Item as array in wp-includes/media.php:217
* Bug fix: Image not automatically offloaded if subsizes not expected
Expand Down
2 changes: 1 addition & 1 deletion vendor/Gcp/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return \ComposerAutoloaderInit9fd2c411275a48d2ebf11f5a1bd6ca20::getLoader();
return \ComposerAutoloaderInitad2bbf672104326f33ebff61e2e8e9b8::getLoader();
2 changes: 1 addition & 1 deletion vendor/Gcp/composer/autoload_classmap.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion vendor/Gcp/composer/autoload_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// autoload_files.php @generated by Composer
$vendorDir = \dirname(\dirname(__FILE__));
$baseDir = \dirname($vendorDir);
return array('7b11c4dc42b3b3023073cb14e519683c' => $vendorDir . '/ralouphie/getallheaders/src/getallheaders.php', 'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php', 'a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php', '37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php');
return array('dbi_as3cf_gcp_7b11c4dc42b3b3023073cb14e519683c' => $vendorDir . '/ralouphie/getallheaders/src/getallheaders.php', 'dbi_as3cf_gcp_c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php', 'dbi_as3cf_gcp_a0edc8309cc5e1d60e3047b5df6b7052' => $vendorDir . '/guzzlehttp/psr7/src/functions_include.php', 'dbi_as3cf_gcp_37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php');
2 changes: 1 addition & 1 deletion vendor/Gcp/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// autoload_psr4.php @generated by Composer
$vendorDir = \dirname(\dirname(__FILE__));
$baseDir = \dirname($vendorDir);
return array('DeliciousBrains\\WP_Offload_Media\\Gcp\\Psr\\Log\\' => array($vendorDir . '/psr/log/Psr/Log'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Psr\\Cache\\' => array($vendorDir . '/psr/cache/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Google\\Cloud\\Storage\\' => array($vendorDir . '/google/cloud-storage/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Google\\Cloud\\Core\\' => array($vendorDir . '/google/cloud-core/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Google\\Auth\\' => array($vendorDir . '/google/auth/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Firebase\\JWT\\' => array($vendorDir . '/firebase/php-jwt/src'));
return array('DeliciousBrains\\WP_Offload_Media\\Gcp\\Psr\\Log\\' => array($vendorDir . '/psr/log/Psr/Log'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Psr\\Cache\\' => array($vendorDir . '/psr/cache/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Google\\Cloud\\Storage\\' => array($vendorDir . '/google/cloud-storage/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Google\\Cloud\\Core\\' => array($vendorDir . '/google/cloud-core/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Google\\CRC32\\' => array($vendorDir . '/google/crc32/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Google\\Auth\\' => array($vendorDir . '/google/auth/src'), 'DeliciousBrains\\WP_Offload_Media\\Gcp\\Firebase\\JWT\\' => array($vendorDir . '/firebase/php-jwt/src'));
Loading

0 comments on commit 0142a3b

Please sign in to comment.