From a99c92ffce506afdab5a6cc95e1e7a4362f83ce9 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Thu, 19 Oct 2023 12:31:35 -0600 Subject: [PATCH] Refactor notices to use the component/view system --- src/Uplink/Components/Controller.php | 2 +- src/Uplink/Notice/Notice.php | 27 ++++------------- src/Uplink/Notice/Notice_Controller.php | 39 +++++++++++++++++++++++++ src/Uplink/Notice/Notice_Handler.php | 14 +++++++-- src/Uplink/Notice/Provider.php | 7 ++++- src/views/admin/notice.php | 14 +++++++++ 6 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 src/Uplink/Notice/Notice_Controller.php create mode 100644 src/views/admin/notice.php diff --git a/src/Uplink/Components/Controller.php b/src/Uplink/Components/Controller.php index f02b5eee..af49e925 100644 --- a/src/Uplink/Components/Controller.php +++ b/src/Uplink/Components/Controller.php @@ -39,7 +39,7 @@ protected function classes( array $classes ): string { return ''; } - $classes = array_unique( array_map( 'sanitize_html_class', $classes ) ); + $classes = array_unique( array_map( 'sanitize_html_class', array_filter( $classes ) ) ); return implode( ' ', $classes ); } diff --git a/src/Uplink/Notice/Notice.php b/src/Uplink/Notice/Notice.php index 11787029..e6635546 100644 --- a/src/Uplink/Notice/Notice.php +++ b/src/Uplink/Notice/Notice.php @@ -83,28 +83,11 @@ public function __construct( $this->large = $large; } - public function get(): string { - $type = sprintf( 'notice-%s', sanitize_html_class( $this->type ) ); - - $class_map = [ - 'notice' => true, - $type => true, - 'is-dismissible' => $this->dismissible, - 'notice-alt' => $this->alt, - 'notice-large' => $this->large, - ]; - - $classes = ''; - - foreach ( $class_map as $class => $include ) { - if ( ! $include ) { - continue; - } - - $classes .= sprintf( ' %s', $class ); - } - - return sprintf( '

%2$s

', esc_attr( $classes ), esc_html( $this->message ) ); + /** + * @return array{type: string, message: string, dismissible: bool, alt: bool, large: bool} + */ + public function toArray(): array { + return get_object_vars( $this ); } } diff --git a/src/Uplink/Notice/Notice_Controller.php b/src/Uplink/Notice/Notice_Controller.php new file mode 100644 index 00000000..ca1aeecf --- /dev/null +++ b/src/Uplink/Notice/Notice_Controller.php @@ -0,0 +1,39 @@ +view->render( self::VIEW, [ + 'message' => $args['message'], + 'classes' => $this->classes( $classes ) + ] ); + } + +} diff --git a/src/Uplink/Notice/Notice_Handler.php b/src/Uplink/Notice/Notice_Handler.php index c9154a8b..3d8b122b 100644 --- a/src/Uplink/Notice/Notice_Handler.php +++ b/src/Uplink/Notice/Notice_Handler.php @@ -11,13 +11,21 @@ final class Notice_Handler { public const TRANSIENT = 'stellarwp_uplink_notices'; + /** + * Handles rendering notices. + * + * @var Notice_Controller + */ + private $controller; + /** * @var Notice[] */ private $notices; - public function __construct() { - $this->notices = $this->all(); + public function __construct( Notice_Controller $controller ) { + $this->notices = $this->all(); + $this->controller = $controller; } /** @@ -45,7 +53,7 @@ public function display(): void { } foreach ( $this->notices as $notice ) { - echo $notice->get(); + $this->controller->render( $notice->toArray() ); } $this->clear(); diff --git a/src/Uplink/Notice/Provider.php b/src/Uplink/Notice/Provider.php index cf2c598a..28b0c4cb 100644 --- a/src/Uplink/Notice/Provider.php +++ b/src/Uplink/Notice/Provider.php @@ -10,7 +10,12 @@ final class Provider extends Abstract_Provider { * @inheritDoc */ public function register(): void { - add_action( 'admin_notices', function(): void { + $this->container->bind( Notice_Controller::class, Notice_Controller::class ); + $this->container->bind( Notice_Handler::class, static function ( $c ): Notice_Handler { + return new Notice_Handler( $c->get( Notice_Controller::class ) ); + } ); + + add_action( 'admin_notices', function (): void { $this->container->get( Notice_Handler::class )->display(); }, 12, 0 ); } diff --git a/src/views/admin/notice.php b/src/views/admin/notice.php new file mode 100644 index 00000000..a93e3829 --- /dev/null +++ b/src/views/admin/notice.php @@ -0,0 +1,14 @@ + +
+

+