Skip to content

Commit

Permalink
Add branded notices to inform users when AspireUpdate is in operation…
Browse files Browse the repository at this point in the history
… on a screen. (#142)
  • Loading branch information
costdev authored Nov 7, 2024
1 parent 59925b3 commit b0593eb
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 9 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ The plugin menu appears under the Dashboard main menu item. Don't look for it in

The plugin can use the following configuration options in wp-config.php:

| Configuration Parameter | Description | Default, if any |
| :---------------------- | ------------------------------------------------------------------------------------------: | -------------------------------------: |
| AP_ENABLE | Enable API rewrite | false |
| AP_API_KEY | The API Key for AspireCloud (not currently enforced) | |
| AP_HOST | API domain name | api.aspirecloud.org |
| AP_DEBUG | Enable Debug Mode | false |
| AP_DEBUG_TYPES | an array of debug modes | array('string', 'request', 'response') |
| AP_DISABLE_SSL | Disabled SSL verification for local testing | true |
| AP_REMOVE_UI | Disables plugin settings user interface, defaults to config parameters set in wp-config.php | false |
| Configuration Parameter | Description | Default, if any |
| :---------------------- | -------------------------------------------------------------------------------------------------------: | -------------------------------------: |
| AP_ENABLE | Enable API rewrite | false |
| AP_API_KEY | The API Key for AspireCloud (not currently enforced) | |
| AP_HOST | API domain name | api.aspirecloud.org |
| AP_DEBUG | Enable Debug Mode | false |
| AP_DEBUG_TYPES | an array of debug modes | array('string', 'request', 'response') |
| AP_DISABLE_SSL | Disabled SSL verification for local testing | true |
| AP_REMOVE_UI | Disables plugin settings user interface and branding, defaults to config parameters set in wp-config.php | false |

To set AP_DEBUG_TYPES use an array to define the constant:

Expand Down
17 changes: 17 additions & 0 deletions assets/css/aspire-update.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@
display: none;
}

.aspireupdate-notice {
background-color: #f0f6fc;
border: 1px solid #70b9e3;
border-radius: 50px;
max-width: max-content;
}

.aspireupdate-notice p::before {
content: '';
display: inline-block;
margin-inline-end: .5em;
vertical-align: middle;
background: url(../images/aspirepress-logo-icon.svg) no-repeat center center / 20px 20px;
height: 20px;
width: 20px;
}

#voltron {
color: transparent;
font-size: clamp(4px, 0.9vw, 8px);
Expand Down
1 change: 1 addition & 0 deletions assets/images/aspirepress-logo-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions includes/class-branding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* The Class for adding branding to the dashboard.
*
* @package aspire-update
*/

namespace AspireUpdate;

