| | |
| | | 'icon' => jvbCSSIcon('settings'), |
| | | 'position' => 0 |
| | | ]; |
| | | $this->subpages = apply_filters('jvbAdminSubpages', []); |
| | | $this->subpages = get_option(BASE.'adminSubpage', []); |
| | | // delete_option(BASE.'admin_actions'); |
| | | // delete_option(BASE.'admin_subpages'); |
| | | // $this->getSubpages(); |
| | |
| | | add_filter(BASE.'admin_action_filter', [$this, 'handleCacheActions'], 10, 3); |
| | | |
| | | add_action('rest_api_init', [$this, 'registerRestRoutes']); |
| | | // Handle form submissions |
| | | add_action('admin_init', [$this, 'handleAdminPageSubmission']); |
| | | add_action('admin_notices', [$this, 'displayAdminNotices']); |
| | | } |
| | | |
| | | /** |
| | |
| | | </script> |
| | | <?php |
| | | } |
| | | |
| | | public static function addSubpage(string $key, array $value):void |
| | | { |
| | | $option = get_option(BASE.'adminSubpage', []); |
| | | if (empty($option) || !array_key_exists($key, $option)) { |
| | | $option[$key] = $value; |
| | | update_option(BASE.'adminSubpage', $option); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Handle admin page form submissions |
| | | * Fires after WordPress admin_init |
| | | */ |
| | | public function handleAdminPageSubmission(): void |
| | | { |
| | | // Only process on our admin pages |
| | | if (!isset($_GET['page']) || strpos($_GET['page'], BASE) !== 0) { |
| | | return; |
| | | } |
| | | |
| | | // Check for form submission |
| | | if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['submit'])) { |
| | | return; |
| | | } |
| | | |
| | | // Verify nonce |
| | | $nonce_field = BASE . 'admin_page_nonce'; |
| | | if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], $nonce_field)) { |
| | | add_action('admin_notices', function() { |
| | | echo '<div class="notice notice-error"><p>Security check failed. Please try again.</p></div>'; |
| | | }); |
| | | return; |
| | | } |
| | | |
| | | $page_slug = sanitize_text_field($_GET['page']); |
| | | |
| | | // Allow other classes to handle their page submissions |
| | | $result = apply_filters('jvb_admin_page_submission', null, $page_slug, $_POST); |
| | | |
| | | // Store result in transient for after redirect |
| | | if (is_array($result)) { |
| | | set_transient(BASE . 'admin_notice_' . get_current_user_id(), $result, 30); |
| | | } |
| | | |
| | | // Redirect to prevent form resubmission (POST-Redirect-GET pattern) |
| | | wp_safe_redirect(add_query_arg(['page' => $page_slug], admin_url('admin.php'))); |
| | | exit; |
| | | } |
| | | |
| | | /** |
| | | * Display admin notices from form submissions |
| | | */ |
| | | public function displayAdminNotices(): void |
| | | { |
| | | $notice = get_transient(BASE . 'admin_notice_' . get_current_user_id()); |
| | | |
| | | if ($notice && is_array($notice)) { |
| | | delete_transient(BASE . 'admin_notice_' . get_current_user_id()); |
| | | |
| | | $type = $notice['success'] ? 'success' : 'error'; |
| | | $message = $notice['message'] ?? 'Settings saved.'; |
| | | |
| | | echo '<div class="notice notice-' . esc_attr($type) . ' is-dismissible"><p>' . esc_html($message) . '</p></div>'; |
| | | } |
| | | } |
| | | } |