Make WordPress Core

Changeset 58448 for branches/6.5


Ignore:
Timestamp:
06/20/2024 04:28:55 PM (6 months ago)
Author:
hellofromTonya
Message:

Editor (Font Library): Store font subdirectory in post meta.

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.

Reviewed by hellofromTonya.
Merges [58353] to the 6.5 branch.

Props costdev, grantmkin, peterwilsoncc.
Fixes #61297.

Location:
branches/6.5
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/6.5

  • branches/6.5/src/wp-includes/fonts.php

    r57907 r58448  
    229229
    230230    $font_files = get_post_meta( $post_id, '_wp_font_face_file', false );
    231     $font_dir   = wp_get_font_dir()['path'];
     231    $font_dir   = untrailingslashit( wp_get_font_dir()['basedir'] );
    232232
    233233    foreach ( $font_files as $font_file ) {
  • branches/6.5/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php

    r57879 r58448  
    917917
    918918        $fonts_dir = wp_get_font_dir();
    919         if ( str_starts_with( $new_path, $fonts_dir['path'] ) ) {
    920             $new_path = str_replace( $fonts_dir, '', $new_path );
     919        if ( str_starts_with( $new_path, $fonts_dir['basedir'] ) ) {
     920            $new_path = str_replace( $fonts_dir['basedir'], '', $new_path );
    921921            $new_path = ltrim( $new_path, '/' );
    922922        }
  • branches/6.5/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php

    r57548 r58448  
    395395
    396396        $this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' );
     397    }
     398
     399    /**
     400     * Ensure that setting a subdirectory on font uploads stores and deletes files as expected.
     401     *
     402     * @ticket 61297
     403     *
     404     * @covers WP_REST_Font_Faces_Controller::create_item
     405     */
     406    public function test_create_item_sub_dir() {
     407        wp_set_current_user( self::$admin_id );
     408        add_filter(
     409            'font_dir',
     410            function ( $font_dir ) {
     411                $subdir             = '/subdir';
     412                $font_dir['subdir'] = $subdir;
     413                $font_dir['path']  .= $subdir;
     414                $font_dir['url']   .= $subdir;
     415                return $font_dir;
     416            }
     417        );
     418
     419        $files = $this->setup_font_file_upload( array( 'woff2' ) );
     420
     421        $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces' );
     422        $request->set_param( 'theme_json_version', WP_REST_Font_Faces_Controller::LATEST_THEME_JSON_VERSION_SUPPORTED );
     423        $request->set_param(
     424            'font_face_settings',
     425            wp_json_encode(
     426                array(
     427                    'fontFamily' => '"Open Sans"',
     428                    'fontWeight' => '200',
     429                    'fontStyle'  => 'normal',
     430                    'src'        => array_keys( $files )[0],
     431                )
     432            )
     433        );
     434        $request->set_file_params( $files );
     435
     436        $response = rest_get_server()->dispatch( $request );
     437        $data     = $response->get_data();
     438
     439        $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' );
     440        $this->check_font_face_data( $data, $data['id'], $response->get_links() );
     441        $this->check_file_meta( $data['id'], array( $data['font_face_settings']['src'] ) );
     442
     443        $settings = $data['font_face_settings'];
     444        unset( $settings['src'] );
     445        $this->assertSame(
     446            array(
     447                'fontFamily' => '"Open Sans"',
     448                'fontWeight' => '200',
     449                'fontStyle'  => 'normal',
     450            ),
     451            $settings,
     452            'The font_face_settings data should match the expected data.'
     453        );
     454
     455        $expected_file_path = WP_CONTENT_DIR . '/uploads/fonts/subdir/' . reset( $files )['name'];
     456        $expected_post_meta = 'subdir/' . reset( $files )['name'];
     457        $this->assertFileExists( $expected_file_path, 'The font file should exist in the expected subdirectory.' );
     458        $this->assertSame( $expected_post_meta, get_post_meta( $data['id'], '_wp_font_face_file', true ), 'The post meta should match the expected subdirectory.' );
     459        $this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' );
     460
     461        // Delete the post.
     462        wp_delete_post( $data['id'], true );
     463        $this->assertFileDoesNotExist( $expected_file_path, 'The font file should have been deleted when the post was deleted.' );
    397464    }
    398465
     
    10491116        $file_meta = get_post_meta( $font_face_id, '_wp_font_face_file' );
    10501117
    1051         foreach ( $src_attributes as $src_attribute ) {
    1052             $file_name = basename( $src_attribute );
    1053             $this->assertContains( $file_name, $file_meta, 'The uploaded font file path should be saved in the post meta.' );
     1118        foreach ( $file_meta as $file ) {
     1119            $base_directory = wp_get_font_dir()['basedir'];
     1120            $this->assertStringStartsNotWith( $base_directory, $file, 'The base directory should not be stored in the post meta.' );
    10541121        }
    10551122    }
Note: See TracChangeset for help on using the changeset viewer.