From c31497ab3c311226737a69fce2108069fcfcf92f Mon Sep 17 00:00:00 2001 From: madhusudhand Date: Thu, 14 Dec 2023 13:17:14 +0530 Subject: [PATCH 1/3] add global configuration variables for font directory --- .../fonts/font-library/class-wp-font-library.php | 13 ++++--------- .../font-library/wpFontLibrary/setUploadDir.php | 14 ++++---------- 2 files changed, 8 insertions(+), 19 deletions(-) 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 9320a554e510c..47ec854a965fc 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -106,7 +106,7 @@ public static function get_font_collection( $id ) { * @return string Path of the upload directory for fonts. */ public static function get_fonts_dir() { - return path_join( WP_CONTENT_DIR, 'fonts' ); + return ( defined( 'WP_FONT_DIR' ) && ! empty( WP_FONT_DIR ) ) ? WP_FONT_DIR : path_join( WP_CONTENT_DIR, 'fonts' ); } /** @@ -119,19 +119,14 @@ public static function get_fonts_dir() { * * @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. */ 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'; + $font_url = ( defined( 'WP_FONT_URL' ) && ! empty( WP_FONT_URL ) ) ? set_url_scheme( WP_FONT_URL ) : content_url( 'fonts' ); + $defaults['path'] = self::get_fonts_dir(); + $defaults['url'] = untrailingslashit( $font_url ); return $defaults; } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/setUploadDir.php b/phpunit/tests/fonts/font-library/wpFontLibrary/setUploadDir.php index daa4c84aad900..ea99a66af1f19 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/setUploadDir.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/setUploadDir.php @@ -14,18 +14,12 @@ class Tests_Fonts_WpFontLibrary_SetUploadDir extends WP_UnitTestCase { public function test_should_set_fonts_upload_dir() { $defaults = array( - 'subdir' => '/abc', - 'basedir' => '/any/path', - 'baseurl' => 'http://example.com/an/arbitrary/url', - 'path' => '/any/path/abc', - 'url' => 'http://example.com/an/arbitrary/url/abc', + 'path' => '/any/path/abc', + 'url' => 'http://example.com/an/arbitrary/url/abc', ); $expected = array( - 'subdir' => '/fonts', - 'basedir' => WP_CONTENT_DIR, - 'baseurl' => content_url(), - 'path' => path_join( WP_CONTENT_DIR, 'fonts' ), - 'url' => content_url() . '/fonts', + 'path' => path_join( WP_CONTENT_DIR, 'fonts' ), + 'url' => content_url( 'fonts' ), ); $this->assertSame( $expected, WP_Font_Library::set_upload_dir( $defaults ) ); } From d6f0a0e201905f878faee1be2db3e543f583b893 Mon Sep 17 00:00:00 2001 From: madhusudhand Date: Mon, 8 Jan 2024 18:02:54 +0530 Subject: [PATCH 2/3] add multi-site based directory path for fonts --- .../font-library/class-wp-font-library.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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 47ec854a965fc..37b1ff5c6e7f0 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -98,6 +98,14 @@ public static function get_font_collection( $id ) { return new WP_Error( 'font_collection_not_found', 'Font collection not found.' ); } + private static function get_multi_site_font_sub_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. * @@ -106,7 +114,9 @@ public static function get_font_collection( $id ) { * @return string Path of the upload directory for fonts. */ public static function get_fonts_dir() { - return ( defined( 'WP_FONT_DIR' ) && ! empty( WP_FONT_DIR ) ) ? WP_FONT_DIR : path_join( WP_CONTENT_DIR, 'fonts' ); + $base_font_dir = ( defined( 'WP_FONT_DIR' ) && ! empty( WP_FONT_DIR ) ) ? WP_FONT_DIR : path_join( WP_CONTENT_DIR, 'fonts' ); + $font_sub_dir = self::get_multi_site_font_sub_dir(); + return rtrim( $base_font_dir, '/' ) . $font_sub_dir; } /** @@ -123,10 +133,11 @@ public static function get_fonts_dir() { * @return array Modified upload directory. */ public static function set_upload_dir( $defaults ) { - $font_url = ( defined( 'WP_FONT_URL' ) && ! empty( WP_FONT_URL ) ) ? set_url_scheme( WP_FONT_URL ) : content_url( 'fonts' ); + $font_url = ( defined( 'WP_FONT_URL' ) && ! empty( WP_FONT_URL ) ) ? set_url_scheme( WP_FONT_URL ) : content_url( 'fonts' ); + $font_sub_dir = self::get_multi_site_font_sub_dir(); $defaults['path'] = self::get_fonts_dir(); - $defaults['url'] = untrailingslashit( $font_url ); + $defaults['url'] = untrailingslashit( $font_url ) . $font_sub_dir; return $defaults; } From b48887a86d5153c5f0e5e6f1d4620d053498e90d Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 9 Jan 2024 12:39:37 -0300 Subject: [PATCH 3/3] add docblock for get_multi_site_font_sub_dir --- .../fonts/font-library/class-wp-font-library.php | 7 +++++++ 1 file changed, 7 insertions(+) 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 4d6ee6c6b5ffd..bb747830f57d5 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -140,6 +140,13 @@ public static function get_font_collection( $id ) { return new WP_Error( 'font_collection_not_found', 'Font collection not found.' ); } + /** + * Gets a multi site sub-dir. + * + * @since 6.5.0 + * + * @return string multi site sub-dir path. + */ private static function get_multi_site_font_sub_dir() { $font_sub_dir = ''; if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {