Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection: Add REST support for jsonAPI endpoints #39432

Open
wants to merge 14 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-json-api-direct-access
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Add REST support for jsonAPI endpoints.
16 changes: 16 additions & 0 deletions projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,11 @@ function ( $methods ) {
if ( $is_connection_ready ) {
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php';
add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 );

require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php';
add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 );

add_action( 'rest_api_init', array( $this, 'maybe_initialize_rest_jsonapi' ) );
}
}

Expand Down Expand Up @@ -6340,6 +6343,19 @@ public function run_initialize_tracking_action() {
do_action( 'jetpack_initialize_tracking' );
}

/**
* Initialize REST jsonAPI if needed.
*
* @return void
*/
public function maybe_initialize_rest_jsonapi() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET['jsonapi'] ) && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) ) {
define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
}
}

/**
* Run plugin post-activation actions if we need to.
*
Expand Down
56 changes: 56 additions & 0 deletions projects/plugins/jetpack/class.json-api-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Status;

require_once __DIR__ . '/json-api-config.php';
Expand Down Expand Up @@ -124,6 +125,13 @@ abstract class WPCOM_JSON_API_Endpoint {
*/
public $path_labels = array();

/**
* The REST endpoint if available.
*
* @var bool
*/
public $rest_route = null;

/**
* Accepted query parameters
*
Expand Down Expand Up @@ -277,6 +285,11 @@ abstract class WPCOM_JSON_API_Endpoint {
*/
public $allow_fallback_to_jetpack_blog_token = false;

/**
* REST namespace.
*/
const REST_NAMESPACE = 'rest/v1';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's omit the version part here since we're gonna need to dynamically calculate it based on each endpoint's max_version definition.


/**
* Constructor.
*
Expand All @@ -300,6 +313,7 @@ public function __construct( $args ) {
'new_version' => WPCOM_JSON_API__CURRENT_VERSION,
'jp_disabled' => false,
'path_labels' => array(),
'rest_route' => null,
'request_format' => array(),
'response_format' => array(),
'query_parameters' => array(),
Expand Down Expand Up @@ -334,6 +348,7 @@ public function __construct( $args ) {
$this->method = $args['method'];
$this->path = $args['path'];
$this->path_labels = $args['path_labels'];
$this->rest_route = $args['rest_route'];
$this->min_version = $args['min_version'];
$this->max_version = $args['max_version'];
$this->deprecated = $args['deprecated'];
Expand Down Expand Up @@ -387,6 +402,10 @@ public function __construct( $args ) {
$this->example_response = $args['example_response'];

$this->api->add( $this );

if ( $this->rest_route ) {
$this->create_rest_route_for_endpoint();
}
}

/**
Expand Down Expand Up @@ -2618,6 +2637,43 @@ public function get_amp_cache_origins( $siteurl ) {
);
}

/**
* Register a REST route for this jsonAPI endpoint.
*
* @return void
* @throws Exception The exception if something goes wrong.
*/
public function create_rest_route_for_endpoint() {
register_rest_route(
static::REST_NAMESPACE,
$this->rest_route,
array(
'methods' => $this->method,
'callback' => array( $this, 'rest_callback' ),
)
);
}

/**
* Handle the rest call.
*
* @param WP_REST_Request $request The request object.
*
* @return mixed|WP_Error
*/
public function rest_callback( WP_REST_Request $request ) {
$manager = new Manager( 'jetpack' );
if ( ! $manager->is_connected() ) {
return new WP_Error( 'site_not_connected' );
}

$blog_id = Jetpack_Options::get_option( 'id' );
return call_user_func_array(
array( $this, 'callback' ),
array_values( array( $this->path, $blog_id ) + $request->get_url_params() )
);
}

/**
* Return endpoint response
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'$site' => '(int|string) Site ID or domain',
'$post_ID' => '(int) The post ID',
),
'rest_route' => '/posts/(?P<id>\d+)',

'allow_fallback_to_jetpack_blog_token' => true,

Expand All @@ -36,6 +37,7 @@
'$site' => '(int|string) Site ID or domain',
'$post_name' => '(string) The post name (a.k.a. slug)',
),
'rest_route' => '/posts/name/(?P<name>[\w_-]+)',

'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/name:blogging-and-stuff',
)
Expand All @@ -54,6 +56,7 @@
'$site' => '(int|string) Site ID or domain',
'$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
),
'rest_route' => '/posts/slug/(?P<slug>[\w_-]+)',

'allow_fallback_to_jetpack_blog_token' => true,

Expand Down
Loading