From f86194af24608e243ad61a828aea5af8bad400d6 Mon Sep 17 00:00:00 2001 From: Huzaifa Al Mesbah Date: Fri, 6 Mar 2026 21:37:15 +0600 Subject: [PATCH 1/5] Fix PHPStan undefined variable errors at level 3 --- src/wp-admin/comment.php | 3 ++- src/wp-settings.php | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/comment.php b/src/wp-admin/comment.php index e1058695a3c5d..96ab5cddce857 100644 --- a/src/wp-admin/comment.php +++ b/src/wp-admin/comment.php @@ -14,8 +14,9 @@ /** * @global string $action + * @global int $comment_id Comment ID. */ -global $action; +global $action, $comment_id; $action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : ''; diff --git a/src/wp-settings.php b/src/wp-settings.php index 023cdccd5ecc9..ef3db0717b59e 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -29,8 +29,10 @@ * @global string[] $required_php_extensions The names of required PHP extensions. * @global string $required_mysql_version The minimum required MySQL version string. * @global string $wp_local_package Locale code of the package. + * @global array $wp_filter WordPress filter hooks. + * @global string $table_prefix Database table prefix. */ -global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package; +global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package, $wp_filter, $table_prefix; require ABSPATH . WPINC . '/version.php'; require ABSPATH . WPINC . '/compat-utf8.php'; require ABSPATH . WPINC . '/compat.php'; From 8b93b41edae8c8acd97736dd4b50a513519fed92 Mon Sep 17 00:00:00 2001 From: Huzaifa Al Mesbah Date: Sat, 7 Mar 2026 10:10:01 +0600 Subject: [PATCH 2/5] Code Quality: Fix PHPStan undefined variable errors in ms-settings.php --- src/wp-admin/comment.php | 6 +++--- src/wp-includes/ms-settings.php | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/comment.php b/src/wp-admin/comment.php index 96ab5cddce857..b834f7b594062 100644 --- a/src/wp-admin/comment.php +++ b/src/wp-admin/comment.php @@ -14,9 +14,8 @@ /** * @global string $action - * @global int $comment_id Comment ID. */ -global $action, $comment_id; +global $action; $action = ! empty( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : ''; @@ -49,7 +48,8 @@ ); } } else { - $comment = null; + $comment_id = 0; + $comment = null; } switch ( $action ) { diff --git a/src/wp-includes/ms-settings.php b/src/wp-includes/ms-settings.php index 4f9d9e35792b3..68a5dba503721 100644 --- a/src/wp-includes/ms-settings.php +++ b/src/wp-includes/ms-settings.php @@ -33,8 +33,10 @@ * Use `get_current_network_id()` instead. * @global bool $public Deprecated. Whether the site found on load is public. * Use `get_site()->public` instead. + * @global string $table_prefix Database table prefix. + * @global wpdb $wpdb WordPress database abstraction object. */ -global $current_site, $current_blog, $domain, $path, $site_id, $public; +global $current_site, $current_blog, $domain, $path, $site_id, $public, $table_prefix, $wpdb; /** WP_Network class */ require_once ABSPATH . WPINC . '/class-wp-network.php'; From 9d9dd59b1924113fa1adaf0fca16e14e0ee5f3db Mon Sep 17 00:00:00 2001 From: Huzaifa Al Mesbah Date: Sun, 8 Mar 2026 14:05:39 +0600 Subject: [PATCH 3/5] Code Quality: Fix PHPStan undefined variables using isset/empty checks --- src/wp-includes/ms-settings.php | 14 ++++++++------ src/wp-settings.php | 8 +++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/ms-settings.php b/src/wp-includes/ms-settings.php index 68a5dba503721..feaa9dd16403d 100644 --- a/src/wp-includes/ms-settings.php +++ b/src/wp-includes/ms-settings.php @@ -33,10 +33,8 @@ * Use `get_current_network_id()` instead. * @global bool $public Deprecated. Whether the site found on load is public. * Use `get_site()->public` instead. - * @global string $table_prefix Database table prefix. - * @global wpdb $wpdb WordPress database abstraction object. */ -global $current_site, $current_blog, $domain, $path, $site_id, $public, $table_prefix, $wpdb; +global $current_site, $current_blog, $domain, $path, $site_id, $public; /** WP_Network class */ require_once ABSPATH . WPINC . '/class-wp-network.php'; @@ -101,9 +99,13 @@ wp_load_core_site_options( $site_id ); } -$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php. -$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id ); -$table_prefix = $wpdb->get_blog_prefix(); +if ( isset( $wpdb ) ) { + if ( isset( $table_prefix ) ) { + $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php. + } + $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id ); + $table_prefix = $wpdb->get_blog_prefix(); +} $_wp_switched_stack = array(); $switched = false; diff --git a/src/wp-settings.php b/src/wp-settings.php index ef3db0717b59e..531c2a2a509cb 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -29,10 +29,8 @@ * @global string[] $required_php_extensions The names of required PHP extensions. * @global string $required_mysql_version The minimum required MySQL version string. * @global string $wp_local_package Locale code of the package. - * @global array $wp_filter WordPress filter hooks. - * @global string $table_prefix Database table prefix. */ -global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package, $wp_filter, $table_prefix; +global $wp_version, $wp_db_version, $tinymce_version, $required_php_version, $required_php_extensions, $required_mysql_version, $wp_local_package; require ABSPATH . WPINC . '/version.php'; require ABSPATH . WPINC . '/compat-utf8.php'; require ABSPATH . WPINC . '/compat.php'; @@ -102,7 +100,7 @@ include WP_CONTENT_DIR . '/advanced-cache.php'; // Re-initialize any hooks added manually by advanced-cache.php. - if ( $wp_filter ) { + if ( ! empty( $wp_filter ) ) { $wp_filter = WP_Hook::build_preinitialized_hooks( $wp_filter ); } } @@ -142,7 +140,7 @@ * * @global string $table_prefix The database table prefix. */ -if ( ! isset( $GLOBALS['table_prefix'] ) ) { +if ( ! isset( $GLOBALS['table_prefix'] ) && isset( $table_prefix ) ) { $GLOBALS['table_prefix'] = $table_prefix; } From e6bbbfc3fff9acca64d812ff2bc70ae57df140de Mon Sep 17 00:00:00 2001 From: Huzaifa Al Mesbah Date: Mon, 9 Mar 2026 10:18:54 +0600 Subject: [PATCH 4/5] Apply alternative suggestion Co-authored-by: Weston Ruter --- src/wp-includes/ms-settings.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/ms-settings.php b/src/wp-includes/ms-settings.php index feaa9dd16403d..b02dc9d161379 100644 --- a/src/wp-includes/ms-settings.php +++ b/src/wp-includes/ms-settings.php @@ -99,13 +99,11 @@ wp_load_core_site_options( $site_id ); } -if ( isset( $wpdb ) ) { - if ( isset( $table_prefix ) ) { - $wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php. - } - $wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id ); - $table_prefix = $wpdb->get_blog_prefix(); -} +assert( isset( $wpdb ) ); +assert( isset( $table_prefix ) ); +$wpdb->set_prefix( $table_prefix, false ); // $table_prefix can be set in sunrise.php. +$wpdb->set_blog_id( $current_blog->blog_id, $current_blog->site_id ); +$table_prefix = $wpdb->get_blog_prefix(); $_wp_switched_stack = array(); $switched = false; From eef6fa51b8ee7ef7bc55bdd788fbdef38ffd1ecd Mon Sep 17 00:00:00 2001 From: Huzaifa Al Mesbah Date: Mon, 9 Mar 2026 10:19:14 +0600 Subject: [PATCH 5/5] Apply alternative suggestion Co-authored-by: Weston Ruter --- src/wp-settings.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-settings.php b/src/wp-settings.php index 531c2a2a509cb..03c95fbe0a870 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -140,7 +140,8 @@ * * @global string $table_prefix The database table prefix. */ -if ( ! isset( $GLOBALS['table_prefix'] ) && isset( $table_prefix ) ) { +if ( ! isset( $GLOBALS['table_prefix'] ) ) { + assert( isset( $table_prefix ) ); $GLOBALS['table_prefix'] = $table_prefix; }