Commit 182132ee59 for woocommerce
commit 182132ee59f93659cb9908ff9380ee3341537de0
Author: Beckin Labs <webmaster@beckin.com>
Date: Thu Dec 4 02:24:04 2025 -0600
Guard array access in woocommerce_subcategory_thumbnail (#62198)
* Guard array access in woocommerce_subcategory_thumbnail
Guard access to the image URL in woocommerce_subcategory_thumbnail() to avoid
"Trying to access array offset on value of type bool" warnings.
wp_get_attachment_image_src() can return false when the attachment lookup
fails. The previous code assumed the return value was always an array and
unconditionally accessed $image[0], which triggers a warning when it is false.
This change checks that the result is an array and contains index 0 before
reading it. If that check fails, the code now treats the image as unavailable
and allows the existing "if ( $image )" logic later in the function to skip
rendering the thumbnail.
Behavior for valid image data is unchanged.
* Add changefile(s) from automation for the following project(s): woocommerce
* Improve subcategory thumbnail fallback handling
When the category thumbnail attachment lookup fails, fall back to
wc_placeholder_img_src() instead of treating the image as false.
Move the image_srcset and image_sizes assignments into the guarded
branches so they are only set when a valid attachment is found and are
explicitly false when using the placeholder.
This keeps the existing output logic intact while avoiding warnings when
the thumbnail attachment is missing.
* Remove PHPStan ignore for fixed subcategory thumbnail warning
---------
Co-authored-by: github-actions <github-actions@github.com>
diff --git a/plugins/woocommerce/changelog/62198-trunk b/plugins/woocommerce/changelog/62198-trunk
new file mode 100644
index 0000000000..9a84734b7c
--- /dev/null
+++ b/plugins/woocommerce/changelog/62198-trunk
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix warning when rendering subcategory thumbnails if the thumbnail attachment lookup fails.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/wc-template-functions.php b/plugins/woocommerce/includes/wc-template-functions.php
index c0777f7ea3..f32edc09ac 100644
--- a/plugins/woocommerce/includes/wc-template-functions.php
+++ b/plugins/woocommerce/includes/wc-template-functions.php
@@ -2840,10 +2840,18 @@ if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
- $image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
- $image = $image[0];
- $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
- $image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
+ $image_data = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
+
+ // Category image guard - fallback to placeholder.
+ if ( is_array( $image_data ) && isset( $image_data[0] ) ) {
+ $image = $image_data[0];
+ $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
+ $image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
+ } else {
+ $image = wc_placeholder_img_src();
+ $image_srcset = false;
+ $image_sizes = false;
+ }
} else {
$image = wc_placeholder_img_src();
$image_srcset = false;
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 38874e36ab..25386a0754 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -53805,12 +53805,6 @@ parameters:
count: 1
path: includes/wc-template-functions.php
- -
- message: '#^Cannot access offset 0 on array\{string, int, int, bool\}\|false\.$#'
- identifier: offsetAccess.nonOffsetAccessible
- count: 1
- path: includes/wc-template-functions.php
-
-
message: '#^Cannot access offset string on mixed\.$#'
identifier: offsetAccess.nonOffsetAccessible