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

@ -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
{
$messages['shop_order'][self::NO_INFO] = __(
'Could not retrieve information. Try again later.',
'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'
);
public function message() : ?Message {
$message = $this->getMessage();
if (! $message) {
return null;
}
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
@ -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,26 +15,23 @@ 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(
/* 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'
),
$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')
)
)
$message = 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'
),
$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) {