2020-09-01 10:24:10 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* The renderer.
|
|
|
|
*
|
2020-09-11 14:11:10 +03:00
|
|
|
* @package WooCommerce\PayPalCommerce\AdminNotices\Renderer
|
2020-09-01 10:24:10 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
namespace WooCommerce\PayPalCommerce\AdminNotices\Renderer;
|
2020-09-01 10:24:10 +03:00
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\AdminNotices\Repository\RepositoryInterface;
|
2024-08-23 17:58:20 +02:00
|
|
|
use WooCommerce\PayPalCommerce\AdminNotices\Endpoint\MuteMessageEndpoint;
|
2020-09-01 10:24:10 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Renderer
|
|
|
|
*/
|
|
|
|
class Renderer implements RendererInterface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The message repository.
|
|
|
|
*
|
|
|
|
* @var RepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2024-08-23 17:58:20 +02:00
|
|
|
/**
|
|
|
|
* Used to enqueue assets.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $module_url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to enqueue assets.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $version;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the current page contains at least one message that can be muted.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $can_mute_message = false;
|
|
|
|
|
2020-09-01 10:24:10 +03:00
|
|
|
/**
|
|
|
|
* Renderer constructor.
|
|
|
|
*
|
|
|
|
* @param RepositoryInterface $repository The message repository.
|
2024-08-23 17:58:20 +02:00
|
|
|
* @param string $module_url The module URL.
|
|
|
|
* @param string $version The module version.
|
2020-09-01 10:24:10 +03:00
|
|
|
*/
|
2024-08-23 17:58:20 +02:00
|
|
|
public function __construct(
|
|
|
|
RepositoryInterface $repository,
|
|
|
|
string $module_url,
|
|
|
|
string $version
|
|
|
|
) {
|
2020-09-01 10:24:10 +03:00
|
|
|
$this->repository = $repository;
|
2024-08-23 17:58:20 +02:00
|
|
|
$this->module_url = untrailingslashit( $module_url );
|
|
|
|
$this->version = $version;
|
2020-09-01 10:24:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-23 17:58:20 +02:00
|
|
|
* {@inheritDoc}
|
2020-09-01 10:24:10 +03:00
|
|
|
*/
|
|
|
|
public function render(): bool {
|
|
|
|
$messages = $this->repository->current_message();
|
|
|
|
foreach ( $messages as $message ) {
|
|
|
|
printf(
|
2023-08-11 10:35:50 +01:00
|
|
|
'<div class="notice notice-%s %s" %s><p>%s</p></div>',
|
2020-09-01 10:24:10 +03:00
|
|
|
$message->type(),
|
|
|
|
( $message->is_dismissable() ) ? 'is-dismissible' : '',
|
2023-08-11 10:57:08 +01:00
|
|
|
( $message->wrapper() ? sprintf( 'data-ppcp-wrapper="%s"', esc_attr( $message->wrapper() ) ) : '' ),
|
2020-09-01 10:24:10 +03:00
|
|
|
wp_kses_post( $message->message() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bool) count( $messages );
|
|
|
|
}
|
2024-08-23 17:58:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function enqueue_admin() : void {
|
|
|
|
if ( ! $this->can_mute_message ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_register_style(
|
|
|
|
'wc-ppcp-admin-notice',
|
|
|
|
$this->module_url . '/assets/css/styles.css',
|
|
|
|
array(),
|
|
|
|
$this->version
|
|
|
|
);
|
|
|
|
wp_register_script(
|
|
|
|
'wc-ppcp-admin-notice',
|
|
|
|
$this->module_url . '/assets/js/boot-admin.js',
|
|
|
|
array(),
|
|
|
|
$this->version,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
wp_localize_script(
|
|
|
|
'wc-ppcp-admin-notice',
|
|
|
|
'wc_admin_notices',
|
|
|
|
$this->script_data_for_admin()
|
|
|
|
);
|
|
|
|
|
|
|
|
wp_enqueue_style( 'wc-ppcp-admin-notice' );
|
|
|
|
wp_enqueue_script( 'wc-ppcp-admin-notice' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data to inject into the current admin page, which is required by JS assets.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function script_data_for_admin() : array {
|
|
|
|
$ajax_url = admin_url( 'admin-ajax.php' );
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'ajax' => array(
|
|
|
|
'mute_message' => array(
|
|
|
|
'endpoint' => add_query_arg(
|
|
|
|
array( 'action' => MuteMessageEndpoint::ENDPOINT ),
|
|
|
|
$ajax_url
|
|
|
|
),
|
|
|
|
'nonce' => wp_create_nonce( MuteMessageEndpoint::nonce() ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-09-01 10:24:10 +03:00
|
|
|
}
|