Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c8072d4
Font Library: add wp_font_face post type and scaffold font face REST …
creativecoder Jan 9, 2024
61da35c
Font Library: create font faces through the REST API (#57702)
creativecoder Jan 11, 2024
42565a3
Refactor Font Family Controller (#57785)
creativecoder Jan 12, 2024
afacdf2
Font Family and Font Face REST API endpoints: better data handling an…
creativecoder Jan 15, 2024
7c82cb9
Font Families REST API endpoint: ensure unique font family slugs (#57…
creativecoder Jan 16, 2024
f84cc6d
Font Library: delete child font faces and font assets when deleting p…
creativecoder Jan 16, 2024
492a3ee
Font Library: refactor client side install functions to work with rev…
jffng Jan 17, 2024
9ebc25f
Cleanup/font library view error handling (#57926)
pbking Jan 17, 2024
92ff955
Fix unique key prop warning when opening modal
mikachan Jan 17, 2024
5b57f3e
Add key props to FontsGrid children
mikachan Jan 17, 2024
8a5efd4
Font Faces endpoint: prevent creating font faces with duplicate setti…
creativecoder Jan 17, 2024
3a739bc
Font Library: Update uninstall/delete on client side (#57932)
mikachan Jan 18, 2024
22253e5
Add slug/id back to FontCollection
mikachan Jan 18, 2024
c646e3d
Change tabsFromCollections inline with Font Collections PR
mikachan Jan 18, 2024
2d3abce
Use child.key for key prop in FontsGrid
mikachan Jan 18, 2024
c6e0fbb
Update packages/edit-site/src/components/global-styles/font-library-m…
mikachan Jan 18, 2024
74be330
Merge branch 'trunk' into try/font-library-refactor
mikachan Jan 18, 2024
a666bb5
Font Library: address JS feedback in #57688 (#57961)
mikachan Jan 18, 2024
32689e1
Font Library REST API endpoints: address initial feedback from featur…
creativecoder Jan 19, 2024
2aa9c64
Font Library: font collection refactor to use the new schema (#57884)
matiasbenedetto Jan 19, 2024
fe2c0ae
Merge branch 'try/font-library-refactor' into font-library/fix-react-…
mikachan Jan 19, 2024
ab889a7
Merge branch 'try/font-library-refactor' into font-library/fix-react-…
mikachan Jan 23, 2024
75c27e5
Remove old WP_REST_Autosave_Fonts_Controller class
mikachan Jan 23, 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
Font Library: delete child font faces and font assets when deleting p…
…arent (#57867)

Co-authored-by: Sarah Norris <[email protected]>
  • Loading branch information
creativecoder and mikachan authored Jan 16, 2024
commit f84cc6d1a0cceeaa6e20929e7060f6e748753eb4
57 changes: 57 additions & 0 deletions lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,60 @@ function wp_get_font_dir( $defaults = array() ) {
return apply_filters( 'font_dir', $defaults );
}
}

// @core-merge: Filters should go in `src/wp-includes/default-filters.php`,
// functions in a general file for font library.
if ( ! function_exists( '_wp_delete_font_family' ) ) {
/**
* Deletes child font faces when a font family is deleted.
*
* @access private
* @since 6.5.0
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @return void
*/
function _wp_delete_font_family( $post_id, $post ) {
if ( 'wp_font_family' !== $post->post_type ) {
return;
}

$font_faces = get_children(
array(
'post_parent' => $post_id,
'post_type' => 'wp_font_face',
)
);

foreach ( $font_faces as $font_face ) {
wp_delete_post( $font_face->ID, true );
}
}
add_action( 'deleted_post', '_wp_delete_font_family', 10, 2 );
}

if ( ! function_exists( '_wp_delete_font_face' ) ) {
/**
* Deletes associated font files when a font face is deleted.
*
* @access private
* @since 6.5.0
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @return void
*/
function _wp_delete_font_face( $post_id, $post ) {
if ( 'wp_font_face' !== $post->post_type ) {
return;
}

$font_files = get_post_meta( $post_id, '_wp_font_face_files', false );

foreach ( $font_files as $font_file ) {
wp_delete_file( wp_get_font_dir()['path'] . '/' . $font_file );
}
}
add_action( 'before_delete_post', '_wp_delete_font_face', 10, 2 );
}
85 changes: 85 additions & 0 deletions phpunit/tests/fonts/font-library/fontLibraryHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Test deleting wp_font_family and wp_font_face post types.
*
* @package WordPress
* @subpackage Font Library
*/

class Tests_Font_Library_Hooks extends WP_UnitTestCase {
public function test_deleting_font_family_deletes_child_font_faces() {
$font_family_id = self::factory()->post->create(
array(
'post_type' => 'wp_font_family',
)
);
$font_face_id = self::factory()->post->create(
array(
'post_type' => 'wp_font_face',
'post_parent' => $font_family_id,
)
);
$other_font_family_id = self::factory()->post->create(
array(
'post_type' => 'wp_font_family',
)
);
$other_font_face_id = self::factory()->post->create(
array(
'post_type' => 'wp_font_face',
'post_parent' => $other_font_family_id,
)
);

wp_delete_post( $font_family_id, true );

$this->assertNull( get_post( $font_face_id ) );
$this->assertNotNull( get_post( $other_font_face_id ) );
}

public function test_deleting_font_faces_deletes_associated_font_files() {
list( $font_face_id, $font_path ) = $this->create_font_face_with_file( 'OpenSans-Regular.woff2' );
list( , $other_font_path ) = $this->create_font_face_with_file( 'OpenSans-Regular.ttf' );

wp_delete_post( $font_face_id, true );

$this->assertFalse( file_exists( $font_path ) );
$this->assertTrue( file_exists( $other_font_path ) );
}

protected function create_font_face_with_file( $filename ) {
$font_face_id = self::factory()->post->create(
array(
'post_type' => 'wp_font_face',
)
);

$font_file = $this->upload_font_file( $filename );

// Make sure the font file uploaded successfully.
$this->assertFalse( $font_file['error'] );

$font_path = $font_file['file'];
$font_filename = basename( $font_path );
add_post_meta( $font_face_id, '_wp_font_face_files', $font_filename );

return array( $font_face_id, $font_path );
}

protected function upload_font_file( $font_filename ) {
// @core-merge Use `DIR_TESTDATA` instead of `GUTENBERG_DIR_TESTDATA`.
$font_file_path = GUTENBERG_DIR_TESTDATA . 'fonts/' . $font_filename;

add_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) );
add_filter( 'upload_dir', 'wp_get_font_dir' );
$font_file = wp_upload_bits(
$font_filename,
null,
file_get_contents( $font_file_path )
);
remove_filter( 'upload_dir', 'wp_get_font_dir' );
remove_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) );

return $font_file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,11 @@ protected function check_font_family_data( $data, $post_id, $links ) {
$this->assertArrayHasKey( 'theme_json_version', $data );
$this->assertSame( WP_Theme_JSON::LATEST_SCHEMA, $data['theme_json_version'] );

$font_face_ids = get_posts(
$font_face_ids = get_children(
array(
'fields' => 'ids',
'post_parent' => $post_id,
'post_type' => 'wp_font_face',
'posts_per_page' => 999,
'fields' => 'ids',
'post_parent' => $post_id,
'post_type' => 'wp_font_face',
)
);
$this->assertArrayHasKey( 'font_faces', $data );
Expand Down