init_hooks(); } /** * Get the single instance of the Admin_Menu class. * * @since 1.0.0 * @access public * * @return Admin_Menu The single instance. */ public static function get_instance() { if ( is_null( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } /** * Initialize WordPress hooks. * * @since 1.0.0 * @access private * * @return void */ private function init_hooks() { add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); add_action( 'wp_ajax_acumenpopup_get_settings', array( $this, 'get_settings' ) ); add_action( 'wp_ajax_acumenpopup_save_settings', array( $this, 'save_settings' ) ); } /** * Register the admin menu and submenus. * * @since 1.0.0 * @access public * * @return void */ public function register_menu() { // Main menu page. add_menu_page( esc_html__( 'Acumen Popup', 'acumen-popup' ), esc_html__( 'Acumen Popup', 'acumen-popup' ), 'manage_options', $this->menu_slug, array( $this, 'render_settings_page' ), 'dashicons-buddicons-pm', 100 ); // Dashboard submenu (same as main menu). add_submenu_page( $this->menu_slug, esc_html__( 'Campaigns', 'acumen-popup' ), esc_html__( 'Campaigns', 'acumen-popup' ), 'manage_options', $this->menu_slug, array( $this, 'render_settings_page' ) ); // Campaigns submenu // Subscribers submenu. add_submenu_page( $this->menu_slug, esc_html__( 'Subscribers', 'acumen-popup' ), esc_html__( 'Subscribers', 'acumen-popup' ), 'manage_options', $this->menu_slug . '-subscribers', array( $this, 'render_settings_page' ) ); // Analytics submenu. add_submenu_page( $this->menu_slug, esc_html__( 'Analytics', 'acumen-popup' ), esc_html__( 'Analytics', 'acumen-popup' ), 'manage_options', $this->menu_slug . '-analytics', array( $this, 'render_settings_page' ) ); // Templates submenu. add_submenu_page( $this->menu_slug, esc_html__( 'Templates', 'acumen-popup' ), esc_html__( 'Templates', 'acumen-popup' ), 'manage_options', $this->menu_slug . '-templates', array( $this, 'render_settings_page' ) ); // A/B Testing submenu. add_submenu_page( $this->menu_slug, esc_html__( 'A/B Testing', 'acumen-popup' ), esc_html__( 'A/B Testing', 'acumen-popup' ), 'manage_options', $this->menu_slug . '-ab-testing', array( $this, 'render_settings_page' ) ); // Settings submenu. add_submenu_page( $this->menu_slug, esc_html__( 'Settings', 'acumen-popup' ), esc_html__( 'Settings', 'acumen-popup' ), 'manage_options', $this->menu_slug . '-settings', array( $this, 'render_settings_page' ) ); } /** * Register plugin settings. * * @since 1.0.0 * @access public * * @return void */ public function register_settings() { // Register settings group. register_setting( 'acumenpopup_settings_group', 'acumenpopup_settings', array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => array(), ) ); } /** * Enqueue admin assets for the dashboard. * * @since 1.0.0 * @access public * * @param string $hook The current admin page hook. * @return void */ public function enqueue_admin_assets( $hook ) { if ( strpos( $hook, $this->menu_slug ) === false ) { return; } $asset_file = ACUMENPOPUP_PLUGIN_DIR . 'build/dashboard/index.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $dependencies = require $asset_file; wp_enqueue_script( 'acumenpopup-dashboard', ACUMENPOPUP_URL_FILE . 'build/dashboard/index.js', $dependencies['dependencies'], $dependencies['version'], true ); wp_enqueue_style( 'acumenpopup-dashboard', ACUMENPOPUP_URL_FILE . 'build/dashboard/index.css', array(), $dependencies['version'] ); wp_localize_script( 'acumenpopup-dashboard', 'acumenpopupDashboard', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'admin_url' => admin_url(), 'nonce' => wp_create_nonce( 'acumenpopup-nonce' ), 'api_url' => esc_url_raw( rest_url( 'acumenpopup/v1' ) ), ) ); } /** * Sanitize settings before saving. * * @since 1.0.0 * @access public * * @param array $input Raw settings input. * * @return array Sanitized settings. */ public function sanitize_settings( $input ) { $sanitized = array(); if ( ! is_array( $input ) ) { return $sanitized; } $boolean_settings = array( 'unfiltered_upload', 'download_remote_image', 'user_consent', 'delete_on_uninstall', 'disable_animations', 'load_fonts_locally', 'remove_dashboard_css' ); // Add sanitization for each setting as needed. foreach ( $input as $key => $value ) { $clean_key = sanitize_key( $key ); if ( in_array( $clean_key, $boolean_settings, true ) ) { $sanitized[ $clean_key ] = rest_sanitize_boolean( $value ); } else { $sanitized[ $clean_key ] = sanitize_text_field( $value ); } } return $sanitized; } /** * Render the settings page. * * @since 1.0.0 * @access public * * @return void */ public function render_settings_page() { // Check user capability. if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'acumen-popup' ) ); } ?>