From 87011cf2421ef97fa5a625e113fc3db3a6b2acd7 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Fri, 3 Oct 2025 21:36:17 +0300 Subject: [PATCH 01/11] Add function to order sections by a predefined order Signed-off-by: meszarosrob --- inc/packages/admin/info.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/inc/packages/admin/info.php b/inc/packages/admin/info.php index d42610b6..c43201c3 100644 --- a/inc/packages/admin/info.php +++ b/inc/packages/admin/info.php @@ -95,6 +95,37 @@ function get_section_title( string $id ) { } } +/** + * Order sections by a predefined order. + * + * @param array $sections Sections to order. + * + * @return array Ordered sections. + */ +function order_sections_by_predefined_order( array $sections ) : array { + $desired_order = [ + 'description', + 'installation', + 'faq', + 'screenshots', + 'changelog', + 'security', + 'reviews', + 'other_notes', + ]; + + $desired_order_index = array_flip( $desired_order ); + + uksort($sections, function( $a, $b ) use ( $desired_order_index ) { + $pos_a = $desired_order_index[ $a ] ?? PHP_INT_MAX; + $pos_b = $desired_order_index[ $b ] ?? PHP_INT_MAX; + + return $pos_a <=> $pos_b; + }); + + return $sections; +} + /** * Render page. * From 42b1e76aeb78648709a9087cffaac9e4c8ad2351 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Fri, 3 Oct 2025 21:37:03 +0300 Subject: [PATCH 02/11] Order the section fetched from MetadataDocument Signed-off-by: meszarosrob --- inc/packages/admin/info.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/packages/admin/info.php b/inc/packages/admin/info.php index c43201c3..de198ac8 100644 --- a/inc/packages/admin/info.php +++ b/inc/packages/admin/info.php @@ -155,7 +155,7 @@ function render_page( MetadataDocument $metadata, string $tab, string $section ) * @param string $section Page section. */ function render( MetadataDocument $doc, string $tab, string $section ) { - $sections = (array) $doc->sections; + $sections = order_sections_by_predefined_order( (array) $doc->sections ); if ( ! isset( $sections[ $section ] ) ) { $section = array_keys( $sections )[0]; From 803ed7239069e0d42f97fed29588d0cc46b1e6f5 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Fri, 3 Oct 2025 21:48:04 +0300 Subject: [PATCH 03/11] Add test for the order_sections_by_predefined_order Signed-off-by: meszarosrob --- .../phpunit/tests/Packages/Admin/InfoTest.php | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/phpunit/tests/Packages/Admin/InfoTest.php diff --git a/tests/phpunit/tests/Packages/Admin/InfoTest.php b/tests/phpunit/tests/Packages/Admin/InfoTest.php new file mode 100644 index 00000000..6e098ea9 --- /dev/null +++ b/tests/phpunit/tests/Packages/Admin/InfoTest.php @@ -0,0 +1,89 @@ +assertSame( + $ordered_sections, + order_sections_by_predefined_order( $sections ) + ); + } + + /** + * Data provider. + */ + public static function data_plugin_detail_sections(): array { + return [ + 'expected sections' => [ + 'arbitrary order' => [ + 'faq' => '', + 'screenshots' => '', + 'changelog' => '', + 'description' => '', + 'security' => '', + 'reviews' => '', + 'other_notes' => '', + 'installation' => '', + ], + 'expected order' => [ + 'description' => '', + 'installation' => '', + 'faq' => '', + 'screenshots' => '', + 'changelog' => '', + 'security' => '', + 'reviews' => '', + 'other_notes' => '', + ], + ], + 'unknown sections' => [ + 'arbitrary order' => [ + 'foo' => '', + 'bar' => '', + 'baz' => '', + ], + 'expected order' => [ + 'foo' => '', + 'bar' => '', + 'baz' => '', + ], + ], + 'expected and unknown sections' => [ + 'arbitrary order' => [ + 'faq' => '', + 'foo' => '', + 'screenshots' => '', + 'changelog' => '', + 'bar' => '', + 'reviews' => '', + 'installation' => '', + 'security' => '', + ], + 'expected order' => [ + 'installation' => '', + 'faq' => '', + 'screenshots' => '', + 'changelog' => '', + 'security' => '', + 'reviews' => '', + 'foo' => '', + 'bar' => '', + ], + ], + 'empty sections' => [ + 'arbitrary order' => [], + 'expected order' => [], + ], + ]; + } +} From 4e7f98ca3ea72348cbf523b3ed08227ca2f4afd0 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Sat, 4 Oct 2025 12:44:36 +0300 Subject: [PATCH 04/11] Add doc blocks for the test class Signed-off-by: meszarosrob --- tests/phpunit/tests/Packages/Admin/InfoTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/Packages/Admin/InfoTest.php b/tests/phpunit/tests/Packages/Admin/InfoTest.php index 6e098ea9..2b14c8f0 100644 --- a/tests/phpunit/tests/Packages/Admin/InfoTest.php +++ b/tests/phpunit/tests/Packages/Admin/InfoTest.php @@ -1,11 +1,18 @@ Date: Sat, 4 Oct 2025 16:00:46 +0300 Subject: [PATCH 05/11] Move order_sections_by_predefined_order under the FAIR\Packages\Admin namespace Signed-off-by: meszarosrob --- inc/packages/admin/info.php | 33 +------------------ inc/packages/admin/namespace.php | 31 +++++++++++++++++ .../Admin/{InfoTest.php => NamespaceTest.php} | 9 ++--- 3 files changed, 37 insertions(+), 36 deletions(-) rename tests/phpunit/tests/Packages/Admin/{InfoTest.php => NamespaceTest.php} (88%) diff --git a/inc/packages/admin/info.php b/inc/packages/admin/info.php index de198ac8..fbc6255f 100644 --- a/inc/packages/admin/info.php +++ b/inc/packages/admin/info.php @@ -95,37 +95,6 @@ function get_section_title( string $id ) { } } -/** - * Order sections by a predefined order. - * - * @param array $sections Sections to order. - * - * @return array Ordered sections. - */ -function order_sections_by_predefined_order( array $sections ) : array { - $desired_order = [ - 'description', - 'installation', - 'faq', - 'screenshots', - 'changelog', - 'security', - 'reviews', - 'other_notes', - ]; - - $desired_order_index = array_flip( $desired_order ); - - uksort($sections, function( $a, $b ) use ( $desired_order_index ) { - $pos_a = $desired_order_index[ $a ] ?? PHP_INT_MAX; - $pos_b = $desired_order_index[ $b ] ?? PHP_INT_MAX; - - return $pos_a <=> $pos_b; - }); - - return $sections; -} - /** * Render page. * @@ -155,7 +124,7 @@ function render_page( MetadataDocument $metadata, string $tab, string $section ) * @param string $section Page section. */ function render( MetadataDocument $doc, string $tab, string $section ) { - $sections = order_sections_by_predefined_order( (array) $doc->sections ); + $sections = Admin\order_sections_by_predefined_order( (array) $doc->sections ); if ( ! isset( $sections[ $section ] ) ) { $section = array_keys( $sections )[0]; diff --git a/inc/packages/admin/namespace.php b/inc/packages/admin/namespace.php index b7ff2093..e844cdc7 100644 --- a/inc/packages/admin/namespace.php +++ b/inc/packages/admin/namespace.php @@ -450,6 +450,37 @@ function alter_slugs( $res, $action, $args ) { return $res; } +/** + * Order sections by a predefined order. + * + * @param array $sections Sections to order. + * + * @return array Ordered sections. + */ +function order_sections_by_predefined_order( array $sections ) : array { + $desired_order = [ + 'description', + 'installation', + 'faq', + 'screenshots', + 'changelog', + 'security', + 'reviews', + 'other_notes', + ]; + + $desired_order_index = array_flip( $desired_order ); + + uksort($sections, function( $a, $b ) use ( $desired_order_index ) { + $pos_a = $desired_order_index[ $a ] ?? PHP_INT_MAX; + $pos_b = $desired_order_index[ $b ] ?? PHP_INT_MAX; + + return $pos_a <=> $pos_b; + }); + + return $sections; +} + /** * Override the install button, for bridged plugins. * diff --git a/tests/phpunit/tests/Packages/Admin/InfoTest.php b/tests/phpunit/tests/Packages/Admin/NamespaceTest.php similarity index 88% rename from tests/phpunit/tests/Packages/Admin/InfoTest.php rename to tests/phpunit/tests/Packages/Admin/NamespaceTest.php index 2b14c8f0..eab5c91e 100644 --- a/tests/phpunit/tests/Packages/Admin/InfoTest.php +++ b/tests/phpunit/tests/Packages/Admin/NamespaceTest.php @@ -1,16 +1,17 @@ Date: Sat, 4 Oct 2025 16:01:55 +0300 Subject: [PATCH 06/11] Hook into default plugin api results to order the sections Signed-off-by: meszarosrob --- inc/packages/admin/namespace.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/inc/packages/admin/namespace.php b/inc/packages/admin/namespace.php index e844cdc7..87b2e528 100644 --- a/inc/packages/admin/namespace.php +++ b/inc/packages/admin/namespace.php @@ -12,6 +12,7 @@ use FAIR\Packages\MetadataDocument; use FAIR\Packages\ReleaseDocument; use FAIR\Updater; +use WP_Error; const TAB_DIRECT = 'fair_direct'; const ACTION_INSTALL = 'fair-install-plugin'; @@ -34,6 +35,7 @@ function bootstrap() { add_action( 'load-plugin-install.php', __NAMESPACE__ . '\\load_plugin_install' ); add_action( 'install_plugins_pre_plugin-information', __NAMESPACE__ . '\\maybe_hijack_plugin_info', 0 ); add_filter( 'plugins_api_result', __NAMESPACE__ . '\\alter_slugs', 10, 3 ); + add_filter( 'plugins_api_result', __NAMESPACE__ . '\\order_plugin_information_sections', 11, 2 ); add_filter( 'plugin_install_action_links', __NAMESPACE__ . '\\maybe_hijack_plugin_install_button', 10, 2 ); add_filter( 'plugin_install_description', __NAMESPACE__ . '\\maybe_add_data_to_description', 10, 2 ); add_action( 'wp_ajax_check_plugin_dependencies', __NAMESPACE__ . '\\set_slug_to_hashed' ); @@ -450,6 +452,32 @@ function alter_slugs( $res, $action, $args ) { return $res; } +/** + * Filters the Plugin Installation API response results. + * + * @since 2.7.0 + * + * @param object|WP_Error $res Response object or WP_Error. + * @param string $action The type of information being requested from the Plugin Installation API. + */ +function order_plugin_information_sections( $res, $action ) { + if ( is_wp_error( $res ) ) { + return $res; + } + + if ( 'plugin_information' !== $action ) { + return $res; + } + + if ( empty( $res->sections ) ) { + return $res; + } + + $res->sections = order_sections_by_predefined_order( $res->sections ); + + return $res; +} + /** * Order sections by a predefined order. * From eea689f6d8f8baea86c03e117d46f9c3dc8c99ab Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Sat, 4 Oct 2025 16:20:22 +0300 Subject: [PATCH 07/11] Update doc blocks of order_plugin_information_sections Signed-off-by: meszarosrob --- inc/packages/admin/namespace.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/packages/admin/namespace.php b/inc/packages/admin/namespace.php index 87b2e528..14f0b5c5 100644 --- a/inc/packages/admin/namespace.php +++ b/inc/packages/admin/namespace.php @@ -453,9 +453,7 @@ function alter_slugs( $res, $action, $args ) { } /** - * Filters the Plugin Installation API response results. - * - * @since 2.7.0 + * Orders the sections of Plugin Installation API response. * * @param object|WP_Error $res Response object or WP_Error. * @param string $action The type of information being requested from the Plugin Installation API. From e826e283138264c1e1ed7acd6f6d70a987414111 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Sun, 5 Oct 2025 10:46:55 +0300 Subject: [PATCH 08/11] Update doc block to use imperative Signed-off-by: meszarosrob --- inc/packages/admin/namespace.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/packages/admin/namespace.php b/inc/packages/admin/namespace.php index 14f0b5c5..f343476b 100644 --- a/inc/packages/admin/namespace.php +++ b/inc/packages/admin/namespace.php @@ -453,7 +453,7 @@ function alter_slugs( $res, $action, $args ) { } /** - * Orders the sections of Plugin Installation API response. + * Order the sections of Plugin Installation API response. * * @param object|WP_Error $res Response object or WP_Error. * @param string $action The type of information being requested from the Plugin Installation API. From b57277fc973a26e8bb52b06cd8ad1459388498c2 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Sun, 5 Oct 2025 10:49:49 +0300 Subject: [PATCH 09/11] Rename and match data provider key with test method param Signed-off-by: meszarosrob --- .../tests/Packages/Admin/NamespaceTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/tests/Packages/Admin/NamespaceTest.php b/tests/phpunit/tests/Packages/Admin/NamespaceTest.php index eab5c91e..d4bd4b12 100644 --- a/tests/phpunit/tests/Packages/Admin/NamespaceTest.php +++ b/tests/phpunit/tests/Packages/Admin/NamespaceTest.php @@ -18,11 +18,11 @@ class NamespaceTest extends WP_UnitTestCase { * @dataProvider data_plugin_detail_sections * * @param array $sections Sections provided in arbitrary order, as if returned from MetadataDocument. - * @param array $ordered_sections The sections in order we expect them to be. + * @param array $expected_order The sections in order we expect them to be. */ - public function test_should_return_sections_in_predefined_order( array $sections, array $ordered_sections ) { + public function test_should_return_sections_in_predefined_order( array $sections, array $expected_order ) { $this->assertSame( - $ordered_sections, + $expected_order, order_sections_by_predefined_order( $sections ) ); } @@ -33,7 +33,7 @@ public function test_should_return_sections_in_predefined_order( array $sections public static function data_plugin_detail_sections(): array { return [ 'expected sections' => [ - 'arbitrary order' => [ + 'sections' => [ 'faq' => '', 'screenshots' => '', 'changelog' => '', @@ -43,7 +43,7 @@ public static function data_plugin_detail_sections(): array { 'other_notes' => '', 'installation' => '', ], - 'expected order' => [ + 'expected_order' => [ 'description' => '', 'installation' => '', 'faq' => '', @@ -55,19 +55,19 @@ public static function data_plugin_detail_sections(): array { ], ], 'unknown sections' => [ - 'arbitrary order' => [ + 'sections' => [ 'foo' => '', 'bar' => '', 'baz' => '', ], - 'expected order' => [ + 'expected_order' => [ 'foo' => '', 'bar' => '', 'baz' => '', ], ], 'expected and unknown sections' => [ - 'arbitrary order' => [ + 'sections' => [ 'faq' => '', 'foo' => '', 'screenshots' => '', @@ -77,7 +77,7 @@ public static function data_plugin_detail_sections(): array { 'installation' => '', 'security' => '', ], - 'expected order' => [ + 'expected_order' => [ 'installation' => '', 'faq' => '', 'screenshots' => '', @@ -89,8 +89,8 @@ public static function data_plugin_detail_sections(): array { ], ], 'empty sections' => [ - 'arbitrary order' => [], - 'expected order' => [], + 'sections' => [], + 'expected_order' => [], ], ]; } From 9f8e91fa4e7931d09adc8bad4dc12f314ee5bb96 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Sun, 5 Oct 2025 10:54:58 +0300 Subject: [PATCH 10/11] Merge subsequent conditions into one Signed-off-by: meszarosrob --- inc/packages/admin/namespace.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/inc/packages/admin/namespace.php b/inc/packages/admin/namespace.php index f343476b..075d8d0a 100644 --- a/inc/packages/admin/namespace.php +++ b/inc/packages/admin/namespace.php @@ -459,15 +459,7 @@ function alter_slugs( $res, $action, $args ) { * @param string $action The type of information being requested from the Plugin Installation API. */ function order_plugin_information_sections( $res, $action ) { - if ( is_wp_error( $res ) ) { - return $res; - } - - if ( 'plugin_information' !== $action ) { - return $res; - } - - if ( empty( $res->sections ) ) { + if ( is_wp_error( $res ) || 'plugin_information' !== $action || empty( $res->sections ) ) { return $res; } From 8c820f3daf5f5479d8fa5c38be797ff353f2ba36 Mon Sep 17 00:00:00 2001 From: meszarosrob Date: Sun, 5 Oct 2025 10:59:22 +0300 Subject: [PATCH 11/11] Update test class per sample test directions Signed-off-by: meszarosrob --- ...aceTest.php => OrderSectionsByPredefinedOrderTest.php} | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename tests/phpunit/tests/Packages/Admin/{NamespaceTest.php => OrderSectionsByPredefinedOrderTest.php} (87%) diff --git a/tests/phpunit/tests/Packages/Admin/NamespaceTest.php b/tests/phpunit/tests/Packages/Admin/OrderSectionsByPredefinedOrderTest.php similarity index 87% rename from tests/phpunit/tests/Packages/Admin/NamespaceTest.php rename to tests/phpunit/tests/Packages/Admin/OrderSectionsByPredefinedOrderTest.php index d4bd4b12..3427d789 100644 --- a/tests/phpunit/tests/Packages/Admin/NamespaceTest.php +++ b/tests/phpunit/tests/Packages/Admin/OrderSectionsByPredefinedOrderTest.php @@ -1,6 +1,6 @@