Make WordPress Core


Ignore:
Timestamp:
03/26/2024 09:22:48 AM (13 months ago)
Author:
swissspidy
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].

Reviewed by swissspidy.
Merges [57868] to the 6.5 branch.

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

Location:
branches/6.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.5

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

    r57875 r57879  
    9393
    9494/**
     95 * Retrieves font uploads directory information.
     96 *
     97 * Same as wp_font_dir() but "light weight" as it doesn't attempt to create the font uploads directory.
     98 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases
     99 * when not uploading files.
     100 *
     101 * @since 6.5.0
     102 *
     103 * @see wp_font_dir()
     104 *
     105 * @return array See wp_font_dir() for description.
     106 */
     107function wp_get_font_dir() {
     108    return wp_font_dir( false );
     109}
     110
     111/**
    95112 * Returns an array containing the current fonts upload directory's path and URL.
    96113 *
    97114 * @since 6.5.0
    98115 *
    99  * @return array $defaults {
    100  *     Array of information about the upload directory.
     116 * @param bool $create_dir Optional. Whether to check and create the font uploads directory. Default true.
     117 * @return array {
     118 *     Array of information about the font upload directory.
    101119 *
    102120 *     @type string       $path    Base directory and subdirectory or full path to the fonts upload directory.
     
    108126 * }
    109127 */
    110 function wp_get_font_dir() {
     128function wp_font_dir( $create_dir = true ) {
     129    /*
     130     * Allow extenders to manipulate the font directory consistently.
     131     *
     132     * Ensures the upload_dir filter is fired both when calling this function
     133     * directly and when the upload directory is filtered in the Font Face
     134     * REST API endpoint.
     135     */
     136    add_filter( 'upload_dir', '_wp_filter_font_directory' );
     137    $font_dir = wp_upload_dir( null, $create_dir, false );
     138    remove_filter( 'upload_dir', '_wp_filter_font_directory' );
     139    return $font_dir;
     140}
     141
     142/**
     143 * Returns the font directory for use by the font library.
     144 *
     145 * This function is a callback for the {@see 'upload_dir'} filter. It is not
     146 * intended to be called directly. Use wp_get_font_dir() instead.
     147 *
     148 * The function can be used when extending the font library to modify the upload
     149 * destination for font files via the upload_dir filter. The recommended way to
     150 * do this is:
     151 *
     152 * ```php
     153 * add_filter( 'upload_dir', '_wp_filter_font_directory' );
     154 * // Your code to upload or sideload a font file.
     155 * remove_filter( 'upload_dir', '_wp_filter_font_directory' );
     156 * ```
     157 *
     158 * @since 6.5.0
     159 * @access private
     160 *
     161 * @param string $font_dir The font directory.
     162 * @return string The modified font directory.
     163 */
     164function _wp_filter_font_directory( $font_dir ) {
     165    if ( doing_filter( 'font_dir' ) ) {
     166        // Avoid an infinite loop.
     167        return $font_dir;
     168    }
     169
    111170    $site_path = '';
    112171    if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
     
    114173    }
    115174
    116     $defaults = array(
     175    $font_dir = array(
    117176        'path'    => path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path,
    118177        'url'     => untrailingslashit( content_url( 'fonts' ) ) . $site_path,
     
    130189     * @since 6.5.0
    131190     *
    132      * @param array $defaults The original fonts directory data.
     191     * @param array $font_dir {
     192     *     Array of information about the font upload directory.
     193     *
     194     *     @type string       $path    Base directory and subdirectory or full path to the fonts upload directory.
     195     *     @type string       $url     Base URL and subdirectory or absolute URL to the fonts upload directory.
     196     *     @type string       $subdir  Subdirectory
     197     *     @type string       $basedir Path without subdir.
     198     *     @type string       $baseurl URL path without subdir.
     199     *     @type string|false $error   False or error message.
     200     * }
    133201     */
    134     return apply_filters( 'font_dir', $defaults );
     202    return apply_filters( 'font_dir', $font_dir );
    135203}
    136204
Note: See TracChangeset for help on using the changeset viewer.