Skip to content

Commit

Permalink
Merge branch 'release/3.5.46'
Browse files Browse the repository at this point in the history
  • Loading branch information
andykillen committed Dec 5, 2024
2 parents 93a09dc + d615d80 commit 7f19c12
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 87 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ If you want to build a local production-ready version of the plugin you can run

## Changelog

#### 3.5.46
* Accelerated Domains Image Resizer: added filter to manage problems when WordPress is unable to produce image dimensions by defaulting to the thumbnail size.
* code re-orgainisation for easier reading/debugging in the purge post section.

#### 3.5.45
* added extra checks to CacheTag creation to deal with plugins that prevent the ID from being readable on is_singular() queries

Expand Down
6 changes: 5 additions & 1 deletion Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Donate link: https://servebolt.com
Requires at least: 4.9.2
Tested up to: 6.6.1
Requires PHP: 7.4
Stable tag: 3.5.45
Stable tag: 3.5.46
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -98,6 +98,10 @@ If you're a Servebolt client, please reach out to our Support Team and we'll be

== Changelog ==

= 3.5.46 =
* Accelerated Domains Image Resizer: added filter to manage problems when WordPress is unable to produce image dimensions by defaulting to the thumbnail size.
* code re-orgainisation for easier reading/debugging in the purge post section.

= 3.5.45 =
* added extra checks to CacheTag creation to deal with plugins that prevent the ID from being readable on is_singular() queries

Expand Down
2 changes: 1 addition & 1 deletion servebolt-optimizer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Servebolt Optimizer
Version: 3.5.45
Version: 3.5.46
Author: Servebolt
Author URI: https://servebolt.com
Description: A plugin that implements Servebolt Security & Performance best practises for WordPress.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct()
if (self::doImageUpscale()) {
ImageUpscale::getInstance();
}
$this->imageResize->correctPotentialBadImagesHook();
}
}

Expand All @@ -65,6 +66,7 @@ private function defaultOptionValues(): void
setDefaultOption('acd_image_resize_half_size_switch', '__return_true');
setDefaultOption('acd_image_resize_src_tag_switch', '__return_true');
setDefaultOption('acd_image_resize_srcset_tag_switch', '__return_true');
setDefaultOption('acd_image_resize_force_thumbnail_minimum_width', '__return_false');
}

/**
Expand Down
22 changes: 21 additions & 1 deletion src/Servebolt/AcceleratedDomains/ImageResize/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

if (!defined('ABSPATH')) exit; // Exit if accessed directly

use function Servebolt\Optimizer\Helpers\smartGetOption;
/**
* Class ImageResize
* @package Servebolt\Optimizer\AcceleratedDomains\ImageResize
Expand Down Expand Up @@ -212,6 +213,12 @@ private function generateImageResizeParameters($image): array
if ($maxWidth && $width > $maxWidth) {
$width = $this->maxWidth();
}
// Set a useable width if the file was not readable to get the sizes.
if(apply_filters('sb_optimizer_acd_image_resize_force_thumbnail_minimum_width', false, $width, $height)) {
global $_wp_additional_image_sizes;
$width = $_wp_additional_image_sizes['post_thumbnail']['width'];
}

$additionalParams['width'] = $width;

// Set max height
Expand All @@ -227,6 +234,20 @@ private function generateImageResizeParameters($image): array
return $this->defaultImageResizeParameters($additionalParams);
}

/**
*
*/
public function correctPotentialBadImages($forceThumbnailMinimumWidth, $width) {
$blogId = (is_multisite()) ? get_current_blog_id() : null;
if($width < 10 && smartGetOption($blogId,'acd_image_resize_force_thumbnail_minimum_width', false)) return true;
return $forceThumbnailMinimumWidth;
}

public function correctPotentialBadImagesHook(): void
{
add_filter('sb_optimizer_acd_image_resize_force_thumbnail_minimum_width', [$this, 'correctPotentialBadImages'], 10, 3);
}

