From 8b327099df94875057fab0e595555a2fadaae23f Mon Sep 17 00:00:00 2001 From: David Stone Date: Sun, 12 Apr 2026 23:02:36 -0600 Subject: [PATCH] fix: guard false return from get_available_site_templates in switch_template() - Update PHPDoc @return to array|false in class-limit-site-templates.php - Guard false (MODE_DEFAULT) before array_map() in class-template-switching-element.php to prevent TypeError on PHP 8+ and incorrect 'not_authorized' errors on PHP 7.4 when a site's product uses the default (allow all) template mode Addresses CodeRabbit review feedback from PR #754. Fixes #813 --- inc/limitations/class-limit-site-templates.php | 2 +- inc/ui/class-template-switching-element.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/inc/limitations/class-limit-site-templates.php b/inc/limitations/class-limit-site-templates.php index 123897b8f..8056575b8 100644 --- a/inc/limitations/class-limit-site-templates.php +++ b/inc/limitations/class-limit-site-templates.php @@ -201,7 +201,7 @@ public function get_all_templates(): array { * Get available themes. * * @since 2.0.0 - * @return array + * @return array|false Array of available template IDs, or false when MODE_DEFAULT (unrestricted). */ public function get_available_site_templates() { diff --git a/inc/ui/class-template-switching-element.php b/inc/ui/class-template-switching-element.php index 7864edb90..442a4b02d 100644 --- a/inc/ui/class-template-switching-element.php +++ b/inc/ui/class-template-switching-element.php @@ -274,9 +274,10 @@ public function switch_template() { $template_id = (int) wu_request('template_id', ''); - $available_templates = array_map('intval', $this->site->get_limitations()->site_templates->get_available_site_templates()); + // false means MODE_DEFAULT (no restriction) — all templates are allowed. + $available_templates = $this->site->get_limitations()->site_templates->get_available_site_templates(); - if (! in_array($template_id, $available_templates, true)) { + if (false !== $available_templates && ! in_array($template_id, array_map('intval', $available_templates), true)) { wp_send_json_error(new \WP_Error('not_authorized', __('You are not allowed to use this template.', 'ultimate-multisite'))); }