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

Font Library: consolidate existing API rest endpoints. #57282

Merged
merged 7 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Autosave Rest Font Families Controller.
*
* This file contains the class for the Autosave REST API Font Families Controller.
*
* @package WordPress
* @subpackage Font Library
* @since 6.5.0
*/

if ( class_exists( 'WP_REST_Autosave_Font_Families_Controller' ) ) {
return;
}

/**
* Autosave Font Families Controller class.
*
* @since 6.5.0
*/
class WP_REST_Autosave_Font_Families_Controller {
public function register_routes() {
// disable autosave endpoints for font families
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* Rest Font Collections Controller.
*
* This file contains the class for the REST API Font Collections Controller.
*
* @package WordPress
* @subpackage Font Library
* @since 6.5.0
*/

if ( class_exists( 'WP_REST_Font_Collections_Controller' ) ) {
return;
}

/**
* Font Library Controller class.
*
* @since 6.5.0
*/
class WP_REST_Font_Collections_Controller extends WP_REST_Controller {

/**
* Constructor.
*
* @since 6.5.0
*/
public function __construct() {
$this->rest_base = 'font-collections';
$this->namespace = 'wp/v2';
}

/**
* Registers the routes for the objects of the controller.
*
* @since 6.5.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_font_collections' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_font_collection' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
),
)
);
}

/**
* Gets a font collection.
*
* @since 6.5.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collection( $request ) {
$id = $request->get_param( 'id' );
$collection = WP_Font_Library::get_font_collection( $id );
// If the collection doesn't exist returns a 404.
if ( is_wp_error( $collection ) ) {
$collection->add_data( array( 'status' => 404 ) );
return $collection;
}
$collection_with_data = $collection->get_data();
// If there was an error getting the collection data, return the error.
if ( is_wp_error( $collection_with_data ) ) {
$collection_with_data->add_data( array( 'status' => 500 ) );
return $collection_with_data;
}
return new WP_REST_Response( $collection_with_data );
}

/**
* Gets the font collections available.
*
* @since 6.5.0
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collections() {
$collections = array();
foreach ( WP_Font_Library::get_font_collections() as $collection ) {
$collections[] = $collection->get_config();
}

return new WP_REST_Response( $collections, 200 );
}

/**
* Checks whether the user has permissions to update the Font Library.
*
* @since 6.5.0
*
* @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise.
*/
public function update_font_library_permissions_check() {
if ( ! current_user_can( 'edit_theme_options' ) ) {
return new WP_Error(
'rest_cannot_update_font_library',
__( 'Sorry, you are not allowed to update the Font Library on this site.', 'gutenberg' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
<?php
/**
* Rest Font Library Controller.
* Rest Font Families Controller.
*
* This file contains the class for the REST API Font Library Controller.
* This file contains the class for the REST API Font Families Controller.
*
* @package WordPress
* @subpackage Font Library
* @since 6.5.0
*/

if ( class_exists( 'WP_REST_Font_Library_Controller' ) ) {
if ( class_exists( 'WP_REST_Font_Families_Controller' ) ) {
return;
}

/**
* Font Library Controller class.
* Font Families Controller class.
*
* @since 6.5.0
*/
class WP_REST_Font_Library_Controller extends WP_REST_Controller {
class WP_REST_Font_Families_Controller extends WP_REST_Posts_Controller {

/**
* Constructor.
*
* @since 6.5.0
*/
public function __construct() {
$this->rest_base = 'fonts';
$this->rest_base = 'font-families';
$this->namespace = 'wp/v2';
$this->post_type = 'wp_font_family';
}

/**
Expand All @@ -36,6 +37,19 @@ public function __construct() {
* @since 6.5.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => function () {
return true;},
),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base,
Expand Down Expand Up @@ -67,71 +81,6 @@ public function register_routes() {
),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/collections',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_font_collections' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/collections' . '/(?P<id>[\/\w-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_font_collection' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
),
)
);
}

/**
* Gets a font collection.
*
* @since 6.5.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collection( $request ) {
$id = $request->get_param( 'id' );
$collection = WP_Font_Library::get_font_collection( $id );
// If the collection doesn't exist returns a 404.
if ( is_wp_error( $collection ) ) {
$collection->add_data( array( 'status' => 404 ) );
return $collection;
}
$collection_with_data = $collection->get_data();
// If there was an error getting the collection data, return the error.
if ( is_wp_error( $collection_with_data ) ) {
$collection_with_data->add_data( array( 'status' => 500 ) );
return $collection_with_data;
}
return new WP_REST_Response( $collection_with_data );
}

/**
* Gets the font collections available.
*
* @since 6.5.0
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collections() {
$collections = array();
foreach ( WP_Font_Library::get_font_collections() as $collection ) {
$collections[] = $collection->get_config();
}

return new WP_REST_Response( $collections, 200 );
}

/**
Expand Down
15 changes: 9 additions & 6 deletions lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
function gutenberg_init_font_library_routes() {
// @core-merge: This code will go into Core's `create_initial_post_types()`.
$args = array(
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'label' => 'Font Library',
'show_in_rest' => true,
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'label' => 'Font Family',
'show_in_rest' => true,
'rest_base' => 'font-families',
'rest_controller_class' => 'WP_REST_Font_Families_Controller',
'autosave_rest_controller_class' => 'WP_REST_Autosave_Font_Families_Controller',
);
register_post_type( 'wp_font_family', $args );

// @core-merge: This code will go into Core's `create_initial_rest_routes()`.
$font_library_controller = new WP_REST_Font_Library_Controller();
$font_library_controller->register_routes();
$font_collections_controller = new WP_REST_Font_Collections_Controller();
$font_collections_controller->register_routes();
}

add_action( 'rest_api_init', 'gutenberg_init_font_library_routes' );
Expand Down
4 changes: 3 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/fonts/font-library/class-wp-font-library.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-font-family-utils.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-font-family.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-library-controller.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-families-controller.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-font-collections-controller.php';
require __DIR__ . '/experimental/fonts/font-library/class-wp-rest-autosave-font-families-controller.php';
require __DIR__ . '/experimental/fonts/font-library/font-library.php';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import apiFetch from '@wordpress/api-fetch';

export async function fetchInstallFonts( data ) {
const config = {
path: '/wp/v2/fonts',
path: '/wp/v2/font-families',
method: 'POST',
body: data,
};
Expand All @@ -21,7 +21,7 @@ export async function fetchUninstallFonts( fonts ) {
font_families: fonts,
};
const config = {
path: '/wp/v2/fonts',
path: '/wp/v2/font-families',
method: 'DELETE',
data,
};
Expand All @@ -30,15 +30,15 @@ export async function fetchUninstallFonts( fonts ) {

export async function fetchFontCollections() {
const config = {
path: '/wp/v2/fonts/collections',
path: '/wp/v2/font-collections',
method: 'GET',
};
return apiFetch( config );
}

export async function fetchFontCollection( id ) {
const config = {
path: `/wp/v2/fonts/collections/${ id }`,
path: `/wp/v2/font-collections/${ id }`,
method: 'GET',
};
return apiFetch( config );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Test Case for WP_REST_Font_Library_Controller tests.
* Test Case for WP_REST_Font_Collections_Controller tests.
*
* @package WordPress
* @subpackage Font Library
*/
abstract class WP_REST_Font_Library_Controller_UnitTestCase extends WP_UnitTestCase {
abstract class WP_REST_Font_Collections_Controller_UnitTestCase extends WP_UnitTestCase {

/**
* Fonts directory.
Expand All @@ -18,8 +18,6 @@ abstract class WP_REST_Font_Library_Controller_UnitTestCase extends WP_UnitTestC
public function set_up() {
parent::set_up();

static::$fonts_dir = WP_Font_Library::get_fonts_dir();

// Create a user with administrator role.
$admin_id = $this->factory->user->create(
array(
Expand All @@ -40,10 +38,5 @@ public function tear_down() {
$property = $reflection->getProperty( 'collections' );
$property->setAccessible( true );
$property->setValue( null, array() );

// Clean up the /fonts directory.
foreach ( $this->files_in_dir( static::$fonts_dir ) as $file ) {
@unlink( $file );
}
}
}
Loading
Loading