Skip to content

Commit

Permalink
Deploying version 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmjones committed Jun 11, 2019
1 parent dc35394 commit b76f400
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 176 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# WP Offload Media Lite for Amazon S3, DigitalOcean Spaces, and Google Cloud Storage #
**Contributors:** bradt, deliciousbrains, ianmjones
**Tags:** uploads, amazon, s3, amazon s3, digitalocean, digitalocean spaces, google cloud storage, gcs, mirror, admin, media, cdn, cloudfront
**Requires at least:** 4.7
**Requires at least:** 4.9
**Tested up to:** 5.2
**Requires PHP:** 5.5
**Stable tag:** 2.2-dev
**Stable tag:** 2.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 @@ -86,6 +86,13 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### WP Offload Media Lite 2.2 - 2019-06-10 ###
* [Release Summary Blog Post](https://deliciousbrains.com/wp-offload-media-2-2-released/?utm_campaign=changelogs&utm_source=wordpress.org&utm_medium=free%2Bplugin%2Blisting)
* New: Use IAM Roles without having to update wp-config.php
* New: Frankfurt (FRA1) region now supported on DigitalOcean Spaces
* Improvement: WP dashboard performance
* Bug fix: Uploaded media files with uppercase extensions get second extension added

### WP Offload Media Lite 2.1.1 - 2019-04-29 ###
* New: Multisite domain mapping via WordPress MU Domain Mapping plugin is now supported
* Improvement: Local to Provider content filtering performance improvements
Expand Down
107 changes: 61 additions & 46 deletions classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,7 @@ public function filter_unique_filename( $filename, $ext, $dir, $post_id = null )

// sanitize the file name before we begin processing
$filename = sanitize_file_name( $filename );
$ext = strtolower( $ext );
$name = wp_basename( $filename, $ext );

// Edge case: if file is named '.ext', treat as an empty name.
Expand All @@ -1534,7 +1535,6 @@ public function filter_unique_filename( $filename, $ext, $dir, $post_id = null )
}

// Rebuild filename with lowercase extension as provider will have converted extension on upload.
$ext = strtolower( $ext );
$filename = $name . $ext;
$time = current_time( 'mysql' );

