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/tests/phpunit/tests/fonts/font-library/wpFontsDir.php

    r57539 r57879  
    7070        $this->assertSame( static::$dir_defaults, $font_dir, 'The wp_get_font_dir() method should return the default values.' );
    7171    }
     72
     73    /**
     74     * @ticket 60652
     75     */
     76    public function test_fonts_dir_filters_do_not_trigger_infinite_loop() {
     77        /*
     78         * Naive filtering of uploads directory to return font directory.
     79         *
     80         * This emulates the approach a plugin developer may take to
     81         * add the filter when extending the font library functionality.
     82         */
     83        add_filter( 'upload_dir', '_wp_filter_font_directory' );
     84
     85        add_filter(
     86            'upload_dir',
     87            function ( $upload_dir ) {
     88                static $count = 0;
     89                ++$count;
     90                // The filter may be applied a couple of times, at five iterations assume an infinite loop.
     91                if ( $count >= 5 ) {
     92                    $this->fail( 'Filtering the uploads directory triggered an infinite loop.' );
     93                }
     94                return $upload_dir;
     95            },
     96            5
     97        );
     98
     99        /*
     100         * Filter the font directory to return the uploads directory.
     101         *
     102         * This emulates moving font files back to the uploads directory due
     103         * to file system structure.
     104         */
     105        add_filter( 'font_dir', 'wp_get_upload_dir' );
     106
     107        wp_get_upload_dir();
     108
     109        // This will never be hit if an infinite loop is triggered.
     110        $this->assertTrue( true );
     111    }
    72112}
Note: See TracChangeset for help on using the changeset viewer.