Skip to content

Commit

Permalink
Add/features enabled rest endpoint (#39475)
Browse files Browse the repository at this point in the history
* Added features enabled rest endpoint

---------

Co-authored-by: sergeymitr <[email protected]>
  • Loading branch information
darssen and sergeymitr authored Sep 23, 2024
1 parent 8caefec commit 1d8d319
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: major
Type: removed

Connection: Removed deprecated features_enabled method
Original file line number Diff line number Diff line change
Expand Up @@ -823,24 +823,6 @@ public function disconnect_blog() {
return false;
}

/**
* Deprecated: This method is no longer part of the Connection package and now lives on the Jetpack plugin.
*
* Returns what features are enabled. Uses the slug of the modules files.
*
* @deprecated since 1.25.0
* @see Jetpack_XMLRPC_Methods::features_enabled() in the Jetpack plugin
*
* @return array
*/
public function features_enabled() {
_deprecated_function( __METHOD__, '1.25.0', 'Jetpack_XMLRPC_Methods::features_enabled()' );
if ( class_exists( 'Jetpack_XMLRPC_Methods' ) ) {
return Jetpack_XMLRPC_Methods::features_enabled();
}
return array();
}

/**
* Deprecated: This method is no longer part of the Connection package and now lives on the Jetpack plugin.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,21 @@ public static function register_endpoints() {
'permission_callback' => array( static::class, 'get_features_permission_check' ),
)
);

/**
* Get the list of enabled Jetpack features.
*
* @since $$next-version$$
*/
register_rest_route(
'jetpack/v4',
'/features/enabled',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( static::class, 'get_features_enabled' ),
'permission_callback' => array( static::class, 'get_features_permission_check' ),
)
);
}

/**
Expand Down Expand Up @@ -4498,6 +4513,21 @@ public static function get_features_available() {
return $modules;
}

/**
* Returns what features are enabled. Uses the slug of the modules files.
*
* @return array
*/
public static function get_features_enabled() {
$raw_modules = Jetpack::get_active_modules();
$modules = array();
foreach ( $raw_modules as $module ) {
$modules[] = Jetpack::get_module_slug( $module );
}

return $modules;
}

/**
* Verify that the API client is allowed to replace user token.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Adding features enabled rest endpoint
2 changes: 2 additions & 0 deletions projects/plugins/jetpack/class-jetpack-xmlrpc-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static function features_available() {
/**
* Returns what features are enabled. Uses the slug of the modules files.
*
* @deprecated $$next-version$$
* @see Jetpack_Core_Json_Api_Endpoints::get_features_enabled()
* @return array
*/
public static function features_enabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,50 @@ public function test_jetpack_rest_api_get_features_available_authentication_succ
$this->assertEquals( 200, $response->get_status() );
}

/**
* @author darssen
*
* Test the 'features/enabled' endpoint authentication.
*
* @since $$next-version$$
*/
public function test_jetpack_rest_api_get_features_enabled_authentication_success() {
add_filter( 'pre_option_jetpack_private_options', array( $this, 'mock_jetpack_private_options' ), 10, 2 );
$token = 'pretend_this_is_valid_blog_token:1:0';
$timestamp = (string) time();
$nonce = 'testing123';
$body_hash = '';

$_GET['token'] = $token;
$_GET['timestamp'] = $timestamp;
$_GET['nonce'] = $nonce;
$_GET['body-hash'] = $body_hash;
$_GET['signature'] = base64_encode(
hash_hmac(
'sha1',
implode(
"\n",
array(
$token,
$timestamp,
$nonce,
$body_hash,
'GET',
'example.org',
'80',
'/jetpack/v4/features/enabled',
'qstest=yep',
)
) . "\n",
'secret_blog',
true
)
);
$this->request = new WP_REST_Request( 'GET', '/jetpack/v4/features/enabled' );
$response = $this->server->dispatch( $this->request );
$this->assertEquals( 200, $response->get_status() );
}

/**
* @author jnylen0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,4 +1237,17 @@ public function test_features_available_unauthorized() {
$this->assertResponseStatus( 401, $response );
$this->assertResponseData( array( 'code' => 'invalid_permission_fetch_features' ), $response );
}

/**
* Test the 'features/enabled' endpoint, unauthorized.
*
* @since $$next-version$$
*/
public function test_features_enabled_unauthorized() {
// Create REST request in JSON format and dispatch
$response = $this->create_and_get_request( 'features/enabled' );

$this->assertResponseStatus( 401, $response );
$this->assertResponseData( array( 'code' => 'invalid_permission_fetch_features' ), $response );
}
} // class end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Connection: Removed features_enabled deprecated method

0 comments on commit 1d8d319

Please sign in to comment.