diff --git a/src/wp-includes/fonts.php b/src/wp-includes/fonts.php index dedf571489dab..4e286b71dc7f2 100644 --- a/src/wp-includes/fonts.php +++ b/src/wp-includes/fonts.php @@ -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 ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php index 91b5f63e964ee..ec59baa5d6c8f 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php @@ -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, '/' ); } diff --git a/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index a4f6d8194902f..f1a53d29effb7 100644 --- a/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -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 */ @@ -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.' ); } }