fix: undefined array key 'context' warning in Gutenberg.php (PHP 8+)#943
fix: undefined array key 'context' warning in Gutenberg.php (PHP 8+)#943nicholasio merged 2 commits intodevelopfrom
Conversation
Add null coalescing operator to check for context parameter existence before accessing it in extend_post_content method. This prevents PHP 8+ "Undefined array key" warnings that corrupt JSON responses when the context query parameter is not explicitly provided in REST API requests. Fixes #940 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: c33e94b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
This PR is being reviewed by Cursor Bugbot
Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
| $params = $request->get_params(); | ||
|
|
||
| if ( 'view' !== $params['context'] ) { | ||
| if ( 'view' !== ( $params['context'] ?? '' ) ) { |
There was a problem hiding this comment.
Default fallback value mismatches WordPress context default
Medium Severity
When the context key is absent from $params, the fallback defaults to '' instead of 'view'. WordPress's standard default for context in REST API endpoints is 'view', so a missing key semantically means context=view. With ?? '', the check 'view' !== '' evaluates to true and the method returns early, skipping block styles — the opposite of what happens when context is explicitly 'view'. Using ?? 'view' as the fallback would correctly match WordPress's intended default and ensure block styles are added for requests that omit the context parameter.
📦 Next.js Bundle Analysis for @10up/wp-nextjs-appThis analysis was generated by the Next.js Bundle Analysis action. 🤖 🎉 Global Bundle Size Decreased
DetailsThe global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster. Any third party scripts you have added directly to your app using the If you want further insight into what is behind the changes, give @next/bundle-analyzer a try! |
📦 Next.js Bundle Analysis for @10up/headstartwpThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |


Summary
contextparameter inextend_post_contentmethodFixes #940
Problem
The
extend_post_contentmethod inGutenberg.phpaccesses$params['context']without checking if the key exists. In PHP 8.0+, this triggers an "Undefined array key" warning when thecontextquery parameter is not explicitly provided in the REST API request.This warning is output before the JSON response, corrupting the response body:
Solution
Use the null coalescing operator to default to an empty string when
contextkey doesn't exist:Test plan
GET /wp-json/wp/v2/pages?slug=testGET /wp-json/wp/v2/pages?slug=test&context=view🤖 Generated with Claude Code
Note
Low Risk
Single defensive check change plus a changeset; behavior only differs when
contextis missing, reducing warnings without affecting normalcontext=viewrequests.Overview
Prevents PHP 8+ "Undefined array key" warnings (and potential JSON response corruption) when
extend_post_contentruns on REST requests that omit thecontextparameter by defaulting$params['context']to an empty string.Adds a patch changeset documenting the fix for
@headstartwp/headstartwp.Written by Cursor Bugbot for commit c33e94b. This will update automatically on new commits. Configure here.