Make WordPress Core


Ignore:
Timestamp:
03/22/2024 10:59:01 PM (14 months ago)
Author:
peterwilsoncc
Message:

Editor: Prevent font folder naive filtering causing infinite loops.

This modifies the font directory API to more closely reflect the upload directory API to help account for naive filtering when uploading fonts.

This moves the protection of infinite loops to the new function _wp_filter_font_directory() to allow developers extending and maintaining the font library to apply the filter without the need for a closure.

These changes also ensure both the upload_dir and font_dir filter are applied consistently when both creating and deleting fonts faces. Prior to this commit the upload_dir filter was only fired when creating fonts faces via the REST API.

Applying the font directory filter to the upload_dir filter is now done by adding the _wp_filter_font_directory function rather than wp_get_font_dir(). Developers who have previously modified the font upload directory using the font_dir filter will NOT need to upload their code.

Extenders wishing to upload files to the font directory can do so via the code:

<?php
add_filter( 'upload_dir', '_wp_filter_font_directory' );
// Your code to upload or sideload a font file.
remove_filter( 'upload_dir', '_wp_filter_font_directory' );

Introduces:

  • wp_font_dir(): Attempt to create and retrieve the font upload directory. The equivalent to wp_upload_dir().
  • _wp_filter_font_directory(): To run on the upload_dir filter, this sets the default destination of the fonts directory and fires the font_dir filter.

wp_get_font_dir() has been modified to be a lightweight getter for the font directory. It returns the location without attempting to create it. The equivalent to wp_get_upload_dir().

Follow up to [57740].

Props peterwilsoncc, mukesh27, mikachan, costdev, mmaattiiaass, swissspidy, youknowriad, dd32, grantmkin.
Fixes #60652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php

    r57804 r57868  
    857857    protected function handle_font_file_upload( $file ) {
    858858        add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) );
    859 
    860         /*
    861          * Set the upload directory to the fonts directory.
    862          *
    863          * wp_get_font_dir() contains the 'font_dir' hook, whose callbacks are
    864          * likely to call wp_get_upload_dir().
    865          *
    866          * To avoid an infinite loop, don't hook wp_get_font_dir() to 'upload_dir'.
    867          * Instead, just pass its return value to the 'upload_dir' callback.
    868          */
    869         $font_dir       = wp_get_font_dir();
    870         $set_upload_dir = function () use ( $font_dir ) {
    871             return $font_dir;
    872         };
    873         add_filter( 'upload_dir', $set_upload_dir );
     859        // Filter the upload directory to return the fonts directory.
     860        add_filter( 'upload_dir', '_wp_filter_font_directory' );
    874861
    875862        $overrides = array(
     
    888875        $uploaded_file = wp_handle_upload( $file, $overrides );
    889876
    890         remove_filter( 'upload_dir', $set_upload_dir );
     877        remove_filter( 'upload_dir', '_wp_filter_font_directory' );
    891878        remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) );
    892879
Note: See TracChangeset for help on using the changeset viewer.