<?php
/**
 * Responsive Visibility settings page (core shell).
 *
 * @package CraftAddonsForWPBakery
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class CAFW_Responsive_Settings
 */
class CAFW_Responsive_Settings {

	const PAGE = 'cafw-responsive';

	/**
	 * @var string
	 */
	private $hook = '';

	/**
	 * @return void
	 */
	public function init() {
		add_action( 'admin_menu', array( $this, 'add_menu' ), 20 );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
		add_action( 'in_admin_header', array( $this, 'suppress_foreign_notices' ), PHP_INT_MAX );
	}

	/**
	 * @return void
	 */
	public function add_menu() {
		$this->hook = (string) add_submenu_page(
			CAFW_Admin::PAGE,
			esc_html__( 'Responsive Visibility', 'craft-addons-for-wpbakery' ),
			esc_html__( 'Responsive Visibility', 'craft-addons-for-wpbakery' ),
			'manage_options',
			self::PAGE,
			array( $this, 'render_page' )
		);
	}

	/**
	 * @param string $hook Current admin page hook.
	 * @return void
	 */
	public function enqueue( $hook ) {
		if ( $hook !== $this->hook ) {
			return;
		}

		wp_enqueue_style(
			'cafw-admin',
			CAFW_URL . 'admin/css/cafw-admin.css',
			array(),
			CAFW_Plugin::asset_ver( 'admin/css/cafw-admin.css' )
		);
	}

	/**
	 * @return void
	 */
	public function suppress_foreign_notices() {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen instanceof WP_Screen || $screen->id !== $this->hook ) {
			return;
		}

		remove_all_actions( 'admin_notices' );
		remove_all_actions( 'all_admin_notices' );
		remove_all_actions( 'user_admin_notices' );
		remove_all_actions( 'network_admin_notices' );
	}

	/**
	 * @return void
	 */
	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$cafw_breakpoints = CAFW_Breakpoints::get();
		$cafw_upgrade_url = ( function_exists( 'cafw_fs' ) && method_exists( cafw_fs(), 'get_upgrade_url' ) ) ? cafw_fs()->get_upgrade_url() : '';
		$cafw_updated     = isset( $_GET['updated'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only flag.

		require CAFW_PATH . 'admin/views/responsive.php';
	}
}
