Skip to content

Commit

Permalink
Editor (Font Library): Store font subdirectory in post meta.
Browse files Browse the repository at this point in the history
Stores the font file sub-directory in the `wp_font_face` post meta. Similar to attachments, only the portion of the path relative to the base directory is stored.

This ensures the files can be deleted alongside their post on sites using a plugin to store font files in sub-directories. Previously running such a plugin would result in the files remaining on the file system post delete.

Props costdev, grantmkin, peterwilsoncc.
Fixes #61297.



git-svn-id: https://develop.svn.wordpress.org/trunk@58353 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Jun 5, 2024
1 parent fb2e84f commit a033cf1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/wp-includes/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function _wp_before_delete_font_face( $post_id, $post ) {
}

$font_files = get_post_meta( $post_id, '_wp_font_face_file', false );
$font_dir = wp_get_font_dir()['path'];
$font_dir = untrailingslashit( wp_get_font_dir()['basedir'] );

foreach ( $font_files as $font_file ) {
wp_delete_file( $font_dir . '/' . $font_file );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,8 @@ protected function relative_fonts_path( $path ) {
$new_path = $path;

$fonts_dir = wp_get_font_dir();
if ( str_starts_with( $new_path, $fonts_dir['path'] ) ) {
$new_path = str_replace( $fonts_dir, '', $new_path );
if ( str_starts_with( $new_path, $fonts_dir['basedir'] ) ) {
$new_path = str_replace( $fonts_dir['basedir'], '', $new_path );
$new_path = ltrim( $new_path, '/' );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,73 @@ public function test_create_item() {
$this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' );
}

/**
* Ensure that setting a subdirectory on font uploads stores and deletes files as expected.
*
* @ticket 61297
*
* @covers WP_REST_Font_Faces_Controller::create_item
*/
public function test_create_item_sub_dir() {
wp_set_current_user( self::$admin_id );
add_filter(
'font_dir',
function ( $font_dir ) {
$subdir = '/subdir';
$font_dir['subdir'] = $subdir;
$font_dir['path'] .= $subdir;
$font_dir['url'] .= $subdir;
return $font_dir;
}
);

$files = $this->setup_font_file_upload( array( 'woff2' ) );

$request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces' );
$request->set_param( 'theme_json_version', WP_REST_Font_Faces_Controller::LATEST_THEME_JSON_VERSION_SUPPORTED );
$request->set_param(
'font_face_settings',
wp_json_encode(
array(
'fontFamily' => '"Open Sans"',
'fontWeight' => '200',
'fontStyle' => 'normal',
'src' => array_keys( $files )[0],
)
)
);
$request->set_file_params( $files );

$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 201, $response->get_status(), 'The response status should be 201.' );
$this->check_font_face_data( $data, $data['id'], $response->get_links() );
$this->check_file_meta( $data['id'], array( $data['font_face_settings']['src'] ) );

$settings = $data['font_face_settings'];
unset( $settings['src'] );
$this->assertSame(
array(
'fontFamily' => '"Open Sans"',
'fontWeight' => '200',
'fontStyle' => 'normal',
),
$settings,
'The font_face_settings data should match the expected data.'
);

$expected_file_path = WP_CONTENT_DIR . '/uploads/fonts/subdir/' . reset( $files )['name'];
$expected_post_meta = 'subdir/' . reset( $files )['name'];
$this->assertFileExists( $expected_file_path, 'The font file should exist in the expected subdirectory.' );
$this->assertSame( $expected_post_meta, get_post_meta( $data['id'], '_wp_font_face_file', true ), 'The post meta should match the expected subdirectory.' );
$this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' );

// Delete the post.
wp_delete_post( $data['id'], true );
$this->assertFileDoesNotExist( $expected_file_path, 'The font file should have been deleted when the post was deleted.' );
}

/**
* @covers WP_REST_Font_Faces_Controller::create_item
*/
Expand Down Expand Up @@ -1048,9 +1115,9 @@ protected function check_font_face_data( $data, $post_id, $links ) {
protected function check_file_meta( $font_face_id, $src_attributes ) {
$file_meta = get_post_meta( $font_face_id, '_wp_font_face_file' );

foreach ( $src_attributes as $src_attribute ) {
$file_name = basename( $src_attribute );
$this->assertContains( $file_name, $file_meta, 'The uploaded font file path should be saved in the post meta.' );
foreach ( $file_meta as $file ) {
$base_directory = wp_get_font_dir()['basedir'];
$this->assertStringStartsNotWith( $base_directory, $file, 'The base directory should not be stored in the post meta.' );
}
}

Expand Down

0 comments on commit a033cf1

Please sign in to comment.