Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d343f65
WIP: Refactor Font API font family GET POST testing round 1
pbking Dec 12, 2023
4b342c5
Added GET font family collection tests and implementation
pbking Dec 12, 2023
3343b98
add font family IDs to API responses
pbking Dec 12, 2023
3374120
Move data into 'data' object
pbking Dec 12, 2023
8a4c798
Added delete API implementation and tests
pbking Dec 13, 2023
140faf0
Adding 'force' to font family delete api
pbking Dec 13, 2023
7d26ae9
Got basic font family updating service working
pbking Dec 13, 2023
f71111c
Moved implementation of adding font faces to use font family put inst…
pbking Dec 13, 2023
2ed64b2
Removed route tests (testing the routes seems sufficient and they are…
pbking Dec 13, 2023
88d70ba
Renamed controller to 'Font Family Controller'
pbking Dec 13, 2023
dc9fb0b
Renamed controller to 'Font Family Controller' (continued)
pbking Dec 13, 2023
95e9ce5
Refactored font collection API into its own controller
pbking Dec 13, 2023
9ef8086
Simplifying font family update logic (WIP)
pbking Dec 13, 2023
f28f616
Added better error handling for Font Family creation and updating. A…
pbking Dec 14, 2023
47eb63a
Adding tests for font families with downloaded font face assets
pbking Dec 14, 2023
759a08b
Added upload font face asset tests. Cleaned up update logic.
pbking Dec 14, 2023
efb51e8
Refactored existing tests into individual files and cleaned them up
pbking Dec 15, 2023
592faac
Refactored existing tests into individual files and cleaned them up
pbking Dec 15, 2023
8aade9f
Added PATCH operation and remove file assets for font faces that were…
pbking Dec 15, 2023
aa15a94
Added ability to update font family via POST
pbking Dec 15, 2023
53e9001
Removed unused functions, moved 'create font folder' logic to class, …
pbking Dec 15, 2023
edefab4
Refactored font face asset handling to ONLY accept UPLOADED and HOSTE…
pbking Jan 10, 2024
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
Prev Previous commit
Next Next commit
Got basic font family updating service working
  • Loading branch information
pbking committed Dec 13, 2023
commit 7d26ae96c70433c4b7b08578734e79a2848b2fd3
11 changes: 11 additions & 0 deletions lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public function get_data() {
return $this->data;
}

public function update( $data ) {
//TODO: Sanitize incoming data
$this->data = array_merge( $this->data, $data);
return $this->get_data();
}

/**
* Gets the font family data.
*
Expand All @@ -129,6 +135,11 @@ public function persist() {
$this->id = $post_id;
return $this->get_data();
}

public function add_font_face( $font_face ) {
$this->data['fontFace'][] = $font_face;
}

/**
* Checks whether the font family has font faces defined.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ public function register_routes() {
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'),
'permission_callback' => array($this, 'update_font_library_permissions_check'),
'args' => array(
'data' => array(
'required' => true,
'type' => 'object',
'properties' => array(
'name' => array(
'type' => 'string',
),
'slug' => array(
'type' => 'string',
),
'fontFamily' => array(
'type' => 'string',
),
),

)
),
),
// 'schema' => array( $this, 'get_items_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . 'all-' . $this->rest_base,
Expand All @@ -107,7 +137,38 @@ public function register_routes() {
),
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/font-faces',
array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'install_font_face'),
'permission_callback' => array($this, 'update_font_library_permissions_check'),
'args' => array(
'data' => array(
'required' => true,
'type' => 'object',
'properties' => array(
'fontWeight' => array(
'required' => true,
'type' => 'string',
),
'fontStyle' => array(
'required' => true,
'type' => 'string',
),
'fontFamily' => array(
'required' => true,
'type' => 'string',
),
),

)
),
),
)
);



Expand Down Expand Up @@ -246,24 +307,88 @@ public function delete_item( $request ) {

$font_family = WP_Font_Family::get_font_family_by_id( $id );

if($font_family) {
$font_family->uninstall( $force );
return new WP_REST_Response( array(
'deleted' => $force,
'previous' => array(
'id' => $font_family->id,
'data' => $font_family->get_data(),
)
) );
if( ! $font_family) {
return new WP_Error(
'rest_font_family_not_found',
__( 'Font Family not found.', 'gutenberg' ),
array( 'status' => 404 )
);
}

return new WP_Error(
'rest_font_family_not_found',
__( 'Font Family not found.', 'gutenberg' ),
array( 'status' => 404 )
);
$font_family->uninstall( $force );

return new WP_REST_Response( array(
'deleted' => $force,
'previous' => array(
'id' => $font_family->id,
'data' => $font_family->get_data(),
)
) );

}

public function update_item( $request ) {

$id = $request->get_param( 'id' );
$font_family_data = $request->get_param( 'data' );
$font_family = WP_Font_Family::get_font_family_by_id( $id );

if( ! $font_family) {
return new WP_Error(
'rest_font_family_not_found',
__( 'Font Family not found.', 'gutenberg' ),
array( 'status' => 404 )
);
}

$font_family->update( $font_family_data );
$font_family->persist();

return new WP_REST_Response( array(
'id' => $font_family->id,
'data' => $font_family->get_data(),
) );
}

public function install_font_face ( $request ) {

$id = $request->get_param( 'id' );
$font_face_data = $request->get_param( 'data' );

$font_family = WP_Font_Family::get_font_family_by_id( $id );

if( ! $font_family) {
return new WP_Error(
'rest_font_family_not_found',
__( 'Font Family not found.', 'gutenberg' ),
array( 'status' => 404 )
);
}

$font_family->add_font_face( $font_face_data );
$font_family->persist();

return new WP_REST_Response( array(
'id' => $font_family->id,
'data' => $font_family->get_data(),
) );
}
















/**
* Gets a font collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,43 @@ public function data_create_font_family() {
);
}

public function test_update_font_family() {

$create_request = new WP_REST_Request( 'POST', '/wp/v2/font-families' );
$create_request->set_param( 'data', array(
'fontFamily' => 'Piazzolla',
'slug' => 'piazzolla',
'name' => 'Piazzolla',
));
$create_response = rest_get_server()->dispatch( $create_request );
$create_data = $create_response->get_data();
$installed_font_id = $create_data['id'];

$update_request = new WP_REST_Request( 'PUT', '/wp/v2/font-families/' . $installed_font_id );
$update_request->set_param( 'data', array(
'fontFamily' => 'Piazzolla, serif',
));
$update_response = rest_get_server()->dispatch( $update_request );
$update_data = $update_response->get_data();

$this->assertSame( 200, $update_response->get_status(), 'The response status is not 200.' );
$this->assertSame( 'piazzolla', $update_data['data']['slug'], 'The slug response did not match expected.' );
$this->assertSame( 'Piazzolla', $update_data['data']['name'], 'The name response did not match expected.' );
$this->assertSame( 'Piazzolla, serif', $update_data['data']['fontFamily'], 'The font_family response did not match expected.' );
$this->assertSame( $installed_font_id, $update_data['id'], 'The id response did not match expected.' );

$verify_request = new WP_REST_Request( 'GET', '/wp/v2/font-families/' . $installed_font_id );
$verify_response = rest_get_server()->dispatch( $verify_request );
$verify_data = $verify_response->get_data();

$this->assertSame( 200, $verify_response->get_status(), 'The response status is not 200.' );
$this->assertSame( 'piazzolla', $verify_data['data']['slug'], 'The slug response did not match expected.' );
$this->assertSame( 'Piazzolla', $verify_data['data']['name'], 'The name response did not match expected.' );
$this->assertSame( 'Piazzolla, serif', $verify_data['data']['fontFamily'], 'The font_family response did not match expected.' );
$this->assertSame( $installed_font_id, $verify_data['id'], 'The id response did not match expected.' );

}

/**
* Test getting a collection of all created font families
*/
Expand Down Expand Up @@ -269,5 +306,43 @@ public function test_get_font_families() {
}
}

