Skip to content

Commit

Permalink
Scheduled Updates: Check if load function exists before using it. (#3…
Browse files Browse the repository at this point in the history
…7081)

* Scheduled Updates: Check if load function exists before using it.

In #36915 we introduced an alternative load path for the endpoint file, which can lead to undefined function errors when loaded outside of the context of Jetpack.

* Fix up versions

* Remove unneeded collection callback

It doesn't do anything currently and creates fatal errors when update-schedules endpoints get called internally. Let's just remove it for now.

* Only call rest_get_server() if we're in a REST API request.
  • Loading branch information
obenland committed Apr 25, 2024
1 parent f31bc10 commit 5c9eae6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Adds an exists check before using wpcom_rest_api_v2_load_plugin() in the API endpoint.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Scheduled_Updates {
*
* @var string
*/
const PACKAGE_VERSION = '0.9.0';
const PACKAGE_VERSION = '0.9.1-alpha';

/**
* The cron event hook for the scheduled plugins update.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,16 @@ public function prepare_response_for_collection( $response ) {
return $response;
}

$data = (array) $response->get_data();
$server = rest_get_server();
$links = $server::get_compact_response_links( $response );
$data = (array) $response->get_data();

if ( ! empty( $links ) ) {
$data['_links'] = $links;
// Only call rest_get_server() if we're in a REST API request.
if ( did_action( 'rest_api_init' ) ) {
$server = rest_get_server();
$links = $server::get_compact_response_links( $response );

if ( ! empty( $links ) ) {
$data['_links'] = $links;
}
}

return $data;
Expand Down Expand Up @@ -834,4 +838,6 @@ public function get_object_params() {
}
}

wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Update_Schedules' );
if ( function_exists( 'wpcom_rest_api_v2_load_plugin' ) ) {
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Update_Schedules' );
}

0 comments on commit 5c9eae6

Please sign in to comment.