*/ private $hook_suffixes = array(); /** * Codes screen controller. * * @var NovoVerify_Codes_Page */ private $codes_page; /** * Settings screen controller. * * @var NovoVerify_Settings_Page */ private $settings_page; /** * Getting Started screen controller. * * @var NovoVerify_Getting_Started */ private $getting_started; /** * Check log table, created on load of its screen. * * @var NovoVerify_Checks_List_Table|null */ private $checks_table = null; /** * Registers the hooks. */ public function __construct() { $this->codes_page = new NovoVerify_Codes_Page(); $this->settings_page = new NovoVerify_Settings_Page(); $this->getting_started = new NovoVerify_Getting_Started(); new NovoVerify_Export(); new NovoVerify_Upgrade(); add_filter( 'set_screen_option_novoverify_checks_per_page', array( $this->codes_page, 'save_per_page' ), 10, 3 ); add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'plugin_action_links_' . NOVOVERIFY_BASENAME, array( $this, 'action_links' ) ); } /** * Adds the menu and sub menu pages. */ public function register_menu() { $this->hook_suffixes['getting-started'] = add_menu_page( __( 'NovoVerify', 'novoverify' ), __( 'NovoVerify', 'novoverify' ), NovoVerify_Config::CAPABILITY, self::MENU_SLUG, array( $this->getting_started, 'render' ), 'dashicons-shield-alt', 56 ); add_submenu_page( self::MENU_SLUG, __( 'Getting Started', 'novoverify' ), __( 'Getting Started', 'novoverify' ), NovoVerify_Config::CAPABILITY, self::MENU_SLUG, array( $this->getting_started, 'render' ) ); $this->hook_suffixes['codes'] = add_submenu_page( self::MENU_SLUG, __( 'Codes', 'novoverify' ), __( 'Codes', 'novoverify' ), NovoVerify_Config::CAPABILITY, NovoVerify_Codes_Page::SLUG, array( $this->codes_page, 'render' ) ); $this->hook_suffixes['import'] = add_submenu_page( self::MENU_SLUG, __( 'Import / Export', 'novoverify' ), __( 'Import / Export', 'novoverify' ), NovoVerify_Config::CAPABILITY, 'novoverify-import', array( $this, 'render_import' ) ); $this->hook_suffixes['log'] = add_submenu_page( self::MENU_SLUG, __( 'Check Log', 'novoverify' ), __( 'Check Log', 'novoverify' ), NovoVerify_Config::CAPABILITY, 'novoverify-log', array( $this, 'render_log' ) ); $this->hook_suffixes['settings'] = add_submenu_page( self::MENU_SLUG, __( 'Settings', 'novoverify' ), __( 'Settings', 'novoverify' ), NovoVerify_Config::CAPABILITY, NovoVerify_Settings_Page::SLUG, array( $this->settings_page, 'render' ) ); add_action( 'load-' . $this->hook_suffixes['codes'], array( $this->codes_page, 'load' ) ); add_action( 'load-' . $this->hook_suffixes['log'], array( $this, 'load_log' ) ); } /** * Enqueues admin styles and scripts on plugin screens only. * * @param string $hook_suffix Current admin screen hook suffix. */ public function enqueue_assets( $hook_suffix ) { wp_register_style( 'novoverify-admin', NOVOVERIFY_URL . 'admin/css/novoverify-admin.css', array(), NOVOVERIFY_VERSION ); /** * Filters the plugin admin screens (screen name => hook suffix), e.g. to add Pro screens. * * @param array $screens Screens. */ $screens = apply_filters( 'novoverify_admin_screens', $this->hook_suffixes ); $screen = array_search( $hook_suffix, $screens, true ); if ( false === $screen ) { return; } wp_enqueue_style( 'novoverify-admin' ); if ( 'codes' === $screen ) { self::enqueue_build_script( 'admin-codes' ); } elseif ( 'import' === $screen ) { self::enqueue_build_script( 'admin-import' ); } /** * Fires when the assets of a plugin admin screen are enqueued. * * @param string $screen Screen name (getting-started, codes, import, log, settings, ...). */ do_action( 'novoverify_admin_enqueue', (string) $screen ); } /** * Hook suffix of a plugin screen. * * @param string $screen Screen name. * @return string Hook suffix, or empty string. */ public function screen_hook( $screen ) { return isset( $this->hook_suffixes[ $screen ] ) ? (string) $this->hook_suffixes[ $screen ] : ''; } /** * Enqueues a script built by @wordpress/scripts, with its dependencies and translations. * * @param string $name Entry name (file name in assets/build without extension). */ public static function enqueue_build_script( $name ) { $asset_file = NOVOVERIFY_PATH . 'assets/build/' . $name . '.asset.php'; if ( ! is_readable( $asset_file ) ) { return; } $asset = include $asset_file; $handle = 'novoverify-' . $name; wp_enqueue_script( $handle, NOVOVERIFY_URL . 'assets/build/' . $name . '.js', $asset['dependencies'], $asset['version'], array( 'in_footer' => true ) ); wp_set_script_translations( $handle, 'novoverify' ); } /** * Adds a "Settings" link on the Plugins screen. * * @param string[] $links Existing links. * @return string[] */ public function action_links( $links ) { $settings = sprintf( '%s', esc_url( admin_url( 'admin.php?page=novoverify-settings' ) ), esc_html__( 'Settings', 'novoverify' ) ); array_unshift( $links, $settings ); return $links; } /** * Renders the Import / Export screen. */ public function render_import() { $this->render_view( 'import' ); } /** * Prepares the check log screen (screen options and table). */ public function load_log() { add_screen_option( 'per_page', array( 'default' => 50, 'option' => 'novoverify_checks_per_page', ) ); $this->checks_table = new NovoVerify_Checks_List_Table(); } /** * Renders the check log screen. */ public function render_log() { if ( ! current_user_can( NovoVerify_Config::CAPABILITY ) || ! $this->checks_table ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'novoverify' ) ); } $table = $this->checks_table; $table->prepare_items(); include NOVOVERIFY_PATH . 'admin/views/checks.php'; } /** * Includes a view file after a capability check. * * @param string $view View name (file name without extension). */ private function render_view( $view ) { if ( ! current_user_can( NovoVerify_Config::CAPABILITY ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'novoverify' ) ); } $file = NOVOVERIFY_PATH . 'admin/views/' . sanitize_key( $view ) . '.php'; if ( is_readable( $file ) ) { include $file; } } }