Skip to content

Commit

Permalink
Merge pull request #1 from spacedmonkey/feature/dependency-api-tweaks
Browse files Browse the repository at this point in the history
Dependencies API tweaks
  • Loading branch information
spacedmonkey authored Oct 30, 2020
2 parents 9bfe1ae + 4170447 commit 65b2d87
Show file tree
Hide file tree
Showing 9 changed files with 1,165 additions and 56 deletions.
28 changes: 0 additions & 28 deletions lib/class-wp-rest-block-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,34 +321,6 @@ protected function prepare_links( $block_type ) {
),
);

$scripts = array( 'editor_script', 'script' );
foreach ( $scripts as $script ) {
if ( ! isset( $block_type->$script ) ) {
continue;
}
$expected_handle = $block_type->$script;
if ( wp_script_is( $expected_handle, 'registered' ) ) {
$links[ 'https://api.w.org/' . $script ] = array(
'href' => rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'scripts', $expected_handle ) ),
'embeddable' => true,
);
}
}

$styles = array( 'editor_style', 'style' );
foreach ( $styles as $style ) {
if ( ! isset( $block_type->$style ) ) {
continue;
}
$expected_handle = $block_type->$style;
if ( wp_style_is( $expected_handle, 'registered' ) ) {
$links[ 'https://api.w.org/' . $style ] = array(
'href' => rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'styles', $expected_handle ) ),
'embeddable' => true,
);
}
}