/**
* The Class for adding branding to the dashboard.
*/
class Branding {
/**
* Hold a single instance of the class.
*
* @var object
*/
private static $instance = null;

/**
* The Constructor.
*/
public function __construct() {
$admin_settings = Admin_Settings::get_instance();
if ( $admin_settings->get_setting( 'enable', false ) ) {
add_action( 'admin_notices', [ $this, 'output_admin_notice' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
}
}

/**
* Initialize Class.
*
* @return object
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}

/**
* Enqueue scripts and styles.
*
* @param string $hook The page identifier.
* @return void
*/
public function admin_enqueue_scripts( $hook ) {
if ( defined( 'AP_REMOVE_UI' ) && AP_REMOVE_UI ) {
return;
}

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

if ( in_array( $hook, $allowed_screens, true ) ) {
wp_enqueue_style( 'aspire_update_settings_css', plugin_dir_url( __DIR__ ) . 'assets/css/aspire-update.css', [], AP_VERSION );
}
}

/**
* Output admin notice.
*
* @return void
*/
public function output_admin_notice() {
if ( defined( 'AP_REMOVE_UI' ) && AP_REMOVE_UI ) {
return;
}

$current_screen = get_current_screen();
if ( ! $current_screen instanceof \WP_Screen ) {
return;
}

$message = '';
switch ( $current_screen->base ) {
case 'plugins':
case 'plugin-install':
$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' ),
'AspireUpdate',
__( 'https://docs.aspirepress.org/aspireupdate/', 'AspireUpdate' )
);
break;
case 'themes':
case 'theme-install':
$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' ),
'AspireUpdate',
__( 'https://docs.aspirepress.org/aspireupdate/', 'AspireUpdate' )
);
break;
case 'update-core':
$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' ),
'AspireUpdate',
__( 'https://docs.aspirepress.org/aspireupdate/', 'AspireUpdate' )
);
break;
}

if ( '' === $message ) {
return;
}

echo wp_kses_post( '<div class="notice aspireupdate-notice notice-info"><p>' . $message . '</p></div>' );
}
}
1 change: 1 addition & 0 deletions includes/class-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct() {
Admin_Settings::get_instance();
Plugins_Screens::get_instance();
Themes_Screens::get_instance();
Branding::get_instance();
$this->api_rewrite();

add_action( 'init', [ $this, 'load_textdomain' ] );
Expand Down
18 changes: 18 additions & 0 deletions languages/AspireUpdate.pot
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ msgstr ""

#. Author URI of the plugin
#: aspire-update.php
#: includes/class-branding.php:91
#: includes/class-branding.php:100
#: includes/class-branding.php:108
msgid "https://docs.aspirepress.org/aspireupdate/"
msgstr ""

Expand Down Expand Up @@ -126,3 +129,18 @@ msgstr ""
#: includes/class-admin-settings.php:609
msgid "Generate API Key"
msgstr ""

#. translators: 1: The name of the plugin, 2: The documentation URL.
#: includes/class-branding.php:89
msgid "Your plugin updates are now powered by <strong>%1$s</strong>. <a href=\"%2$s\">Learn more</a>"
msgstr ""

#. translators: 1: The name of the plugin, 2: The documentation URL.
#: includes/class-branding.php:98
msgid "Your theme updates are now powered by <strong>%1$s</strong>. <a href=\"%2$s\">Learn more</a>"
msgstr ""

#. translators: 1: The name of the plugin, 2: The documentation URL.
#: includes/class-branding.php:106
msgid "Your WordPress, plugin, theme and translation updates are now powered by <strong>%1$s</strong>. <a href=\"%2$s\">Learn more</a>"
msgstr ""
104 changes: 104 additions & 0 deletions tests/Branding/Branding_AdminEnqueueScriptsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Class Branding_AdminEnqueueScriptsTest
*
* @package AspireUpdate
*/

/**
* Tests for Branding::admin_enqueue_scripts()
*
* @covers \AspireUpdate\Branding::admin_enqueue_scripts
*/
class Branding_AdminEnqueueScriptsTest extends WP_UnitTestCase {
/**
* Dequeue the stylesheet after each test runs.
*
* @return void
*/
public function tear_down() {
wp_dequeue_style( 'aspire_update_settings_css' );
}

/**
* Test that the stylesheet is enqueued on certain screens.
*
* @dataProvider data_hooks
*
* @param string $hook The current screen's hook.
*/
public function test_should_enqueue_style_on_certain_screens( $hook ) {
$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( $hook );
$this->assertTrue( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_hooks() {
return self::text_array_to_dataprovider(
[
'update-core.php',
'plugins.php',
'plugin-install.php',
'themes.php',
'theme-install.php',
]
);
}

/**
* Test that the stylesheet is not enqueued on adjacent screens.
*
* @dataProvider data_adjacent_screens
*
* @param string $hook The current screen's hook.
*/
public function test_should_not_enqueue_style_on_adjacent_screens( $hook ) {
$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( $hook );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_adjacent_screens() {
return self::text_array_to_dataprovider(
[
'index.php',
'nav-menus.php',
'plugin-editor.php',
]
);
}

/**
* Test that the stylesheet is not enqueued when there is no screen.
*/
public function test_should_not_enqueue_style_when_there_is_no_screen() {
$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( '' );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}

/**
* Test that the stylesheet is not enqueued when AP_REMOVE_UI is set to true.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_should_not_enqueue_style_when_ap_remove_ui_is_true() {
// Prevent the notice from being displayed.
define( 'AP_REMOVE_UI', true );

$branding = new AspireUpdate\Branding();
$branding->admin_enqueue_scripts( 'plugins.php' );
$this->assertFalse( wp_style_is( 'aspire_update_settings_css' ) );
}
}
67 changes: 67 additions & 0 deletions tests/Branding/Branding_ConstructTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Class Branding_ConstructTest
*
* @package AspireUpdate
*/

/**
* Tests for Branding::__construct()
*
* @covers \AspireUpdate\Branding::__construct
*/
class Branding_ConstructTest extends WP_UnitTestCase {
/**
* Test that hooks are added when API rewriting is enabled.
*
* @dataProvider data_hooks_and_methods
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_add_hooks( $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.
*
* @dataProvider data_hooks_and_methods
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_not_add_hooks( $hook, $method ) {
define( 'AP_ENABLE', false );

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

/**
* Data provider.
*
* @return array[]
*/
public function data_hooks_and_methods() {
return [
'admin_notices -> output_admin_notice' => [
'hook' => 'admin_notices',
'method' => 'output_admin_notice',
],
'admin_enqueue_scripts -> admin_enqueue_scripts' => [
'hook' => 'admin_enqueue_scripts',
'method' => 'admin_enqueue_scripts',
],
];
}
}
Loading

0 comments on commit b0593eb

Please sign in to comment.