Skip to content

Commit

Permalink
Refactor notices to use the component/view system
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctl committed Oct 19, 2023
1 parent 1e23b14 commit a99c92f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Uplink/Components/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
27 changes: 5 additions & 22 deletions src/Uplink/Notice/Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<div class="%1$s"><p>%2$s</p></div>', 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 );
}

}
39 changes: 39 additions & 0 deletions src/Uplink/Notice/Notice_Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare( strict_types=1 );

namespace StellarWP\Uplink\Notice;

use StellarWP\Uplink\Components\Controller;

/**
* Renders a notice.
*/
final class Notice_Controller extends Controller {

public const VIEW = 'admin/notice';

/**
* Render a notice.
*
* @see Notice::toArray()
* @see src/views/admin/notice.php
*
* @param array{type?: string, message?: string, dismissible?: bool, alt?: bool, large?: bool} $args The notice.
*
* @return void
*/
public function render( array $args = [] ): void {
$classes = [
'notice',
sprintf( 'notice-%s', $args['type'] ),
$args['dismissible'] ? 'is-dismissible' : '',
$args['alt'] ? 'notice-alt' : '',
$args['large'] ? 'notice-large' : '',
];

echo $this->view->render( self::VIEW, [
'message' => $args['message'],
'classes' => $this->classes( $classes )
] );
}

}
14 changes: 11 additions & 3 deletions src/Uplink/Notice/Notice_Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -45,7 +53,7 @@ public function display(): void {
}

foreach ( $this->notices as $notice ) {
echo $notice->get();
$this->controller->render( $notice->toArray() );
}

$this->clear();
Expand Down
7 changes: 6 additions & 1 deletion src/Uplink/Notice/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
14 changes: 14 additions & 0 deletions src/views/admin/notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare( strict_types=1 );
/**
* Render a WordPress dashboard notice.
*
* @see \StellarWP\Uplink\Notice\Notice_Controller
*
* @var \League\Plates\Template\Template $this
* @var string $message The message to display.
* @var string $classes The CSS classes for the notice.
*/
?>
<div class="<?php echo esc_attr( $classes ) ?>">
<p><?php echo esc_html( $message ) ?></p>
</div>

0 comments on commit a99c92f

Please sign in to comment.