Skip to content

Commit

Permalink
Jetpack API: add 'jsonAPI' REST endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeymitr committed Jul 25, 2024
1 parent ab38bc9 commit 1c26269
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,16 @@ public static function register_endpoints() {
),
)
);

register_rest_route(
'jetpack/v4',
'/json-api',
array(
'methods' => WP_REST_Server::ALLMETHODS,
'callback' => array( static::class, 'json_api' ),
'permission_callback' => '__return_true',
)
);
}

/**
Expand Down Expand Up @@ -4501,4 +4511,43 @@ public static function get_intro_offers() {
)
);
}

/**
* Handle the JSON API request coming from WPCOM.
*
* @param WP_REST_Request $request The API request.
*
* @return WP_REST_Response|WP_Error The endpoint response.
*/
public static function json_api( $request ) {
$method = $request->get_method();
$url = $request->get_param( 'url' );
$user_details = $request->get_param( 'user_details' );
$locale = $request->get_param( 'locale' );
$auth_args = $request->get_param( 'auth_args' );

$body = $request->get_body();

$xmlrpc_args = array(
array( $method, $url, $body, null, $user_details, $locale ),
$auth_args,
);

$result = Jetpack_XMLRPC_Methods::json_api( $xmlrpc_args );

if ( is_array( $result ) && count( $result ) === 3 ) {
return rest_ensure_response(
array(
'code' => 'success',
'data' => array(
'output' => $result[0],
'nonce' => $result[1],
'hmac' => $result[2],
),
)
);
}

return new WP_Error( 'json_api_error' );
}
} // class end
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-json-api-rest-endpoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

API: add jsonAPI endpoint to supplement the XML-RPC one.

0 comments on commit 1c26269

Please sign in to comment.