HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/sparkle/wp-content/plugins/flexible-product-fields/src/Notice/NoticeIntegration.php
<?php

namespace WPDesk\FPF\Free\Notice;

use VendorFPF\WPDesk\PluginBuilder\Plugin\Hookable;
use VendorFPF\WPDesk\PluginBuilder\Plugin\HookablePluginDependant;
use VendorFPF\WPDesk\PluginBuilder\Plugin\PluginAccess;
use VendorFPF\WPDesk\View\Renderer\SimplePhpRenderer;
use VendorFPF\WPDesk\View\Resolver\DirResolver;

/**
 * Supports ability to display notice and its management.
 */
class NoticeIntegration implements Hookable, HookablePluginDependant {

	use PluginAccess;

	/**
	 * @var Notice
	 */
	private $notice;

	/**
	 * @var SimplePhpRenderer
	 */
	private $renderer;

	/**
	 * Class constructor.
	 */
	public function __construct( Notice $notice ) {
		$this->notice   = $notice;
		$this->renderer = new SimplePhpRenderer( new DirResolver( dirname( dirname( __DIR__ ) ) . '/templates' ) );
	}

	/**
	 * {@inheritdoc}
	 */
	public function hooks() {
		add_filter( 'admin_init', [ $this, 'init_admin_notice' ] );
		add_action( 'wp_ajax_fpf_close_' . $this->notice->get_notice_name(), [ $this, 'hide_admin_notice' ] );
	}

	/**
	 * @return void
	 * @internal
	 */
	public function init_admin_notice() {
		if ( ! $this->notice->is_active() ) {
			return;
		}

		add_filter( 'admin_notices', [ $this, 'load_admin_notice_template' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'load_styles_for_notice' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'load_scripts_for_notice' ] );
	}

	/**
	 * @return void
	 * @internal
	 */
	public function load_admin_notice_template() {
		echo $this->renderer->render( // phpcs:ignore
			$this->notice->get_template_path(),
			array_merge( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				[
					'ajax_url'    => esc_attr( admin_url( 'admin-ajax.php' ) ),
					'ajax_action' => esc_attr( 'fpf_close_' . $this->notice->get_notice_name() ),
				],
				$this->notice->get_vars_for_view()
			)
		);
	}

	/**
	 * @return void
	 * @internal
	 */
	public function load_styles_for_notice() {
		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.css' : '.min.css';

		wp_register_style(
			'fpf-notice',
			trailingslashit( $this->plugin->get_plugin_assets_url() ) . 'css/admin-notice' . $suffix,
			[],
			( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : $this->plugin->get_script_version()
		);
		wp_enqueue_style( 'fpf-notice' );
	}

	/**
	 * @return void
	 * @internal
	 */
	public function load_scripts_for_notice() {
		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.js' : '.min.js';

		wp_register_script(
			'fpf-notice',
			trailingslashit( $this->plugin->get_plugin_assets_url() ) . 'js/admin-notice' . $suffix,
			[],
			( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : $this->plugin->get_script_version(),
			true
		);
		wp_enqueue_script( 'fpf-notice' );
	}

	/**
	 * @return void
	 * @internal
	 */
	public function hide_admin_notice() {
		$is_permanently = ( isset( $_POST['is_permanently'] ) && $_POST['is_permanently'] ); // phpcs:ignore
		$this->notice->set_notice_as_hidden( $is_permanently );
	}
}