Skip to content

Commit

Permalink
Implement true oop in Admin Notices
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-mendonca committed Jul 12, 2024
1 parent 4997d16 commit b104ee6
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 48 deletions.
2 changes: 1 addition & 1 deletion includes/class-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function activate_notice() {
'dismissible' => true,
'message' => $activation_message,
);
Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );

// Delete transient, only display this notice once.
delete_transient( 'translation_stats_activate' );
Expand Down
159 changes: 121 additions & 38 deletions includes/class-admin-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,78 +24,161 @@ class Admin_Notice {


/**
* Display formatted admin notice.
* WordPress core notice types: 'error', 'warning', 'warning-spin', 'success' or 'info'. Defaults to none.
*
* WordPress core notice types ( 'error', 'warning', 'warning-spin', 'success' and 'info' ).
* The child type 'warning-spin' is the spinning variation of main 'warning' icon (if 'update-icon' is set to 'true'). The css class will be kept the parent 'warning'.
* Use 'force_show' => true to ignore the 'show_warnings' setting.
* @var string
*/
public $type;

/**
* Show message alternative color scheme with class 'notice-alt': true or false. Defaults no false.
*
* @since 0.8.0
* @since 0.9.5 Array with all the notice data.
* @since 1.1.1 Renamed from tstats_notice_message() to notice_message().
* @since 1.2.0 Renamed from notice_message() to message().
* Added support for 'wrap' properties, defaults to 'p' tag for backwards compatibility.
* @var bool
*/
public $notice_alt = false;

/**
* Is inline. Defaults to true.
*
* @param array $args Array of message data.
* @var bool
*/
public $inline = true;

/**
* Is dismissible. Defaults to false.
*
* @return void
* @var bool
*/
public $dismissible = false;

/**
* Array some extra CSS classes.
*
* @var array
*/
public static function message( $args ) {
public $css_class = array();

/**
* Show update message icons. Defaults to false.
*
* @var bool
*/
public $update_icon = false;

/**
* Message to show.
*
* @var string
*/
public $message;

/**
* HTML tag of the message wrapper. Defaults to 'p' (<p>... </p>).
*
* @var string
*/
public $wrap = 'p';

/**
* Some extra HTML to show.
*
* @var string
*/
public $extra_html;


/**
* Constructor.
*
* @param array $args Array of message data.
*/
public function __construct( $args ) {

// Prepare all the admin notice fields.
$this->prepare_notice( $args );

// Check if 'show_warnings' is true.
$wp_option = get_option( TRANSLATION_STATS_WP_OPTION );
if ( empty( $wp_option['settings']['show_warnings'] ) && empty( $args['force_show'] ) ) {
return;
}

// Use defaults if properties not set.
$notice = array(
'type' => isset( $args['type'] ) ? ' notice-' . $args['type'] : '', // WordPress core notice types: 'error', 'warning', 'warning-spin', 'success' or 'info'. Defaults to none.
'notice-alt' => isset( $args['notice-alt'] ) && $args['notice-alt'] ? ' notice-alt' : '', // Show message alternative color scheme with class 'notice-alt': true or false. Defaults no false.
'inline' => isset( $args['inline'] ) && ! $args['inline'] ? '' : ' inline', // Defaults to true.
'dismissible' => isset( $args['dismissible'] ) && $args['dismissible'] ? ' is-dismissible' : '', // Defaults to false.
'css-class' => isset( $args['css-class'] ) ? ' ' . $args['css-class'] : '', // Some extra CSS classes.
'update-icon' => isset( $args['update-icon'] ) && $args['update-icon'] ? true : '', // Show update message icons. Defaults to false.
'message' => isset( $args['message'] ) ? $args['message'] : '', // Message to show.
'wrap' => isset( $args['wrap'] ) && self::is_supported( $args['wrap'] ) ? $args['wrap'] : 'p', // HTML tag to wrap the message. Defaults to 'p' (paragraph).
'extra-html' => isset( $args['extra-html'] ) ? $args['extra-html'] : '', // Some extra HTML to show.
);
// Render the Admin Notice.
$this->render_notice();
}

if ( $notice['update-icon'] ) {
switch ( $args['type'] ) {

/**
* Prepare all the admin notice fields.
*
* @since 1.3.2
*
* @param array $args Array of message data.
*/
public function prepare_notice( $args ) {

Check failure on line 118 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Method Translation_Stats\Admin_Notice::prepare_notice() has no return type specified.

Check failure on line 118 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Method Translation_Stats\Admin_Notice::prepare_notice() has no return type specified.

Check failure on line 118 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Method Translation_Stats\Admin_Notice::prepare_notice() has no return type specified.

Check failure on line 118 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Method Translation_Stats\Admin_Notice::prepare_notice() has no return type specified.

Check failure on line 118 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Method Translation_Stats\Admin_Notice::prepare_notice() has no return type specified.

// Use defaults if properties not set.
// TODO: Sanitize fields.
$this->type = isset( $args['type'] ) ? ' notice-' . $args['type'] : '';
$this->notice_alt = isset( $args['notice-alt'] ) && $args['notice-alt'] ? ' notice-alt' : '';

Check failure on line 123 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$notice_alt (bool) does not accept string.

Check failure on line 123 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$notice_alt (bool) does not accept string.

Check failure on line 123 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$notice_alt (bool) does not accept string.

Check failure on line 123 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$notice_alt (bool) does not accept string.

Check failure on line 123 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$notice_alt (bool) does not accept string.
$this->inline = isset( $args['inline'] ) && ! $args['inline'] ? '' : ' inline';

Check failure on line 124 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$inline (bool) does not accept string.

Check failure on line 124 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$inline (bool) does not accept string.

Check failure on line 124 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$inline (bool) does not accept string.

Check failure on line 124 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$inline (bool) does not accept string.

Check failure on line 124 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$inline (bool) does not accept string.
$this->dismissible = isset( $args['dismissible'] ) && $args['dismissible'] ? ' is-dismissible' : '';

Check failure on line 125 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$dismissible (bool) does not accept string.

Check failure on line 125 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$dismissible (bool) does not accept string.

Check failure on line 125 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$dismissible (bool) does not accept string.

Check failure on line 125 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$dismissible (bool) does not accept string.

Check failure on line 125 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$dismissible (bool) does not accept string.
$this->css_class = isset( $args['css-class'] ) ? ' ' . $args['css-class'] : '';

Check failure on line 126 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$css_class (array) does not accept string.

Check failure on line 126 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$css_class (array) does not accept string.

Check failure on line 126 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$css_class (array) does not accept string.

Check failure on line 126 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$css_class (array) does not accept string.

Check failure on line 126 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$css_class (array) does not accept string.
if ( isset( $args['update-icon'] ) && $args['update-icon'] ) {
switch ( $this->type ) {
case 'error':
$notice['update-icon'] = ' update-message'; // Error icon.
$this->update_icon = ' update-message'; // Error icon.

Check failure on line 130 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 130 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 130 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 130 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 130 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.
break;
case 'warning':
$notice['update-icon'] = ' update-message'; // Update icon.
$this->update_icon = ' update-message'; // Update icon.

Check failure on line 133 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 133 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 133 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 133 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 133 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.
break;
case 'warning-spin':
$notice['update-icon'] = ' updating-message'; // Spins the update icon.
$notice['type'] = ' notice-warning'; // Set the notice type to the default parent 'warning' class.
$this->update_icon = ' updating-message'; // Spins the update icon.

Check failure on line 136 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 136 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 136 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 136 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 136 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.
$this->type = ' notice-warning'; // Set the notice type to the default parent 'warning' class.
break;
case 'success':
$notice['update-icon'] = ' updated-message'; // Updated icon (check mark).
$this->update_icon = ' updated-message'; // Updated icon (check mark).

Check failure on line 140 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 140 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 140 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 140 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 140 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.
break;
case 'info':
$notice['update-icon'] = ''; // No icon.
$this->update_icon = ''; // No icon.

Check failure on line 143 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.2)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 143 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.1)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 143 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.0)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 143 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 8.3)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.

Check failure on line 143 in includes/class-admin-notice.php

View workflow job for this annotation

GitHub Actions / Static Analysis (PHP 7.4)

Property Translation_Stats\Admin_Notice::$update_icon (bool) does not accept string.
break;
default:
$notice['update-icon'] = ''; // Defaults to none.
$this->update_icon = ''; // Defaults to none.
break;
}
}
$this->message = isset( $args['message'] ) ? $args['message'] : '';
$this->wrap = isset( $args['wrap'] ) && self::is_supported( $args['wrap'] ) ? $args['wrap'] : 'p';
$this->extra_html = isset( $args['extra-html'] ) ? $args['extra-html'] : '';
}


