Skip to content

Commit 209fd26

Browse files
authored
fix: undefined array key 'context' warning in Gutenberg.php (PHP 8+) (#943)
## Summary - Adds null coalescing check for `context` parameter in `extend_post_content` method - Prevents PHP 8+ "Undefined array key" warnings that corrupt JSON responses Fixes #940 ## Problem The `extend_post_content` method in `Gutenberg.php` accesses `$params['context']` without checking if the key exists. In PHP 8.0+, this triggers an "Undefined array key" warning when the `context` query parameter is not explicitly provided in the REST API request. This warning is output before the JSON response, corrupting the response body: ``` <br /> <b>Warning</b>: Undefined array key "context" in <b>.../Gutenberg.php</b> on line <b>90</b><br /> [{"id":123,...}] ``` ## Solution Use the null coalescing operator to default to an empty string when `context` key doesn't exist: ```php // Before if ( 'view' !== $params['context'] ) { // After if ( 'view' !== ( $params['context'] ?? '' ) ) { ``` ## Test plan - [ ] Make REST API request without context parameter: `GET /wp-json/wp/v2/pages?slug=test` - [ ] Verify response is clean JSON without PHP warnings - [ ] Make REST API request with context parameter: `GET /wp-json/wp/v2/pages?slug=test&context=view` - [ ] Verify block_styles are still added to response when context=view 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Single defensive check change plus a changeset; behavior only differs when `context` is missing, reducing warnings without affecting normal `context=view` requests. > > **Overview** > Prevents PHP 8+ "Undefined array key" warnings (and potential JSON response corruption) when `extend_post_content` runs on REST requests that omit the `context` parameter by defaulting `$params['context']` to an empty string. > > Adds a patch changeset documenting the fix for `@headstartwp/headstartwp`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c33e94b. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 8e1f9df commit 209fd26

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@headstartwp/headstartwp": patch
3+
---
4+
5+
Fix: Add null coalescing check for context parameter in extend_post_content to prevent PHP 8+ "Undefined array key" warning. Fixes #940

wp/headless-wp/includes/classes/Integrations/Gutenberg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function extend_post_content( \WP_REST_Response $data, \WP_Post $post, \W
8787

8888
$params = $request->get_params();
8989

90-
if ( 'view' !== $params['context'] ) {
90+
if ( 'view' !== ( $params['context'] ?? '' ) ) {
9191
return $data;
9292
}
9393

0 commit comments

Comments
 (0)