Skip to content

Commit

Permalink
Remove the install & uninstall endpoints from the block-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Jun 16, 2020
1 parent a7dc7ef commit c66486a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 180 deletions.
143 changes: 1 addition & 142 deletions lib/class-wp-rest-block-directory-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,6 @@ public function register_routes() {
'schema' => array( $this, 'get_public_item_schema' ),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/install',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => array(
'slug' => array(
'required' => true,
),
),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/uninstall',
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'slug' => array(
'required' => true,
),
),
)
);
}

/**
Expand Down Expand Up @@ -136,117 +106,6 @@ public function get_items( $request ) {
return rest_ensure_response( $result );
}

/**
* Checks whether a given request has permission to install and activate plugins.
*
* @since 5.5.0
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|bool True if the request has permission, WP_Error object otherwise.
*/
public function create_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( ! current_user_can( 'install_plugins' ) || ! current_user_can( 'activate_plugins' ) ) {
return new WP_Error(
'rest_block_directory_cannot_create',
__( 'Sorry, you are not allowed to install blocks.', 'gutenberg' ),
array( 'status' => rest_authorization_required_code() )
);
}

return true;
}

/**
* Installs and activates a plugin
*
* @since 5.5.0
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function create_item( $request ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';

$existing = $this->find_plugin_for_slug( $request['slug'] );

if ( $existing ) {
$activate = new WP_REST_Request( 'PUT', '/__experimental/plugins/' . substr( $existing, 0, - 4 ) );
$activate->set_body_params( array( 'status' => 'active' ) );

return rest_do_request( $activate );
}

$inner_request = new WP_REST_Request( 'POST', '/__experimental/plugins' );
$inner_request->set_body_params(
array(
'slug' => $request['slug'],
'status' => 'active',
)
);

return rest_do_request( $inner_request );
}

/**
* Checks whether a given request has permission to remove/deactivate plugins.
*
* @since 5.5.0
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|bool True if the request has permission, WP_Error object otherwise.
*/
public function delete_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( ! current_user_can( 'delete_plugins' ) || ! current_user_can( 'deactivate_plugins' ) ) {
return new WP_Error(
'rest_block_directory_cannot_delete',
__( 'Sorry, you are not allowed to uninstall blocks.', 'gutenberg' ),
array( 'status' => rest_authorization_required_code() )
);
}

return true;
}

/**
* Deactivates and deletes a plugin
*
* @since 5.5.0
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function delete_item( $request ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';

$slug = trim( $request->get_param( 'slug' ) );

if ( ! $slug ) {
return new WP_Error( 'slug_not_provided', 'Valid slug not provided.', array( 'status' => 400 ) );
}

$plugin_file = $this->find_plugin_for_slug( $slug );

if ( ! $plugin_file ) {
return new WP_Error( 'block_not_found', 'Valid slug not provided.', array( 'status' => 400 ) );
}

$route = '/__experimental/plugins/' . substr( $plugin_file, 0, - 4 );
$deactivate = new WP_REST_Request( 'PUT', $route );
$deactivate->set_body_params( array( 'status' => 'inactive' ) );

$deactivated = rest_do_request( $deactivate );

if ( $deactivated->is_error() ) {
return $deactivated->as_error();
}

return rest_do_request( new WP_REST_Request( 'DELETE', $route ) );
}

/**
* Parse block metadata for a block, and prepare it for an API repsonse.
*
Expand Down Expand Up @@ -277,7 +136,7 @@ public function prepare_item_for_response( $plugin, $request ) {
'assets' => array(),
'last_updated' => $plugin['last_updated'],
'humanized_updated' => sprintf(
/* translators: %s: Human-readable time difference. */
/* translators: %s: Human-readable time difference. */
__( '%s ago', 'gutenberg' ),
human_time_diff( strtotime( $plugin['last_updated'] ) )
),
Expand Down
40 changes: 2 additions & 38 deletions phpunit/class-wp-rest-block-directory-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public static function wpSetUpBeforeClass( $factory ) {
if ( is_multisite() ) {
grant_super_admin( self::$admin_id );
}

if ( ! defined( 'FS_METHOD' ) ) {
define( 'FS_METHOD', 'direct' );
}
}

public static function wpTearDownAfterClass() {
Expand Down Expand Up @@ -95,28 +91,15 @@ public function test_get_item() {
}

public function test_create_item() {
if ( isset( get_plugins()['hello-dolly/hello.php'] ) ) {
delete_plugins( array( 'hello-dolly/hello.php' ) );
}

wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'POST', '/__experimental/block-directory/install' );
$request->set_body_params( array( 'slug' => 'hello-dolly' ) );

$response = rest_do_request( $request );
$this->skip_on_filesystem_error( $response );
$this->assertNotWPError( $response->as_error() );
$this->assertEquals( 201, $response->get_status() );
$this->assertEquals( 'Hello Dolly', $response->get_data()['name'] );
$this->markTestSkipped( 'Controller does not have create_item route.' );
}

public function test_update_item() {
$this->markTestSkipped( 'Controller does not have update_item route.' );
}

public function test_delete_item() {
$this->markTestSkipped( 'Covered by Plugins controller tests.' );
$this->markTestSkipped( 'Controller does not have delete_item route.' );
}

public function test_prepare_item() {
Expand Down Expand Up @@ -172,25 +155,6 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'assets', $properties );
}

/**
* Skips the test if the response is an error due to the filesystem being unavailable.
*
* @since 5.5.0
*
* @param WP_REST_Response $response The response object to inspect.
*/
protected function skip_on_filesystem_error( WP_REST_Response $response ) {
if ( ! $response->is_error() ) {
return;
}

$code = $response->as_error()->get_error_code();

if ( 'fs_unavailable' === $code || false !== strpos( $code, 'mkdir_failed' ) ) {
$this->markTestSkipped( 'Filesystem is unavailable.' );
}
}

/**
* Simulate a network failure on outbound http requests to a given hostname.
*
Expand Down

0 comments on commit c66486a

Please sign in to comment.