/**
* Display formatted admin notice.
*
* WordPress core notice types ( 'error', 'warning', 'warning-spin', 'success' and 'info' ).
* The child type 'warning-spin' is the spinning variation of main 'warning' icon (if 'update-icon' is set to 'true'). The css class will be kept the parent 'warning'.
* Use 'force_show' => true to ignore the 'show_warnings' setting.
*
* @since 1.3.1
*
* @return void
*/
public function render_notice() {

// TODO: return the admin notice.

?>
<div class="notice<?php echo esc_attr( $notice['type'] ) . esc_attr( $notice['notice-alt'] ) . esc_attr( $notice['inline'] ) . esc_attr( $notice['update-icon'] ) . esc_attr( $notice['css-class'] ) . esc_attr( $notice['dismissible'] ); ?>">
<div class="notice<?php echo esc_attr( $this->type ) . esc_attr( $this->notice_alt ) . esc_attr( $this->inline ) . esc_attr( $this->update_icon ) . esc_attr( $this->css_class ) . esc_attr( $this->dismissible ); ?>">
<?php

$opening_tag = $notice['wrap'] ? '<' . esc_html( $notice['wrap'] ) . '>' : '';
$closing_tag = $notice['wrap'] ? '</' . esc_html( $notice['wrap'] ) . '>' : '';
$opening_tag = $this->wrap ? '<' . esc_html( $this->wrap ) . '>' : '';
$closing_tag = $this->wrap ? '</' . esc_html( $this->wrap ) . '>' : '';

echo wp_kses_post( $opening_tag . $notice['message'] . $closing_tag );
echo wp_kses_post( $opening_tag . $this->message . $closing_tag );

// Extra HTML.
echo wp_kses( $notice['extra-html'], Utils::allowed_html() );
echo wp_kses( $this->extra_html, Utils::allowed_html() );
?>
</div>
<?php
Expand Down
6 changes: 3 additions & 3 deletions includes/class-db-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function update_settings_version( $installed_version ) {
'force_show' => true,
'message' => $update_message,
);
Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );
}


