Skip to content

Commit

Permalink
Font Library: add wp_get_font_dir() function. (#57730)
Browse files Browse the repository at this point in the history
* adding wp_font_dir() function

* use wp_font_dir() instead of Font Library method

* update php unit teests

* rename function
  • Loading branch information
matiasbenedetto authored Jan 10, 2024
1 parent c527d4d commit d8ef7d6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 88 deletions.
9 changes: 4 additions & 5 deletions lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function uninstall() {
*/
private static function delete_asset( $src ) {
$filename = basename( $src );
$file_path = path_join( WP_Font_Library::get_fonts_dir(), $filename );
$file_path = path_join( wp_get_font_dir()['path'], $filename );

wp_delete_file( $file_path );

Expand Down Expand Up @@ -163,7 +163,6 @@ private static function delete_font_face_assets( $font_face ) {
return true;
}


/**
* Gets the overrides for the 'wp_handle_upload' function.
*
Expand Down Expand Up @@ -394,7 +393,7 @@ private function download_or_move_font_faces( $files ) {

// If the font face requires the use of the filesystem, create the fonts dir if it doesn't exist.
if ( ! empty( $font_face['downloadFromUrl'] ) && ! empty( $font_face['uploadedFile'] ) ) {
wp_mkdir_p( WP_Font_Library::get_fonts_dir() );
wp_mkdir_p( wp_get_font_dir()['path'] );
}

// If installing google fonts, download the font face assets.
Expand Down Expand Up @@ -599,9 +598,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', 'fonts_dir' ) );
add_filter( 'upload_dir', 'wp_get_font_dir' );
$were_assets_written = $this->download_or_move_font_faces( $files );
remove_filter( 'upload_dir', array( 'WP_Font_Library', 'fonts_dir' ) );
remove_filter( 'upload_dir', 'wp_get_font_dir' );
remove_filter( 'upload_mimes', array( 'WP_Font_Library', 'set_allowed_mime_types' ) );

if ( ! $were_assets_written ) {
Expand Down
67 changes: 0 additions & 67 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,74 +140,7 @@ public static function get_font_collection( $id ) {
return new WP_Error( 'font_collection_not_found', 'Font collection not found.' );
}

/**
* Returns an array containing the current fonts upload directory's path and URL.
*
* @since 6.5.0
*
* @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 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 );
}

/**
* Gets the Site dir for fonts, using the blog ID if multi-site, empty otherwise.
*
* @since 6.5.0
*
* @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.
*
* @since 6.5.0
*
* @return string Path of the upload directory for fonts.
*/
public static function get_fonts_dir() {
$fonts_dir_settings = self::fonts_dir();
return $fonts_dir_settings['path'];
}

/**
* Sets the allowed mime types for fonts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function update_font_library_permissions_check() {
* @return bool Whether the font directory exists.
*/
private function has_upload_directory() {
$upload_dir = WP_Font_Library::get_fonts_dir();
$upload_dir = wp_get_font_dir()['path'];
return is_dir( $upload_dir );
}

Expand All @@ -290,7 +290,7 @@ private function has_upload_directory() {
private function has_write_permission() {
// The update endpoints requires write access to the temp and the fonts directories.
$temp_dir = get_temp_dir();
$upload_dir = WP_Font_Library::get_fonts_dir();
$upload_dir = wp_get_font_dir()['path'];
if ( ! is_writable( $temp_dir ) || ! wp_is_writable( $upload_dir ) ) {
return false;
}
Expand Down Expand Up @@ -353,7 +353,7 @@ public function install_fonts( $request ) {
}

if ( $this->needs_write_permission( $font_family_settings ) ) {
$upload_dir = WP_Font_Library::get_fonts_dir();
$upload_dir = wp_get_font_dir()['path'];
if ( ! $this->has_upload_directory() ) {
if ( ! wp_mkdir_p( $upload_dir ) ) {
$errors[] = new WP_Error(
Expand Down
49 changes: 49 additions & 0 deletions lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,52 @@ function wp_unregister_font_collection( $collection_id ) {
);

wp_register_font_collection( $default_font_collection );

// @core-merge: This code should probably go into Core's src/wp-includes/functions.php.
if ( ! function_exists( 'wp_get_font_dir' ) ) {
/**
* Returns an array containing the current fonts upload directory's path and URL.
*
* @since 6.5.0
*
* @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.
* }
*/
function wp_get_font_dir( $defaults = array() ) {
// Multi site path
$site_path = '';
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$site_path = '/sites/' . get_current_blog_id();
}

// 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( 'font_dir', $defaults );
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
/**
* Test WP_Font_Library::fonts_dir().
* Test wp_get_font_dir().
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*
* @covers WP_Font_Library::fonts_dir
* @covers wp_get_font_dir
*/
class Tests_Fonts_WpFontLibrary_FontsDir extends WP_Font_Library_UnitTestCase {
class Tests_Fonts_WpFontDir extends WP_UnitTestCase {
private $dir_defaults;

public function __construct() {
Expand All @@ -26,8 +26,8 @@ public function __construct() {
}

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

public function test_fonts_dir_with_filter() {
Expand All @@ -43,10 +43,10 @@ function set_new_values( $defaults ) {
}

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

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

$expected = array(
'path' => '/custom-path/fonts/my-custom-subdir',
Expand All @@ -57,14 +57,14 @@ function set_new_values( $defaults ) {
'error' => false,
);

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

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

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

$this->assertEquals( $fonts_dir, $this->dir_defaults, 'The fonts_dir() method should return the default values.' );
$this->assertEquals( $font_dir, $this->dir_defaults, 'The wp_get_font_dir() method should return the default values.' );
}
}
2 changes: 1 addition & 1 deletion phpunit/tests/fonts/font-library/wpFontFamily/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class WP_Font_Family_UnitTestCase extends WP_UnitTestCase {
public static function set_up_before_class() {
parent::set_up_before_class();

static::$fonts_dir = WP_Font_Library::get_fonts_dir();
static::$fonts_dir = wp_get_font_dir()['path'];
wp_mkdir_p( static::$fonts_dir );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class WP_REST_Font_Families_Controller_UnitTestCase extends WP_UnitTest
public function set_up() {
parent::set_up();

static::$fonts_dir = WP_Font_Library::get_fonts_dir();
static::$fonts_dir = wp_get_font_dir()['path'];

// Create a user with administrator role.
$admin_id = $this->factory->user->create(
Expand Down

0 comments on commit d8ef7d6

Please sign in to comment.