Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#41555 closed defect (bug) (fixed)

REST API: Call `rest_get_server()` instead of accessing the `$wp_rest_server` global

Reported by: jnylen0's profile jnylen0 Owned by: jnylen0's profile jnylen0
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.4
Component: REST API Keywords: has-patch has-unit-tests commit
Focuses: rest-api Cc:

Description

It is possible, though unlikely, that REST API functions can be called from within a context that has not actually loaded a WP_REST_Server instance into the $wp_rest_server variable. This has caused a few crashes on WP.com; details are well within the nightmare zone of customized WordPress load order.

There is already a rest_get_server() function to ensure that the server object is initialized before attempting to do anything with it; we should be consistent in using this function rather than the global variable.

This was mostly done in [36529] (cc @rmccue); the patch attached here cleans up all remaining uses of $wp_rest_server except for those within the rest_get_server() function, along with a few errant @global annotations.

Attachments (3)

41555.diff (2.9 KB) - added by jnylen0 7 years ago.
41555.2.diff (4.1 KB) - added by jnylen0 7 years ago.
Add a previously failing test case
41555.3.diff (4.2 KB) - added by jnylen0 7 years ago.
Fix PHPUnit setUp and tearDown calls

Download all attachments as: .zip

Change History (6)

@jnylen0
7 years ago

@jnylen0
7 years ago

Add a previously failing test case

#1 @jnylen0
7 years ago

  • Keywords has-unit-tests added

Added a unit test in 41555.2.diff. Before these changes, it fails with the same message we saw on WP.com:

Fatal error: Call to a member function register_route() on null in src/wp-includes/rest-api.php on line 77

#2 @rmccue
7 years ago

  • Keywords commit added

Makes sense to me.

@jnylen0
7 years ago

Fix PHPUnit setUp and tearDown calls

#3 @jnylen0
7 years ago

  • Owner set to jnylen0
  • Resolution set to fixed
  • Status changed from new to closed

In 41238:

REST API: Always call rest_get_server() instead of accessing the $wp_rest_server global.

This is a consistency improvement and also a bug fix for fairly obscure cases involving modified WP load order.

Fixes #41555.

Note: See TracTickets for help on using tickets.