From 56c1f548a091361dc6501119805d78c1d0d57f18 Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Wed, 21 Feb 2024 13:46:24 -0800 Subject: [PATCH 1/6] Filter the font directory to work with the VIP fs --- files/class-vip-filesystem.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/files/class-vip-filesystem.php b/files/class-vip-filesystem.php index ac51a04b76..64f6fd4fb2 100644 --- a/files/class-vip-filesystem.php +++ b/files/class-vip-filesystem.php @@ -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 @@ -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' ); } @@ -461,4 +463,24 @@ 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 ) { + // Without removing the filter, there will be an infinite loop, because + // calling wp_upload_dir will trigger upload_dir, which includes wp_get_font_dir + // in the context of uploading fonts. That ultimately calls this function + // again through the font_dir filter. + remove_filter( 'upload_dir', 'wp_get_font_dir' ); + $upload_dir = wp_upload_dir(); + add_filter( 'upload_dir', 'wp_get_font_dir' ); + + $defaults['basedir'] = $upload_dir['basedir'] . '/fonts'; + $defaults['baseurl'] = $upload_dir['baseurl'] . '/fonts'; + $defaults['path'] = $defaults['basedir']; + $defaults['url'] = $defaults['baseurl']; + + return $defaults; + } } From 64a336f5c646e696f3aad4348411e5315584c2bd Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Wed, 21 Feb 2024 16:20:47 -0800 Subject: [PATCH 2/6] Add unit test --- tests/files/test-vip-filesystem.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/files/test-vip-filesystem.php b/tests/files/test-vip-filesystem.php index fe0250b45f..f070fedecc 100644 --- a/tests/files/test-vip-filesystem.php +++ b/tests/files/test-vip-filesystem.php @@ -4,6 +4,7 @@ use Automattic\Test\Constant_Mocker; use ErrorException; +use Parsely\RemoteAPI\Base_Endpoint_Remote; use WP_Error; use WP_Filesystem_Base; use WP_Filesystem_Direct; @@ -386,4 +387,25 @@ 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->assertEquals( true, true ); + return; + } + // Simulate filter behavior which happens during upload: + add_filter( 'upload_dir', '\wp_get_font_dir' ); + $font_dir = \wp_get_font_dir(); + remove_filter( 'upload_dir', '\zwp_get_font_dir' ); + + $this->assertEquals( $font_dir, [ + 'path' => 'vip://wp-contentt/uploads/fonts', + 'basedir' => 'vip://wp-contentt/uploads/fonts', + 'url' => 'http://example.org/wp-content/uploads/fonts', + 'baseurl' => 'http://example.org/wp-content/uploads/fonts', + 'subdir' => '', + 'error' => false, + ] ); + } } From 9af8314e3d680afd21162c1a268dc4c3881f9f7d Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Wed, 21 Feb 2024 17:02:56 -0800 Subject: [PATCH 3/6] Fix unit test --- tests/files/test-vip-filesystem.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/files/test-vip-filesystem.php b/tests/files/test-vip-filesystem.php index f070fedecc..9c6938e596 100644 --- a/tests/files/test-vip-filesystem.php +++ b/tests/files/test-vip-filesystem.php @@ -4,7 +4,6 @@ use Automattic\Test\Constant_Mocker; use ErrorException; -use Parsely\RemoteAPI\Base_Endpoint_Remote; use WP_Error; use WP_Filesystem_Base; use WP_Filesystem_Direct; @@ -394,10 +393,8 @@ public function test_wp_font_dir() { $this->assertEquals( true, true ); return; } - // Simulate filter behavior which happens during upload: - add_filter( 'upload_dir', '\wp_get_font_dir' ); + $font_dir = \wp_get_font_dir(); - remove_filter( 'upload_dir', '\zwp_get_font_dir' ); $this->assertEquals( $font_dir, [ 'path' => 'vip://wp-contentt/uploads/fonts', From 03c5f2d8bae4345c560a49e1c46667f7fa0f362a Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Mon, 4 Mar 2024 12:32:40 -0800 Subject: [PATCH 4/6] Remove infinite loop workaround --- files/class-vip-filesystem.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/files/class-vip-filesystem.php b/files/class-vip-filesystem.php index 64f6fd4fb2..a2031341e9 100644 --- a/files/class-vip-filesystem.php +++ b/files/class-vip-filesystem.php @@ -468,13 +468,7 @@ public function filter_wp_read_image_metadata( $meta, $file ) { * Changes the Font Library directory to work with the VIP Filesystem. */ public function filter_wp_font_dir( $defaults ) { - // Without removing the filter, there will be an infinite loop, because - // calling wp_upload_dir will trigger upload_dir, which includes wp_get_font_dir - // in the context of uploading fonts. That ultimately calls this function - // again through the font_dir filter. - remove_filter( 'upload_dir', 'wp_get_font_dir' ); $upload_dir = wp_upload_dir(); - add_filter( 'upload_dir', 'wp_get_font_dir' ); $defaults['basedir'] = $upload_dir['basedir'] . '/fonts'; $defaults['baseurl'] = $upload_dir['baseurl'] . '/fonts'; From 71fa22fab0baab8ba5a46a90e2b8f85c75ed3926 Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Mon, 4 Mar 2024 12:53:01 -0800 Subject: [PATCH 5/6] Use skipped test instead of true assertion --- tests/files/test-vip-filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/files/test-vip-filesystem.php b/tests/files/test-vip-filesystem.php index 9c6938e596..496b0e8d63 100644 --- a/tests/files/test-vip-filesystem.php +++ b/tests/files/test-vip-filesystem.php @@ -390,7 +390,7 @@ public function data_get_transport_for_path(): iterable { public function test_wp_font_dir() { // Only available in WP 6.5 and newer: if ( ! function_exists( '\wp_get_font_dir' ) ) { - $this->assertEquals( true, true ); + $this->markTestSkipped( 'test_wp_font_dir does not need to run for WP < 6.5.' ); return; } From 6bc5fa23ebbf5f465dbd28d3a1cdee135f6948b1 Mon Sep 17 00:00:00 2001 From: Noah Allen Date: Fri, 15 Mar 2024 15:03:55 -0700 Subject: [PATCH 6/6] Try updating constants to fix string substitution issue --- tests/files/test-vip-filesystem.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/files/test-vip-filesystem.php b/tests/files/test-vip-filesystem.php index 496b0e8d63..c23692bd69 100644 --- a/tests/files/test-vip-filesystem.php +++ b/tests/files/test-vip-filesystem.php @@ -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 { @@ -397,8 +397,8 @@ public function test_wp_font_dir() { $font_dir = \wp_get_font_dir(); $this->assertEquals( $font_dir, [ - 'path' => 'vip://wp-contentt/uploads/fonts', - 'basedir' => 'vip://wp-contentt/uploads/fonts', + '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' => '',