Skip to content

Commit

Permalink
Merge branch 'fix/uploadcare-uuid' into master
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	readme.txt
  • Loading branch information
andrew72ru committed May 26, 2021
2 parents 1b56b08 + bfb21f3 commit 9666a37
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based now on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.0.5] — 2021-04-20
## [3.0.6] — 2021-05-26

* Fixed an issue with missing UUIDs for previously uploaded images, which led to an error while rendering images.
* Various fixes and improvements.

## [3.0.5] — 2021-04-07

* Now you can copy your WordPress Media Library to Uploadcare cloud storage. Files in Media Library don’t get removed.
* Image source URLs in posts don’t change. However, when a post is rendered, images get delivered with Uploadcare CDN powered by Adaptive Delivery technology.
Expand Down Expand Up @@ -279,7 +284,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

[uploadcare/uploadcare-php]: https://github.com/uploadcare/uploadcare-php

[Unreleased]: https://github.com/uploadcare/uploadcare-wordpress/compare/v3.0.4..HEAD
[Unreleased]: https://github.com/uploadcare/uploadcare-wordpress/compare/v3.0.6..HEAD
[3.0.6]: https://github.com/uploadcare/uploadcare-wordpress/compare/v3.0.5..v3.0.6
[3.0.5]: https://github.com/uploadcare/uploadcare-wordpress/compare/v3.0.4..v3.0.5
[3.0.4]: https://github.com/uploadcare/uploadcare-wordpress/compare/v3.0.3..v3.0.4
[3.0.3]: https://github.com/uploadcare/uploadcare-wordpress/compare/v3.0.2..v3.0.3
[3.0.2]: https://github.com/uploadcare/uploadcare-wordpress/compare/v3.0.1..v3.0.2
Expand Down
41 changes: 24 additions & 17 deletions includes/UcFront.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class UcFront
{
const IMAGE_REGEX = '/(<img\s?.+)(src=\")(.[^\"]+)(.+)/m';
public const IMAGE_REGEX = '/(<img\s?.+)(src=\")(.[^\"]+)(.+)/m';

/**
* @var string
Expand Down Expand Up @@ -48,14 +48,20 @@ public function editorPostMeta(): void

public function prepareAttachment(array $response, WP_Post $attachment, $meta): array
{
if (empty(\get_post_meta($attachment->ID, 'uploadcare_url', true))) {
$ucUrl = \get_post_meta($attachment->ID, 'uploadcare_url_modifiers', true);
if ($ucUrl) {
return $response;
}

$uuid = \get_post_meta($attachment->ID, 'uploadcare_uuid', true);
if (empty($uuid)) {
$uuid = UploadcareMain::getUuid($ucUrl);
}

$response['meta'] = [
'uploadcare_url_modifiers' => \get_post_meta($attachment->ID, 'uploadcare_url_modifiers', true),
'uploadcare_url' => \get_post_meta($attachment->ID, 'uploadcare_url', true),
'uploadcare_uuid' => \get_post_meta($attachment->ID, 'uploadcare_uuid', true),
'uploadcare_url' => $ucUrl,
'uploadcare_uuid' => $uuid,
];

return $response;
Expand Down Expand Up @@ -122,15 +128,13 @@ protected function changeContent(string $content, int $imageId): string
$modifiers = \get_post_meta($imageId, 'uploadcare_url_modifiers', true);

$crawler->filterXPath('//img')->each(function (Crawler $node) use (&$collation, $imageId, $modifiers) {
$imageUrl = $node->attr('src');
$attachedFile = \get_post_meta($imageId, '_wp_attached_file', true);
$isLocal = true;

if (\strpos($attachedFile, \get_option('uploadcare_cdn_base')) !== false) {
$imageUrl = \sprintf('https://%s/%s/', \get_option('uploadcare_cdn_base'), \get_post_meta($imageId, 'uploadcare_uuid', true));
$imageUrl = \sprintf('https://%s/%s/', \get_option('uploadcare_cdn_base'), $this->getUuid($imageId));
$isLocal = false;
}
if ($isLocal && \strpos($imageUrl, \get_option('uploadcare_cdn_base')) !== true) {
} else {
$imageUrl = \wp_get_attachment_image_url($imageId, 'large');
}

Expand All @@ -140,15 +144,7 @@ protected function changeContent(string $content, int $imageId): string
}
// If Adaptive delivery is on and Secure uploads is on too we have to change url to uuid and ignore all local files
if ($this->adaptiveDelivery && $this->secureUploads) {
$uuid = \get_post_meta($imageId, 'uploadcare_uuid', true);
if (!$uuid) {
$uuid = UploadcareMain::getUuid(\get_post_meta($imageId, 'uploadcare_url', true));
if ($uuid !== null) {
\update_post_meta($imageId, 'uploadcare_uuid', $uuid);
}
}

$imageUrl = !$isLocal ? $uuid : null;
$imageUrl = !$isLocal ? $this->getUuid($imageId) : null;
}
if ($imageUrl !== null && $isLocal === false) {
$imageUrl = \sprintf('%s/%s', \rtrim($imageUrl, '/'), $modifiers);
Expand Down Expand Up @@ -180,6 +176,17 @@ protected function changeContent(string $content, int $imageId): string
return $content;
}

protected function getUuid(int $postId): string
{
$uuid = \get_post_meta($postId, 'uploadcare_uuid', true);
if (empty($uuid)) {
$uuid = UploadcareMain::getUuid(\get_post_meta($postId, 'uploadcare_url', true));
\update_post_meta($postId, 'uploadcare_uuid', $uuid);
}

return $uuid;
}

/**
* Calls on `post_thumbnail_html`. If thumbnail is an uploadcare image, make it an adaptive delivered.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/UploadcareMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class UploadcareMain
public const SCALE_CROP_TEMPLATE = '%s-/preview/%s/';
public const RESIZE_TEMPLATE = '%s-/preview/%s/-/quality/lightest/-/format/auto/';
public const PREVIEW_TEMPLATE = '%s-/preview/160x160/-/resize/160x/-/scale_crop/160x160/';
public const UUID_REGEX = '/\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b/';
public const UUID_REGEX = '/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/';

/**
* @var UcLoader
Expand Down
9 changes: 7 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Contributors: andrew72ru, rsedykh
Tags: file upload, cdn, storage, adaptive delivery, responsive, lazy loading, optimization, performance
Requires at least: 5.0
Tested up to: 5.7
Tested up to: 5.7.2
Requires PHP: 7.1
Stable tag: 3.0.5
Stable tag: 3.0.6
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Donate link: https://uploadcare.com/pricing/
Expand Down Expand Up @@ -132,6 +132,11 @@ Access all files in your Uploadcare account via Media Library.

== Changelog ==

= 3.0.6 =

* Fixed an issue with missing UUIDs for previously uploaded images, which led to an error while rendering images.
* Various fixes and improvements.

= 3.0.5 =

* Now you can copy your WordPress Media Library to Uploadcare cloud storage. Files in Media Library don’t get removed.
Expand Down
4 changes: 2 additions & 2 deletions uploadcare.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Plugin Name: Uploadcare File Uploader and Adaptive Delivery
* Plugin URI: https://github.com/uploadcare/uploadcare-wordpress
* Description: Upload and store any file of any size from any device or cloud. No more slow downs when serving your images with automatic responsiviness and lazy loading. Improve your WP performance to boost Customer Experience and SEO.
* Version: 3.0.5
* Version: 3.0.6
* Author: Uploadcare
* Author URI: https://uploadcare.com/
* License: GPL-2.0+
Expand All @@ -19,7 +19,7 @@
*/
class Uploadcare_Wordpress_Plugin {

public const UPLOADCARE_VERSION = '3.0.5';
public const UPLOADCARE_VERSION = '3.0.6';

public function __construct()
{
Expand Down

0 comments on commit 9666a37

Please sign in to comment.