Skip to content

Code Quality: Resolve variable.undefined errors in wp-mail.php and wp-trackback.php - #13880

Closed
robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:trac-65817-undefined-globals
Closed

robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:trac-65817-undefined-globals

Conversation

@robelsust

@robelsust robelsust commented Oct 1, 2026 •

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/65817
Follow-up to #13810 (review)

Resolves three variable.undefined baseline entries covering six error occurrences in wp-mail.php and wp-trackback.php. The wp-activate.php and wp-signup.php changes are handled separately in PR #13810.

File Variable Count Cause
wp-mail.php $subject 1 Assigned inside foreach only when a Subject: header matches
wp-trackback.php $wpdb 4 Set in wp-settings.php via $GLOBALS, used after require wp-load.php
wp-trackback.php $posts 1 Set in wp-settings.php via $GLOBALS, used after require wp-load.php

Changes

  1. src/wp-mail.php: Initialize $subject = ''; alongside the other per-message variables ($bodysignal, $boundary, $charset, etc.) before the foreach ( $message as $line ) loop. Without this, $subject is only assigned when a line matches /Subject: /i, so PHPStan correctly flags it as potentially undefined when the loop completes without a match.

  2. src/wp-trackback.php: Add global $wpdb, $posts; with @global docblocks in file scope. $wpdb is used four times (two prepare calls, one get_results, one insert_id) and $posts once ($posts[0]->ID), all reachable after require wp-load.php.

  3. tests/phpstan/baselines/variable.undefined.neon: Remove the three resolved suppression entries (6 errors total).

Background

  • The $subject variable in wp-mail.php has been conditionally assigned inside the header-parsing loop since the file's earliest form. The surrounding variables ($bodysignal, $boundary, $charset, $content, $content_type, $content_transfer_encoding, $post_author, $author_found, $post_date, $post_date_gmt) were all given explicit initializations in r42343 (2017-11-30, Gary Pendergast), but $subject was missed.

  • The $wpdb and $posts usages in wp-trackback.php date to the earliest revisions of the file. Variable names were cleaned up in r53719 (2022-07-19, Sergey Biryukov), but no global declaration was added since the file operates at the global scope via require.

Testing instructions

  1. On trunk, npm run typecheck:php reports [OK] No errors, because all three occurrences are baselined.
  2. Remove the three entries from tests/phpstan/baselines/variable.undefined.neon without applying the source changes, then run again. PHPStan reports:
    • Variable $subject might not be defined. in src/wp-mail.php
    • Variable $wpdb might not be defined. (×4) in src/wp-trackback.php
    • Variable $posts might not be defined. in src/wp-trackback.php
  3. With this branch applied, npm run typecheck:php reports [OK] No errors with all three baseline entries removed and no new errors elsewhere.
  4. composer lint is clean on both changed files.
  5. The baseline was regenerated with composer phpstan:baselines -- --identifier=variable.undefined, not edited by hand.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity IDE
Model(s): Claude Opus 4
Used for: Locating the variable.undefined occurrences via the PHPStan baseline, git blame/git log -S archaeology to trace each variable's origin, and drafting this description. The source changes, baseline regeneration, and verification against PHPStan and PHPCS were reviewed and confirmed by me in a local development environment.

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props therssoftware, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@mukeshpanchal27

Copy link
Copy Markdown
Member

@robelsust Thanks for the PR!

I already addressed that feedback in my PR, so please remove those changes from this PR. For the remaining baseline issues, please fix the variable.undefined errors instead.

Comment thread src/wp-trackback.php Outdated
@robelsust
robelsust force-pushed the trac-65817-undefined-globals branch from 27eb717 to 33dccdb Compare October 1, 2026 06:28
@robelsust robelsust changed the title Code Quality: Resolve variable.undefined errors in root entry-point scripts Code Quality: Resolve variable.undefined errors in wp-mail.php and wp-trackback.php Oct 1, 2026
wporg-sync pushed a commit that referenced this pull request Oct 2, 2026
…d `wp-trackback.php`.

1. **`src/wp-mail.php`**: Initialize `$subject = '';` alongside the other per-message variables (`$bodysignal`, `$boundary`, `$charset`, etc.) before the `foreach ( $message as $line )` loop. Without this, `$subject` is only assigned when a line matches `/Subject: /i`, so PHPStan correctly flags it as potentially undefined when the loop completes without a match.

2. **`src/wp-trackback.php`**: Add `global $wpdb, $posts;` with `@global` docblocks in file scope. `$wpdb` is used four times (two `prepare` calls, one `get_results`, one `insert_id`) and `$posts` once (`$posts[0]->ID`), all reachable after `require wp-load.php`.

3. **`tests/phpstan/baselines/variable.undefined.neon`**: Remove the three resolved suppression entries (6 errors total).

Developed in #13880.

Follow-up to r51850, r53719.

Props therssoftware, mukesh27.
See #65817.

git-svn-id: https://develop.svn.wordpress.org/trunk@64072 602fd350-edb4-49c9-b593-d223f7449a82
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Oct 2, 2026
…d `wp-trackback.php`.

1. **`src/wp-mail.php`**: Initialize `$subject = '';` alongside the other per-message variables (`$bodysignal`, `$boundary`, `$charset`, etc.) before the `foreach ( $message as $line )` loop. Without this, `$subject` is only assigned when a line matches `/Subject: /i`, so PHPStan correctly flags it as potentially undefined when the loop completes without a match.

2. **`src/wp-trackback.php`**: Add `global $wpdb, $posts;` with `@global` docblocks in file scope. `$wpdb` is used four times (two `prepare` calls, one `get_results`, one `insert_id`) and `$posts` once (`$posts[0]->ID`), all reachable after `require wp-load.php`.

3. **`tests/phpstan/baselines/variable.undefined.neon`**: Remove the three resolved suppression entries (6 errors total).

Developed in WordPress/wordpress-develop#13880.

Follow-up to r51850, r53719.

Props therssoftware, mukesh27.
See #65817.
Built from https://develop.svn.wordpress.org/trunk@64072


git-svn-id: http://core.svn.wordpress.org/trunk@63231 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@SergeyBiryukov

Copy link
Copy Markdown
Member

Thanks for the PR! Merged in r64072.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants