-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add fallback for the default font folder path #6259
Changes from 24 commits
6e1e8e0
0fb2cec
7a46783
b85cdf3
b8e35cc
4f4e63c
e655888
8e23ae4
25d3c80
030b626
48d7534
171b621
596f452
9b05b6a
895984b
d2f6e5c
928a564
9c51258
e405ba8
c3f21ea
b2a34dd
e901a3f
ac77349
5f7b3d1
de55afb
e3e8289
90a3982
f7de278
45c8ece
f0c3f3d
206543a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,36 @@ function wp_get_font_dir() { | |
'error' => false, | ||
); | ||
|
||
$made_dir = wp_mkdir_p( $defaults['path'] ); | ||
|
||
if ( ! $made_dir || ! wp_is_writable( $defaults['path'] ) ) { | ||
$upload_dir = wp_get_upload_dir(); | ||
$defaults = array( | ||
'path' => path_join( $upload_dir['basedir'], 'fonts' ), | ||
'url' => $upload_dir['baseurl'] . '/fonts', | ||
'subdir' => '', | ||
'basedir' => path_join( $upload_dir['basedir'], 'fonts' ), | ||
'baseurl' => $upload_dir['baseurl'] . '/fonts', | ||
'error' => false, | ||
); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As @peterwilsoncc mentions above thinking it would probably be good to run the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd really prefer to just run it once after WordPress figures out what it will present as the default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm understanding correctly you prefer this function to do something like this?
|
||
$made_dir = wp_mkdir_p( $defaults['path'] ); | ||
|
||
if ( ! $made_dir || ! wp_is_writable( $defaults['path'] ) ) { | ||
if ( str_starts_with( $defaults['path'], ABSPATH ) ) { | ||
$error_path = str_replace( ABSPATH, '', $defaults['path'] ); | ||
} else { | ||
$error_path = wp_basename( $defaults['path'] ); | ||
} | ||
|
||
$defaults['error'] = sprintf( | ||
/* translators: %s: Directory path. */ | ||
__( 'Unable to create directory %s. Is its parent directory writable by the server?' ), | ||
esc_html( $error_path ) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Filters the fonts directory data. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,18 +12,30 @@ | |
*/ | ||
class Tests_Fonts_WpFontDir extends WP_UnitTestCase { | ||
private static $dir_defaults; | ||
private static $fake_fonts_file; | ||
|
||
public static function set_up_before_class() { | ||
parent::set_up_before_class(); | ||
|
||
static::$dir_defaults = array( | ||
'path' => path_join( WP_CONTENT_DIR, 'fonts' ), | ||
'url' => content_url( 'fonts' ), | ||
$path = path_join( WP_CONTENT_DIR, 'fonts' ); | ||
$url = content_url( 'fonts' ); | ||
self::$dir_defaults = array( | ||
'path' => $path, | ||
'url' => $url, | ||
'subdir' => '', | ||
'basedir' => path_join( WP_CONTENT_DIR, 'fonts' ), | ||
'baseurl' => content_url( 'fonts' ), | ||
'basedir' => $path, | ||
'baseurl' => $url, | ||
'error' => false, | ||
); | ||
|
||
static::$fake_fonts_file = $path; | ||
} | ||
|
||
public function tear_down() { | ||
$this->remove_fonts_directory(); | ||
$this->remove_no_new_directories_in_wp_content_fake(); | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
public function test_fonts_dir() { | ||
|
@@ -69,4 +81,57 @@ function set_new_values( $defaults ) { | |
|
||
$this->assertSame( static::$dir_defaults, $font_dir, 'The wp_get_font_dir() method should return the default values.' ); | ||
} | ||
|
||
public function test_should_created_fonts_dir_in_uploads_when_fails_in_wp_content() { | ||
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Set the expected results. | ||
$upload_dir = wp_upload_dir(); | ||
$path = path_join( $upload_dir['basedir'], 'fonts' ); | ||
$expected = array( | ||
'path' => $path, | ||
'url' => $upload_dir['baseurl'] . '/fonts', | ||
'subdir' => '', | ||
'basedir' => $path, | ||
'baseurl' => $upload_dir['baseurl'] . '/fonts', | ||
'error' => false, | ||
); | ||
|
||
$this->fake_no_new_directories_in_wp_content(); | ||
$this->assertFileExists( self::$fake_fonts_file ); | ||
|
||
$font_dir = wp_get_font_dir(); | ||
|
||
$this->assertDirectoryDoesNotExist( path_join( WP_CONTENT_DIR, 'fonts' ), 'The `wp-content/fonts` directory should not exist' ); | ||
$this->assertDirectoryExists( $font_dir['path'], 'The `uploads/fonts` directory should exist' ); | ||
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertSame( $expected, $font_dir, 'The font directory should be a subdir in the uploads directory.' ); | ||
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private function remove_fonts_directory() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the cleanup code to a separate method to make it reusable. All of the Font Library tests should be removing the fonts directory between tests. Moving it here is the step. Then a trait or base TestCase can be add that is shared amongst all of the test classes (though out-of-scope for this PR). |
||
$directories = array( | ||
path_join( WP_CONTENT_DIR, 'fonts' ), | ||
path_join( WP_CONTENT_DIR, 'uploads/fonts' ), | ||
'/custom-path/fonts/my-custom-subdir', | ||
); | ||
|
||
foreach ( $directories as $dir ) { | ||
if ( ! is_dir( $dir ) ) { | ||
continue; | ||
} | ||
|
||
$this->rmdir( $dir ); | ||
@rmdir( $dir ); | ||
} | ||
} | ||
|
||
private function fake_no_new_directories_in_wp_content() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a weird fake, but it works. The way it works is this: A file named That will then cause the fallback branch to execute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created it in a method for 2 reasons:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if the method name should be focused on what it does -> makes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the name is fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed this function
matiasbenedetto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
file_put_contents( | ||
self::$fake_fonts_file, | ||
'fake file' | ||
); | ||
} | ||
|
||
private function remove_no_new_directories_in_wp_content_fake() { | ||
if ( file_exists( self::$fake_fonts_file ) ) { | ||
@unlink( self::$fake_fonts_file ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need or should add error suppression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed here: f7de278 |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've come to realize that attempting to create the directory here is problematic.
As the code runs before the
font_dir
filter, it's unknown at this point in the execution whether the directory path will be overridden by a plugin. If it is overridden then this code may create a junk directory in the sites filesystem that will never be used.What I think the code will need to do is:
wp_upload_dir()
to create the directory after theupload_dir
filter runs (which will include firing thefont_dir
filter)wp_upload_dir()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1.
Seems the first two checks:
are also done in
wp_mkdir_p()
. Somewhat unsure if they need to be done (repeated) before running wp_mkdir_p()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good points @peterwilsoncc. I agree 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call; in this commit I implemented the change to create the folder only after the
font_dir
filter runs.