Skip to content

WPCOM_JSON_API_Endpoint: Add individual checks for array properties to prevent warnings #43656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

coder-karen
Copy link
Contributor

@coder-karen coder-karen commented May 28, 2025

Fixes VULCAN-128

Proposed changes:

  • This PR is a follow-on from WPCOM_JSON_API_Endpoint: Ensure properties and keys are not undefined #43570, but with individual property checks to prevent the following warnings (and others not necessarily caught in logs):
    • Undefined property: stdClass::$ID (on lines 1677, 1685, 1697, 1698, 1709)
    • Undefined property: stdClass::$post_parent (on lines 1701, 1886)
    • Undefined property: stdClass::$guid (line 1699)
  • The fix proposed in the previous PR resulted in some fatals such as Fatal error: Uncaught Error: Cannot use object of type WP_Error as array (in class.wpcom-json-api-upload-media-v1-1-endpoint.php:168).
  • Since the endpoint expects an array to be returned even if the $media_item is not a proper WP_Post object, individual property checks have been added throughout the function instead.
    • null is used as a fallback for $response values in most cases as this would replicate what would be returned if properties were undefined, but in some cases I've added an empty string instead where an empty string would make more sense (alt, description, for example).

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

pdWQjU-1g0-p2

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

To replicate the warnings:
While I can't replicate the warnings 'naturally', they can be replicated with a bit of manipulation:

  • Make sure your Simple site is sandboxed.
  • Open the developer console - https://developer.wordpress.com/docs/api/console/ - and open up the Network tab in the browser console.
  • Do a post request for the WPCOM API, v1.1, /sites/{your simple site ID}/media/new. Use the dropdown to select the API request, and under media_urls add a URL for a video already on your Simple site (or any other URL to a video such as a sample video online). You could add a link to an image URL as well.
  • Clicking submit should add a new video (or image) to your site.
  • Check the network tab for the request data, and view the "Response". Under 'Body', copy the media array.
  • Above the check for the $media_item here (in production, assuming that is the Jetpack plugin folder being used while testing), override $media_item with a new object. Format such as:
$media_item = (object) array(
			'ID'      => 1234,
			'URL'     => 'https://yoursite.com/wp-content/uploads/2025/05/yourupload.mov',
);
  • You won't need all key value pairs, and specifically want to test omitting some such as ID.
  • Once added, submit the same request via the developer console as before.
  • You should see undefined property warnings.

To test the fix:

  • Apply this PR using the command in the generated comment below.
  • Re-add the $media_item from before (but in moon or sun, whichever is active), and there should be no warnings.
  • Test the Happy Path - remove the added $media_path item and test again, the video / image should upload as expected still.

@coder-karen coder-karen self-assigned this May 28, 2025
@coder-karen coder-karen added [Type] Bug When a feature is broken and / or not performing as intended [Feature] WPCOM API [Status] In Progress [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels May 28, 2025
Copy link
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: June 3, 2025
    • Code freeze: June 2, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

Copy link
Contributor

github-actions bot commented May 28, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the update/wpcom-json-api-endpoint-undefined-properties branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack update/wpcom-json-api-endpoint-undefined-properties

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@coder-karen coder-karen marked this pull request as ready for review May 28, 2025 12:33
Copy link

jp-launch-control bot commented May 28, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/class.json-api-endpoints.php 272/1336 (20.36%) -0.02% 1 ❤️‍🩹

Full summary · PHP report · JS report

$metadata = wp_get_attachment_metadata( $media_item->ID );
if ( isset( $metadata['height'], $metadata['width'] ) ) {
if ( isset( $metadata['height'] ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No warnings were being generated for this, other than a codesniffer message suggestingisset should only check one parameter at a time, hence the change. Same further down as well (around line 1774).

@coder-karen coder-karen added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels May 28, 2025
@coder-karen coder-karen requested a review from a team May 28, 2025 13:02
Copy link
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix proposed in the previous PR resulted in some fatals such as Fatal error: Uncaught Error: Cannot use object of type WP_Error as array (in class.wpcom-json-api-upload-media-v1-1-endpoint.php:168).

Out of curiosity, since the previous fix seems to cover all these cases how come we are not fixing this in class.wpcom-json-api-upload-media-v1-1-endpoint.php:168 by removing 'file' => $media_item['ID'],?
I believe we should fix this one anyways. Unless there were more fatals other than the ones coming from that endpoint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] WPCOM API [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Review This PR is ready for review. [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants