powerstack_get_common_admin_pages(); foreach ( $common_pages as $page ) { $key = $create_unique_key( $page['url'] ); if ( ! isset( $seen_keys[ $key ] ) ) { $menu_items[] = $page; $seen_keys[ $key ] = true; } } // Parse main menu items if ( isset( $menu ) && is_array( $menu ) ) { foreach ( $menu as $item ) { if ( ! empty( $item[0] ) && current_user_can( $item[1] ) ) { $url = $this->powerstack_get_admin_url( $item[2] ); $title = wp_strip_all_tags( $item[0] ); // Skip separator items and invalid titles if ( empty( $title ) || trim( $title ) === '' || strpos( $title, 'wp-menu-separator' ) !== false ) { continue; } // Skip items that are just HTML or special characters if ( preg_match( '/^[<\s]+$/', $title ) ) { continue; } $key = $create_unique_key( $url ); // Skip if key already exists (deduplication) if ( isset( $seen_keys[ $key ] ) ) { continue; } $menu_items[] = array( 'title' => $title, 'url' => $url, 'capability' => $item[1], 'icon' => $item[6] ?? '', 'type' => 'main_menu', 'category' => 'WordPress Admin', 'keywords' => $this->powerstack_generate_keywords( $title ) ); $seen_keys[ $key ] = true; } } } // Parse submenu items with hierarchy as separate field if ( isset( $submenu ) && is_array( $submenu ) ) { foreach ( $submenu as $parent => $items ) { foreach ( $items as $item ) { if ( ! empty( $item[0] ) && current_user_can( $item[1] ) ) { $url = $this->powerstack_get_admin_url( $item[2] ); $title = wp_strip_all_tags( $item[0] ); // Skip separator items and invalid titles if ( empty( $title ) || trim( $title ) === '' || strpos( $title, 'wp-menu-separator' ) !== false ) { continue; } // Skip items that are just HTML or special characters if ( preg_match( '/^[<\s]+$/', $title ) ) { continue; } $key = $create_unique_key( $url ); // Skip if key already exists (deduplication) if ( isset( $seen_keys[ $key ] ) ) { continue; } // Get parent name for hierarchy (displayed below title) $parent_name = isset( $parent_names[ $parent ] ) ? wp_strip_all_tags( $parent_names[ $parent ] ) : ''; $menu_items[] = array( 'title' => $title, 'url' => $url, 'capability' => $item[1], 'parent' => $parent, 'hierarchy' => $parent_name, // Separate hierarchy field 'type' => 'submenu', 'category' => 'WordPress Admin', 'keywords' => $this->powerstack_generate_keywords( $title ) ); $seen_keys[ $key ] = true; } } } } // Add post type quick links $post_type_links = $this->powerstack_get_post_type_links(); foreach ( $post_type_links as $link ) { $key = $create_unique_key( $link['url'] ); if ( ! isset( $seen_keys[ $key ] ) ) { $menu_items[] = $link; $seen_keys[ $key ] = true; } } // Add settings pages $settings_pages = $this->powerstack_get_settings_pages(); foreach ( $settings_pages as $page ) { $key = $create_unique_key( $page['url'] ); if ( ! isset( $seen_keys[ $key ] ) ) { $menu_items[] = $page; $seen_keys[ $key ] = true; } } // Add WooCommerce pages (these take precedence due to better titles) $woocommerce_pages = $this->powerstack_get_woocommerce_pages(); foreach ( $woocommerce_pages as $page ) { $key = $create_unique_key( $page['url'] ); // Remove any existing item with the same key $menu_items = array_filter( $menu_items, function( $item ) use ( $key, $create_unique_key ) { if ( isset( $item['url'] ) ) { return $create_unique_key( $item['url'] ) !== $key; } return true; } ); $menu_items[] = $page; $seen_keys[ $key ] = true; } // Re-index array after filtering $menu_items = array_values( $menu_items ); return apply_filters( 'powerstack_admin_search_parsed_menu_items', $menu_items ); } /** * Get common admin pages * @return array */ private function powerstack_get_common_admin_pages() { $common_pages = array( array( 'title' => __( 'Dashboard', 'powerstack-admin-search' ), 'url' => admin_url(), 'capability' => 'read', 'icon' => 'dashicons-dashboard', 'type' => 'common', 'category' => 'Quick Access', 'keywords' => array( 'home', 'main', 'overview' ) ), array( 'title' => __( 'New Post', 'powerstack-admin-search' ), 'url' => admin_url( 'post-new.php' ), 'capability' => 'edit_posts', 'icon' => 'dashicons-plus-alt', 'type' => 'common', 'category' => 'Quick Access', 'keywords' => array( 'create', 'write', 'article', 'blog' ) ), array( 'title' => __( 'New Page', 'powerstack-admin-search' ), 'url' => admin_url( 'post-new.php?post_type=page' ), 'capability' => 'edit_pages', 'icon' => 'dashicons-plus-alt', 'type' => 'common', 'category' => 'Quick Access', 'keywords' => array( 'create', 'write', 'content' ) ), array( 'title' => __( 'Media Library', 'powerstack-admin-search' ), 'url' => admin_url( 'upload.php' ), 'capability' => 'upload_files', 'icon' => 'dashicons-media-default', 'type' => 'common', 'category' => 'Content', 'keywords' => array( 'images', 'files', 'uploads', 'photos' ) ), array( 'title' => __( 'Comments', 'powerstack-admin-search' ), 'url' => admin_url( 'edit-comments.php' ), 'capability' => 'moderate_comments', 'icon' => 'dashicons-admin-comments', 'type' => 'common', 'category' => 'Content', 'keywords' => array( 'discussion', 'feedback', 'replies' ) ), array( 'title' => __( 'Users', 'powerstack-admin-search' ), 'url' => admin_url( 'users.php' ), 'capability' => 'list_users', 'icon' => 'dashicons-admin-users', 'type' => 'common', 'category' => 'Users', 'keywords' => array( 'accounts', 'profiles', 'people' ) ), array( 'title' => __( 'Plugins', 'powerstack-admin-search' ), 'url' => admin_url( 'plugins.php' ), 'capability' => 'activate_plugins', 'icon' => 'dashicons-admin-plugins', 'type' => 'common', 'category' => 'Settings', 'keywords' => array( 'extensions', 'add-ons', 'modules' ) ), array( 'title' => __( 'Themes', 'powerstack-admin-search' ), 'url' => admin_url( 'themes.php' ), 'capability' => 'switch_themes', 'icon' => 'dashicons-admin-appearance', 'type' => 'common', 'category' => 'Appearance', 'keywords' => array( 'design', 'template', 'layout' ) ), ); // Filter by user capabilities return array_filter( $common_pages, function( $page ) { return current_user_can( $page['capability'] ); } ); } /** * Get post type links * @return array */ private function powerstack_get_post_type_links() { $post_types = get_post_types( array( 'show_ui' => true ), 'objects' ); $links = array(); foreach ( $post_types as $post_type ) { if ( ! current_user_can( $post_type->cap->edit_posts ) ) { continue; } // Add list view $links[] = array( /* translators: %s: post type name */ 'title' => sprintf( __( 'All %s', 'powerstack-admin-search' ), $post_type->labels->name ), 'url' => admin_url( 'edit.php?post_type=' . $post_type->name ), 'capability' => $post_type->cap->edit_posts, 'icon' => 'dashicons-list-view', 'type' => 'post_type', 'category' => 'Content', 'keywords' => array_merge( array( strtolower( $post_type->labels->name ), strtolower( $post_type->labels->singular_name ) ), $this->powerstack_generate_keywords( $post_type->labels->name ) ) ); // Add new post/page $links[] = array( /* translators: %s: post type singular name */ 'title' => sprintf( __( 'New %s', 'powerstack-admin-search' ), $post_type->labels->singular_name ), 'url' => admin_url( 'post-new.php?post_type=' . $post_type->name ), 'capability' => $post_type->cap->edit_posts, 'icon' => 'dashicons-plus-alt', 'type' => 'post_type', 'category' => 'Content', 'keywords' => array_merge( array( 'create', 'new', 'add', strtolower( $post_type->labels->singular_name ) ), $this->powerstack_generate_keywords( $post_type->labels->singular_name ) ) ); } return $links; } /** * Get settings pages * @return array */ private function powerstack_get_settings_pages() { $settings_pages = array(); // General settings if ( current_user_can( 'manage_options' ) ) { $settings_pages[] = array( 'title' => __( 'General Settings', 'powerstack-admin-search' ), 'url' => admin_url( 'options-general.php' ), 'capability' => 'manage_options', 'icon' => 'dashicons-admin-settings', 'type' => 'settings', 'category' => 'Settings', 'keywords' => array( 'general', 'basic', 'configuration' ) ); $settings_pages[] = array( 'title' => __( 'Permalinks', 'powerstack-admin-search' ), 'url' => admin_url( 'options-permalink.php' ), 'capability' => 'manage_options', 'icon' => 'dashicons-admin-links', 'type' => 'settings', 'category' => 'Settings', 'keywords' => array( 'urls', 'seo', 'links', 'slugs' ) ); } return $settings_pages; } /** * Get WooCommerce specific pages * @return array */ private function powerstack_get_woocommerce_pages() { $woocommerce_pages = array(); if ( ! class_exists( 'WooCommerce' ) ) { return $woocommerce_pages; } // WooCommerce Dashboard if ( current_user_can( 'manage_woocommerce' ) ) { $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Dashboard', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-admin' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-dashboard', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'dashboard', 'overview' ) ); // WooCommerce Orders $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Orders', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-orders' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-cart', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'orders', 'sales' ) ); // WooCommerce Products $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Products', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-products' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-products', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'products', 'inventory' ) ); // WooCommerce Customers $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Customers', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-customers' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-groups', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'customers', 'users' ) ); // WooCommerce Coupons $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Coupons', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-coupons' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-tickets', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'coupons', 'discounts' ) ); // WooCommerce Analytics - Fix potential 404 errors with correct URLs $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Analytics', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-admin&path=/analytics/overview' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-chart-bar', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'analytics', 'reports', 'stats' ) ); // WooCommerce Reports $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Reports', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-reports' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-chart-area', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'reports', 'analytics', 'sales' ) ); // WooCommerce Settings $woocommerce_pages[] = array( 'title' => __( 'WooCommerce Settings', 'powerstack-admin-search' ), 'url' => admin_url( 'admin.php?page=wc-settings' ), 'capability' => 'manage_woocommerce', 'icon' => 'dashicons-admin-settings', 'type' => 'woocommerce', 'category' => 'WooCommerce', 'keywords' => array( 'woocommerce', 'wc', 'settings', 'configuration' ) ); } return $woocommerce_pages; } /** * Get proper admin URL with query parameters * @param string $url * @return string */ private function powerstack_get_admin_url( $url ) { // If URL is empty, return admin_url if ( empty( $url ) ) { return admin_url(); } // If the URL is already a full URL (http or https), return as-is if ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) { return $url; } // If the URL is already a full admin URL, return as-is if ( strpos( $url, admin_url() ) === 0 ) { return $url; } // Handle different URL formats if ( strpos( $url, '?' ) !== false ) { // URL contains query parameters // If it starts with a slash, it's a relative path from admin root if ( strpos( $url, '/' ) === 0 ) { return admin_url( ltrim( $url, '/' ) ); } // If it doesn't start with a slash, prepend admin_url return admin_url( $url ); } else { // Simple URL without query parameters // Check if it's a file (has .php extension) or a custom slug if ( strpos( $url, '.php' ) !== false ) { // It's a PHP file, use admin_url directly if ( strpos( $url, '/' ) === 0 ) { return admin_url( ltrim( $url, '/' ) ); } return admin_url( $url ); } else { // It's a custom slug, treat as a page parameter return admin_url( 'admin.php?page=' . $url ); } } } /** * Generate keywords for search * @param string $title * @return array */ private function powerstack_generate_keywords( $title ) { $keywords = array(); $words = preg_split( '/[\s\-_]+/', strtolower( $title ) ); foreach ( $words as $word ) { $word = trim( $word, '.,!?;:' ); if ( strlen( $word ) > 2 ) { $keywords[] = $word; } } return array_unique( $keywords ); } /** * Get cached menu items * @return array */ public function powerstack_get_cached_menu_items() { // Bypass cache temporarily to see changes immediately return $this->powerstack_parse_admin_menus(); } /** * Clear menu cache */ public function powerstack_clear_menu_cache() { $cache_key = 'powerstack_menu_items_' . get_current_user_id(); wp_cache_delete( $cache_key ); } }