/**
* Get image quality.
*
Expand Down Expand Up @@ -413,7 +434,6 @@ public function regexOperation($content)
$newsrc = $this->buildImageUrl($src[1], ['width' => $image_width ]);
// Replace src="content" with src="new content", double replace keeps src=""
$content = str_replace($src[0], str_replace($src[1], $newsrc, $src[0]), $content);

}
}
return $content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ public function addHalfSizesToSrcsetHook(): void
add_filter('wp_calculate_image_srcset', [$this, 'addHalfSizesToSrcset'], 9, 5);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private function getSettingsItems(): array
'acd_image_resize_quality',
'acd_image_resize_metadata_optimization_level',
'acd_image_resize_upscale',
'acd_image_resize_force_thumbnail_minimum_width',
];
}
}
30 changes: 29 additions & 1 deletion src/Servebolt/CachePurge/Drivers/Cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Servebolt\Optimizer\Api\Cloudflare\Cloudflare as CloudflareApi;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeUrlInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeAllInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeValidateUrlCandidate;
use Servebolt\Optimizer\Exceptions\CloudflareApiError;
use Servebolt\Optimizer\Traits\Singleton;
use Servebolt\Optimizer\Sdk\Cloudflare\Exceptions\ApiError as CloudflareSdkApiError;
Expand All @@ -15,10 +16,37 @@
* Class Cloudflare
* @package Servebolt\Optimizer\CachePurge\Drivers
*/
class Cloudflare implements CachePurgeAllInterface, CachePurgeUrlInterface
class Cloudflare implements CachePurgeAllInterface, CachePurgeUrlInterface, CachePurgeValidateUrlCandidate
{
use Singleton;

/**
* Weed out URL's that can never be cached.
* @param string $url
* @return bool
*/
public function validateUrl(string $url): bool
{
$path = parse_url($url, PHP_URL_PATH);
if (empty($path)) {
return false;
}
$never_cached_paths = [
'/wp-admin/',
'/index.php/',
// '/wp-login.php',
// '/wp-cron.php',
// '/xmlrpc.php',
// '/wp-comments-post.php',
];
foreach($never_cached_paths as $never_cached_path) {
if (strpos($path, $never_cached_path) !== false) {
return false;
}
}
return true;
}

/**
* Purge a URL.
*
Expand Down
30 changes: 29 additions & 1 deletion src/Servebolt/CachePurge/Drivers/Servebolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,45 @@
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeAllInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgePrefixInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeTagInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeValidateUrlCandidate;
use Servebolt\Optimizer\Exceptions\ServeboltApiError;
use function Servebolt\Optimizer\Helpers\getDomainNameOfWebSite;

/**
* Class Servebolt
* @package Servebolt\Optimizer\CachePurge\Drivers
*/
class Servebolt implements CachePurgeAllInterface, CachePurgeUrlInterface, CachePurgePrefixInterface, CachePurgeTagInterface
class Servebolt implements CachePurgeAllInterface, CachePurgeUrlInterface, CachePurgePrefixInterface, CachePurgeTagInterface, CachePurgeValidateUrlCandidate
{
use Singleton, ServeboltDriverTrait;

/**
* Weed out URL's that can never be cached.
* @param string $url
* @return bool
*/
public function validateUrl(string $url): bool
{
$path = parse_url($url, PHP_URL_PATH);
if (empty($path)) {
return false;
}
$never_cached_paths = [
'/wp-admin/',
'/wp-login.php',
'/wp-cron.php',
'/xmlrpc.php',
'/index.php/',
'/wp-comments-post.php',
];
foreach($never_cached_paths as $never_cached_path) {
if (strpos($path, $never_cached_path) !== false) {
return false;
}
}
return true;
}

/**
* @param string $url
* @return bool
Expand Down
45 changes: 38 additions & 7 deletions src/Servebolt/CachePurge/Drivers/ServeboltCdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeAllInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeUrlInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeTagInterface;
use Servebolt\Optimizer\CachePurge\Interfaces\CachePurgeValidateUrlCandidate;
use Servebolt\Optimizer\Traits\Singleton;
use Servebolt\Optimizer\Exceptions\ServeboltApiError;
use function Servebolt\Optimizer\Helpers\getDomainNameOfWebSite;
Expand All @@ -15,10 +16,37 @@
* Class ServeboltCdn
* @package Servebolt\Optimizer\CachePurge\Drivers
*/
class ServeboltCdn implements CachePurgeAllInterface, CachePurgeTagInterface, CachePurgeUrlInterface
class ServeboltCdn implements CachePurgeAllInterface, CachePurgeTagInterface, CachePurgeUrlInterface, CachePurgeValidateUrlCandidate
{
use Singleton, ServeboltDriverTrait;

/**
* Weed out URL's that can never be cached.
* @param string $url
* @return bool
*/
public function validateUrl(string $url): bool
{
$path = parse_url($url, PHP_URL_PATH);
if (empty($path)) {
return false;
}
$never_cached_paths = [
'/wp-admin/',
'/wp-login.php',
'/wp-cron.php',
'/xmlrpc.php',
'/index.php/',
'/wp-comments-post.php',
];
foreach($never_cached_paths as $never_cached_path) {
if (strpos($path, $never_cached_path) !== false) {
return false;
}
}
return true;
}

/**
* @param string $url
* @return bool
Expand All @@ -36,7 +64,7 @@ public function purgeByUrl(string $url): bool
if ($response->wasSuccessful()) {
return true;
} else {
throw new ServeboltApiError($response->getErrors(), $response);
throw new ServeboltApiError($response->getErrors(), $response, 'serveboltcdn');
}
}

Expand All @@ -49,12 +77,15 @@ public function purgeByUrls(array $urls): bool
{
$response = $this->apiInstance->environment->purgeCdnCache(
$this->apiInstance->getEnvironmentId(),
$urls
$urls// files
// prefixes
// tags
// hosts
);
if ($response->wasSuccessful()) {
return true;
} else {
throw new ServeboltApiError($response->getErrors(), $response);
throw new ServeboltApiError($response->getErrors(), $response, 'serveboltcdn');
}
}

Expand All @@ -76,7 +107,7 @@ public function purgeAll(): bool
if ($response->wasSuccessful()) {
return true;
} else {
throw new ServeboltApiError($response->getErrors(), $response);
throw new ServeboltApiError($response->getErrors(), $response, 'serveboltcdn');
}
}

Expand All @@ -99,7 +130,7 @@ public function purgeByTags(array $tags = []) : bool
if ($response->wasSuccessful()) {
return true;
} else {
throw new ServeboltApiError($response->getErrors(), $response);
throw new ServeboltApiError($response->getErrors(), $response, 'serveboltcdn');
}
}

Expand All @@ -122,7 +153,7 @@ public function purgeAllNetwork(): bool
if ($response->wasSuccessful()) {
return true;
} else {
throw new ServeboltApiError($response->getErrors(), $response);
throw new ServeboltApiError($response->getErrors(), $response, 'serveboltcdn');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Servebolt\Optimizer\CachePurge\Interfaces;

if (!defined('ABSPATH')) exit; // Exit if accessed directly

/**
* Interface CachePurgeValidateUrlCandidate
*
* Implement this interface to validate URL candidates before trying to purge them.
* @package Servebolt\Optimizer\CachePurge\Interfaces
*/
interface CachePurgeValidateUrlCandidate {
public function validateUrl(string $url): bool;
}
Loading

0 comments on commit 7f19c12

Please sign in to comment.