Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fonts Library: change upload directory to wp-content/fonts #54076

Closed
wants to merge 10 commits into from
79 changes: 30 additions & 49 deletions lib/experimental/fonts/font-library/class-wp-font-family.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,32 +163,6 @@ private static function delete_font_face_assets( $font_face ) {
return true;
}


/**
* Gets the overrides for the 'wp_handle_upload' function.
*
* @since 6.4.0
*
* @param string $filename The filename to be used for the uploaded file.
* @return array The overrides for the 'wp_handle_upload' function.
*/
private function get_upload_overrides( $filename ) {
return array(
// Arbitrary string to avoid the is_uploaded_file() check applied
// when using 'wp_handle_upload'.
'action' => 'wp_handle_font_upload',
// Not testing a form submission.
'test_form' => false,
// Seems mime type for files that are not images cannot be tested.
// See wp_check_filetype_and_ext().
'test_type' => false,
'unique_filename_callback' => static function() use ( $filename ) {
// Keep the original filename.
return $filename;
},
);
}

/**
* Downloads a font asset from a specified source URL and saves it to
* the font directory.
Expand Down Expand Up @@ -217,25 +191,17 @@ private function download_asset( $url, $filename ) {
return false;
}

$overrides = $this->get_upload_overrides( $filename );

$file = array(
'tmp_name' => $temp_file,
'name' => $filename,
);

$handled_file = wp_handle_upload( $file, $overrides );
$destination = WP_Font_Library::get_fonts_dir() . '/' . basename( $filename );
if ( ! rename( $temp_file, $destination ) ) {
return false;
}

// Cleans the temp file.
@unlink( $temp_file );

if ( ! isset( $handled_file['url'] ) ) {
return false;
}

// Returns the relative path to the downloaded font asset to be used as
// font face src.
return $handled_file['url'];
return $destination;
}

/**
Expand Down Expand Up @@ -268,19 +234,14 @@ private function move_font_face_asset( $font_face, $file ) {
return $new_font_face;
}

// Move the uploaded font asset from the temp folder to the fonts directory.
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}

$overrides = $this->get_upload_overrides( $filename );
$destination = WP_Font_Library::get_fonts_dir() . '/' . basename( $filename );

$handled_file = wp_handle_upload( $file, $overrides );

if ( isset( $handled_file['url'] ) ) {
if ( rename( $file['tmp_name'], $destination ) ) {
// If the file was successfully moved, update the font face definition
// to reference the new file location.
$new_font_face['src'] = $handled_file['url'];
$new_font_face['src'] = $destination;
} else {
return false;
}

return $new_font_face;
Expand Down Expand Up @@ -374,6 +335,7 @@ private function download_or_move_font_faces( $files ) {
}

$new_font_faces = array();

foreach ( $this->data['fontFace'] as $font_face ) {
// If the fonts are not meant to be dowloaded or uploaded
// (for example to install fonts that use a remote url).
Expand Down Expand Up @@ -532,6 +494,24 @@ private function create_or_update_font_post() {
return $this->create_font_post();
}

/**
* Creates font directory if it doesn't exist.
*
* @since 6.4.0
*
* @return bool If the directory was created successfully, false otherwise.
*/
private function make_fonts_dir() {
$fonts_dir = WP_Font_Library::get_fonts_dir();

// Ensure the fonts directory exists or create it.
if ( ! is_dir( $fonts_dir ) ) {
return wp_mkdir_p( $fonts_dir );
}

return false;
}

/**
* Installs the font family into the library.
*
Expand All @@ -542,6 +522,7 @@ private function create_or_update_font_post() {
*/
public function install( $files = null ) {
add_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) );
$this->make_fonts_dir();
$were_assets_written = $this->download_or_move_font_faces( $files );
remove_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) );

Expand Down
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 WP_CONTENT_DIR . '/fonts';
}

/**
Expand Down
6 changes: 2 additions & 4 deletions phpunit/tests/fonts/font-library/wpFontFamily/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
abstract class WP_Font_Family_UnitTestCase extends WP_UnitTestCase {

/**
* Fonts directory (in uploads).
* Fonts directory.
*
* @var string
*/
Expand All @@ -28,9 +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/';
wp_mkdir_p( static::$fonts_dir );
static::$fonts_dir = WP_CONTENT_DIR . '/fonts/';
}

public function set_up() {
Expand Down
4 changes: 2 additions & 2 deletions phpunit/tests/fonts/font-library/wpFontFamily/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public function test_should_download_fontfaces_and_create_post( $font_data, arra
// 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" );
$this->assertFileDoesNotExist( $font_file, "Font file [{$font_file}] should not exist in the wp-content/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" );
$this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the wp-content/fonts/ directory after installing" );
}
$this->assertInstanceOf( WP_Post::class, $font->get_font_post(), 'Font post should exist after install' );
}
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' => 'http://example.com/wp-content',
'path' => '/var/www/html/wp-content/fonts',
'url' => 'http://example.com/wp-content/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
Loading