post_content; if (has_shortcode($content, 'aiwest_menu')) { self::enqueue_assets(); return; } if (!has_shortcode($content, 'aiwest_header')) return; $header = get_post((int) get_option('aiwest_active_header_page_id', 0)); if ($header && has_shortcode((string) $header->post_content, 'aiwest_menu')) { self::enqueue_assets(); } } public static function shortcode($atts=[]) { return self::render_menu(); } public static function default_structure() { return [ ['title'=>__('Product', 'ai-webmaster-studio'),'url'=>self::url_for_title('AI Webmaster Studio', true),'columns'=>[ ['heading'=>__('Overview', 'ai-webmaster-studio'),'items'=>[__('What is AI Webmaster Studio', 'ai-webmaster-studio'),__('Who We Are', 'ai-webmaster-studio'),__('Project Roadmap', 'ai-webmaster-studio')]], ['heading'=>__('Start', 'ai-webmaster-studio'),'items'=>[__('Download AI Webmaster Studio', 'ai-webmaster-studio'),__('Getting Started', 'ai-webmaster-studio'),__('Documentation Hub', 'ai-webmaster-studio')]], ]], ['title'=>__('Features', 'ai-webmaster-studio'),'url'=>self::url_for_title(__('Features', 'ai-webmaster-studio'), true),'columns'=>[ ['heading'=>__('Website tools', 'ai-webmaster-studio'),'items'=>[__('Site Builder', 'ai-webmaster-studio'),__('Content Development', 'ai-webmaster-studio'),__('AI Webmaster Studio Operator', 'ai-webmaster-studio')]], ['heading'=>__('Content tools', 'ai-webmaster-studio'),'items'=>[__('Graphics and Media', 'ai-webmaster-studio'),__('AI Policies', 'ai-webmaster-studio'),__('Directory Map', 'ai-webmaster-studio')]], ]], ['title'=>__('Documentation', 'ai-webmaster-studio'),'url'=>self::url_for_title(__('Documentation Hub', 'ai-webmaster-studio'), true),'columns'=>[ ['heading'=>__('Setup', 'ai-webmaster-studio'),'items'=>[__('OpenAI API', 'ai-webmaster-studio'),__('API Keys', 'ai-webmaster-studio'),__('External Services', 'ai-webmaster-studio')]], ['heading'=>__('Guides', 'ai-webmaster-studio'),'items'=>[__('Getting Started', 'ai-webmaster-studio'),__('Administrator-Controlled AI for WordPress', 'ai-webmaster-studio'),__('Artificial Intelligence (AI) in AI Webmaster Studio', 'ai-webmaster-studio')]], ]], ['title'=>__('Support', 'ai-webmaster-studio'),'url'=>self::url_for_title(__('Support Center', 'ai-webmaster-studio'), true),'columns'=>[ ['heading'=>__('Help', 'ai-webmaster-studio'),'items'=>[__('Contact Support', 'ai-webmaster-studio'),__('Get Support', 'ai-webmaster-studio'),__('Contact – Get in Touch with AI Webmaster Studio', 'ai-webmaster-studio')]], ]], ['title'=>__('Download', 'ai-webmaster-studio'),'url'=>self::url_for_title(__('Download AI Webmaster Studio', 'ai-webmaster-studio'), true),'columns'=>[]], ]; } public static function get_structure() { $saved = get_option('aiwest_dropdown_menu_structure', []); if (is_array($saved) && $saved) { return $saved; } return self::default_structure(); } public static function save_default_structure($create_drafts=true) { $structure = self::default_structure(); if ($create_drafts) foreach ($structure as &$main) { $main['url'] = self::url_for_title($main['title'], true); if (!empty($main['columns'])) foreach ($main['columns'] as &$col) if (!empty($col['items'])) { $items = []; foreach ($col['items'] as $item) $items[] = ['title'=>$item, 'url'=>self::url_for_title($item, true)]; $col['items'] = $items; } } update_option('aiwest_dropdown_menu_structure', $structure, false); return $structure; } public static function render_menu() { $structure = self::get_structure(); if (!$structure) return ''; self::enqueue_assets(); ob_start(); ?> true, 'aria-label' => true]; return wp_kses($html, $allowed); } public static function inject_into_active_header($create_drafts=true, $reset_to_default=true) { $post_id=intval(get_option('aiwest_active_header_page_id',0)); if(!$post_id) return new WP_Error('aiwest_no_header', __('No active AI Header.', 'ai-webmaster-studio')); $post=get_post($post_id); if(!$post||$post->post_type!=='page') return new WP_Error('aiwest_no_header_post', __('The active AI Header project page was not found.', 'ai-webmaster-studio')); if ($reset_to_default) { self::save_default_structure($create_drafts); } $content=$post->post_content; // Remove all previous menu shortcode occurrences so the action can repair placement. $content=preg_replace('/\[aiwest_menu\s*\/?\]/i','',$content); // Remove old generated static span menu blocks. $content=preg_replace('/]*display\s*:\s*flex[^>]*>\s*(?:]*>.*?<\/span>\s*){3,}<\/div>/isu','',$content,1); /* * Put [aiwest_menu] inside the first .aiwest-header-draft section. * This works also when Gutenberg comments are present after . */ if (preg_match('/(]*class=["\'][^"\']*aiwest-header-draft[^"\']*["\'][^>]*>[\s\S]*?)(<\/section>)/isu', $content)) { $content=preg_replace( '/(]*class=["\'][^"\']*aiwest-header-draft[^"\']*["\'][^>]*>[\s\S]*?)(<\/section>)/isu', '$1
[aiwest_menu]
$2', $content, 1 ); } elseif (preg_match('/<\/div>\s*$/i', trim($content))) { $content=preg_replace('/<\/div>\s*$/i','
[aiwest_menu]
',trim($content),1); } else { $content.="\n
[aiwest_menu]
"; } if(class_exists('AIWEST_Undo')) { AIWEST_Undo::snapshot($post_id, 'ai_dropdown_menu', __('Backup before adding or repairing the AI Header dropdown menu.', 'ai-webmaster-studio')); } $result=wp_update_post(['ID'=>$post_id,'post_content'=>$content],true); if(is_wp_error($result)) return $result; update_post_meta($post_id,'_aiwest_dropdown_menu_enabled','yes'); return ['post_id'=>$post_id, 'message'=>__('The dropdown menu has been added or repaired in the active AI Header.', 'ai-webmaster-studio')]; } public static function url_for_title($title,$create_draft=false){ $title = trim(wp_strip_all_tags($title)); if ($title === '') return '#'; $special = [ 'home' => home_url('/'), 'ai webmaster' => home_url('/'), 'download' => home_url('/download-ai-webmaster/'), 'download ai webmaster' => home_url('/download-ai-webmaster/'), 'documentation' => home_url('/documentation/'), 'documentation hub' => home_url('/documentation-hub/'), 'support' => home_url('/support/'), 'support center' => home_url('/support-center/'), 'contact support' => home_url('/contact-support/'), 'contact – get in touch with ai webmaster' => home_url('/contact/'), 'directory map' => home_url('/ai-directory-map/'), 'locations & partners map' => home_url('/ai-directory-map/'), ]; $key = mb_strtolower($title); if (in_array($key, ['directory map', 'locations & partners map', 'ai directory map'], true) && class_exists('AIWEST_Directory_Map')) { if ($create_draft) { $map_page_id = AIWEST_Directory_Map::ensure_map_page(false); if (!is_wp_error($map_page_id) && $map_page_id) return get_permalink((int) $map_page_id); } $map_page = AIWEST_Directory_Map::resolve_map_page(); return $map_page instanceof WP_Post ? get_permalink($map_page->ID) : home_url('/ai-directory-map/'); } if (isset($special[$key])) { if ($create_draft && $key !== 'home' && $key !== 'ai webmaster') { self::ensure_page_for_url_title($title, trim((string) wp_parse_url($special[$key], PHP_URL_PATH), '/')); } return $special[$key]; } $post = self::find_post($title); if ($post) return get_permalink($post->ID); if (!$create_draft) return home_url('/' . sanitize_title($title) . '/'); $new_id = wp_insert_post([ 'post_title'=>$title, 'post_name'=>sanitize_title($title), 'post_type'=>'page', 'post_status'=>'draft', 'post_content'=>self::draft_content_for($title), 'post_author'=>class_exists('AIWEST_Technical_Operator') ? AIWEST_Technical_Operator::post_author_id(get_current_user_id()) : (get_current_user_id() ?: 1) ], true); if (is_wp_error($new_id)) return home_url('/' . sanitize_title($title) . '/'); update_post_meta($new_id, '_aiwest_generated', 'yes'); update_post_meta($new_id, '_aiwest_created_for_ai_menu', 'yes'); if (class_exists('AIWEST_Technical_Operator')) { AIWEST_Technical_Operator::annotate_post($new_id, 'create_menu_page', AIWEST_Technical_Operator::explain_decision('create_page', 'content_page_create', ['post_type'=>'page','post_status'=>'draft'])); } return get_permalink($new_id); } protected static function ensure_page_for_url_title($title, $slug) { if (!$slug || get_page_by_path($slug, OBJECT, 'page')) return; $new_id = wp_insert_post([ 'post_title' => $title, 'post_name' => sanitize_title($slug), 'post_type' => 'page', 'post_status' => 'draft', 'post_content' => self::draft_content_for($title), 'post_author' => class_exists('AIWEST_Technical_Operator') ? AIWEST_Technical_Operator::post_author_id(get_current_user_id()) : (get_current_user_id() ?: 1), ], true); if (!is_wp_error($new_id)) { update_post_meta($new_id, '_aiwest_generated', 'yes'); update_post_meta($new_id, '_aiwest_created_for_ai_menu', 'yes'); } } protected static function find_post($title) { $slug = sanitize_title($title); $by_slug = get_page_by_path($slug, OBJECT, ['page', 'post']); if ($by_slug && $by_slug->post_status !== 'trash') return $by_slug; $posts = get_posts([ 'numberposts' => 8, 'post_type' => ['page', 'post'], 'post_status' => ['publish', 'draft', 'pending', 'private'], 's' => $title, ]); foreach ($posts as $post) { if (stripos(get_the_title($post), $title) !== false || stripos($title, get_the_title($post)) !== false) return $post; } return null; } protected static function draft_content_for($title){ if (stripos($title, 'map') !== false || stripos($title, 'directory') !== false) { return '

' . esc_html(__('This page was prepared as a placeholder for the AI Webmaster Studio directory/map module.', 'ai-webmaster-studio')) . '

[aiwest_directory_map]'; } if (stripos($title, 'support') !== false || stripos($title, 'contact') !== false) { return '

' . esc_html(__('This is a draft support page created by AI Webmaster Studio. Expand it with clear support instructions, contact options and next steps.', 'ai-webmaster-studio')) . '

'; } return '

' . esc_html(__('This page was created as an AI Webmaster Studio menu draft. Expand it in Content Development before publishing.', 'ai-webmaster-studio')) . '

'; } } AIWEST_AI_Menu::init();