Skip to content

Commit

Permalink
Deploying version 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmjones committed Dec 7, 2022
1 parent 95a3183 commit 509afee
Show file tree
Hide file tree
Showing 409 changed files with 4,223 additions and 1,720 deletions.
238 changes: 165 additions & 73 deletions assets/js/settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/settings.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/settings.min.js

Large diffs are not rendered by default.

133 changes: 87 additions & 46 deletions classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
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\Fix_Broken_Item_Extra_Data;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Clear_Postmeta_Cache;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Content_Replace_URLs;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_EDD_Replace_URLs;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_File_Sizes;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Filter_Post_Excerpt;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Fix_Broken_Item_Extra_Data;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Item_Extra_Data;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Items_Table;
use DeliciousBrains\WP_Offload_Media\Upgrades\Upgrade_Meta_WP_Error;
Expand Down Expand Up @@ -236,21 +236,24 @@ public function init( $plugin_file_path ) {
$this->set_storage_provider();
$this->set_delivery_provider();

// Bundled SDK may require AWS setup before data migrations.
// Bundled SDK may require AWS setup before any use.
$this->handle_aws_access_key_migration();

new Upgrade_Region_Meta( $this );
new Upgrade_File_Sizes( $this );
new Upgrade_Meta_WP_Error( $this );
new Upgrade_Content_Replace_URLs( $this );
new Upgrade_EDD_Replace_URLs( $this );
new Upgrade_Filter_Post_Excerpt( $this );
new Upgrade_WPOS3_To_AS3CF( $this );
new Upgrade_Items_Table( $this );
new Upgrade_Tools_Errors( $this );
new Upgrade_Item_Extra_Data( $this );
new Clear_Postmeta_Cache( $this );
new Fix_Broken_Item_Extra_Data( $this );
// Only instantiate upgrade classes on single site installs or primary subsite.
if ( ! is_multisite() || is_network_admin() || $this->is_current_blog( get_current_blog_id() ) ) {
new Upgrade_Region_Meta( $this );
new Upgrade_File_Sizes( $this );
new Upgrade_Meta_WP_Error( $this );
new Upgrade_Content_Replace_URLs( $this );
new Upgrade_EDD_Replace_URLs( $this );
new Upgrade_Filter_Post_Excerpt( $this );
new Upgrade_WPOS3_To_AS3CF( $this );
new Upgrade_Items_Table( $this );
new Upgrade_Tools_Errors( $this );
new Upgrade_Item_Extra_Data( $this );
new Upgrade_Clear_Postmeta_Cache( $this );
new Upgrade_Fix_Broken_Item_Extra_Data( $this );
}

// Plugin setup
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
Expand All @@ -268,7 +271,9 @@ public function init( $plugin_file_path ) {
add_action( 'as3cf_init', array( $this, 'enable_integrations' ) );

// Register REST API Endpoints once everything has been initialized.
add_action( 'as3cf_init', array( $this, 'register_api_endpoints' ) );
if ( is_admin() || AS3CF_Utils::is_rest_api() ) {
add_action( 'as3cf_init', array( $this, 'register_api_endpoints' ) );
}

// Content filtering
$this->filter_local = new AS3CF_Local_To_S3( $this );
Expand Down Expand Up @@ -784,7 +789,7 @@ public function get_setting( string $key, $default = '' ) {
in_array( $key, array( 'copy-to-s3', 'serve-from-s3' ) ) &&
! isset( $settings[ $key ] )
) {
return '1';
return true;
}

// Some settings should default to true on first set up.
Expand All @@ -808,7 +813,7 @@ public function get_setting( string $key, $default = '' ) {

// Turn on object versioning by default
if ( 'object-versioning' == $key && ! isset( $settings['object-versioning'] ) ) {
return '1';
return true;
}

// Default object prefix
Expand All @@ -821,18 +826,20 @@ public function get_setting( string $key, $default = '' ) {
return get_option( 'uploads_use_yearmonth_folders' );
}

// Default enable object prefix - enabled unless path is empty
// Default enable object prefix - enabled unless path is empty in db (defined empty can be intentional, legacy).
if ( 'enable-object-prefix' == $key ) {
if ( isset( $settings['enable-object-prefix'] ) && '0' == $settings['enable-object-prefix'] ) {
return 0;
if ( isset( $settings['enable-object-prefix'] ) && empty( $settings['enable-object-prefix'] ) ) {
return false;
}

if ( isset( $settings['object-prefix'] ) && '' == trim( $settings['object-prefix'] ) ) {
if ( false === $this->get_defined_setting( 'object-prefix', false ) ) {
return 0;
}
if (
isset( $settings['object-prefix'] ) &&
empty( $settings['object-prefix'] ) &&
false === $this->get_defined_setting( 'object-prefix', false )
) {
return false;
} else {
return 1;
return true;
}
}

Expand Down Expand Up @@ -2174,6 +2181,7 @@ public function plugin_load() {
'create_bucket_name_short' => __( 'Bucket name too short.', 'amazon-s3-and-cloudfront' ),
'create_bucket_name_long' => __( 'Bucket name too long.', 'amazon-s3-and-cloudfront' ),
'create_bucket_invalid_chars' => __( 'Invalid character. Bucket names can contain lowercase letters, numbers, periods and hyphens.', 'amazon-s3-and-cloudfront' ),
'select_bucket_invalid_chars' => __( 'Invalid character. Bucket names can contain lowercase letters, numbers, periods and hyphens. Legacy buckets may also include uppercase letters and underscores.', 'amazon-s3-and-cloudfront' ),
'no_bucket_selected' => __( 'No bucket selected.', 'amazon-s3-and-cloudfront' ),
'defined_region_invalid' => __( 'Invalid region defined in wp-config.', 'amazon-s3-and-cloudfront' ),
'save_bucket_error' => __( 'Error saving bucket', 'amazon-s3-and-cloudfront' ),
Expand Down Expand Up @@ -3385,20 +3393,23 @@ public function output_diagnostic_info() {
}
$output .= "\r\n";

$output .= 'Copy Files to Bucket: ';
$output .= 'Offload Media: ';
$output .= $this->on_off( 'copy-to-s3' );
$output .= "\r\n";
$output .= 'Enable Path: ';
$output .= 'Remove Local Media: ';
$output .= $this->on_off( 'remove-local-file' );
$output .= "\r\n";
$output .= 'Enable Add Prefix to Bucket Path: ';
$output .= $this->on_off( 'enable-object-prefix' );
$output .= "\r\n";
$value = $this->get_setting( 'object-prefix' );
$output .= 'Custom Path: ';
$output .= 'Custom Prefix for Bucket Path: ';
$output .= empty( $value ) ? '(none)' : esc_html( $value );
$output .= "\r\n";
$output .= 'Use Year/Month: ';
$output .= 'Add Year & Month to Bucket Path: ';
$output .= $this->on_off( 'use-yearmonth-folders' );
$output .= "\r\n";
$output .= 'Object Versioning: ';
$output .= 'Add Object Version to Bucket Path: ';
$output .= $this->on_off( 'object-versioning' );
$output .= "\r\n";
$output .= "\r\n";
Expand All @@ -3411,12 +3422,12 @@ public function output_diagnostic_info() {
} else {
$output .= 'Delivery Provider: ' . $delivery_provider::get_provider_service_name();
$output .= "\r\n";
$output .= 'Rewrite Media URLs: ';
$output .= 'Deliver Offloaded Media: ';
$output .= $this->on_off( 'serve-from-s3' );
$output .= "\r\n";

if ( $delivery_provider::delivery_domain_allowed() ) {
$output .= 'Enable Custom Domain (CNAME): ';
$output .= 'Use Custom Domain Name (CNAME): ';
$output .= $this->on_off( 'enable-delivery-domain' );
$output .= "\r\n";
$value = $this->get_setting( 'delivery-domain' );
Expand All @@ -3426,18 +3437,18 @@ public function output_diagnostic_info() {
}

if ( $delivery_provider::use_signed_urls_key_file_allowed() ) {
$output .= 'Enable Signed URLs: ';
$output .= 'Serve Private Media: ';
$output .= $this->on_off( 'enable-signed-urls' );
$output .= "\r\n";
$output .= 'Signed URLs Key ID Set: ';
$output .= 'Public Key ID Set: ';
$output .= $delivery_provider->get_signed_urls_key_id() ? 'Yes' : 'No';
$output .= "\r\n";
$value = $this->get_setting( 'signed-urls-key-file-path' );
$output .= 'Signed URLs Key File Path: ';
$output .= 'Private Key File Path: ';
$output .= empty( $value ) ? '(none)' : esc_html( $value );
$output .= "\r\n";
$value = $this->get_setting( 'signed-urls-object-prefix' );
$output .= 'Signed URLs Private Prefix: ';
$output .= 'Private Bucket Path: ';
$output .= empty( $value ) ? '(none)' : esc_html( $value );
$output .= "\r\n";
}
Expand All @@ -3449,10 +3460,6 @@ public function output_diagnostic_info() {
$output .= "\r\n";
$output .= "\r\n";

$output .= 'Remove Files From Server: ';
$output .= $this->on_off( 'remove-local-file' );
$output .= "\r\n\r\n";

$output = apply_filters( 'as3cf_diagnostic_info', $output );
if ( has_action( 'as3cf_diagnostic_info' ) ) {
$output .= "\r\n";
Expand Down Expand Up @@ -3798,21 +3805,41 @@ public function get_memory_limit() {
*
* @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
* @param int $forced_blog_id Optional, restrict the force count of media items to only this blog ID, ignored if $force is false
*
* @return array
*/
public function media_counts( $skip_transient = false, $force = false ) {
public function media_counts( bool $skip_transient = false, bool $force = false, int $forced_blog_id = 0 ): array {
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;

$skip_transient_requested = $skip_transient;
$force_requested = $force;

foreach ( $table_prefixes as $blog_id => $table_prefix ) {
$this->switch_to_blog( $blog_id );

$skip_transient = $skip_transient_requested;
$force = $force_requested;

// If forcing an update from database for a specific blog ID, get others from transients if possible.
if ( $force && ! empty( $forced_blog_id ) && $forced_blog_id !== $blog_id ) {
$skip_transient = false;
$force = false;
}

// If on a multisite and not doing a blog specific update, don't skip transient.
if ( is_multisite() && $skip_transient && empty( $forced_blog_id ) ) {
$skip_transient = false;
$force = false;
}

/** @var Item $class */
foreach ( $this->get_source_type_classes() as $class ) {
$counts = $class::count_items( $skip_transient, $force );
$counts = $class::count_items( $skip_transient, $force, $blog_id );
$total += $counts['total'];
$offloaded += $counts['offloaded'];
$not_offloaded += $counts['not_offloaded'];
Expand All @@ -3827,7 +3854,21 @@ public function media_counts( $skip_transient = false, $force = false ) {
'not_offloaded' => $not_offloaded,
);

set_site_transient( 'as3cf_attachment_counts', $attachment_counts, 5 * MINUTE_IN_SECONDS );
ksort( $attachment_counts );

/**
* How many minutes should total media counts be cached?
*
* Min: 1 minute.
* Max: 1 day (1440 minutes).
*
* @param int $minutes Default 5.
*
* @retun int
*/
$timeout = min( max( 1, (int) apply_filters( 'as3cf_media_counts_timeout', 5 ) ), 1440 );

set_site_transient( 'as3cf_attachment_counts', $attachment_counts, $timeout * MINUTE_IN_SECONDS );
}

return $attachment_counts;
Expand Down Expand Up @@ -4268,9 +4309,9 @@ public function settings_saved_notice() {
* Migrate access keys from AWS database setting to this plugin's settings record and raise any notices.
*/
private function handle_aws_access_key_migration() {
add_action( 'aws_access_key_form_header', array( $this, 'handle_aws_access_key_form_header' ) );

if ( is_plugin_active( 'amazon-web-services/amazon-web-services.php' ) ) {
add_action( 'aws_access_key_form_header', array( $this, 'handle_aws_access_key_form_header' ) );

$message = sprintf(
__( '<strong>Amazon Web Services Plugin No Longer Required</strong> &mdash; As of version 1.6 of WP Offload Media, the <a href="%1$s">Amazon Web Services</a> plugin is no longer required. We have removed the dependency by bundling a small portion of the AWS SDK into WP Offload Media. As long as none of your other active plugins or themes depend on the Amazon Web Services plugin, it should be safe to deactivate and delete it. %2$s', 'amazon-s3-and-cloudfront' ),
'https://wordpress.org/plugins/amazon-web-services/',
Expand Down
30 changes: 15 additions & 15 deletions classes/as3cf-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,18 @@ protected function get_urls_from_img_src( $content, &$to_cache ) {

$url = $src[1];

if ( ! AS3CF_Utils::usable_url( $url ) ) {
continue;
}

if ( ! $this->url_needs_replacing( $url ) ) {
// URL already correct, skip
continue;
}

$url = AS3CF_Utils::reduce_url( $url );
$bare_url = AS3CF_Utils::reduce_url( $url );

$item_sources[ $url ] = array(
$item_sources[ $bare_url ] = array(
'id' => absint( $class_id[1] ),
'source_type' => Media_Library_Item::source_type(),
);
Expand Down Expand Up @@ -348,20 +352,12 @@ protected function get_urls_from_content( $content, $cache, &$to_cache ) {
foreach ( $matches as $url ) {
$url = preg_replace( '/[^a-zA-Z0-9]$/', '', $url );

if ( ! $this->url_needs_replacing( $url ) ) {
// URL already correct, skip
if ( ! AS3CF_Utils::usable_url( $url ) ) {
continue;
}

$parts = AS3CF_Utils::parse_url( $url );

if ( ! isset( $parts['path'] ) ) {
// URL doesn't have a path, continue
continue;
}

if ( ! pathinfo( $parts['path'], PATHINFO_EXTENSION ) ) {
// URL doesn't have a file extension, continue
if ( ! $this->url_needs_replacing( $url ) ) {
// URL already correct, skip
continue;
}

Expand Down Expand Up @@ -440,8 +436,12 @@ protected function is_failure( $value ) {
*
* @return bool
*/
public function item_matches_src( $item_source, $url ) {
if ( Item::is_empty_item_source( $item_source ) || Media_Library_Item::source_type() !== $item_source['source_type'] ) {
public function item_matches_src( array $item_source, string $url ): bool {
if (
Item::is_empty_item_source( $item_source ) ||
Media_Library_Item::source_type() !== $item_source['source_type'] ||
get_post_type( $item_source['id'] ) !== 'attachment'
) {
return false;
}
$meta = get_post_meta( $item_source['id'], '_wp_attachment_metadata', true );
Expand Down
36 changes: 32 additions & 4 deletions classes/as3cf-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function is_full_size( $size ) {
* @return string
*/
public static function reduce_url( $url ) {
$parts = self::parse_url( $url );
$parts = static::parse_url( $url );
$host = isset( $parts['host'] ) ? $parts['host'] : '';
$port = isset( $parts['port'] ) ? ":{$parts['port']}" : '';
$path = isset( $parts['path'] ) ? $parts['path'] : '';
Expand Down Expand Up @@ -180,12 +180,12 @@ public static function parse_url( $url, $component = -1 ) {
/**
* Is the string a URL?
*
* @param string $string
* @param mixed $string
*
* @return bool
*/
public static function is_url( $string ) {
if ( ! is_string( $string ) ) {
public static function is_url( $string ): bool {
if ( empty( $string ) || ! is_string( $string ) ) {
return false;
}

Expand Down Expand Up @@ -835,6 +835,34 @@ public static function maybe_fix_serialized_string( $input ) {
return $output;
}

/**
* Is the given string a usable URL?
*
* We need URLs that include at least a domain and filename with extension
* for URL rewriting in either direction.
*
* @param mixed $url
*
* @return bool
*/
public static function usable_url( $url ): bool {
if ( ! static::is_url( $url ) ) {
return false;
}

$parts = static::parse_url( $url );

if (
empty( $parts['host'] ) ||
empty( $parts['path'] ) ||
! pathinfo( $parts['path'], PATHINFO_EXTENSION )
) {
return false;
}

return true;
}

/**
* Fixes the broken string segments of a serialized data string.
*
Expand Down
Loading

0 comments on commit 509afee

Please sign in to comment.