Expand Down Expand Up @@ -183,7 +183,7 @@ public function settings_update_v0_to_v1() {
'wrap' => false,
'message' => $message,
);
Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );

$message = sprintf(
'<h3>%s</h3><pre>%s</pre>',
Expand All @@ -200,7 +200,7 @@ public function settings_update_v0_to_v1() {
'wrap' => false,
'message' => $message,
);
Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions includes/class-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function render_plugin_stats_column( $column_name, $plugin_file ) {
'notice-alt' => true,
'message' => esc_html__( 'Plugin not found on WordPress.org', 'translation-stats' ),
);
Admin_Notice::message( $admin_notice ); // TODO: Add alternative GlotPress API.
new Admin_Notice( $admin_notice ); // TODO: Add alternative GlotPress API.

} elseif ( ! $plugin_translation_on_wporg ) { // Check if translation project is on WordPress.org.

Expand All @@ -134,7 +134,7 @@ public function render_plugin_stats_column( $column_name, $plugin_file ) {
'notice-alt' => true,
'message' => esc_html__( 'Translation project not found on WordPress.org', 'translation-stats' ),
);
Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );

} else {

Expand Down Expand Up @@ -272,7 +272,7 @@ public function plugin_widget_content() {
'force_show' => true,
'message' => esc_html__( 'Waiting...', 'translation-stats' ),
);
Admin_Notice::message( $admin_notice_waiting );
new Admin_Notice( $admin_notice_waiting );
?>
<div class="content"></div>
<?php
Expand Down Expand Up @@ -608,7 +608,7 @@ public function render_notices( $project_stats, $project_slug, $locale ) {
<div class="translation-stats-content-notices">
<?php

Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );

?>
</div>
Expand Down
4 changes: 2 additions & 2 deletions includes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function settings_reset_callback() {
'force_show' => true,
'message' => '<strong>' . esc_html__( 'Settings restored successfully.', 'translation-stats' ) . '</strong>',
);
Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ public function transients_delete_callback() {
'force_show' => true,
'message' => '<strong>' . esc_html__( 'Cache cleaned successfully.', 'translation-stats' ) . '</strong>',
);
Admin_Notice::message( $admin_notice );
new Admin_Notice( $admin_notice );
}
}

Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/includes/test-admin-notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Class Admin_Notice Test.
*
* @package Translation_Stats
*/

Use Translation_Stats\Admin_Notice;


/**
* Admin_Notice test case.
*/
class Test_Admin_Notice extends WP_UnitTestCase {

/**
* Test display formatted admin notice.
*/
public function test_message() {

$admin_notice = array(
'type' => 'error',
'notice-alt' => true,
'message' => 'Translation project not found on WordPress.org',
);
$admin_notice = new Admin_Notice( $admin_notice );

echo "\nAdmin Notice:\n";
var_dump( $admin_notice );

$this->assertIsObject( $admin_notice );

}

}

0 comments on commit b104ee6

Please sign in to comment.