Add admin notice persistence.

This commit is contained in:
Pedro Silva 2024-02-28 11:14:59 +00:00
parent 9968537f06
commit 562a9eed4c
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
4 changed files with 84 additions and 2 deletions

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\AdminNotices;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
@ -40,6 +42,22 @@ class AdminNotices implements ModuleInterface {
$renderer->render();
}
);
add_action(
Repository::NOTICES_FILTER,
function ( $notices ) use ( $c ) {
$admin_notices = $c->get( 'admin-notices.repository' );
assert( $admin_notices instanceof Repository );
$persisted_notices = $admin_notices->get_persisted_and_clear();
if ( $persisted_notices ) {
$notices = array_merge( $notices, $persisted_notices );
}
return $notices;
}
);
}
/**

View file

@ -92,4 +92,18 @@ class Message {
public function wrapper(): string {
return $this->wrapper;
}
/**
* Returns the object as array.
*
* @return array
*/
public function to_array(): array {
return array(
'type' => $this->type,
'message' => $this->message,
'dismissable' => $this->dismissable,
'wrapper' => $this->wrapper,
);
}
}

View file

@ -16,7 +16,8 @@ use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
*/
class Repository implements RepositoryInterface {
const NOTICES_FILTER = 'ppcp.admin-notices.current-notices';
const NOTICES_FILTER = 'ppcp.admin-notices.current-notices';
const PERSISTED_NOTICES_OPTION = 'woocommerce_ppcp-admin-notices';
/**
* Returns the current messages.
@ -37,4 +38,40 @@ class Repository implements RepositoryInterface {
}
);
}
/**
* Adds a message to persist between page reloads.
*
* @param Message $message The message.
* @return void
*/
public function persist( Message $message ): void {
$persisted_notices = get_option( self::PERSISTED_NOTICES_OPTION ) ?: array();
$persisted_notices[] = $message->to_array();
update_option( self::PERSISTED_NOTICES_OPTION, $persisted_notices );
}
/**
* Adds a message to persist between page reloads.
*
* @return array|Message[]
*/
public function get_persisted_and_clear(): array {
$notices = array();
$persisted_data = get_option( self::PERSISTED_NOTICES_OPTION ) ?: array();
foreach ( $persisted_data as $notice_data ) {
$notices[] = new Message(
(string) ( $notice_data['message'] ?? '' ),
(string) ( $notice_data['type'] ?? '' ),
(bool) ( $notice_data['dismissable'] ?? true ),
(string) ( $notice_data['wrapper'] ?? '' )
);
}
update_option( self::PERSISTED_NOTICES_OPTION, array(), true );
return $notices;
}
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway;
use Psr\Log\LoggerInterface;
use Throwable;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
@ -646,6 +647,8 @@ class WCGatewayModule implements ModuleInterface {
add_action(
'woocommerce_order_action_ppcp_reauthorize_order',
static function ( WC_Order $wc_order ) use ( $container ) {
$admin_notices = $container->get( 'admin-notices.repository' );
assert( $admin_notices instanceof Repository );
/**
* The authorized payments processor.
@ -653,7 +656,17 @@ class WCGatewayModule implements ModuleInterface {
* @var AuthorizedPaymentsProcessor $authorized_payments_processor
*/
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
$authorized_payments_processor->reauthorize_payment( $wc_order );
if ( $authorized_payments_processor->reauthorize_payment( $wc_order ) !== AuthorizedPaymentsProcessor::SUCCESSFUL ) {
$message = sprintf(
'%1$s %2$s',
esc_html__( 'Reauthorization with PayPal failed: ', 'woocommerce-paypal-payments' ),
$authorized_payments_processor->reauthorization_failure_reason() ?: ''
);
$admin_notices->persist( new Message( $message, 'error' ) );
} else {
$admin_notices->persist( new Message( 'Payment reauthorized.', 'info' ) );
}
}
);
}