Skip to content

Commit

Permalink
Font Library: changing the upload directory to wp-content/fonts (#54122)
Browse files Browse the repository at this point in the history
* Try just changing the upload dir.

* Update static::$fonts-dir

* Fix upload dir test.

* $font_dir is filled using the font library method instead of harcoding the path

* fixes the paths to avoid errors for the missing trailing slash in $fonts_dir path

---------

Co-authored-by: Matias Benedetto <matias.benedetto@gmail.com>
  • Loading branch information
jffng and matiasbenedetto authored Sep 1, 2023
1 parent 961052e commit ac32ed5
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
10 changes: 6 additions & 4 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function get_font_collection( $id ) {
* @return string Path of the upload directory for fonts.
*/
public static function get_fonts_dir() {
return wp_upload_dir()['basedir'] . '/fonts';
return path_join( WP_CONTENT_DIR, 'fonts' );
}

/**
Expand All @@ -110,9 +110,11 @@ public static function get_fonts_dir() {
* @return array Modified upload directory.
*/
public static function set_upload_dir( $defaults ) {
$defaults['subdir'] = '/fonts';
$defaults['path'] = $defaults['basedir'] . $defaults['subdir'];
$defaults['url'] = $defaults['baseurl'] . $defaults['subdir'];
$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;
}
Expand Down
5 changes: 2 additions & 3 deletions phpunit/tests/fonts/font-library/wpFontFamily/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ abstract class WP_Font_Family_UnitTestCase extends WP_UnitTestCase {
public static function set_up_before_class() {
parent::set_up_before_class();

$uploads_dir = wp_upload_dir();
static::$fonts_dir = $uploads_dir['basedir'] . '/fonts/';
static::$fonts_dir = WP_Font_Library::get_fonts_dir();
wp_mkdir_p( static::$fonts_dir );
}

Expand Down Expand Up @@ -61,7 +60,7 @@ public function set_up() {
'size' => 123,
),
),
'font_filename' => static::$fonts_dir . 'merriweather_normal_400.ttf',
'font_filename' => path_join( static::$fonts_dir, 'merriweather_normal_400.ttf' ),
);
}

Expand Down
12 changes: 6 additions & 6 deletions phpunit/tests/fonts/font-library/wpFontFamily/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ public function data_should_not_download_when_no_fontface() {
public function test_should_download_fontfaces_and_create_post( $font_data, array $expected ) {
// Pre-checks to ensure starting conditions.
foreach ( $expected as $font_file ) {
$font_file = static::$fonts_dir . $font_file;
$this->assertFileDoesNotExist( $font_file, "Font file [{$font_file}] should not exist in the uploads/fonts/ directory after installing" );
$font_file = path_join( static::$fonts_dir, $font_file );
$this->assertFileDoesNotExist( $font_file, "Font file [{$font_file}] should not exist in the fonts/ directory after installing" );
}
$font = new WP_Font_Family( $font_data );

// Test.
$font->install();
foreach ( $expected as $font_file ) {
$font_file = static::$fonts_dir . $font_file;
$this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the uploads/fonts/ directory after installing" );
$font_file = path_join( static::$fonts_dir, $font_file );
$this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" );
}
$this->assertInstanceOf( WP_Post::class, $font->get_font_post(), 'Font post should exist after install' );
}
Expand Down Expand Up @@ -168,8 +168,8 @@ public function test_should_move_local_fontfaces( $font_data, array $files_data,
$font->install( $files_data );

foreach ( $expected as $font_file ) {
$font_file = static::$fonts_dir . $font_file;
$this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the uploads/fonts/ directory after installing" );
$font_file = path_join( static::$fonts_dir, $font_file );
$this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the fonts/ directory after installing" );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function test_should_uninstall_only_its_font_family( $font_data, array $f

// Check that the files were uninstalled.
foreach ( $files_to_uninstall as $font_file ) {
$font_file = static::$fonts_dir . $font_file;
$font_file = path_join( static::$fonts_dir, $font_file );
$this->assertFileDoesNotExist( $font_file, "Font file [{$font_file}] should not exists in the uploads/fonts/ directory after uninstalling" );
}
// Check that the Merriweather file was not uninstalled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
class Tests_Fonts_WpFontLibrary_GetFontsDir extends WP_UnitTestCase {

public function test_get_fonts_dir() {
$this->assertStringEndsWith( '/wp-content/uploads/fonts', WP_Font_Library::get_fonts_dir() );
$this->assertStringEndsWith( '/wp-content/fonts', WP_Font_Library::get_fonts_dir() );
}
}
12 changes: 6 additions & 6 deletions phpunit/tests/fonts/font-library/wpFontLibrary/setUploadDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Tests_Fonts_WpFontLibrary_SetUploadDir extends WP_UnitTestCase {
public function test_should_set_fonts_upload_dir() {
$defaults = array(
'subdir' => '/abc',
'basedir' => '/var/www/html/wp-content/uploads',
'baseurl' => 'http://example.com/wp-content/uploads',
'basedir' => '/var/www/html/wp-content',
'baseurl' => 'http://example.com/wp-content',
);
$expected = array(
'subdir' => '/fonts',
'basedir' => '/var/www/html/wp-content/uploads',
'baseurl' => 'http://example.com/wp-content/uploads',
'path' => '/var/www/html/wp-content/uploads/fonts',
'url' => 'http://example.com/wp-content/uploads/fonts',
'basedir' => '/var/www/html/wp-content',
'baseurl' => content_url(),
'path' => '/var/www/html/wp-content/fonts',
'url' => content_url() . '/fonts',
);
$this->assertSame( $expected, WP_Font_Library::set_upload_dir( $defaults ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function data_install_fonts() {
'fontFamily' => 'Piazzolla',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => '/wp-content/uploads/fonts/piazzolla_normal_400.ttf',
'src' => '/wp-content/fonts/piazzolla_normal_400.ttf',
),
),
),
Expand All @@ -120,7 +120,7 @@ public function data_install_fonts() {
'fontFamily' => 'Montserrat',
'fontStyle' => 'normal',
'fontWeight' => '100',
'src' => '/wp-content/uploads/fonts/montserrat_normal_100.ttf',
'src' => '/wp-content/fonts/montserrat_normal_100.ttf',
),
),
),
Expand Down Expand Up @@ -261,7 +261,7 @@ public function data_install_fonts() {
'fontFamily' => 'Piazzolla',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => '/wp-content/uploads/fonts/piazzolla_normal_400.ttf',
'src' => '/wp-content/fonts/piazzolla_normal_400.ttf',
),
),
),
Expand All @@ -274,7 +274,7 @@ public function data_install_fonts() {
'fontFamily' => 'Montserrat',
'fontStyle' => 'normal',
'fontWeight' => '100',
'src' => '/wp-content/uploads/fonts/montserrat_normal_100.ttf',
'src' => '/wp-content/fonts/montserrat_normal_100.ttf',
),
),
),
Expand Down

1 comment on commit ac32ed5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in ac32ed5.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6053784645
📝 Reported issues:

Please sign in to comment.