if ( $block_type->is_dynamic() ) {
$links['https://api.w.org/render-block']['href'] = add_query_arg( 'context', 'edit', rest_url( sprintf( '%s/%s/%s', 'wp/v2', 'block-renderer', $block_type->name ) ) );
}
Expand Down
35 changes: 20 additions & 15 deletions lib/class-wp-rest-dependencies-controller.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Dependencies controller.
* Dependencies controller. An abstract class intended to be extended by scripts/styles endpoints.
*
* @package gutenberg
*/
Expand All @@ -12,7 +12,7 @@
*
* @see WP_REST_Controller
*/
class WP_REST_Dependencies_Controller extends WP_REST_Controller {
abstract class WP_REST_Dependencies_Controller extends WP_REST_Controller {

/**
* Dependencies core object.
Expand All @@ -37,8 +37,17 @@ class WP_REST_Dependencies_Controller extends WP_REST_Controller {
*/
protected $block_dependency = '';

/**
* Core assets collection.
*
* @var null|array
*/
protected $core_assets;

/**
* Register routes.
*
* @return void
*/
public function register_routes() {
register_rest_route(
Expand Down Expand Up @@ -98,6 +107,9 @@ public function get_items( $request ) {

if ( $handle ) {
foreach ( $filter as $dependency_handle ) {
if ( $handle === $dependency_handle ) {
continue;
}
foreach ( $this->object->registered as $dependency ) {
if ( $dependency_handle === $dependency->handle ) {
$item = $this->prepare_item_for_response( $dependency, $request );
Expand Down Expand Up @@ -179,9 +191,8 @@ public function get_items_permissions_check( $request ) {
$check = $this->check_read_permission( $request['dependency'] );
if ( is_wp_error( $check ) ) {
return $check;
} else if ( true === $check ) {
return $check;
} else if ( current_user_can( 'manage_options' ) ) {
}
if ( true === $check || current_user_can( 'manage_options' ) ) {
return true;
}

Expand All @@ -199,9 +210,8 @@ public function get_item_permissions_check( $request ) {
$check = $this->check_read_permission( $request['handle'] );
if ( is_wp_error( $check ) ) {
return $check;
} else if ( true === $check ) {
return $check;
} else if ( current_user_can( 'manage_options' ) ) {
}
if ( true === $check || current_user_can( 'manage_options' ) ) {
return true;
}

Expand Down Expand Up @@ -291,16 +301,11 @@ protected function check_read_permission( $handle ) {
}

/**
* Get core assets.
* Get core assets. Abstract method that needs to be covered by all child classes.
*
* @return array
*/
public function get_core_assets() {
/* translators: %s: Method name. */
_doing_it_wrong( sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'gutenberg' ), __METHOD__ ), 'x.x' );

return array();
}
abstract public function get_core_assets();

/**
* Block asset.
Expand Down
17 changes: 10 additions & 7 deletions lib/class-wp-rest-scripts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Class WP_REST_Scripts_Controller
*/
class WP_REST_Scripts_Controller extends WP_REST_Dependencies_Controller {

/**
* WP_REST_Scripts_Controller constructor.
*/
Expand Down Expand Up @@ -49,13 +50,15 @@ public function get_url( $src, $ver, $handle ) {
* @return array
*/
public function get_core_assets() {
$wp_scripts = new WP_Scripts();
wp_default_scripts( $wp_scripts );
wp_default_packages_vendor( $wp_scripts );
wp_default_packages_scripts( $wp_scripts );
$handles = wp_list_pluck( $wp_scripts->registered, 'handle' );
$handles = array_values( $handles );
if ( null === $this->core_assets ) {
$wp_scripts = new WP_Scripts();
wp_default_scripts( $wp_scripts );
wp_default_packages_vendor( $wp_scripts );
wp_default_packages_scripts( $wp_scripts );
$handles = wp_list_pluck( $wp_scripts->registered, 'handle' );
$this->core_assets = array_values( $handles );
}

return $handles;
return $this->core_assets;
}
}
13 changes: 8 additions & 5 deletions lib/class-wp-rest-styles-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Class WP_REST_Styles_Controller
*/
class WP_REST_Styles_Controller extends WP_REST_Dependencies_Controller {

/**
* WP_REST_Styles_Controller constructor.
*/
Expand Down Expand Up @@ -39,11 +40,13 @@ public function get_url( $src, $ver, $handle ) {
* @return array
*/
public function get_core_assets() {
$wp_styles = new WP_Styles();
wp_default_styles( $wp_styles );
$handles = wp_list_pluck( $wp_styles->registered, 'handle' );
$handles = array_values( $handles );
if ( null === $this->core_assets ) {
$wp_styles = new WP_Styles();
wp_default_styles( $wp_styles );
$handles = wp_list_pluck( $wp_styles->registered, 'handle' );
$this->core_assets = array_values( $handles );
}

return $handles;
return $this->core_assets;
}
}
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function gutenberg_is_experiment_enabled( $name ) {
}
if ( ! class_exists( 'WP_REST_Styles_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-styles-controller.php';
}
}
if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-customizer-nonces.php';
}
Expand Down
46 changes: 46 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,49 @@ function gutenberg_term_search_handler( $search_handlers ) {
return $search_handlers;
}
add_filter( 'wp_rest_search_handlers', 'gutenberg_term_search_handler', 10, 5 );

/**
* Adds style/script links to Block Types API endpoint response.
*
* @param \WP_REST_Response $response API response.
* @param \WP_Block_Type $block_type Block type.
*
* @return \WP_REST_Response Modified API response.
*/
function gutenberg_add_assets_links_to_block_type( $response, $block_type ) {
$links = array();
$scripts = array( 'editor_script', 'script' );
foreach ( $scripts as $script ) {
if ( ! isset( $block_type->$script ) ) {
continue;
}
$expected_handle = $block_type->$script;
if ( wp_script_is( $expected_handle, 'registered' ) ) {
$links[ 'https://api.w.org/' . $script ] = array(
'href' => rest_url( sprintf( '%s/%s/%s', '__experimental', 'scripts', $expected_handle ) ),
'embeddable' => true,
);
}
}

$styles = array( 'editor_style', 'style' );
foreach ( $styles as $style ) {
if ( ! isset( $block_type->$style ) ) {
continue;
}
$expected_handle = $block_type->$style;
if ( wp_style_is( $expected_handle, 'registered' ) ) {
$links[ 'https://api.w.org/' . $style ] = array(
'href' => rest_url( sprintf( '%s/%s/%s', '__experimental', 'styles', $expected_handle ) ),
'embeddable' => true,
);
}
}

if ( ! empty( $links ) ) {
$response->add_links( $links );
}

return $response;
}
add_filter( 'rest_prepare_block_type', 'gutenberg_add_assets_links_to_block_type', 10, 2 );
150 changes: 150 additions & 0 deletions phpunit/class-rest-block-types-controller-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* REST API: REST_Block_Types_Controller_Test class
*
* @package WordPress
* @subpackage REST_API
*/

/**
* Tests for REST API for Widgets.
*
* @see WP_Test_REST_Controller_Testcase
*/
class REST_Block_Types_Controller_Test extends WP_Test_REST_TestCase {
/**
* @var int
*/
protected static $admin_id;

/**
* Create fake data before our tests run.
*
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
wp_register_script( 'core-assets-test-script', home_url( '/assets/test-script.js' ) );
wp_register_script( 'core-assets-test-editor-script', home_url( '/assets/test-editor-script.js' ) );
wp_register_style( 'core-assets-test-style', home_url( '/assets/test-style.css' ) );
wp_register_style( 'core-assets-test-editor-style', home_url( '/assets/test-editor-style.css' ) );
}

/**
* Tear down after class tests are done.
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
unregister_block_type( 'core/assets-test' );
}

/**
* Test whether test block type contains scripts/styles links.
*
* @dataProvider dataProviderTestCases
*/
public function test_links_in_block( $block_name, $block_parameters ) {
wp_set_current_user( self::$admin_id );
register_block_type( $block_name, $block_parameters );
$endpoint = sprintf( '/wp/v2/block-types/%s', $block_name );
$request = new WP_REST_Request( 'GET', $endpoint );
$response = rest_get_server()->dispatch( $request );
$links = $response->get_links();
$this->assertEquals( 200, $response->get_status() );
foreach ( $block_parameters as $block_parameter => $parameter_value ) {
$links_key = sprintf( 'https://api.w.org/%s', $block_parameter );
if ( empty( $parameter_value ) ) {
$this->assertArrayNotHasKey( $links_key, $links );
} else {
$this->assertArrayHasKey( $links_key, $links );
$this->assertNotEmpty( $links[ $links_key ] );
}
}
}

/**
* Provide test cases.
*
* @return array[]
*/
public function dataProviderTestCases() {
return array(
array(
'core/assets-test-1',
array(
'script' => '',
'style' => '',
'editor_script' => '',
'editor_style' => '',
),
),
array(
'core/assets-test-2',
array(
'script' => 'core-assets-test-script',
'style' => '',
'editor_script' => '',
'editor_style' => '',
),
),
array(
'core/assets-test-3',
array(
'script' => '',
'style' => 'core-assets-test-style',
'editor_script' => '',
'editor_style' => '',
),
),
array(
'core/assets-test-4',
array(
'script' => '',
'style' => '',
'editor_script' => 'core-assets-test-editor-script',
'editor_style' => '',
),
),
array(
'core/assets-test-5',
array(
'script' => '',
'style' => '',
'editor_script' => '',
'editor_style' => 'core-assets-test-editor-style',
),
),
array(
'core/assets-test-6',
array(
'script' => '',
'style' => 'core-assets-test-style',
'editor_script' => '',
'editor_style' => 'core-assets-test-editor-style',
),
),
array(
'core/assets-test-7',
array(
'script' => 'core-assets-test-script',
'style' => 'core-assets-test-style',
'editor_script' => 'core-assets-test-editor-script',
'editor_style' => 'core-assets-test-editor-style',
),
),
array(
'core/assets-test-8',
array(
'script' => 'core-assets-test-script',
'style' => '',
'editor_script' => 'core-assets-test-editor-script',
'editor_style' => 'core-assets-test-editor-style',
),
),
);
}
}
Loading

0 comments on commit 65b2d87

Please sign in to comment.