Skip to content

Commit

Permalink
Branding: Add multisite support. (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
costdev authored Nov 8, 2024
1 parent 3ffcbb0 commit 6a7e120
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 41 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/phpunit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ on:

jobs:
phpunit:
name: Run tests
name: Run tests (PHP ${{ matrix.php-version }}, ${{ matrix.multisite && 'Multisite' || 'Single Site' }})
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['7.4', '8.3']
multisite: [ true, false ]
services:
database:
image: mysql:latest
Expand All @@ -38,4 +39,4 @@ jobs:
run: bash bin/install-wp-tests.sh wordpress_tests root root 127.0.0.1 latest true

- name: Run tests
run: phpunit
run: XDEBUG_MODE=off phpunit${{ matrix.multisite && ' -c tests/multisite.xml' || '' }}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"scripts": {
"format": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf --report=summary,source",
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary, source",
"test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit" ]
"test": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit" ],
"test:multisite": [ "Composer\\Config::disableProcessTimeout", "@php ./vendor/phpunit/phpunit/phpunit -c tests/multisite.xml" ]
}


Expand Down
35 changes: 27 additions & 8 deletions includes/class-branding.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Branding {
public function __construct() {
$admin_settings = Admin_Settings::get_instance();
if ( $admin_settings->get_setting( 'enable', false ) ) {
add_action( 'admin_notices', [ $this, 'output_admin_notice' ] );
$admin_notices_hook = is_multisite() ? 'network_admin_notices' : 'admin_notices';
add_action( $admin_notices_hook, [ $this, 'output_admin_notice' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
}
}
Expand Down Expand Up @@ -53,14 +54,15 @@ public function admin_enqueue_scripts( $hook ) {
}

$allowed_screens = [
'update-core.php',
'plugins.php',
'plugin-install.php',
'themes.php',
'theme-install.php',
'update-core',
'plugins',
'plugin-install',
'themes',
'theme-install',
];

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

$message = '';
switch ( $current_screen->base ) {
switch ( $current_screen->id ) {
case 'plugins':
case 'plugin-install':
if ( is_multisite() ) {
break;
}
// Fall-through.
case 'plugins-network':
case 'plugin-install-network':
$message = sprintf(
/* translators: 1: The name of the plugin, 2: The documentation URL. */
__( 'Your plugin updates are now powered by <strong>%1$s</strong>. <a href="%2$s">Learn more</a>', 'AspireUpdate' ),
Expand All @@ -93,6 +101,12 @@ public function output_admin_notice() {
break;
case 'themes':
case 'theme-install':
if ( is_multisite() ) {
break;
}
// Fall-through.
case 'themes-network':
case 'theme-install-network':
$message = sprintf(
/* translators: 1: The name of the plugin, 2: The documentation URL. */
__( 'Your theme updates are now powered by <strong>%1$s</strong>. <a href="%2$s">Learn more</a>', 'AspireUpdate' ),
Expand All @@ -101,6 +115,11 @@ public function output_admin_notice() {
);
break;
case 'update-core':
if ( is_multisite() ) {
break;
}
// Fall-through.
case 'update-core-network':
$message = sprintf(
/* translators: 1: The name of the plugin, 2: The documentation URL. */
__( 'Your WordPress, plugin, theme and translation updates are now powered by <strong>%1$s</strong>. <a href="%2$s">Learn more</a>', 'AspireUpdate' ),
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>ms-required</group>
</exclude>
</groups>
<coverage includeUncoveredFiles="true" processUncoveredFiles="false" pathCoverage="true" cacheDirectory="./tests/cache">
<include>
<file>./aspire-update.php</file>
Expand Down
23 changes: 14 additions & 9 deletions tests/Branding/Branding_AdminEnqueueScriptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public function test_should_enqueue_style_on_certain_screens( $hook ) {
public function data_hooks() {
return self::text_array_to_dataprovider(
[
'update-core.php',
'plugins.php',
'plugin-install.php',
'themes.php',
'theme-install.php',
'update-core',
'plugins',
'plugin-install',
'themes',
'theme-install',
]
);
}
Expand All @@ -58,6 +58,10 @@ public function data_hooks() {
* @param string $hook The current screen's hook.
*/
public function test_should_not_enqueue_style_on_adjacent_screens( $hook ) {
if ( is_multisite() ) {
$hook .= '-network';
}

$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( $hook );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
Expand All @@ -71,9 +75,9 @@ public function test_should_not_enqueue_style_on_adjacent_screens( $hook ) {
public function data_adjacent_screens() {
return self::text_array_to_dataprovider(
[
'index.php',
'nav-menus.php',
'plugin-editor.php',
'dashboard',
'nav-menus',
'plugin-editor',
]
);
}
Expand All @@ -97,8 +101,9 @@ public function test_should_not_enqueue_style_when_ap_remove_ui_is_true() {
// Prevent the notice from being displayed.
define( 'AP_REMOVE_UI', true );

$hook = is_multisite() ? 'plugins-network' : 'plugins';
$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( 'plugins.php' );
$branding->admin_enqueue_scripts( $hook );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}
}
76 changes: 69 additions & 7 deletions tests/Branding/Branding_ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,39 @@
*/
class Branding_ConstructTest extends WP_UnitTestCase {
/**
* Test that hooks are added when API rewriting is enabled.
* Test that hooks are added when API rewriting is enabled in single site.
*
* @dataProvider data_hooks_and_methods
* @dataProvider data_single_site_hooks_and_methods
*
* @group ms-excluded
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_add_hooks( $hook, $method ) {
public function test_should_add_hooks_in_single_site( $hook, $method ) {
define( 'AP_ENABLE', true );

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

/**
* Test that hooks are not added when API rewriting is disabled.
* Test that hooks are not added when API rewriting is disabled in single-site.
*
* @dataProvider data_single_site_hooks_and_methods
*
* @dataProvider data_hooks_and_methods
* @group ms-excluded
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_not_add_hooks( $hook, $method ) {
public function test_should_not_add_hooks_in_single_site( $hook, $method ) {
define( 'AP_ENABLE', false );

$branding = new AspireUpdate\Branding();
Expand All @@ -52,7 +56,7 @@ public function test_should_not_add_hooks( $hook, $method ) {
*
* @return array[]
*/
public function data_hooks_and_methods() {
public function data_single_site_hooks_and_methods() {
return [
'admin_notices -> output_admin_notice' => [
'hook' => 'admin_notices',
Expand All @@ -64,4 +68,62 @@ public function data_hooks_and_methods() {
],
];
}

/**
* Test that hooks are added when API rewriting is enabled in multisite.
*
* @dataProvider data_multisite_hooks_and_methods
*
* @group ms-required
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_add_hooks_in_multisite( $hook, $method ) {
define( 'AP_ENABLE', true );

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

/**
* Test that hooks are not added when API rewriting is disabled in multisite.
*
* @dataProvider data_multisite_hooks_and_methods
*
* @group ms-required
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_not_add_hooks_in_multisite( $hook, $method ) {
define( 'AP_ENABLE', false );

$branding = new AspireUpdate\Branding();
$this->assertFalse( has_action( $hook, [ $branding, $method ] ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_multisite_hooks_and_methods() {
return [
'network_admin_notices -> output_admin_notice' => [
'hook' => 'network_admin_notices',
'method' => 'output_admin_notice',
],
'admin_enqueue_scripts -> admin_enqueue_scripts' => [
'hook' => 'admin_enqueue_scripts',
'method' => 'admin_enqueue_scripts',
],
];
}
}
50 changes: 36 additions & 14 deletions tests/Branding/Branding_OutputAdminNoticeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,56 @@ class Branding_OutputAdminNoticeTest extends WP_UnitTestCase {
* @param string $expected The expected substring to find.
*/
public function test_should_output_admin_notice( $hook, $expected ) {
if ( is_multisite() ) {
$hook .= '-network';
}
set_current_screen( $hook );

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

/**
* Test that no admin notice is output on adjacent screens.
*
* @dataProvider data_screen_specific_messages
*
* @group ms-required
*
* @param string $hook The current screen's hook.
*/
public function test_should_not_output_notice_on_single_site_screens_in_multisite( $hook ) {
set_current_screen( $hook );

$branding = new AspireUpdate\Branding();
$this->assertSame( '', get_echo( [ $branding, 'output_admin_notice' ] ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_screen_specific_messages() {
return [
'update-core.php' => [
'hook' => 'update-core.php',
'update-core' => [
'hook' => 'update-core',
'expected' => 'WordPress, plugin, theme and translation updates',
],
'plugins.php' => [
'hook' => 'plugins.php',
'plugins' => [
'hook' => 'plugins',
'expected' => 'plugin updates',
],
'plugin-install.php' => [
'hook' => 'plugin-install.php',
'plugin-install' => [
'hook' => 'plugin-install',
'expected' => 'plugin updates',
],
'themes.php' => [
'hook' => 'themes.php',
'themes' => [
'hook' => 'themes',
'expected' => 'theme updates',
],
'theme-install.php' => [
'hook' => 'theme-install.php',
'theme-install' => [
'hook' => 'theme-install',
'expected' => 'theme updates',
],
];
Expand All @@ -64,6 +83,9 @@ public function data_screen_specific_messages() {
* @param string $hook The current screen's hook.
*/
public function test_should_not_output_notice_on_adjacent_screens( $hook ) {
if ( is_multisite() ) {
$hook .= '-network';
}
set_current_screen( $hook );

$branding = new AspireUpdate\Branding();
Expand All @@ -78,9 +100,9 @@ public function test_should_not_output_notice_on_adjacent_screens( $hook ) {
public function data_adjacent_screens() {
return self::text_array_to_dataprovider(
[
'index.php',
'nav-menus.php',
'plugin-editor.php',
'dashboard',
'nav-menus',
'plugin-editor',
]
);
}
Expand Down Expand Up @@ -108,7 +130,7 @@ public function test_should_not_output_notice_when_there_is_no_screen() {
*/
public function test_should_not_output_notice_when_ap_remove_ui_is_true() {
// Set to a screen that should display an admin notice.
set_current_screen( 'plugins.php' );
set_current_screen( is_multisite() ? 'plugins-network' : 'plugins' );

// Prevent the notice from being displayed.
define( 'AP_REMOVE_UI', true );
Expand Down
Loading

0 comments on commit 6a7e120

Please sign in to comment.