Expand Down Expand Up @@ -2925,6 +2925,7 @@ function get_settings_whitelist() {
'secret-access-key',
'key-file-path',
'key-file',
'use-server-roles',
'bucket',
'region',
'domain',
Expand Down Expand Up @@ -3011,7 +3012,7 @@ public function handle_post_request() {
// If anything about the Provider has changed then we need to verify the bucket selection.
// Otherwise we can let the filter decide whether there is an action to take.
// Last implementer will win, but the above handlers take care of grouping things appropriately.
if ( in_array( $key, array( 'provider', 'access-key-id', 'secret-access-key', 'key-file' ) ) && ! $this->get_defined_setting( 'bucket', false ) ) {
if ( in_array( $key, array( 'provider', 'access-key-id', 'secret-access-key', 'key-file', 'use-server-roles' ) ) && ! $this->get_defined_setting( 'bucket', false ) ) {
$action = 'change-bucket';
break;
} else {
Expand Down Expand Up @@ -3189,6 +3190,10 @@ protected function handle_save_settings() {
}
}

if ( 'use-server-roles' === $var && 'server-role' !== $_POST['authmethod'] ) {
continue;
}

$this->set_setting( $var, $value );

// Some setting changes might have knock-on effects that require confirmation of secondary settings.
Expand Down Expand Up @@ -3713,14 +3718,14 @@ function output_diagnostic_info( $escape = true ) {
* Media
*/

$media_counts = $this->diagnostic_media_counts();
$media_counts = $this->media_counts();

$output .= 'Media Files: ';
$output .= number_format_i18n( $media_counts['all'] );
$output .= number_format_i18n( $media_counts['total'] );
$output .= "\r\n";

$output .= 'Offloaded Media Files: ';
$output .= number_format_i18n( $media_counts['s3'] );
$output .= number_format_i18n( $media_counts['offloaded'] );
$output .= "\r\n";

$output .= 'Number of Image Sizes: ';
Expand Down Expand Up @@ -3815,9 +3820,9 @@ function output_diagnostic_info( $escape = true ) {
$output .= "\r\n";

if ( $provider::use_server_roles_allowed() ) {
$output .= 'Use Server Role: ' . $this->on_off( $provider::use_server_roles() );
$output .= 'Use Server Roles: ' . $this->on_off( $provider->use_server_roles() );
} else {
$output .= 'Use Server Role: N/A';
$output .= 'Use Server Roles: N/A';
}
$output .= "\r\n";

Expand Down Expand Up @@ -4273,70 +4278,80 @@ public function get_memory_limit() {
}

/**
* Count attachments on a site
* Count attachments on a site.
*
* @param string $prefix
* @param null|bool $uploaded_to_provider
* null - All attachments
* true - Attachments only uploaded to provider
* false - Attachments not uploaded to provider
* @param bool $skip_transient Whether to force database query and skip transient, default false
* @param string $prefix
* @param bool $skip_transient Whether to force database query and skip transient, default false
* @param bool $force Whether to force database query and skip static cache, implies $skip_transient, default false
*
* @return int
* @return array Keys:
* total: Total media count for site (prefix)
* offloaded: Count of offloaded media for site (prefix)
* not_offloaded: Difference between total and offloaded
*/
public function count_attachments( $prefix, $uploaded_to_provider = null, $skip_transient = false ) {
public function count_attachments( $prefix, $skip_transient = false, $force = false ) {
global $wpdb;

$transient_key = 'as3cf_' . $prefix . '_media_count';
static $counts;
static $skips;

$sql = "SELECT COUNT(DISTINCT p.ID)
FROM `{$prefix}posts` p";
$transient_key = 'as3cf_' . $prefix . '_attachment_counts';

$where = "WHERE p.post_type = 'attachment'";

if ( ! is_null( $uploaded_to_provider ) && is_bool( $uploaded_to_provider ) ) {
$sql .= " LEFT OUTER JOIN `{$prefix}postmeta` pm
ON p.`ID` = pm.`post_id`
AND pm.`meta_key` = 'amazonS3_info'";
// Been here, done it, won't do it again!
// Well, unless this is the first transient skip for the prefix, then we need to do it.
if ( ! $force && ! empty( $counts[ $transient_key ] ) && ( false === $skip_transient || ! empty( $skips[ $transient_key ] ) ) ) {
return $counts[ $transient_key ];
}

$operator = $uploaded_to_provider ? 'not ' : '';
$where .= " AND pm.`post_id` is {$operator}null";
if ( $force || $skip_transient || false === ( $attachment_counts = get_site_transient( $transient_key ) ) ) {
$sql = "
SELECT COUNT(DISTINCT p.`ID`) total, COUNT(DISTINCT pm.`post_id`) offloaded
FROM `{$prefix}posts` p
LEFT OUTER JOIN `{$prefix}postmeta` pm ON p.`ID` = pm.`post_id` AND pm.`meta_key` = 'amazonS3_info'
WHERE p.`post_type` = 'attachment'
";

$transient_key .= ( $uploaded_to_provider ) ? '_offloaded' : 'not_offloaded';
}
$attachment_counts = $wpdb->get_row( $sql, ARRAY_A );

$sql .= ' ' . $where;
$attachment_counts['not_offloaded'] = $attachment_counts['total'] - $attachment_counts['offloaded'];

if ( true === $skip_transient || false === ( $count = get_site_transient( $transient_key ) ) ) {
$count = (int) $wpdb->get_var( $sql );
set_site_transient( $transient_key, $attachment_counts, 2 * MINUTE_IN_SECONDS );

set_site_transient( $transient_key, $count, 2 * MINUTE_IN_SECONDS );
// One way or another we've skipped the transient.
$skips[ $transient_key ] = true;
}

return $count;
$counts[ $transient_key ] = $attachment_counts;

return $attachment_counts;
}

/**
* Get the total attachment and total S3 attachment counts for the diagnostic log
* Get the total attachment and total offloaded/not offloaded attachment counts
*
* @param bool $skip_transient Whether to force database query and skip transient, default false
* @param bool $force Whether to force database query and skip static cache, implies $skip_transient, default false
*
* @return array
*/
protected function diagnostic_media_counts() {
if ( false === ( $attachment_counts = get_site_transient( 'as3cf_attachment_counts' ) ) ) {
$table_prefixes = $this->get_all_blog_table_prefixes();
$all_media = 0;
$all_media_provider = 0;
public function media_counts( $skip_transient = false, $force = false ) {
if ( $skip_transient || false === ( $attachment_counts = get_site_transient( 'as3cf_attachment_counts' ) ) ) {
$table_prefixes = $this->get_all_blog_table_prefixes();
$total = 0;
$offloaded = 0;
$not_offloaded = 0;

foreach ( $table_prefixes as $blog_id => $table_prefix ) {
$count = $this->count_attachments( $table_prefix );
$all_media += $count;
$provider_count = $this->count_attachments( $table_prefix, true );
$all_media_provider += $provider_count;
$counts = $this->count_attachments( $table_prefix, $skip_transient, $force );
$total += $counts['total'];
$offloaded += $counts['offloaded'];
$not_offloaded += $counts['not_offloaded'];
}

$attachment_counts = array(
'all' => $all_media,
's3' => $all_media_provider,
'total' => $total,
'offloaded' => $offloaded,
'not_offloaded' => $not_offloaded,
);

set_site_transient( 'as3cf_attachment_counts', $attachment_counts, 2 * MINUTE_IN_SECONDS );
Expand Down
6 changes: 3 additions & 3 deletions classes/as3cf-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function __clone() {
*/
public function add_notice( $message, $args = array() ) {
$defaults = array(
'type' => 'info',
'type' => 'notice-info',
'dismissible' => true,
'inline' => false,
'flash' => true,
Expand Down Expand Up @@ -250,7 +250,7 @@ public function undismiss_notice_for_all( $notice_id ) {

$users = get_users( $args );

foreach( $users as $user ) {
foreach ( $users as $user ) {
$this->undismiss_notice_for_user( $notice_id, $user->ID );
}
}
Expand Down Expand Up @@ -464,7 +464,7 @@ public function ajax_dismiss_notice() {
*/
protected function update_user_meta( $user_id, $key, $value ) {
if ( empty( $value ) ) {
delete_user_meta( $user_id, $key);
delete_user_meta( $user_id, $key );
} else {
update_user_meta( $user_id, $key, $value );
}
Expand Down
1 change: 1 addition & 0 deletions classes/providers/digitalocean-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class DigitalOcean_Provider extends AWS_Provider {
'ams3' => 'Amsterdam',
'sgp1' => 'Singapore',
'sfo2' => 'San Francisco',
'fra1' => 'Frankfurt',
);

/**
Expand Down
25 changes: 17 additions & 8 deletions classes/providers/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ abstract class Provider {
*/
protected static $secret_access_key_setting_name = 'secret-access-key';

/**
* @var string
*/
protected static $use_server_roles_setting_name = 'use-server-roles';

/**
* @var string
*/
Expand Down Expand Up @@ -218,7 +223,7 @@ public static function use_access_keys_allowed() {
* @return bool
*/
public function needs_access_keys() {
if ( static::use_server_roles() ) {
if ( $this->use_server_roles() ) {
return false;
}

Expand Down Expand Up @@ -313,7 +318,7 @@ public static function use_server_roles_allowed() {
*
* @return string
*/
public static function preferred_use_server_role_constant() {
public static function preferred_use_server_roles_constant() {
if ( static::use_server_roles_allowed() ) {
return static::$use_server_roles_constants[0];
} else {
Expand All @@ -328,22 +333,26 @@ public static function preferred_use_server_role_constant() {
*
* @return bool
*/
public static function use_server_roles() {
public function use_server_roles() {
if ( ! static::use_server_roles_allowed() ) {
return false;
}

$constant = static::use_server_role_constant();
if ( static::use_server_roles_constant() ) {
$constant = static::use_server_roles_constant();

return $constant && constant( $constant );
return $constant ? constant( $constant ) : false;
}

return $this->as3cf->get_core_setting( static::$use_server_roles_setting_name, false );
}

/**
* Get the constant used to enable the use of EC2 IAM roles.
* Get the constant used to enable the use of IAM roles.
*
* @return string|false Constant name if defined, otherwise false
*/
public static function use_server_role_constant() {
public static function use_server_roles_constant() {
return AS3CF_Utils::get_first_defined_constant( static::$use_server_roles_constants );
}

Expand Down Expand Up @@ -572,7 +581,7 @@ private function _init_client( Array $args ) {

if ( is_null( $this->client ) ) {
// There's no extra client authentication config required when using server roles.
if ( ! static::use_server_roles() ) {
if ( ! $this->use_server_roles() ) {
// Some providers can supply Key File contents or Key File Path.
if ( static::use_key_file() ) {
// Key File contents take precedence over Key File Path.
Expand Down
14 changes: 6 additions & 8 deletions classes/upgrades/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ protected function run_upgrade() {
break;
}
} while ( $blog_id = $this->next_blog_id() );

} catch ( No_More_Blogs_Exception $e ) {
/*
* The upgrade is complete when there are no more blogs left to finish.
Expand All @@ -322,10 +321,10 @@ protected function run_upgrade() {
/**
* Upgrade the current blog.
*
* @throws Too_Many_Errors_Exception
* @return bool true if all items for the blog were upgraded, otherwise false.
* @throws Batch_Limits_Exceeded_Exception
*
* @return bool true if all items for the blog were upgraded, otherwise false.
* @throws Too_Many_Errors_Exception
*/
protected function upgrade_blog() {
$total = $this->count_items_to_process();
Expand Down Expand Up @@ -358,9 +357,9 @@ protected function upgrade_blog() {
/**
* Get the next sequential blog ID if there is one.
*
* @return int
* @throws No_More_Blogs_Exception
*
* @return int
*/
protected function next_blog_id() {
$blog_id = $this->blog_id ?: $this->last_blog_id;
Expand All @@ -371,7 +370,6 @@ protected function next_blog_id() {
if ( $blog_id < 1 ) {
throw new No_More_Blogs_Exception;
}

} while ( ! $this->is_blog_processable( $blog_id ) );

return $blog_id;
Expand Down Expand Up @@ -505,16 +503,16 @@ protected function calculate_progress() {
} else {
// Set up any per-site state
$this->switch_to_blog( get_current_blog_id() );
$total_items = $this->as3cf->count_attachments( $this->blog_prefix );
$counts = $this->as3cf->count_attachments( $this->blog_prefix );

// If there are no attachments, disable progress calculation
// and protect against division by zero.
if ( ! $total_items ) {
if ( ! $counts['total'] ) {
return false;
}

$remaining = $this->count_items_to_process();
$decimal = ( $total_items - $remaining ) / $total_items;
$decimal = ( $counts['total'] - $remaining ) / $counts['total'];
}

return round( $decimal * 100, 2 );
Expand Down
Loading

0 comments on commit b76f400

Please sign in to comment.