Skip to content

Commit

Permalink
Photon: avoid deprecation warnings when src is null (#39685)
Browse files Browse the repository at this point in the history
* Photon: avoid deprecation warnings when src is null

In some scenarios, one can run into the following deprecation notices:

Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in /wordpress/plugins/jetpack/13.9-a.7/jetpack_vendor/automattic/jetpack-image-cdn/src/class-image-cdn.php on line 682

This should avoid those notices.

* Check for the validity of the src earlier

See #39685 (review)

---------

Co-authored-by: Brad Jorsch <[email protected]>
  • Loading branch information
jeherve and anomiex authored Oct 10, 2024
1 parent d7e9351 commit 1566a4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Avoid deprecation notice when an image URL does not have an expected format.
26 changes: 18 additions & 8 deletions projects/packages/image-cdn/src/class-image-cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,21 @@ public static function filter_the_content( $content ) {
continue;
}

// Identify image source.
$src_orig = $processor->get_attribute( 'src' );
$src = $src_orig;

/*
* Only examine tags that are considered an image. If encountering
* a closing tag then this is not the image being sought.
* Only examine tags that are considered an image,
* with a valid src attribute.
* If encountering a closing tag then this is not the image being sought.
*/
if ( $processor->is_tag_closer() || ! in_array( $processor->get_tag(), $image_tags, true ) ) {
if (
$processor->is_tag_closer()
|| ! in_array( $processor->get_tag(), $image_tags, true )
|| ! is_string( $src )
|| $src === ''
) {
continue;
}

Expand Down Expand Up @@ -397,10 +407,6 @@ public static function filter_the_content( $content ) {
// Flag if we need to munge a fullsize URL.
$fullsize_url = false;

// Identify image source.
$src_orig = $processor->get_attribute( 'src' );
$src = $src_orig;

/**
* Allow specific images to be skipped by Photon.
*
Expand Down Expand Up @@ -679,7 +685,11 @@ public static function filter_the_content( $content ) {
$processor->set_attribute( 'data-recalc-dims', '1' );
}
}
} elseif ( preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src ) && is_string( $nearest_preceding_href ) && self::validate_image_url( $nearest_preceding_href ) ) {
} elseif (
preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src )
&& is_string( $nearest_preceding_href )
&& self::validate_image_url( $nearest_preceding_href )
) {
$processor->seek( 'link' );
$processor->set_attribute( 'href', Image_CDN_Core::cdn_url( $nearest_preceding_href ) );
$processor->seek( 'image' );
Expand Down

0 comments on commit 1566a4e

Please sign in to comment.