From fc80a071b1ef2f5301c2be1fe67f642b1780dad2 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Tue, 8 Oct 2024 17:53:38 +0200 Subject: [PATCH 1/2] 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. --- .../image-cdn/changelog/fix-photon-deprecation-notice | 4 ++++ projects/packages/image-cdn/src/class-image-cdn.php | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 projects/packages/image-cdn/changelog/fix-photon-deprecation-notice diff --git a/projects/packages/image-cdn/changelog/fix-photon-deprecation-notice b/projects/packages/image-cdn/changelog/fix-photon-deprecation-notice new file mode 100644 index 0000000000000..bfccccc097d80 --- /dev/null +++ b/projects/packages/image-cdn/changelog/fix-photon-deprecation-notice @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Avoid deprecation notice when an image URL does not have an expected format. diff --git a/projects/packages/image-cdn/src/class-image-cdn.php b/projects/packages/image-cdn/src/class-image-cdn.php index 7f82a5b1809d7..ee214d49e2e93 100644 --- a/projects/packages/image-cdn/src/class-image-cdn.php +++ b/projects/packages/image-cdn/src/class-image-cdn.php @@ -679,7 +679,12 @@ 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 ( + is_string( $src ) + && 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' ); From 20b0de6ee7d9c1462f3595f4623a8b548523c505 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 9 Oct 2024 17:34:49 +0200 Subject: [PATCH 2/2] Check for the validity of the src earlier See https://github.com/Automattic/jetpack/pull/39685#pullrequestreview-2357095359 --- .../image-cdn/src/class-image-cdn.php | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/projects/packages/image-cdn/src/class-image-cdn.php b/projects/packages/image-cdn/src/class-image-cdn.php index ee214d49e2e93..31998fd89ed4c 100644 --- a/projects/packages/image-cdn/src/class-image-cdn.php +++ b/projects/packages/image-cdn/src/class-image-cdn.php @@ -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; } @@ -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. * @@ -680,8 +686,7 @@ public static function filter_the_content( $content ) { } } } elseif ( - is_string( $src ) - && preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src ) + preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src ) && is_string( $nearest_preceding_href ) && self::validate_image_url( $nearest_preceding_href ) ) {