-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: delete child font faces and font assets when deleting parent #57867
Merged
creativecoder
merged 2 commits into
try/font-library-refactor
from
update/delete-child-font-faces-assets
Jan 16, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
delete
rather thandeleted
too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hook is intentionally
deleted_post
, which happens after the font family has been deleted, rather than before. That way we're waiting to make sure it gets deleted, first, before deleting child font faces.With the font face, we need to run the hook earlier, because the post meta needs to still exist to find the files associated with the font face.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that makes sense 👍