Make WordPress Core

Opened 22 months ago

Closed 19 months ago

Last modified 19 months ago

#62292 closed defect (bug) (fixed)

Warning in WP_REST_Posts_Controller::get_items()

Reported by: apermo Owned by: flixos90
Priority: normal Milestone: 6.8
Component: REST API Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

On my sentry installation I get Warning: Undefined array key "paged" for /wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php in WP_REST_Posts_Controller::get_items at line 400

Same also technically applies to the WP_REST_Global_Styles_Revisions_Controller and WP_REST_Revisions_Controller.

A Github PR for this follows.

Change History (10)

This ticket was mentioned in PR #7635 on WordPress/wordpress-develop by @apermo.


22 months ago
#1

  • Keywords has-patch added

WP_REST_Revisions_Controller::get_items(), WP_REST_Posts_Controller::get_items() and WP_REST_Global_Styles_Revisions_Controller::get_items() can throw a warning if paged is not set in query_args

This PR fixes that, by duplicating and adjusting the same logic already present for offset in both Revisions controller, ensuring that the result in $page will still be 0 if it was empty, and the int value otherwise, just catching the warning.

Trac ticket: [](https://core.trac.wordpress.org/ticket/62292)

#2 @Hemant Ahir
22 months ago

  • Focuses ui docs rest-api performance added

Here's the concise solution for the warning Undefined array key "paged" in the WP_REST_Posts_Controller:

Solution: Add a check to verify if the "paged" parameter exists in the request before accessing it. If it's not present, assign a default value.

Fix (around line 400 in class-wp-rest-posts-controller.php):

<?php
$paged = isset( $request['paged'] ) ? absint( $request['paged'] ) : 1;
?>

This same logic can be applied to WP_REST_Global_Styles_Revisions_Controller and WP_REST_Revisions_Controller as well. This ensures that no warnings occur when the "paged" key is missing.

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


21 months ago

This ticket was mentioned in PR #8021 on WordPress/wordpress-develop by @pbearne.


20 months ago
#4

  • Keywords has-unit-tests added

#5 @pbearne
20 months ago

  • Milestone Awaiting Review6.8
  • Owner set to pbearne
  • Status newassigned

@apermo commented on PR #7635:


20 months ago
#6

Closing in favor of #8021

#7 @swissspidy
19 months ago

  • Focuses ui docs rest-api performance removed

#8 @flixos90
19 months ago

  • Owner changed from pbearne to flixos90
  • Status assignedreviewing

Great catch @apermo! The PR by @pbearne looks good. While I agree this is unrelated to performance, this is a worthwhile win. I'll take a closer look at the PR later.

#9 @flixos90
19 months ago

  • Resolutionfixed
  • Status reviewingclosed

In 59630:

REST API: Fix PHP warning about undefined paged argument in various REST API endpoints.

This bug could occur in WP_REST_Posts_Controller, WP_REST_Global_Styles_Revisions_Controller, WP_REST_Revisions_Controller, and any of their child classes. This changeset fixes it throughout.

Props apermo, pbearne, hemant-ahir, flixos90.
Fixes #62292.

Note: See TracTickets for help on using tickets.