From 8313f82d4549964591df72d70381d387c0fdf3ed Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 May 2022 12:46:54 +0300 Subject: [PATCH 1/3] Fix PHP 8.1 deprecation in the gallery block --- packages/block-library/src/gallery/index.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php index 4aed9cfed051b..4ef7d5a79beee 100644 --- a/packages/block-library/src/gallery/index.php +++ b/packages/block-library/src/gallery/index.php @@ -50,9 +50,16 @@ function block_core_gallery_render( $attributes, $content ) { // because we only want to match against the value, not the CSS attribute. if ( is_array( $gap ) ) { foreach ( $gap as $key => $value ) { - $gap[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; + $gap[ $key ] = null; + // Needed for PHP 8.1 deprecation: preg_match() expects parameter 2 to be a string, + // and without the check there's a chance it might be `null`. + if ( is_string( $value ) ) { + $gap[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; + } } } else { + // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match(). + $gap = is_string( $gap ) ? $gap : ''; $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap; } From 631e36c0865a91312a490a2a294bec55fcf5bac0 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 20 May 2022 12:55:23 +0300 Subject: [PATCH 2/3] improve the check --- packages/block-library/src/gallery/index.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php index 4ef7d5a79beee..be750029e43fc 100644 --- a/packages/block-library/src/gallery/index.php +++ b/packages/block-library/src/gallery/index.php @@ -50,15 +50,12 @@ function block_core_gallery_render( $attributes, $content ) { // because we only want to match against the value, not the CSS attribute. if ( is_array( $gap ) ) { foreach ( $gap as $key => $value ) { - $gap[ $key ] = null; - // Needed for PHP 8.1 deprecation: preg_match() expects parameter 2 to be a string, - // and without the check there's a chance it might be `null`. - if ( is_string( $value ) ) { - $gap[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; - } + // Make sure $gap[ $key ] is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null. + $gap[ $key ] = is_string( $gap[ $key ] ) ? $gap[ $key ] : ''; + $gap[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; } } else { - // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match(). + // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null. $gap = is_string( $gap ) ? $gap : ''; $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap; } From cab89c06d3e427487929a83ee6a1d60cec635350 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 23 May 2022 09:52:55 +0300 Subject: [PATCH 3/3] fix var - props @SergeyBiryukov --- packages/block-library/src/gallery/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/gallery/index.php b/packages/block-library/src/gallery/index.php index be750029e43fc..ff7b5880c2b92 100644 --- a/packages/block-library/src/gallery/index.php +++ b/packages/block-library/src/gallery/index.php @@ -50,8 +50,8 @@ function block_core_gallery_render( $attributes, $content ) { // because we only want to match against the value, not the CSS attribute. if ( is_array( $gap ) ) { foreach ( $gap as $key => $value ) { - // Make sure $gap[ $key ] is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null. - $gap[ $key ] = is_string( $gap[ $key ] ) ? $gap[ $key ] : ''; + // Make sure $value is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null. + $value = is_string( $value ) ? $value : ''; $gap[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; } } else {