introduce admin-notices

This commit is contained in:
David Remer 2020-04-28 11:42:23 +03:00
parent 1b9b206c14
commit 6793d7217a
15 changed files with 303 additions and 56 deletions

View file

@ -23,11 +23,16 @@
{
"type": "path",
"url": "modules.local/ppcp-wc-gateway"
},
{
"type": "path",
"url": "modules.local/ppcp-admin-notices"
}
],
"require": {
"dhii/module-interface": "0.2.x-dev",
"psr/container": "^1.0",
"inpsyde/ppcp-admin-notices": "dev-master",
"inpsyde/ppcp-button": "dev-master",
"inpsyde/ppcp-wc-gateway": "dev-master",
"oomphinc/composer-installers-extender": "^1.1",

View file

@ -0,0 +1,12 @@
{
"name": "inpsyde/ppcp-admin-notices",
"type": "inpsyde-module",
"require": {
"dhii/module-interface": "0.2.x-dev"
},
"autoload": {
"psr-4": {
"Inpsyde\\PayPalCommerce\\AdminNotices\\": "src/"
}
}
}

View file

@ -0,0 +1,5 @@
<?php
declare(strict_types=1);
return [
];

View file

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\AdminNotices;
use Dhii\Modular\Module\ModuleInterface;
return function (): ModuleInterface {
return new AdminNotices();
};

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button;
use Dhii\Data\Container\ContainerInterface;
use Inpsyde\PayPalCommerce\AdminNotices\Renderer\Renderer;
use Inpsyde\PayPalCommerce\AdminNotices\Renderer\RendererInterface;
use Inpsyde\PayPalCommerce\AdminNotices\Repository\Repository;
use Inpsyde\PayPalCommerce\AdminNotices\Repository\RepositoryInterface;
use Inpsyde\PayPalCommerce\Button\Assets\DisabledSmartButton;
use Inpsyde\PayPalCommerce\Button\Assets\SmartButton;
use Inpsyde\PayPalCommerce\Button\Assets\SmartButtonInterface;
use Inpsyde\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use Inpsyde\PayPalCommerce\Button\Endpoint\RequestData;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
return [
'admin-notices.renderer' => function(ContainerInterface $container) : RendererInterface {
$repository = $container->get('admin-notices.repository');
return new Renderer($repository);
},
'admin-notices.repository' => function(ContainerInterface $container) : RepositoryInterface {
return new Repository();
}
];

View file

@ -0,0 +1,37 @@
<?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

@ -0,0 +1,34 @@
<?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,31 @@
<?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

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

View file

@ -0,0 +1,26 @@
<?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

@ -0,0 +1,15 @@
<?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

@ -4,6 +4,7 @@
"require": {
"dhii/module-interface": "0.2.x-dev",
"inpsyde/ppcp-session": "dev-master",
"inpsyde/ppcp-admin-notices": "dev-master",
"inpsyde/ppcp-api-client": "dev-master"
},
"autoload": {

View file

@ -3,38 +3,70 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Notice;
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
class AuthorizeOrderActionNotice
{
const QUERY_PARAM = 'ppcp-authorized-message';
const NO_INFO = 81;
const ALREADY_CAPTURED = 82;
const FAILED = 83;
const SUCCESS = 84;
const NOT_FOUND = 85;
public function registerMessages(array $messages): array
public function message() : ?Message {
$message = $this->getMessage();
if (! $message) {
return null;
}
return new Message($message['message'], $message['type']);
}
public function getMessage(): array
{
$messages['shop_order'][self::NO_INFO] = __(
$messages[self::NO_INFO] = [
'message' => __(
'Could not retrieve information. Try again later.',
'woocommerce-paypal-gateway'
);
$messages['shop_order'][self::ALREADY_CAPTURED] = __(
),
'type' => 'error',
];
$messages[self::ALREADY_CAPTURED] = [
'message' => __(
'Payment already captured.',
'woocommerce-paypal-gateway'
);
$messages['shop_order'][self::FAILED] = __(
),
'type' => 'error',
];
$messages[self::FAILED] = [
'message' => __(
'Failed to capture. Try again later.',
'woocommerce-paypal-gateway'
);
$messages['shop_order'][self::NOT_FOUND] = __(
),
'type' => 'error',
];
$messages[self::NOT_FOUND] = [
'message' => __(
'Could not find payment to process.',
'woocommerce-paypal-gateway'
);
$messages['shop_order'][self::SUCCESS] = __(
),
'type' => 'error',
];
$messages[self::SUCCESS] = [
'message' => __(
'Payment successfully captured.',
'woocommerce-paypal-gateway'
);
),
'type' => 'success',
];
return $messages;
if (! isset($_GET['ppcp-message'])) {
return [];
}
$messageId = absint($_GET[self::QUERY_PARAM]);
return (isset($messages[$messageId])) ? $messages[$messageId] : [];
}
public static function displayMessage(int $messageCode): void
@ -43,7 +75,7 @@ class AuthorizeOrderActionNotice
'redirect_post_location',
function ($location) use ($messageCode) {
return add_query_arg(
'message',
self::QUERY_PARAM,
$messageCode,
$location
);

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Notice;
use Inpsyde\PayPalCommerce\AdminNotices\Entity\Message;
use Inpsyde\PayPalCommerce\WcGateway\Settings\Settings;
class ConnectAdminNotice
@ -14,15 +15,13 @@ class ConnectAdminNotice
$this->settings = $settings;
}
public function display()
public function connectMessage() : ?Message
{
if (!$this->shouldDisplay()) {
return;
return null;
}
echo sprintf(
'<div class="notice notice-warning"><p>%s</p></div>',
wp_kses_post(
sprintf(
$message = sprintf(
/* translators: %1$s the gateway name */
__(
'%1$s is almost ready. To get started, <a href="%2$s">connect your account</a>.',
@ -31,9 +30,8 @@ class ConnectAdminNotice
$this->settings->get('title'),
// TODO: find a better way to get the url
admin_url('admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway')
)
)
);
return new Message( $message, 'warning');
}
protected function shouldDisplay(): bool

View file

@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\WcGateway;
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Inpsyde\PayPalCommerce\AdminNotices\Repository\Repository;
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderDetail;
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
@ -47,14 +48,24 @@ class WcGatewayModule implements ModuleInterface
}
);
add_action(
'admin_notices',
function () use ($container) : void {
add_filter(
Repository::NOTICES_FILTER,
function ($notices) use ($container) : array {
$notice = $container->get('wcgateway.notice.connect');
/**
* @var ConnectAdminNotice $notice
*/
$notice->display();
$connectMessage = $notice->connectMessage();
if ($connectMessage) {
$notices[] = $connectMessage;
}
$authorizeOrderAction = $container->get('wcgateway.notice.authorize-order-action');
$authorizedMessage = $authorizeOrderAction->message();
if ($authorizedMessage) {
$notices[] = $authorizedMessage;
}
return $notices;
}
);
@ -80,18 +91,6 @@ class WcGatewayModule implements ModuleInterface
}
);
add_filter(
'post_updated_messages',
function ($messages) use ($container) {
/**
* @var AuthorizeOrderActionNotice $authorizeOrderAction
*/
$authorizeOrderAction = $container->get('wcgateway.notice.authorize-order-action');
return $authorizeOrderAction->registerMessages($messages);
},
20
);
add_action(
'woocommerce_order_actions_start',
function ($wcOrderId) use ($container) {