prefix . 'qmen_history'; } /** * Create history table on plugin activation. */ function qmen_create_history_table() { global $wpdb; $table_name = qmen_get_history_table_name(); $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, user_id bigint(20) NOT NULL, title varchar(255) NOT NULL, url text NOT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id), KEY user_id (user_id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } /** * Add an item to history. * * @param int $user_id User ID. * @param string $title Menu title. * @param string $url Menu URL. */ function qmen_add_history_item( $user_id, $title, $url ) { global $wpdb; $table_name = qmen_get_history_table_name(); // Check if already exists (most recent) to prevent duplicates at top $last = $wpdb->get_row( $wpdb->prepare( "SELECT title, url FROM $table_name WHERE user_id = %d ORDER BY created_at DESC LIMIT 1", $user_id ) ); if ( $last && $last->url === $url ) { // Just update timestamp? Or ignore? Let's ignore to reduce DB writes for repetitive clicks same item return; } // Also remove if exists elsewhere in history to avoid duplicates filling the list $wpdb->delete( $table_name, array( 'user_id' => $user_id, 'url' => $url ) ); // Insert new $wpdb->insert( $table_name, array( 'user_id' => $user_id, 'title' => sanitize_text_field( $title ), 'url' => esc_url_raw( $url ), 'created_at' => current_time( 'mysql' ), ) ); // Limit to last 10 // Get IDs to keep $keep_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM $table_name WHERE user_id = %d ORDER BY created_at DESC LIMIT 10", $user_id ) ); if ( ! empty( $keep_ids ) ) { $ids_str = implode( ',', array_map( 'absint', $keep_ids ) ); $wpdb->query( "DELETE FROM $table_name WHERE user_id = $user_id AND id NOT IN ($ids_str)" ); } } /** * Get history items for a user. * * @param int $user_id User ID. * @return array List of history objects. */ function qmen_get_history_items( $user_id ) { global $wpdb; $table_name = qmen_get_history_table_name(); // Check if table exists (in case activated before this update) if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) { return array(); } return $wpdb->get_results( $wpdb->prepare( "SELECT title, url FROM $table_name WHERE user_id = %d ORDER BY created_at DESC LIMIT 10", $user_id ) ); } /** * Track current page visit in history. * Hooked to admin_head to ensure $title and environment are ready. */ function qmen_track_current_page() { // Only track GET requests (viewing pages), not saving forms/AJAX if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { return; } // Skip AJAX, Cron, etc. if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { return; } // Ensure we are in admin context if ( ! is_admin() || ! is_user_logged_in() || ! qmen_is_enabled( 'recent' ) ) { return; } $user_id = get_current_user_id(); // Get Page Title // get_admin_page_title() works but sometimes relies on globals being set. // In admin_head, mostly set. $title = get_admin_page_title(); // Fallback if title is empty (common in some weird screens) if ( empty( $title ) ) { // Specific fix for AdminFly Settings page (hidden submenu) if ( isset( $_GET['page'] ) && 'qmen-quickmenu' === $_GET['page'] ) { $title = __( 'AdminFly', 'adminfly-fast-navigator' ); } else { global $title; // Try global if ( empty( $title ) && function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( $screen ) { $title = $screen->base; } } } } if ( empty( $title ) ) { $title = __( 'Admin Page', 'adminfly-fast-navigator' ); } // Get URL // We want the relative admin URL or full? Database stores expected URL. // $_SERVER['REQUEST_URI'] includes /wp-admin/... // Let's store full URL or relative to admin? // The function expects a full URL or one that works. // admin_url() gives full. // We can reconstruct current URL. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); // Save qmen_add_history_item( $user_id, $title, $current_url ); } add_action( 'admin_head', 'qmen_track_current_page' );