diff --git a/lib/experimental/fonts/font-library/class-wp-font-family.php b/lib/experimental/fonts/font-library/class-wp-font-family.php index e47cf0afdac1de..f64aebc0c8efa7 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-family.php +++ b/lib/experimental/fonts/font-library/class-wp-font-family.php @@ -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 ); @@ -163,7 +163,6 @@ private static function delete_font_face_assets( $font_face ) { return true; } - /** * Gets the overrides for the 'wp_handle_upload' function. * @@ -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. @@ -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 ) { diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index 99de81e0bd74a3..01ee702673e57b 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -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. diff --git a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php index 0147d80b7bde94..ede8762c88c6dc 100644 --- a/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php +++ b/lib/experimental/fonts/font-library/class-wp-rest-font-families-controller.php @@ -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 ); } @@ -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; } @@ -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( diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index 711a6bb40c282b..4a2f1be69ef719 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -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 ); + } +} diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/fontsDir.php b/phpunit/tests/fonts/font-library/fontsDir.php similarity index 67% rename from phpunit/tests/fonts/font-library/wpFontLibrary/fontsDir.php rename to phpunit/tests/fonts/font-library/fontsDir.php index 9926bb74090888..5c13f1d120f9a5 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/fontsDir.php +++ b/phpunit/tests/fonts/font-library/fontsDir.php @@ -1,6 +1,6 @@ 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() { @@ -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', @@ -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.' ); } } diff --git a/phpunit/tests/fonts/font-library/wpFontFamily/base.php b/phpunit/tests/fonts/font-library/wpFontFamily/base.php index 3650ac7dab9972..3f6ff153fa12f5 100644 --- a/phpunit/tests/fonts/font-library/wpFontFamily/base.php +++ b/phpunit/tests/fonts/font-library/wpFontFamily/base.php @@ -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 ); } diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController/base.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController/base.php index 5ab71a4379851f..e2d190cd76af1f 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController/base.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController/base.php @@ -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(