woocommerce-paypal-payments/modules/ppcp-admin-notices/src/Renderer/Renderer.php

148 lines
3.2 KiB
PHP
Raw Normal View History

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;
use WooCommerce\PayPalCommerce\AdminNotices\Endpoint\MuteMessageEndpoint;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\PersistentMessage;
2020-09-01 10:24:10 +03:00
/**
* Class Renderer
*/
class Renderer implements RendererInterface {
/**
* The message repository.
*
* @var RepositoryInterface
*/
private $repository;
/**
* 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.
* @param string $module_url The module URL.
* @param string $version The module version.
2020-09-01 10:24:10 +03:00
*/
public function __construct(
RepositoryInterface $repository,
string $module_url,
string $version
) {
2020-09-01 10:24:10 +03:00
$this->repository = $repository;
$this->module_url = untrailingslashit( $module_url );
$this->version = $version;
2020-09-01 10:24:10 +03:00
}
/**
* {@inheritDoc}
2020-09-01 10:24:10 +03:00
*/
public function render(): bool {
$messages = $this->repository->current_message();
2020-09-01 10:24:10 +03:00
foreach ( $messages as $message ) {
$mute_message_id = '';
if ( $message instanceof PersistentMessage ) {
$this->can_mute_message = true;
$mute_message_id = $message->id();
}
2020-09-01 10:24:10 +03:00
printf(
'<div class="notice notice-%s %s" %s%s><p>%s</p></div>',
2020-09-01 10:24:10 +03:00
$message->type(),
2024-08-23 18:19:11 +02:00
( $message->is_dismissible() ) ? 'is-dismissible' : '',
2023-08-11 10:57:08 +01:00
( $message->wrapper() ? sprintf( 'data-ppcp-wrapper="%s"', esc_attr( $message->wrapper() ) ) : '' ),
// Use `empty()` in condition, to avoid false phpcs warning.
( empty( $mute_message_id ) ? '' : sprintf( 'data-ppcp-msg-id="%s"', esc_attr( $mute_message_id ) ) ),
2020-09-01 10:24:10 +03:00
wp_kses_post( $message->message() )
);
}
return (bool) count( $messages );
}
/**
* {@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
}