Skip to content

Commit 6a7e120

Browse files
authored
Branding: Add multisite support. (#160)
1 parent 3ffcbb0 commit 6a7e120

8 files changed

+197
-41
lines changed

.github/workflows/phpunit-tests.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ on:
1111

1212
jobs:
1313
phpunit:
14-
name: Run tests
14+
name: Run tests (PHP ${{ matrix.php-version }}, ${{ matrix.multisite && 'Multisite' || 'Single Site' }})
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
1818
php-version: ['7.4', '8.3']
19+
multisite: [ true, false ]
1920
services:
2021
database:
2122
image: mysql:latest
@@ -38,4 +39,4 @@ jobs:
3839
run: bash bin/install-wp-tests.sh wordpress_tests root root 127.0.0.1 latest true
3940

4041
- name: Run tests
41-
run: phpunit
42+
run: XDEBUG_MODE=off phpunit${{ matrix.multisite && ' -c tests/multisite.xml' || '' }}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"scripts": {
2727
"format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source",
2828
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary, source",
29-
"test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit" ]
29+
"test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit" ],
30+
"test:multisite": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit -c tests/multisite.xml" ]
3031
}
3132

3233

includes/class-branding.php

+27-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class Branding {
2424
public function __construct() {
2525
$admin_settings = Admin_Settings::get_instance();
2626
if ( $admin_settings->get_setting( 'enable', false ) ) {
27-
add_action( 'admin_notices', [ $this, 'output_admin_notice' ] );
27+
$admin_notices_hook = is_multisite() ? 'network_admin_notices' : 'admin_notices';
28+
add_action( $admin_notices_hook, [ $this, 'output_admin_notice' ] );
2829
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
2930
}
3031
}
@@ -53,14 +54,15 @@ public function admin_enqueue_scripts( $hook ) {
5354
}
5455

5556
$allowed_screens = [
56-
'update-core.php',
57-
'plugins.php',
58-
'plugin-install.php',
59-
'themes.php',
60-
'theme-install.php',
57+
'update-core',
58+
'plugins',
59+
'plugin-install',
60+
'themes',
61+
'theme-install',
6162
];
6263

63-
if ( in_array( $hook, $allowed_screens, true ) ) {
64+
$screen = \WP_Screen::get( $hook );
65+
if ( in_array( $screen->id, $allowed_screens, true ) ) {
6466
wp_enqueue_style( 'aspire_update_settings_css', plugin_dir_url( __DIR__ ) . 'assets/css/aspire-update.css', [], AP_VERSION );
6567
}
6668
}
@@ -81,9 +83,15 @@ public function output_admin_notice() {
8183
}
8284

8385
$message = '';
84-
switch ( $current_screen->base ) {
86+
switch ( $current_screen->id ) {
8587
case 'plugins':
8688
case 'plugin-install':
89+
if ( is_multisite() ) {
90+
break;
91+
}
92+
// Fall-through.
93+
case 'plugins-network':
94+
case 'plugin-install-network':
8795
$message = sprintf(
8896
/* translators: 1: The name of the plugin, 2: The documentation URL. */
8997
__( 'Your plugin updates are now powered by <strong>%1$s</strong>. <a href="%2$s">Learn more</a>', 'AspireUpdate' ),
@@ -93,6 +101,12 @@ public function output_admin_notice() {
93101
break;
94102
case 'themes':
95103
case 'theme-install':
104+
if ( is_multisite() ) {
105+
break;
106+
}
107+
// Fall-through.
108+
case 'themes-network':
109+
case 'theme-install-network':
96110
$message = sprintf(
97111
/* translators: 1: The name of the plugin, 2: The documentation URL. */
98112
__( 'Your theme updates are now powered by <strong>%1$s</strong>. <a href="%2$s">Learn more</a>', 'AspireUpdate' ),
@@ -101,6 +115,11 @@ public function output_admin_notice() {
101115
);
102116
break;
103117
case 'update-core':
118+
if ( is_multisite() ) {
119+
break;
120+
}
121+
// Fall-through.
122+
case 'update-core-network':
104123
$message = sprintf(
105124
/* translators: 1: The name of the plugin, 2: The documentation URL. */
106125
__( 'Your WordPress, plugin, theme and translation updates are now powered by <strong>%1$s</strong>. <a href="%2$s">Learn more</a>', 'AspireUpdate' ),

phpunit.xml.dist

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<directory suffix="Test.php">./tests/</directory>
1515
</testsuite>
1616
</testsuites>
17+
<groups>
18+
<exclude>
19+
<group>ms-required</group>
20+
</exclude>
21+
</groups>
1722
<coverage includeUncoveredFiles="true" processUncoveredFiles="false" pathCoverage="true" cacheDirectory="./tests/cache">
1823
<include>
1924
<file>./aspire-update.php</file>

tests/Branding/Branding_AdminEnqueueScriptsTest.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public function test_should_enqueue_style_on_certain_screens( $hook ) {
4141
public function data_hooks() {
4242
return self::text_array_to_dataprovider(
4343
[
44-
'update-core.php',
45-
'plugins.php',
46-
'plugin-install.php',
47-
'themes.php',
48-
'theme-install.php',
44+
'update-core',
45+
'plugins',
46+
'plugin-install',
47+
'themes',
48+
'theme-install',
4949
]
5050
);
5151
}
@@ -58,6 +58,10 @@ public function data_hooks() {
5858
* @param string $hook The current screen's hook.
5959
*/
6060
public function test_should_not_enqueue_style_on_adjacent_screens( $hook ) {
61+
if ( is_multisite() ) {
62+
$hook .= '-network';
63+
}
64+
6165
$branding = new AspireUpdate\Branding();
6266
$branding->admin_enqueue_scripts( $hook );
6367
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
@@ -71,9 +75,9 @@ public function test_should_not_enqueue_style_on_adjacent_screens( $hook ) {
7175
public function data_adjacent_screens() {
7276
return self::text_array_to_dataprovider(
7377
[
74-
'index.php',
75-
'nav-menus.php',
76-
'plugin-editor.php',
78+
'dashboard',
79+
'nav-menus',
80+
'plugin-editor',
7781
]
7882
);
7983
}
@@ -97,8 +101,9 @@ public function test_should_not_enqueue_style_when_ap_remove_ui_is_true() {
97101
// Prevent the notice from being displayed.
98102
define( 'AP_REMOVE_UI', true );
99103

104+
$hook = is_multisite() ? 'plugins-network' : 'plugins';
100105
$branding = new AspireUpdate\Branding();
101-
$branding->admin_enqueue_scripts( 'plugins.php' );
106+
$branding->admin_enqueue_scripts( $hook );
102107
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
103108
}
104109
}

tests/Branding/Branding_ConstructTest.php

+69-7
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,39 @@
1212
*/
1313
class Branding_ConstructTest extends WP_UnitTestCase {
1414
/**
15-
* Test that hooks are added when API rewriting is enabled.
15+
* Test that hooks are added when API rewriting is enabled in single site.
1616
*
17-
* @dataProvider data_hooks_and_methods
17+
* @dataProvider data_single_site_hooks_and_methods
18+
*
19+
* @group ms-excluded
1820
*
1921
* @runInSeparateProcess
2022
* @preserveGlobalState disabled
2123
*
2224
* @string $hook The hook's name.
2325
* @string $method The method to hook.
2426
*/
25-
public function test_should_add_hooks( $hook, $method ) {
27+
public function test_should_add_hooks_in_single_site( $hook, $method ) {
2628
define( 'AP_ENABLE', true );
2729

2830
$branding = new AspireUpdate\Branding();
2931
$this->assertIsInt( has_action( $hook, [ $branding, $method ] ) );
3032
}
3133

3234
/**
33-
* Test that hooks are not added when API rewriting is disabled.
35+
* Test that hooks are not added when API rewriting is disabled in single-site.
36+
*
37+
* @dataProvider data_single_site_hooks_and_methods
3438
*
35-
* @dataProvider data_hooks_and_methods
39+
* @group ms-excluded
3640
*
3741
* @runInSeparateProcess
3842
* @preserveGlobalState disabled
3943
*
4044
* @string $hook The hook's name.
4145
* @string $method The method to hook.
4246
*/
43-
public function test_should_not_add_hooks( $hook, $method ) {
47+
public function test_should_not_add_hooks_in_single_site( $hook, $method ) {
4448
define( 'AP_ENABLE', false );
4549

4650
$branding = new AspireUpdate\Branding();
@@ -52,7 +56,7 @@ public function test_should_not_add_hooks( $hook, $method ) {
5256
*
5357
* @return array[]
5458
*/
55-
public function data_hooks_and_methods() {
59+
public function data_single_site_hooks_and_methods() {
5660
return [
5761
'admin_notices -> output_admin_notice' => [
5862
'hook' => 'admin_notices',
@@ -64,4 +68,62 @@ public function data_hooks_and_methods() {
6468
],
6569
];
6670
}
71+
72+
/**
73+
* Test that hooks are added when API rewriting is enabled in multisite.
74+
*
75+
* @dataProvider data_multisite_hooks_and_methods
76+
*
77+
* @group ms-required
78+
*
79+
* @runInSeparateProcess
80+
* @preserveGlobalState disabled
81+
*
82+
* @string $hook The hook's name.
83+
* @string $method The method to hook.
84+
*/
85+
public function test_should_add_hooks_in_multisite( $hook, $method ) {
86+
define( 'AP_ENABLE', true );
87+
88+
$branding = new AspireUpdate\Branding();
89+
$this->assertIsInt( has_action( $hook, [ $branding, $method ] ) );
90+
}
91+
92+
/**
93+
* Test that hooks are not added when API rewriting is disabled in multisite.
94+
*
95+
* @dataProvider data_multisite_hooks_and_methods
96+
*
97+
* @group ms-required
98+
*
99+
* @runInSeparateProcess
100+
* @preserveGlobalState disabled
101+
*
102+
* @string $hook The hook's name.
103+
* @string $method The method to hook.
104+
*/
105+
public function test_should_not_add_hooks_in_multisite( $hook, $method ) {
106+
define( 'AP_ENABLE', false );
107+
108+
$branding = new AspireUpdate\Branding();
109+
$this->assertFalse( has_action( $hook, [ $branding, $method ] ) );
110+
}
111+
112+
/**
113+
* Data provider.
114+
*
115+
* @return array[]
116+
*/
117+
public function data_multisite_hooks_and_methods() {
118+
return [
119+
'network_admin_notices -> output_admin_notice' => [
120+
'hook' => 'network_admin_notices',
121+
'method' => 'output_admin_notice',
122+
],
123+
'admin_enqueue_scripts -> admin_enqueue_scripts' => [
124+
'hook' => 'admin_enqueue_scripts',
125+
'method' => 'admin_enqueue_scripts',
126+
],
127+
];
128+
}
67129
}

tests/Branding/Branding_OutputAdminNoticeTest.php

+36-14
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,56 @@ class Branding_OutputAdminNoticeTest extends WP_UnitTestCase {
2020
* @param string $expected The expected substring to find.
2121
*/
2222
public function test_should_output_admin_notice( $hook, $expected ) {
23+
if ( is_multisite() ) {
24+
$hook .= '-network';
25+
}
2326
set_current_screen( $hook );
2427

2528
$branding = new AspireUpdate\Branding();
2629
$this->assertStringContainsString( $expected, get_echo( [ $branding, 'output_admin_notice' ] ) );
2730
}
2831

32+
/**
33+
* Test that no admin notice is output on adjacent screens.
34+
*
35+
* @dataProvider data_screen_specific_messages
36+
*
37+
* @group ms-required
38+
*
39+
* @param string $hook The current screen's hook.
40+
*/
41+
public function test_should_not_output_notice_on_single_site_screens_in_multisite( $hook ) {
42+
set_current_screen( $hook );
43+
44+
$branding = new AspireUpdate\Branding();
45+
$this->assertSame( '', get_echo( [ $branding, 'output_admin_notice' ] ) );
46+
}
47+
2948
/**
3049
* Data provider.
3150
*
3251
* @return array[]
3352
*/
3453
public function data_screen_specific_messages() {
3554
return [
36-
'update-core.php' => [
37-
'hook' => 'update-core.php',
55+
'update-core' => [
56+
'hook' => 'update-core',
3857
'expected' => 'WordPress, plugin, theme and translation updates',
3958
],
40-
'plugins.php' => [
41-
'hook' => 'plugins.php',
59+
'plugins' => [
60+
'hook' => 'plugins',
4261
'expected' => 'plugin updates',
4362
],
44-
'plugin-install.php' => [
45-
'hook' => 'plugin-install.php',
63+
'plugin-install' => [
64+
'hook' => 'plugin-install',
4665
'expected' => 'plugin updates',
4766
],
48-
'themes.php' => [
49-
'hook' => 'themes.php',
67+
'themes' => [
68+
'hook' => 'themes',
5069
'expected' => 'theme updates',
5170
],
52-
'theme-install.php' => [
53-
'hook' => 'theme-install.php',
71+
'theme-install' => [
72+
'hook' => 'theme-install',
5473
'expected' => 'theme updates',
5574
],
5675
];
@@ -64,6 +83,9 @@ public function data_screen_specific_messages() {
6483
* @param string $hook The current screen's hook.
6584
*/
6685
public function test_should_not_output_notice_on_adjacent_screens( $hook ) {
86+
if ( is_multisite() ) {
87+
$hook .= '-network';
88+
}
6789
set_current_screen( $hook );
6890

6991
$branding = new AspireUpdate\Branding();
@@ -78,9 +100,9 @@ public function test_should_not_output_notice_on_adjacent_screens( $hook ) {
78100
public function data_adjacent_screens() {
79101
return self::text_array_to_dataprovider(
80102
[
81-
'index.php',
82-
'nav-menus.php',
83-
'plugin-editor.php',
103+
'dashboard',
104+
'nav-menus',
105+
'plugin-editor',
84106
]
85107
);
86108
}
@@ -108,7 +130,7 @@ public function test_should_not_output_notice_when_there_is_no_screen() {
108130
*/
109131
public function test_should_not_output_notice_when_ap_remove_ui_is_true() {
110132
// Set to a screen that should display an admin notice.
111-
set_current_screen( 'plugins.php' );
133+
set_current_screen( is_multisite() ? 'plugins-network' : 'plugins' );
112134

113135
// Prevent the notice from being displayed.
114136
define( 'AP_REMOVE_UI', true );

0 commit comments

Comments
 (0)