Skip to content

Commit d8652cd

Browse files
authored
Sort plugin modal tabs (#310)
Signed-off-by: Andy Fragen <[email protected]>
1 parent 3bea3e1 commit d8652cd

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

inc/packages/admin/namespace.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function bootstrap() {
3535
add_action( 'load-plugin-install.php', __NAMESPACE__ . '\\load_plugin_install' );
3636
add_action( 'install_plugins_pre_plugin-information', __NAMESPACE__ . '\\maybe_hijack_plugin_info', 0 );
3737
add_filter( 'plugins_api_result', __NAMESPACE__ . '\\alter_slugs', 10, 3 );
38+
add_filter( 'plugins_api_result', __NAMESPACE__ . '\\sort_sections_in_api', 15, 1 );
3839
add_filter( 'plugin_install_action_links', __NAMESPACE__ . '\\maybe_hijack_plugin_install_button', 10, 2 );
3940
add_filter( 'plugin_install_description', __NAMESPACE__ . '\\maybe_add_data_to_description', 10, 2 );
4041
add_action( 'wp_ajax_check_plugin_dependencies', __NAMESPACE__ . '\\set_slug_to_hashed' );
@@ -430,6 +431,34 @@ function alter_slugs( $res, $action, $args ) {
430431
return $res;
431432
}
432433

434+
/**
435+
* Sort plugin modal tabs.
436+
*
437+
* Based on standard tab listing order.
438+
*
439+
* @param object|WP_Error $res Response object or WP_Error.
440+
* @return object|WP_Error
441+
*/
442+
function sort_sections_in_api( $res ) {
443+
$ordered_sections = [
444+
'description',
445+
'installation',
446+
'faq',
447+
'screenshots',
448+
'changelog',
449+
'upgrade_notice',
450+
'security',
451+
'other_notes',
452+
'reviews',
453+
];
454+
if ( property_exists( $res, 'sections' ) && is_array( $res->sections ) ) {
455+
$properly_ordered = array_merge( array_fill_keys( $ordered_sections, '' ), $res->sections );
456+
$res->sections = array_filter( $properly_ordered );
457+
}
458+
459+
return $res;
460+
}
461+
433462
/**
434463
* Override the install button, for bridged plugins.
435464
*

inc/packages/namespace.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use FAIR\Packages\DID\PLC;
1414
use FAIR\Packages\DID\Web;
1515
use FAIR\Updater;
16+
use function FAIR\Packages\Admin\sort_sections_in_api;
1617
use WP_Error;
1718
use WP_Upgrader;
1819

@@ -170,6 +171,14 @@ function fetch_metadata_doc( string $url ) {
170171
} elseif ( $code !== 200 ) {
171172
return new WP_Error( 'fair.packages.metadata.failure', __( 'HTTP error code received', 'fair' ) );
172173
}
174+
175+
// Reorder sections before caching.
176+
$body = json_decode( $response['body'] );
177+
$body->sections = (array) $body->sections;
178+
$body = sort_sections_in_api( $body );
179+
$body->sections = (object) $body->sections;
180+
$response['body'] = json_encode( $body );
181+
173182
set_transient( $cache_key, $response, CACHE_LIFETIME );
174183
}
175184

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Tests for FAIR\Packages\Admin\sort_sections_in_api().
4+
*
5+
* @package FAIR
6+
*/
7+
8+
use function FAIR\Packages\Admin\sort_sections_in_api;
9+
10+
/**
11+
* Tests for FAIR\Packages\Admin\sort_sections_in_api().
12+
*
13+
* @covers FAIR\Packages\Admin\sort_sections_in_api
14+
*/
15+
class SortSectionsInApi extends WP_UnitTestCase {
16+
17+
/**
18+
* Test that sections are ordered in a predefined order.
19+
*
20+
* @dataProvider data_plugin_detail_sections
21+
*
22+
* @param array $sections Sections provided in arbitrary order, as if returned from MetadataDocument.
23+
* @param array $expected_order The sections in order we expect them to be.
24+
*/
25+
public function test_should_return_sections_in_predefined_order( array $sections, array $expected_order ) {
26+
$res = new stdClass();
27+
$res->sections = $sections;
28+
$actual = sort_sections_in_api( $res );
29+
$this->assertIsObject( $actual, 'The response is not an object.' );
30+
// $this->assertObjectHasProperty( 'sections', $actual, 'The response object has no sections.' );
31+
$this->assertSame(
32+
$expected_order,
33+
$actual->sections,
34+
'The sections were not in the expected order.'
35+
);
36+
}
37+
38+
/**
39+
* Data provider.
40+
*/
41+
public static function data_plugin_detail_sections(): array {
42+
return [
43+
'expected sections' => [
44+
'sections' => [
45+
'faq' => 'faq',
46+
'screenshots' => 'screenshots',
47+
'changelog' => 'changelog',
48+
'description' => 'description',
49+
'security' => 'security',
50+
'reviews' => 'reviews',
51+
'other_notes' => 'other_notes',
52+
'installation' => 'installation',
53+
'upgrade_notice' => 'upgrade_notice',
54+
],
55+
'expected_order' => [
56+
'description' => 'description',
57+
'installation' => 'installation',
58+
'faq' => 'faq',
59+
'screenshots' => 'screenshots',
60+
'changelog' => 'changelog',
61+
'upgrade_notice' => 'upgrade_notice',
62+
'security' => 'security',
63+
'other_notes' => 'other_notes',
64+
'reviews' => 'reviews',
65+
],
66+
],
67+
'unknown sections' => [
68+
'sections' => [
69+
'foo' => 'foo',
70+
'bar' => 'bar',
71+
'baz' => 'baz',
72+
],
73+
'expected_order' => [
74+
'foo' => 'foo',
75+
'bar' => 'bar',
76+
'baz' => 'baz',
77+
],
78+
],
79+
'expected and unknown sections' => [
80+
'sections' => [
81+
'faq' => 'faq',
82+
'foo' => 'foo',
83+
'screenshots' => 'screenshots',
84+
'changelog' => 'changelog',
85+
'bar' => 'bar',
86+
'reviews' => 'reviews',
87+
'installation' => 'installation',
88+
'security' => 'security',
89+
],
90+
'expected_order' => [
91+
'installation' => 'installation',
92+
'faq' => 'faq',
93+
'screenshots' => 'screenshots',
94+
'changelog' => 'changelog',
95+
'security' => 'security',
96+
'reviews' => 'reviews',
97+
'foo' => 'foo',
98+
'bar' => 'bar',
99+
],
100+
],
101+
'some empty sections' => [
102+
'sections' => [
103+
'faq' => '',
104+
'foo' => '',
105+
'screenshots' => 'screenshots',
106+
'changelog' => '',
107+
'bar' => '',
108+
'reviews' => 'reviews',
109+
'installation' => '',
110+
'security' => '',
111+
],
112+
'expected_order' => [
113+
'screenshots' => 'screenshots',
114+
'reviews' => 'reviews',
115+
],
116+
],
117+
'no sections' => [
118+
'sections' => [],
119+
'expected_order' => [],
120+
],
121+
];
122+
}
123+
}

0 commit comments

Comments
 (0)