Skip to content

Commit

Permalink
Prevent infinite loop filtering fonts directory. Props @costdev.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Mar 3, 2024
1 parent 07790fe commit 97744d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@
add_action( 'deleted_post', '_wp_after_delete_font_family', 10, 2 );
add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 );
add_action( 'init', '_wp_register_default_font_collections' );
add_filter( 'font_dir', 'wp_filter_font_dir' );

// It might be nice to use a filter instead of an action, but the `WP_REST_Templates_Controller` doesn't
// provide one (unlike e.g. `WP_REST_Posts_Controller`, which has `rest_pre_insert_{$this->post_type}`).
Expand Down
57 changes: 43 additions & 14 deletions src/wp-includes/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ function wp_unregister_font_collection( string $slug ) {
*
* @since 6.5.0
*
* @return array $defaults {
* Array of information about the upload directory.
* @param array|null $font_dir Optional. Array of unfiltered uploads directory.
* This is used when the function is called from
* the upload_dir filter. Default null.
* @return array {
* Array of information about the font upload directory.
*
* @type string $path Base directory and subdirectory or full path to the fonts upload directory.
* @type string $url Base URL and subdirectory or absolute URL to the fonts upload directory.
Expand All @@ -107,13 +110,48 @@ function wp_unregister_font_collection( string $slug ) {
* @type string|false $error False or error message.
* }
*/
function wp_get_font_dir() {
function wp_get_font_dir( $font_dir = null ) {
if ( doing_filter( 'font_dir' ) ) {
/*
* The font_dir filter is being run, avoid an infinite loop.
*
* This indicates that a plugin is calling wp_upload_dir() while filtering
* the font directory and avoids an infinite loop.
*/
return $font_dir;
}

/**
* Filters the fonts directory data.
*
* This filter allows developers to modify the fonts directory data.
*
* @since 6.5.0
*
* @param array $font_dir The original fonts directory data.
*/
return apply_filters( 'font_dir', $font_dir );
}

/**
* Filters the fonts directory data.
*
* Modifies the default fonts directory data, runs on @see 'font_dir' filter.
*
* @since 6.5.0
* @access private
* @see wp_get_font_dir()
*
* @param array $font_dir The original fonts directory data.
* @return array The filtered fonts directory data.
*/
function wp_filter_font_dir( $font_dir ) {
$site_path = '';
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$site_path = '/sites/' . get_current_blog_id();
}

$defaults = array(
$font_dir = array(
'path' => path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path,
'url' => untrailingslashit( content_url( 'fonts' ) ) . $site_path,
'subdir' => '',
Expand All @@ -122,16 +160,7 @@ function wp_get_font_dir() {
'error' => false,
);

/**
* Filters the fonts directory data.
*
* This filter allows developers to modify the fonts directory data.
*
* @since 6.5.0
*
* @param array $defaults The original fonts directory data.
*/
return apply_filters( 'font_dir', $defaults );
return $font_dir;
}

/**
Expand Down

0 comments on commit 97744d7

Please sign in to comment.