Skip to content

Commit

Permalink
Block Directory: Update endpoints to remove __experimental namespace (
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle authored Jul 1, 2020
1 parent df55dee commit 7f8b9da
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 1,219 deletions.
11 changes: 6 additions & 5 deletions lib/class-wp-rest-block-directory-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/**
* Controller which provides REST endpoint for the blocks.
*
* This class can be removed when plugin support requires WordPress 5.5.0+.
*
* @since 5.5.0
*
* @see WP_REST_Controller
Expand All @@ -20,7 +22,7 @@ class WP_REST_Block_Directory_Controller extends WP_REST_Controller {
* Constructs the controller.
*/
public function __construct() {
$this->namespace = '__experimental';
$this->namespace = 'wp/v2';
$this->rest_base = 'block-directory';
}

Expand Down Expand Up @@ -177,15 +179,15 @@ public function prepare_item_for_response( $plugin, $request ) {
protected function prepare_links( $plugin ) {
$links = array(
'https://api.w.org/install-plugin' => array(
'href' => add_query_arg( 'slug', urlencode( $plugin['slug'] ), rest_url( '__experimental/plugins' ) ),
'href' => add_query_arg( 'slug', urlencode( $plugin['slug'] ), rest_url( 'wp/v2/plugins' ) ),
),
);

$plugin_file = $this->find_plugin_for_slug( $plugin['slug'] );

if ( $plugin_file ) {
$links['https://api.w.org/plugin'] = array(
'href' => rest_url( '__experimental/plugins/' . substr( $plugin_file, 0, - 4 ) ),
'href' => rest_url( 'wp/v2/plugins/' . substr( $plugin_file, 0, - 4 ) ),
'embeddable' => true,
);
}
Expand Down Expand Up @@ -322,8 +324,7 @@ public function get_item_schema() {
public function get_collection_params() {
$query_params = parent::get_collection_params();

$query_params['context']['default'] = 'view';
$query_params['per_page']['default'] = 3;
$query_params['context']['default'] = 'view';

$query_params['term'] = array(
'description' => __( 'Limit result set to blocks matching the search term.', 'gutenberg' ),
Expand Down
4 changes: 3 additions & 1 deletion lib/class-wp-rest-plugins-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/**
* Core class to access plugins via the REST API.
*
* This class can be removed when plugin support requires WordPress 5.5.0+.
*
* @since 5.5.0
*
* @see WP_REST_Controller
Expand All @@ -24,7 +26,7 @@ class WP_REST_Plugins_Controller extends WP_REST_Controller {
* @since 5.5.0
*/
public function __construct() {
$this->namespace = '__experimental';
$this->namespace = 'wp/v2';
$this->rest_base = 'plugins';
}

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-directory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@wordpress/icons": "file:../icons",
"@wordpress/notices": "file:../notices",
"@wordpress/plugins": "file:../plugins",
"@wordpress/url": "file:../url",
"lodash": "^4.17.15"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-directory/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function* installBlockType( block ) {
}
yield setIsInstalling( block.id, true );
const response = yield apiFetch( {
path: '__experimental/plugins',
path: 'wp/v2/plugins',
data: {
slug: block.id,
status: 'active',
Expand Down
11 changes: 8 additions & 3 deletions packages/block-directory/src/store/controls.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { getPath } from '@wordpress/url';

/**
* Loads a JavaScript file.
*
Expand All @@ -6,7 +11,7 @@
* @return {Promise} Promise which will resolve when the asset is loaded.
*/
export const loadScript = ( asset ) => {
if ( ! asset || ! /\.js$/.test( asset ) ) {
if ( ! asset || ! /\.js$/.test( getPath( asset ) ) ) {
return Promise.reject( new Error( 'No script found.' ) );
}
return new Promise( ( resolve, reject ) => {
Expand All @@ -30,7 +35,7 @@ export const loadScript = ( asset ) => {
* @return {Promise} Promise which will resolve when the asset is added.
*/
export const loadStyle = ( asset ) => {
if ( ! asset || ! /\.css$/.test( asset ) ) {
if ( ! asset || ! /\.css$/.test( getPath( asset ) ) ) {
return Promise.reject( new Error( 'No style found.' ) );
}
return new Promise( ( resolve, reject ) => {
Expand Down Expand Up @@ -60,7 +65,7 @@ export function loadAssets( assets ) {
const controls = {
LOAD_ASSETS( { assets } ) {
const scripts = assets.map( ( asset ) =>
asset.match( /\.js$/ ) !== null
getPath( asset ).match( /\.js$/ ) !== null
? loadScript( asset )
: loadStyle( asset )
);
Expand Down
4 changes: 2 additions & 2 deletions packages/block-directory/src/store/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
try {
yield fetchDownloadableBlocks( filterValue );
const results = yield apiFetch( {
path: `__experimental/block-directory/search?term=${ filterValue }`,
path: `wp/v2/block-directory/search?term=${ filterValue }`,
} );
const blocks = results.map( ( result ) =>
mapKeys( result, ( value, key ) => {
Expand All @@ -45,7 +45,7 @@ export default {
try {
const response = yield apiFetch( {
method: 'OPTIONS',
path: `__experimental/block-directory/search`,
path: `wp/v2/block-directory/search`,
parse: false,
} );

Expand Down
6 changes: 3 additions & 3 deletions packages/block-directory/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { installBlockType, uninstallBlockType } from '../actions';

describe( 'actions', () => {
const endpoint = '/wp-json/__experimental/plugins/block/block';
const endpoint = '/wp-json/wp/v2/plugins/block/block';
const item = {
id: 'block/block',
name: 'Test Block',
Expand Down Expand Up @@ -43,7 +43,7 @@ describe( 'actions', () => {
expect( generator.next().value ).toMatchObject( {
type: 'API_FETCH',
request: {
path: '__experimental/plugins',
path: 'wp/v2/plugins',
method: 'POST',
},
} );
Expand Down Expand Up @@ -122,7 +122,7 @@ describe( 'actions', () => {
expect( generator.next().value ).toMatchObject( {
type: 'API_FETCH',
request: {
path: '__experimental/plugins',
path: 'wp/v2/plugins',
method: 'POST',
},
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import {

// Urls to mock
const SEARCH_URLS = [
'/__experimental/block-directory/search',
`rest_route=${ encodeURIComponent(
'/__experimental/block-directory/search'
) }`,
'/wp/v2/block-directory/search',
`rest_route=${ encodeURIComponent( '/wp/v2/block-directory/search' ) }`,
];

const INSTALL_URLS = [
'/__experimental/plugins',
`rest_route=${ encodeURIComponent( '/__experimental/plugins' ) }`,
'/wp/v2/plugins',
`rest_route=${ encodeURIComponent( '/wp/v2/plugins' ) }`,
];

// Example Blocks
Expand All @@ -36,7 +34,7 @@ const MOCK_BLOCK1 = {
author: 'No Author',
icon: 'block-default',
assets: [
'fake_url.com/block.js', // we will mock this
'https://fake_url.com/block.js', // we will mock this
],
humanized_updated: '5 months ago',
};
Expand Down Expand Up @@ -86,7 +84,7 @@ const block = `( function() {
} )();`;

const MOCK_OPTIONS = {
namespace: '__experimental',
namespace: 'wp/v2',
methods: [ 'GET' ],
endpoints: [
{
Expand Down
181 changes: 0 additions & 181 deletions phpunit/class-wp-rest-block-directory-controller-test.php

This file was deleted.

Loading

0 comments on commit 7f8b9da

Please sign in to comment.