Skip to content

Commit

Permalink
Font Library: filter fonts upload directory (WordPress#57697)
Browse files Browse the repository at this point in the history
* add global configuration variables for font directory

* add multi-site based directory path for fonts

* add docblock for get_multi_site_font_sub_dir

* Rename function for accuracy.

* Use filter instead of constant to determine where fonts are uploaded.

* Format php.

* simplify the filters code  and output the same array as upload_filter does

* remove tests no longer used

* add a test case for the filter

* rename function. Replaces the misleading 'subdir' term by 'dir'.

---------

Co-authored-by: madhusudhand <[email protected]>
Co-authored-by: Matias Benedetto <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2024
1 parent e93f250 commit 4b1beb4
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 74 deletions.
4 changes: 2 additions & 2 deletions lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ private function create_or_update_font_post() {
*/
public function install( $files = null ) {
add_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) );
add_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) );
add_filter( 'upload_dir', array( 'WP_Font_Library', 'fonts_dir' ) );
$were_assets_written = $this->download_or_move_font_faces( $files );
remove_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) );
remove_filter( 'upload_dir', array( 'WP_Font_Library', 'fonts_dir' ) );
remove_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) );

if ( ! $were_assets_written ) {
Expand Down
76 changes: 54 additions & 22 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,40 +141,72 @@ public static function get_font_collection( $id ) {
}

/**
* Gets the upload directory for fonts.
* Returns an array containing the current fonts upload directory's path and URL.
*
* @since 6.5.0
*
* @return string Path of the upload directory for fonts.
* @param array $defaults {
* Array of information about the 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.
* @type string $subdir Subdirectory
* @type string $basedir Path without subdir.
* @type string $baseurl URL path without subdir.
* @type string|false $error False or error message.
* }
*
* @return array $defaults {
* Array of information about the 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.
* @type string $subdir Subdirectory
* @type string $basedir Path without subdir.
* @type string $baseurl URL path without subdir.
* @type string|false $error False or error message.
* }
*/
public static function get_fonts_dir() {
return path_join( WP_CONTENT_DIR, 'fonts' );
public static function fonts_dir( $defaults = array() ) {
$site_path = self::get_multi_site_dir();

// Sets the defaults.
$defaults['path'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
$defaults['url'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
$defaults['subdir'] = '';
$defaults['basedir'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
$defaults['baseurl'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
$defaults['error'] = false;

// Filters the fonts directory data.
return apply_filters( 'fonts_dir', $defaults );
}

/**
* Sets the upload directory for fonts.
* Gets the Site dir for fonts, using the blog ID if multi-site, empty otherwise.
*
* @since 6.5.0
*
* @param array $defaults {
* Default upload directory.
* @return string Site dir path.
*/
private static function get_multi_site_dir() {
$font_sub_dir = '';
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$font_sub_dir = '/sites/' . get_current_blog_id();
}
return $font_sub_dir;
}

/**
* Gets the upload directory for fonts.
*
* @type string $path Path to the directory.
* @type string $url URL for the directory.
* @type string $subdir Sub-directory of the directory.
* @type string $basedir Base directory.
* @type string $baseurl Base URL.
* }
* @return array Modified upload directory.
* @since 6.5.0
*
* @return string Path of the upload directory for fonts.
*/
public static function set_upload_dir( $defaults ) {
$defaults['basedir'] = WP_CONTENT_DIR;
$defaults['baseurl'] = content_url();
$defaults['subdir'] = '/fonts';
$defaults['path'] = self::get_fonts_dir();
$defaults['url'] = $defaults['baseurl'] . '/fonts';

return $defaults;
public static function get_fonts_dir() {
$fonts_dir_settings = self::fonts_dir();
return $fonts_dir_settings['path'];
}

/**
Expand Down
70 changes: 70 additions & 0 deletions phpunit/tests/fonts/font-library/wpFontLibrary/fontsDir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Test WP_Font_Library::fonts_dir().
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*
* @covers WP_Font_Library::fonts_dir
*/
class Tests_Fonts_WpFontLibrary_FontsDir extends WP_Font_Library_UnitTestCase {
private $dir_defaults;

public function __construct() {
parent::__construct();
$this->dir_defaults = array(
'path' => path_join( WP_CONTENT_DIR, 'fonts' ),
'url' => content_url( 'fonts' ),
'subdir' => '',
'basedir' => path_join( WP_CONTENT_DIR, 'fonts' ),
'baseurl' => content_url( 'fonts' ),
'error' => false,
);
}

public function test_fonts_dir() {
$fonts_dir = WP_Font_Library::fonts_dir();
$this->assertEquals( $fonts_dir, $this->dir_defaults );
}

public function test_fonts_dir_with_filter() {
// Define a callback function to pass to the filter.
function set_new_values( $defaults ) {
$defaults['path'] = '/custom-path/fonts/my-custom-subdir';
$defaults['url'] = 'http://example.com/custom-path/fonts/my-custom-subdir';
$defaults['subdir'] = 'my-custom-subdir';
$defaults['basedir'] = '/custom-path/fonts';
$defaults['baseurl'] = 'http://example.com/custom-path/fonts';
$defaults['error'] = false;
return $defaults;
}

// Add the filter.
add_filter( 'fonts_dir', 'set_new_values' );

// Gets the fonts dir.
$fonts_dir = WP_Font_Library::fonts_dir();

$expected = array(
'path' => '/custom-path/fonts/my-custom-subdir',
'url' => 'http://example.com/custom-path/fonts/my-custom-subdir',
'subdir' => 'my-custom-subdir',
'basedir' => '/custom-path/fonts',
'baseurl' => 'http://example.com/custom-path/fonts',
'error' => false,
);

$this->assertEquals( $fonts_dir, $expected, 'The fonts_dir() method should return the expected values.' );

// Remove the filter.
remove_filter( 'fonts_dir', 'set_new_values' );

// Gets the fonts dir.
$fonts_dir = WP_Font_Library::fonts_dir();

$this->assertEquals( $fonts_dir, $this->dir_defaults, 'The fonts_dir() method should return the default values.' );
}
}
18 changes: 0 additions & 18 deletions phpunit/tests/fonts/font-library/wpFontLibrary/getFontsDir.php

This file was deleted.

32 changes: 0 additions & 32 deletions phpunit/tests/fonts/font-library/wpFontLibrary/setUploadDir.php

This file was deleted.

0 comments on commit 4b1beb4

Please sign in to comment.