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", "type": "path",
"url": "modules.local/ppcp-wc-gateway" "url": "modules.local/ppcp-wc-gateway"
},
{
"type": "path",
"url": "modules.local/ppcp-admin-notices"
} }
], ],
"require": { "require": {
"dhii/module-interface": "0.2.x-dev", "dhii/module-interface": "0.2.x-dev",
"psr/container": "^1.0", "psr/container": "^1.0",
"inpsyde/ppcp-admin-notices": "dev-master",
"inpsyde/ppcp-button": "dev-master", "inpsyde/ppcp-button": "dev-master",
"inpsyde/ppcp-wc-gateway": "dev-master", "inpsyde/ppcp-wc-gateway": "dev-master",
"oomphinc/composer-installers-extender": "^1.1", "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": { "require": {
"dhii/module-interface": "0.2.x-dev", "dhii/module-interface": "0.2.x-dev",
"inpsyde/ppcp-session": "dev-master", "inpsyde/ppcp-session": "dev-master",
"inpsyde/ppcp-admin-notices": "dev-master",
"inpsyde/ppcp-api-client": "dev-master" "inpsyde/ppcp-api-client": "dev-master"
}, },
"autoload": { "autoload": {

View file

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

View file

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

View file

@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\WcGateway;
use Dhii\Container\ServiceProvider; use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface; use Dhii\Modular\Module\ModuleInterface;
use Inpsyde\PayPalCommerce\AdminNotices\Repository\Repository;
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderDetail; use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderDetail;
use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn;
use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail;
@ -47,14 +48,24 @@ class WcGatewayModule implements ModuleInterface
} }
); );
add_action( add_filter(
'admin_notices', Repository::NOTICES_FILTER,
function () use ($container) : void { function ($notices) use ($container) : array {
$notice = $container->get('wcgateway.notice.connect'); $notice = $container->get('wcgateway.notice.connect');
/** /**
* @var ConnectAdminNotice $notice * @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( add_action(
'woocommerce_order_actions_start', 'woocommerce_order_actions_start',
function ($wcOrderId) use ($container) { function ($wcOrderId) use ($container) {