dashboard
1 month ago
extension
9 months ago
Assets.php
4 months ago
Dashboard.php
4 months ago
Module_Settings.php
1 month ago
Plugin_Installer.php
4 months ago
Dashboard.php
203 lines
| 1 | <?php |
| 2 | namespace SPEL\includes\Admin; |
| 3 | |
| 4 | // Exit if accessed directly |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Class Admin_Settings |
| 11 | * |
| 12 | * @package spiderElements\includes\Admin |
| 13 | */ |
| 14 | class Dashboard { |
| 15 | |
| 16 | const PAGE_ID = 'spider_elements_settings'; |
| 17 | |
| 18 | /** |
| 19 | * List of Spider Elements admin page slugs |
| 20 | */ |
| 21 | private array $plugin_pages = [ |
| 22 | 'spider_elements_settings', |
| 23 | 'spider_elements_elements', |
| 24 | 'spider_elements_features', |
| 25 | 'spider_elements_integration', |
| 26 | ]; |
| 27 | |
| 28 | public function __construct() { |
| 29 | add_action( 'admin_menu', [ $this, 'add_menu_page' ] ); |
| 30 | add_action( 'admin_init', [ $this, 'hide_admin_notices' ] ); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Hide all admin notices on Spider Elements pages. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function hide_admin_notices(): void { |
| 40 | // Check if we're on a Spider Elements page |
| 41 | if ( ! isset( $_GET['page'] ) || ! in_array( $_GET['page'], $this->plugin_pages, true ) ) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // Remove all admin notices |
| 46 | remove_all_actions( 'admin_notices' ); |
| 47 | remove_all_actions( 'all_admin_notices' ); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Add menu page to the WordPress admin dashboard. |
| 53 | * |
| 54 | * @return void |
| 55 | */ |
| 56 | public function add_menu_page(): void { |
| 57 | // Add main menu page |
| 58 | add_menu_page( |
| 59 | esc_html__( 'Spider Elements', 'spider-elements' ), |
| 60 | esc_html__( 'Spider Elements', 'spider-elements' ), |
| 61 | 'manage_options', |
| 62 | 'spider_elements_settings', |
| 63 | [ $this, 'render_plugin_page' ], |
| 64 | $this->icon(), |
| 65 | 30 |
| 66 | ); |
| 67 | |
| 68 | // Add submenus (matching the tab-menu items in sidebar) |
| 69 | add_submenu_page( |
| 70 | 'spider_elements_settings', |
| 71 | esc_html__( 'Dashboard', 'spider-elements' ), |
| 72 | esc_html__( 'Dashboard', 'spider-elements' ), |
| 73 | 'manage_options', |
| 74 | 'spider_elements_settings', // Same as parent to make it the default |
| 75 | [ $this, 'render_plugin_page' ] |
| 76 | ); |
| 77 | |
| 78 | add_submenu_page( |
| 79 | 'spider_elements_settings', |
| 80 | esc_html__( 'Elements', 'spider-elements' ), |
| 81 | esc_html__( 'Elements', 'spider-elements' ), |
| 82 | 'manage_options', |
| 83 | 'spider_elements_elements', |
| 84 | [ $this, 'render_plugin_page' ] |
| 85 | ); |
| 86 | |
| 87 | add_submenu_page( |
| 88 | 'spider_elements_settings', |
| 89 | esc_html__( 'Features', 'spider-elements' ), |
| 90 | esc_html__( 'Features', 'spider-elements' ), |
| 91 | 'manage_options', |
| 92 | 'spider_elements_features', |
| 93 | [ $this, 'render_plugin_page' ] |
| 94 | ); |
| 95 | |
| 96 | add_submenu_page( |
| 97 | 'spider_elements_settings', |
| 98 | esc_html__( 'Integration', 'spider-elements' ), |
| 99 | esc_html__( 'Integration', 'spider-elements' ), |
| 100 | 'manage_options', |
| 101 | 'spider_elements_integration', |
| 102 | [ $this, 'render_plugin_page' ] |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Get the active tab based on the current page URL. |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | public function get_active_tab(): string { |
| 113 | $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : 'spider_elements_settings'; |
| 114 | |
| 115 | $tab_map = [ |
| 116 | 'spider_elements_settings' => 'welcome', |
| 117 | 'spider_elements_elements' => 'elements', |
| 118 | 'spider_elements_features' => 'features', |
| 119 | 'spider_elements_integration' => 'integration', |
| 120 | ]; |
| 121 | |
| 122 | return $tab_map[ $page ] ?? 'welcome'; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Render the content of the menu page. |
| 128 | * This is where you would add your custom HTML |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | public function render_plugin_page(): void { |
| 133 | // Get the active tab based on which submenu was clicked |
| 134 | $active_tab = $this->get_active_tab(); |
| 135 | |
| 136 | // Map tab names to submenu page slugs |
| 137 | $tab_to_page = [ |
| 138 | 'welcome' => 'spider_elements_settings', |
| 139 | 'elements' => 'spider_elements_elements', |
| 140 | 'features' => 'spider_elements_features', |
| 141 | 'integration' => 'spider_elements_integration', |
| 142 | ]; |
| 143 | |
| 144 | // Get the page slug for the active tab |
| 145 | $current_page = $tab_to_page[ $active_tab ] ?? 'spider_elements_settings'; |
| 146 | $form_action = admin_url( 'admin.php?page=' . $current_page ); |
| 147 | |
| 148 | echo '<form action="' . esc_url( $form_action ) . '" method="post" id="spel_settings" name="spel_settings" class="wrapper spel_dashboard" data-active-tab="' . esc_attr( $active_tab ) . '">'; |
| 149 | |
| 150 | // Hidden field to track current active tab (updated by JavaScript) |
| 151 | echo '<input type="hidden" name="spel_active_tab" id="spel_active_tab" value="' . esc_attr( $active_tab ) . '">'; |
| 152 | |
| 153 | //Sidebar's menu |
| 154 | echo '<div class="sidebar_navigation">'; |
| 155 | |
| 156 | if ( file_exists( __DIR__ . '/dashboard/sidebar.php' ) ) { |
| 157 | require_once __DIR__ . '/dashboard/sidebar.php'; |
| 158 | } |
| 159 | |
| 160 | echo '</div>'; //End of sidebar menu |
| 161 | |
| 162 | |
| 163 | // Tab contents |
| 164 | echo '<div class="tab_contents">'; |
| 165 | |
| 166 | if ( file_exists( __DIR__ . '/dashboard/welcome.php' ) ) { |
| 167 | require_once __DIR__ . '/dashboard/welcome.php'; |
| 168 | } |
| 169 | |
| 170 | if ( file_exists( __DIR__ . '/dashboard/elements.php' ) ) { |
| 171 | require_once __DIR__ . '/dashboard/elements.php'; |
| 172 | } |
| 173 | |
| 174 | if ( file_exists( __DIR__ . '/dashboard/features.php' ) ) { |
| 175 | require_once __DIR__ . '/dashboard/features.php'; |
| 176 | } |
| 177 | |
| 178 | if ( file_exists( __DIR__ . '/dashboard/integration.php' ) ) { |
| 179 | require_once __DIR__ . '/dashboard/integration.php'; |
| 180 | } |
| 181 | |
| 182 | if ( file_exists( __DIR__ . '/dashboard/popup-pro.php' ) ) { |
| 183 | require_once __DIR__ . '/dashboard/popup-pro.php'; |
| 184 | } |
| 185 | |
| 186 | echo '</div>'; //End of tab contents |
| 187 | |
| 188 | echo '</form>'; //End of wrapper |
| 189 | |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * Return the base64 encoded SVG icon. |
| 195 | * |
| 196 | * @return string |
| 197 | */ |
| 198 | public function icon(): string { |
| 199 | return 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iODYiIGhlaWdodD0iODMiIHZpZXdCb3g9IjAgMCA4NiA4MyIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGcgY2xpcC1wYXRoPSJ1cmwoI2NsaXAwXzRfMjExMikiPgo8cGF0aCBkPSJNODYgMTkuNjE0MlYxNS45OTc2VjBINzAuMjI4M0gxNS43NzE3QzcuMDYwNTcgMCAwIDcuMTYxNzEgMCAxNS45OTc2SDc2LjAxMjJDNzguNzMxNCAxOC4yNTg0IDgyLjIwODcgMTkuNjE0MiA4NiAxOS42MTQyWiIgZmlsbD0idXJsKCNwYWludDBfbGluZWFyXzRfMjExMikiLz4KPHBhdGggZD0iTTAgNjMuMzg1N1Y2Ny4wMDI0VjgzSDE1Ljc3MTdINzAuMjI4M0M3OC45Mzk0IDgzIDg2IDc1LjgzODIgODYgNjcuMDAyNEg5Ljk4Nzc4QzcuMjY4NjUgNjQuNzQxNSAzLjc5MTI3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