add ppcp admin notices module

This commit is contained in:
David Remer 2020-09-01 10:24:10 +03:00
parent e35c4fe419
commit 0adc94d7e0
15 changed files with 290 additions and 167 deletions

View file

@ -1,6 +1,10 @@
<?php
/**
* The extensions of the admin notice module.
*
* @package Inpsyde\PayPalCommerce\Button
*/
declare(strict_types=1);
return [
];
return array();

View file

@ -1,4 +1,9 @@
<?php
/**
* The admin notice module.
*
* @package Inpsyde\PayPalCommerce\Button
*/
declare(strict_types=1);
@ -7,5 +12,5 @@ namespace Inpsyde\PayPalCommerce\AdminNotices;
use Dhii\Modular\Module\ModuleInterface;
return static function (): ModuleInterface {
return new AdminNotices();
return new AdminNotices();
};

View file

@ -1,4 +1,9 @@
<?php
/**
* The services of the admin notice module.
*
* @package Inpsyde\PayPalCommerce\Button
*/
declare(strict_types=1);
@ -18,14 +23,14 @@ use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
return [
'admin-notices.renderer' => static function (ContainerInterface $container): RendererInterface {
return array(
'admin-notices.renderer' => static function ( ContainerInterface $container ): RendererInterface {
$repository = $container->get('admin-notices.repository');
return new Renderer($repository);
},
'admin-notices.repository' => static function (ContainerInterface $container): RepositoryInterface {
$repository = $container->get( 'admin-notices.repository' );
return new Renderer( $repository );
},
'admin-notices.repository' => static function ( ContainerInterface $container ): RepositoryInterface {
return new Repository();
},
];
return new Repository();
},
);

View file

@ -1,37 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
class AdminNotices implements ModuleInterface
{
public function setup(): ServiceProviderInterface
{
return new ServiceProvider(
require __DIR__.'/../services.php',
require __DIR__.'/../extensions.php'
);
}
/**
* @inheritDoc
*/
public function run(ContainerInterface $container)
{
add_action(
'admin_notices',
function() use ($container) {
$renderer = $container->get('admin-notices.renderer');
$renderer->render();
}
);
}
}

View file

@ -1,34 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Entity;
class Message
{
private $message;
private $type;
private $dismissable;
public function __construct(string $message, string $type, bool $dismissable = true)
{
$this->type = $type;
$this->message = $message;
$this->dismissable = $dismissable;
}
public function message(): string
{
return $this->message;
}
public function type(): string
{
return $this->type;
}
public function isDismissable(): bool
{
return $this->dismissable;
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* The message entity.
*
* @package Inpsyde\PayPalCommerce\AdminNotices\Entity
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Entity;
/**
* Class Message
*/
class Message {
/**
* The messagte text.
*
* @var string
*/
private $message;
/**
* The message type.
*
* @var string
*/
private $type;
/**
* Whether the message is dismissable.
*
* @var bool
*/
private $dismissable;
/**
* Message constructor.
*
* @param string $message The message text.
* @param string $type The message type.
* @param bool $dismissable Whether the message is dismissable.
*/
public function __construct( string $message, string $type, bool $dismissable = true ) {
$this->type = $type;
$this->message = $message;
$this->dismissable = $dismissable;
}
/**
* Returns the message text.
*
* @return string
*/
public function message(): string {
return $this->message;
}
/**
* Returns the message type.
*
* @return string
*/
public function type(): string {
return $this->type;
}
/**
* Returns whether the message is dismissable.
*
* @return bool
*/
public function is_dismissable(): bool {
return $this->dismissable;
}
}

View file

@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Renderer;
use Inpsyde\PayPalCommerce\AdminNotices\Repository\RepositoryInterface;
class Renderer implements RendererInterface
{
private $repository;
public function __construct(RepositoryInterface $repository)
{
$this->repository = $repository;
}
public function render(): bool
{
$messages = $this->repository->currentMessages();
foreach ($messages as $message) {
printf(
'<div class="notice notice-%s %s"><p>%s</p></div>',
$message->type(),
($message->isDismissable()) ? 'is-dismissible' : '',
wp_kses_post($message->message())
);
}
return (bool) count($messages);
}
}

View file

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Renderer;
interface RendererInterface
{
public function render(): bool;
}

View file

@ -0,0 +1,53 @@
<?php
/**
* The renderer.
*
* @package Inpsyde\PayPalCommerce\AdminNotices\Renderer
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Renderer;
use Inpsyde\PayPalCommerce\AdminNotices\Repository\RepositoryInterface;
/**
* Class Renderer
*/
class Renderer implements RendererInterface {
/**
* The message repository.
*
* @var RepositoryInterface
*/
private $repository;
/**
* Renderer constructor.
*
* @param RepositoryInterface $repository The message repository.
*/
public function __construct( RepositoryInterface $repository ) {
$this->repository = $repository;
}
/**
* Renders the current messages.
*
* @return bool
*/
public function render(): bool {
$messages = $this->repository->current_message();
foreach ( $messages as $message ) {
printf(
'<div class="notice notice-%s %s"><p>%s</p></div>',
$message->type(),
( $message->is_dismissable() ) ? 'is-dismissible' : '',
wp_kses_post( $message->message() )
);
}
return (bool) count( $messages );
}
}

View file

@ -0,0 +1,23 @@
<?php
/**
* The renderer interface.
*
* @package Inpsyde\PayPalCommerce\AdminNotices\Renderer
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Renderer;
/**
* Interface RendererInterface
*/
interface RendererInterface {
/**
* Renders the messages.
*
* @return bool
*/
public function render(): bool;
}

View file

@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Repository;
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
class Repository implements RepositoryInterface
{
const NOTICES_FILTER = 'ppcp.admin-notices.current-notices';
public function currentMessages(): array
{
return array_filter(
(array) apply_filters(
self::NOTICES_FILTER,
[]
),
function($element) : bool {
return is_a($element, Message::class);
}
);
}
}

View file

@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Repository;
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
interface RepositoryInterface
{
/**
* @return Message[]
*/
public function currentMessages(): array;
}

View file

@ -0,0 +1,37 @@
<?php
/**
* The message repository.
*
* @package Inpsyde\PayPalCommerce\AdminNotices\Repository
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Repository;
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
/**
* Class Repository
*/
class Repository implements RepositoryInterface {
const NOTICES_FILTER = 'ppcp.admin-notices.current-notices';
/**
* Returns the current messages.
*
* @return Message[]
*/
public function current_message(): array {
return array_filter(
(array) apply_filters(
self::NOTICES_FILTER,
array()
),
function( $element ) : bool {
return is_a( $element, Message::class );
}
);
}
}

View file

@ -0,0 +1,26 @@
<?php
/**
* The repository interface.
*
* @package Inpsyde\PayPalCommerce\AdminNotices\Repository
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices\Repository;
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
/**
* Interface RepositoryInterface
*/
interface RepositoryInterface {
/**
* Returns the current messages.
*
* @return Message[]
*/
public function current_message(): array;
}

View file

@ -0,0 +1,48 @@
<?php
/**
* The admin notice module.
*
* @package Inpsyde\PayPalCommerce\Button
*/
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
/**
* Class AdminNotices
*/
class AdminNotices implements ModuleInterface {
/**
* Sets up the module.
*
* @return ServiceProviderInterface
*/
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
/**
* Runs the module.
*
* @param ContainerInterface $container The container.
*/
public function run( ContainerInterface $container ) {
add_action(
'admin_notices',
function() use ( $container ) {
$renderer = $container->get( 'admin-notices.renderer' );
$renderer->render();
}
);
}
}