Skip to content

Commit

Permalink
Various tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jono committed Jan 6, 2025
1 parent f2dfef3 commit 0f5c855
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 43 deletions.
3 changes: 1 addition & 2 deletions assets/css/images.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
.edge-images-container {
display: block;
position: relative;
max-width: var(--max-width, 100%);
max-width: var(--wp--style--global--content-size, var(--max-width, 100%));
align-content: center;

img {
display: block;
max-width: 100%;
max-height: 100%;
height: auto;
margin: auto;
border-radius: inherit; // Inherit the border radius from the container

// SVGs are a sometimes headache.
Expand Down
2 changes: 1 addition & 1 deletion assets/css/images.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion classes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function register(): void {
// Gallery pattern - matches the outer gallery wrapper and all nested figures
'gallery' => '/<figure[^>]*\bwp-block-gallery\b[^>]*>(?:[^<]*|<(?!figure[^>]*>|\/figure>)[^<]*|<figure[^>]*>(?:[^<]*|<(?!figure[^>]*>|\/figure>)[^<]*)*<\/figure>)*<\/figure>/s',
// Image pattern - matches figures with wp-block-image class
'image' => '/<figure[^>]*class="[^"]*\bwp-block-image\b[^"]*"[^>]*>.*?<\/figure>/s',
'image' => '/<(?:figure|div)[^>]*class="[^"]*\bwp-block-image\b[^"]*"[^>]*>.*?<\/(?:figure|div)>/s',
];


Expand Down
30 changes: 8 additions & 22 deletions classes/class-content-transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ public function transform(string $content): string {
* @return string Modified content.
*/
private function transform_block_images(string $content): string {

// Get all registered block handlers
$handlers = Blocks::get_handlers();

// Bail if we don't have any handlers
if (empty($handlers)) {
return $content;
}

foreach ($handlers as $block_type => $handler) {

// Get the block pattern from the Blocks class
$pattern = Blocks::get_block_pattern($block_type);
if (!$pattern) {
Expand All @@ -63,6 +70,7 @@ private function transform_block_images(string $content): string {

// If we have a pattern, use it to find blocks
if (preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE)) {

// Process matches in reverse order to maintain offsets
$matches[0] = array_reverse($matches[0]);

Expand Down Expand Up @@ -97,28 +105,6 @@ private function transform_block_images(string $content): string {
}
}

// Also handle wp-block-image divs that aren't caught by block handlers
if (preg_match_all('/<div[^>]*class="[^"]*\bwp-block-image\b[^"]*"[^>]*>.*?<\/div>/s', $content, $matches, PREG_OFFSET_CAPTURE)) {
$matches[0] = array_reverse($matches[0]);
foreach ($matches[0] as $match) {
$block_html = $match[0];
$start = $match[1];

// Skip if already processed
if (strpos($block_html, 'edge-images-processed') !== false) {
continue;
}

// Extract the img tag and transform it
if (preg_match('/<figure[^>]*>.*?<img[^>]+>.*?<\/figure>/s', $block_html, $figure_matches)) {
$figure_html = $figure_matches[0];
$transformed = $this->transform_single_image($figure_html, 'content');
$transformed_block = str_replace($figure_html, $transformed, $block_html);
$content = substr_replace($content, $transformed_block, $start, strlen($block_html));
}
}
}

return $content;
}

Expand Down
29 changes: 18 additions & 11 deletions classes/class-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,24 @@ public static function should_transform_url(string $url): bool {
* @return bool Whether the URL is local.
*/
public static function is_local_url(string $url): bool {
$site_url = site_url();
$home_url = home_url();

// Remove protocol and www
$url = preg_replace('#^https?://(www\.)?#', '', $url);
$site_url = preg_replace('#^https?://(www\.)?#', '', $site_url);
$home_url = preg_replace('#^https?://(www\.)?#', '', $home_url);

// Check if URL starts with either site_url or home_url
$is_local = (strpos($url, $site_url) === 0) || (strpos($url, $home_url) === 0);

// If we can't parse the URL, assume it's not local
$url_parts = wp_parse_url($url);
if (!$url_parts || empty($url_parts['host'])) {
error_log('Edge Images: Could not parse URL or no host found: ' . $url);
return false;
}

// Get the list of internal hosts
$internal_hosts = wp_internal_hosts();
$url_host = strtolower($url_parts['host']);

error_log('Edge Images: Checking URL host: ' . $url_host);
error_log('Edge Images: Against internal hosts: ' . implode(', ', $internal_hosts));

// Check if the URL's host matches any internal host
$is_local = in_array($url_host, $internal_hosts, true);
error_log('Edge Images: Is local? ' . ($is_local ? 'yes' : 'no'));

return $is_local;
}

Expand Down
31 changes: 29 additions & 2 deletions classes/integrations/yoast-seo/class-social-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Social_Images extends Integration {
* @since 4.5.0
* @var int
*/
private const SOCIAL_HEIGHT = 630;
private const SOCIAL_HEIGHT = 675;

/**
* Add integration-specific filters.
Expand All @@ -69,6 +69,32 @@ protected function add_filters(): void {

add_filter('wpseo_opengraph_image', [$this, 'transform_social_image']);
add_filter('wpseo_twitter_image', [$this, 'transform_social_image']);
add_filter('wpseo_opengraph_image_width', [$this, 'transform_social_image_width']);
add_filter('wpseo_opengraph_image_height', [$this, 'transform_social_image_height']);
}

/**
* Set the width for the social image.
*
* @since 4.5.0
*
* @param integer|string $width
* @return integer
*/
public function transform_social_image_width($width): int {
return (int)self::SOCIAL_WIDTH;
}

/**
* Set the height for the social image.
*
* @since 4.5.0
*
* @param integer|string $height
* @return integer
*/
public function transform_social_image_height($height): int {
return (int)self::SOCIAL_HEIGHT;
}

/**
Expand All @@ -89,6 +115,7 @@ protected function add_filters(): void {
* @return string The transformed image URL.
*/
public function transform_social_image(string $image_url): string {

// Skip if empty or not local
if (empty($image_url) || !Helpers::is_local_url($image_url)) {
return $image_url;
Expand All @@ -98,7 +125,7 @@ public function transform_social_image(string $image_url): string {
return Helpers::edge_src($image_url, [
'width' => self::SOCIAL_WIDTH,
'height' => self::SOCIAL_HEIGHT,
'fit' => 'cover',
'fit' => 'contain',
'quality' => 85,
]);
}
Expand Down
1 change: 1 addition & 0 deletions classes/integrations/yoast-seo/class-xml-sitemaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function add_filters(): void {
* @return string The transformed image URL.
*/
public function transform_sitemap_image(string $image_url): string {

// Skip if empty or not local
if (empty($image_url) || !Helpers::is_local_url($image_url)) {
return $image_url;
Expand Down
6 changes: 3 additions & 3 deletions edge-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* @license GPL-2.0-or-later
* @link https://github.com/jonoalderson/edge-images/
* @since 1.0.0
* @version 5.2
* @version 5.2.1
*
* @wordpress-plugin
* Plugin Name: Edge Images
* Plugin URI: https://github.com/jonoalderson/edge-images/
* Description: Routes images through edge providers (like Cloudflare or Accelerated Domains) for automatic optimization and transformation. Improves page speed and image loading performance.
* Version: 5.2
* Version: 5.2.1
* Requires PHP: 7.4
* Requires at least: 5.6
* Tested up to: 6.7
Expand All @@ -33,7 +33,7 @@
}

// Define plugin constants.
define( 'EDGE_IMAGES_VERSION', '5.2' );
define( 'EDGE_IMAGES_VERSION', '5.2.1' );
define( 'EDGE_IMAGES_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'EDGE_IMAGES_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: jonoaldersonwp
Tags: images, optimization, cdn, cloudflare, performance
Tested up to: 6.7
Requires PHP: 7.4
Stable tag: 5.2
Stable tag: 5.2.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down

0 comments on commit 0f5c855

Please sign in to comment.