Commit 209fd26
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
- .changeset
- wp/headless-wp/includes/classes/Integrations
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
| 90 | + | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| |||
0 commit comments