public function test_add_font_face() {

$create_request = new WP_REST_Request( 'POST', '/wp/v2/font-families' );
$create_request->set_param( 'data', array(
'fontFamily' => 'Piazzolla',
'slug' => 'piazzolla',
'name' => 'Piazzolla',
));
$create_response = rest_get_server()->dispatch( $create_request );
$create_data = $create_response->get_data();
$installed_font_id = $create_data['id'];

$add_font_face_request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . $installed_font_id . '/font-faces' );
$add_font_face_request->set_param( 'data', array(
'fontFamily' => 'Piazzolla',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => 'http://fonts.gstatic.com/s/piazzolla/v33/N0b72SlTPu5rIkWIZjVgI-TckS03oGpPETyEJ88Rbvi0_TzOzKcQhZqx3gX9BRy5m5M.ttf',
));
$add_font_face_response = rest_get_server()->dispatch( $add_font_face_request );
$add_font_face_data = $add_font_face_response->get_data();

$this->assertSame( 200, $add_font_face_response->get_status(), 'The response status is not 200.' );
$this->assertSame( 'Piazzolla', $add_font_face_data['data']['fontFace'][0]['fontFamily'], 'The font_family response did not match expected.' );
$this->assertSame( 'normal', $add_font_face_data['data']['fontFace'][0]['fontStyle'], 'The font_family response did not match expected.' );
$this->assertSame( '400', $add_font_face_data['data']['fontFace'][0]['fontWeight'], 'The font_family response did not match expected.' );
$this->assertSame( 'http://fonts.gstatic.com/s/piazzolla/v33/N0b72SlTPu5rIkWIZjVgI-TckS03oGpPETyEJ88Rbvi0_TzOzKcQhZqx3gX9BRy5m5M.ttf', $add_font_face_data['data']['fontFace'][0]['src'], 'The font_family response did not match expected.' );

$verify_request = new WP_REST_Request( 'GET', '/wp/v2/font-families/' . $installed_font_id );
$verify_response = rest_get_server()->dispatch( $verify_request );
$verify_data = $verify_response->get_data();

$this->assertSame( 200, $verify_response->get_status(), 'The response status is not 200.' );
$this->assertSame( 'Piazzolla', $verify_data['data']['fontFace'][0]['fontFamily'], 'The font_family response did not match expected.' );
$this->assertSame( 'normal', $verify_data['data']['fontFace'][0]['fontStyle'], 'The font_family response did not match expected.' );
$this->assertSame( '400', $verify_data['data']['fontFace'][0]['fontWeight'], 'The font_family response did not match expected.' );
$this->assertSame( 'http://fonts.gstatic.com/s/piazzolla/v33/N0b72SlTPu5rIkWIZjVgI-TckS03oGpPETyEJ88Rbvi0_TzOzKcQhZqx3gX9BRy5m5M.ttf', $verify_data['data']['fontFace'][0]['src'], 'The font_family response did not match expected.' );

}
}