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

Filter the font directory to work with the VIP Filesystem #5265

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions files/class-vip-filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private function add_filters() {
add_filter( 'get_attached_file', [ $this, 'filter_get_attached_file' ], 20 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'filter_wp_generate_attachment_metadata' ], 10, 2 );
add_filter( 'wp_read_image_metadata', [ $this, 'filter_wp_read_image_metadata' ], 10, 2 );
add_filter( 'font_dir', [ $this, 'filter_wp_font_dir' ], 10, 1 );

/**
* The core's function recurse_dirsize would call to opendir() which is not supported by the
Expand All @@ -124,6 +125,7 @@ private function remove_filters() {
remove_filter( 'get_attached_file', [ $this, 'filter_get_attached_file' ], 20 );
remove_filter( 'wp_generate_attachment_metadata', [ $this, 'filter_wp_generate_attachment_metadata' ] );
remove_filter( 'wp_read_image_metadata', [ $this, 'filter_wp_read_image_metadata' ], 10, 2 );
remove_filter( 'font_dir', [ $this, 'filter_wp_font_dir' ], 10, 1 );
remove_filter( 'pre_recurse_dirsize', '__return_zero' );
}

Expand Down Expand Up @@ -461,4 +463,18 @@ public function filter_wp_read_image_metadata( $meta, $file ) {

return $meta;
}

/**
* Changes the Font Library directory to work with the VIP Filesystem.
*/
public function filter_wp_font_dir( $defaults ) {
$upload_dir = wp_upload_dir();
noahtallen marked this conversation as resolved.
Show resolved Hide resolved

$defaults['basedir'] = $upload_dir['basedir'] . '/fonts';
$defaults['baseurl'] = $upload_dir['baseurl'] . '/fonts';
$defaults['path'] = $defaults['basedir'];
$defaults['url'] = $defaults['baseurl'];

return $defaults;
}
}
23 changes: 21 additions & 2 deletions tests/files/test-vip-filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class VIP_Filesystem_Test extends WP_UnitTestCase {

public static function configure_constant_mocker(): void {
Constant_Mocker::clear();
define( 'LOCAL_UPLOADS', '/wp/uploads' );
define( 'WP_CONTENT_DIR', '/wp/wordpress/wp-content' );
define( 'LOCAL_UPLOADS', '/tmp/uploads' );
define( 'WP_CONTENT_DIR', '/tmp/wordpress/wp-content' );
}

public function setUp(): void {
Expand Down Expand Up @@ -386,4 +386,23 @@ public function data_get_transport_for_path(): iterable {
[ constant( 'WP_CONTENT_DIR' ) . '/languages/test.txt', 'direct' ],
];
}

public function test_wp_font_dir() {
// Only available in WP 6.5 and newer:
if ( ! function_exists( '\wp_get_font_dir' ) ) {
$this->markTestSkipped( 'test_wp_font_dir does not need to run for WP < 6.5.' );
return;
}

$font_dir = \wp_get_font_dir();
Copy link
Contributor Author

@noahtallen noahtallen Mar 4, 2024

Choose a reason for hiding this comment

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

We need to follow this PR: WordPress/wordpress-develop#6211

In case the function is renamed/changed. (Or test it by doing the filter instead.)

Choose a reason for hiding this comment

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

We need to follow this PR: WordPress/wordpress-develop#6211

In case the function is renamed/changed. (Or test it by doing the filter instead.)

I'll try to remember to come back and let you know.

I've been doing some expreiments on my local build and don't intend to change the name of the function or the filter but will confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!


$this->assertEquals( $font_dir, [
'path' => 'vip://wp-content/uploads/fonts',
'basedir' => 'vip://wp-content/uploads/fonts',
'url' => 'http://example.org/wp-content/uploads/fonts',
'baseurl' => 'http://example.org/wp-content/uploads/fonts',
'subdir' => '',
'error' => false,
] );
}
}
Loading