Make WordPress Core

Opened 5 months ago

Closed 2 months ago

Last modified 2 months ago

#62292 closed defect (bug) (fixed)

Warning in WP_REST_Posts_Controller::get_items()

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

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.


5 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
5 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.


5 months ago

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


3 months ago
#4

  • Keywords has-unit-tests added

#5 @pbearne
3 months ago

  • Milestone changed from Awaiting Review to 6.8
  • Owner set to pbearne
  • Status changed from new to assigned

@apermo commented on PR #7635:


3 months ago
#6

Closing in favor of #8021

#7 @swissspidy
2 months ago

  • Focuses ui docs rest-api performance removed

#8 @flixos90
2 months ago

  • Owner changed from pbearne to flixos90
  • Status changed from assigned to reviewing

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
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

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.