Skip to content

Commit

Permalink
* Added --skip-processed flag to mediacloud:storage makewebp comm…
Browse files Browse the repository at this point in the history
…and to skip any attachments that already have webp

  metadata associated with them. (Premium)
* Added a setting to *Cloud Storage* settings called *Force WebP* that forces Media Cloud to return .webp urls for images
  that have webp metadata associated with them.  Note this does not do any sort of content negotiation or browser
  sniffing, it simply returns the webp url for the image.  Consider that only IE 11 and much older versions of Safari
  do not support webp.
  • Loading branch information
jawngee committed Dec 9, 2022
1 parent e55094c commit b9ebbd6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 9 deletions.
3 changes: 3 additions & 0 deletions classes/Tools/Storage/CLI/StorageCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,9 @@ public function syncLocal( $args, $assoc_args )
* - desc
* ---
*
* [--skip-processed]
* : Skips images that have already been processed.
*
* @when after_wp_load
*
* @param $args
Expand Down
6 changes: 6 additions & 0 deletions classes/Tools/Storage/StorageContentHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,16 @@ public function filterContent($content, $context = 'post') {
foreach($replacements as $idSize => $data) {
$idSizeParts = explode(',', $idSize);
$content = $this->replaceImageInContent($idSizeParts[0], $data, $content);
if ($this->settings->forceWebP) {
$content = str_replace('.webp.webp', '.webp', $content);
}
}

foreach($resizedReplacements as $id => $data) {
$content = $this->replaceImageInContent($data['id'], $data, $content);
if ($this->settings->forceWebP) {
$content = str_replace('.webp.webp', '.webp', $content);
}
}

if ($this->settings->replaceAnchorHrefs) {
Expand Down
24 changes: 18 additions & 6 deletions classes/Tools/Storage/StorageTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,15 @@ public function getAttachmentURLFromMeta( $meta )
}
$cdn = apply_filters( 'media-cloud/storage/override-cdn', StorageToolSettings::cdn() );
$docCdn = apply_filters( 'media-cloud/storage/override-doc-cdn', StorageToolSettings::docCdn() );
$hasWebP = false;
$oldKey = $key = arrayPath( $meta, 's3/key', null );

if ( !empty($this->settings->forceWebP) && !empty(arrayPath( $meta, 's3/formats/webp', null )) ) {
$hasWebP = true;
$oldKey = $key;
$key = arrayPath( $meta, 's3/formats/webp', null );
}

$type = typeFromMeta( $meta );
$privacy = arrayPath( $meta, 's3/privacy', 'private' );
$doSign = $this->client->usesSignedURLs( $type ) && $privacy != 'public-read' || $privacy !== 'public-read' && is_admin();
Expand All @@ -2155,9 +2164,9 @@ public function getAttachmentURLFromMeta( $meta )
if ( $doSign ) {

if ( $privacy !== 'public-read' && is_admin() ) {
$url = $this->client->presignedUrl( $meta['s3']['key'], $this->client->signedURLExpirationForType( $type ) );
$url = $this->client->presignedUrl( $key, $this->client->signedURLExpirationForType( $type ) );
} else {
$url = $this->client->url( $meta['s3']['key'], $type );
$url = $this->client->url( $key, $type );
}


Expand All @@ -2180,7 +2189,7 @@ public function getAttachmentURLFromMeta( $meta )
} else {

if ( !empty($cdn) && empty($ignoreCDN) ) {
return $cdn . '/' . $meta['s3']['key'];
return $cdn . '/' . $key;
} else {

if ( isset( $meta['s3']['url'] ) ) {
Expand All @@ -2195,11 +2204,14 @@ public function getAttachmentURLFromMeta( $meta )
'png'
);
if ( !in_array( $ext, $image_exts ) ) {
return trim( $docCdn, '/' ) . '/' . $meta['s3']['key'];
return trim( $docCdn, '/' ) . '/' . $key;
}
}

$new_url = $meta['s3']['url'];
if ( $hasWebP && $this->settings->forceWebP ) {
$new_url = str_replace( $oldKey, $key, $new_url );
}
if ( !empty($new_url) ) {
if ( strpos( $new_url, '//s3-.amazonaws' ) !== false ) {
$new_url = str_replace( '//s3-.amazonaws', '//s3.amazonaws', $new_url );
Expand All @@ -2211,10 +2223,10 @@ public function getAttachmentURLFromMeta( $meta )
}

try {
return $this->client->url( $meta['s3']['key'] );
return $this->client->url( $key );
} catch ( \Exception $ex ) {
Logger::error(
"Error trying to generate url for {$meta['s3']['key']}. Message:" . $ex->getMessage(),
"Error trying to generate url for {$key}. Message:" . $ex->getMessage(),
[],
__METHOD__,
__LINE__
Expand Down
2 changes: 2 additions & 0 deletions classes/Tools/Storage/StorageToolSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @property bool $disableSrcSet
* @property bool $disableEWWWBackgroundProcessing
* @property bool $extractPDFPageSize
* @property bool $forceWebP
*
*/
class StorageToolSettings extends ToolSettings {
Expand Down Expand Up @@ -115,6 +116,7 @@ class StorageToolSettings extends ToolSettings {
"disableEWWWBackgroundProcessing" => ["mcloud-storage-disable-eww-background-processing", null, true],
'queuedDeletesDelay' => ["mcloud-storage-queue-deletes-delay", null, 2],
'extractPDFPageSize' => ["mcloud-storage-extract-pdf-page-size", null, false],
'forceWebP' => ["mcloud-storage-force-webp", null, false],
];


Expand Down
6 changes: 6 additions & 0 deletions config/storage.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@
"title" => "URL Replacement",
"description" => "",
"options" => [
"mcloud-storage-force-webp" => [
"title" => "Force WebP",
"description" => "When this is enabled, Media Cloud will output URLs for webp images, if a webp image has been generated for an attachment via the EWWW Image Optimization plugin.",
"type" => "checkbox",
"default" => false
],
"mcloud-storage-filter-content" => [
"title" => "Replace URLs",
"description" => "When this is enabled, Media Cloud will replace URLs in content on the fly. <strong>You should not turn this off in most circumstances.</strong> However, if you've been using Media Cloud since day zero of your WordPress site, you may be able to turn this setting off.",
Expand Down
4 changes: 2 additions & 2 deletions ilab-media-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Plugin URI: https://github.com/interfacelab/ilab-media-tools
Description: Automatically upload media to Amazon S3 and integrate with Imgix, a real-time image processing CDN. Boosts site performance and simplifies workflows.
Author: interfacelab
Version: 4.5.17
Version: 4.5.19
Requires PHP: 7.4
Author URI: http://interfacelab.io
*/
Expand Down Expand Up @@ -114,7 +114,7 @@
}

// Version Defines
define( 'MEDIA_CLOUD_VERSION', '4.5.17' );
define( 'MEDIA_CLOUD_VERSION', '4.5.19' );
define( 'MEDIA_CLOUD_INFO_VERSION', '4.0.2' );
define( 'MCLOUD_IS_BETA', false );
// Debugging
Expand Down
11 changes: 10 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Requires at least: 4.9
Tested up to: 6.1
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Stable tag: 4.5.17
Stable tag: 4.5.19
Requires PHP: 7.4

Automatically store media on Amazon S3, Cloudflare R2, Google Cloud Storage, DigitalOcean Spaces + others. Serve CSS/JS assets through CDNs. Integrate with Imgix.
Expand Down Expand Up @@ -105,6 +105,15 @@ Imgix is a content delivery network with a twist. In addition to distributing y

== Changelog ==

= 4.5.19 - 12/9/2022 =

* Added `--skip-processed` flag to `mediacloud:storage makewebp` command to skip any attachments that already have webp
metadata associated with them. (Premium)
* Added a setting to *Cloud Storage* settings called *Force WebP* that forces Media Cloud to return .webp urls for images
that have webp metadata associated with them. Note this does not do any sort of content negotiation or browser
sniffing, it simply returns the webp url for the image. Consider that only IE 11 and much older versions of Safari
do not support webp.

= 4.5.17 - 12/8/2022 =

* Added `wp mediacloud:storage syncAllLocal` command to download all cloud storage files to your local server.
Expand Down

0 comments on commit b9ebbd6

Please sign in to comment.