'', 'chapter_id' => '', ], $atts, 'aiaas_editor'); if (!is_user_logged_in()) { $login_url = wp_login_url(self::current_url()); return '

'.esc_html(self::t('auth.login_required_portal','You must be logged in to access AI Authors Studio (AAS).')).'

'.esc_html(self::t('auth.login','Log in')).'

'; } $project_id = self::resolve_project_id_from_request($atts['project_id']); $chapter_id = self::resolve_chapter_id_from_request($atts['chapter_id']); ob_start(); echo '
'; self::render_app_header(self::t('nav.write','Write')); self::render_frontend_nav('write'); self::render_notice_frontend(); // Page purpose banner (Write) echo '
' . esc_html(self::t('page.write.purpose','Create/Edit Content and Versions')) . '
'; if (method_exists(__CLASS__, 'render_free_upgrade_icon_row')) { self::render_free_upgrade_icon_row('write'); } if (method_exists(__CLASS__, 'render_beginner_mode_banner')) { self::render_beginner_mode_banner('write', (int) $project_id, (int) $chapter_id); } if (!$project_id) { echo '

'.esc_html(self::t('common.select_project_to_edit','Select a project to edit:')).'

'; self::render_project_picker_frontend(); echo '
'; return ob_get_clean(); } if (!self::current_user_can_access_project($project_id)) { echo '

'.esc_html(self::t('common.project_not_found','Project not found or you do not have access.')).'

'; echo ''; return ob_get_clean(); } echo '
'; if (function_exists('aiaas_render_editor_tools_panel')) { aiaas_render_editor_tools_panel([ 'mode' => 'write', 'plan' => \AIAAS\Entitlements::current_plan_label(), 'project_id' => (int) $project_id, 'chapter_id' => (int) $chapter_id, ]); } // Remember last project for quick-return workflows. self::set_last_project_id($project_id); $chapters = self::query_chapters($project_id); if (!$chapters) { echo '

'.esc_html(self::t('common.no_chapters_yet','No chapters yet.')).'

'; $outline_url = self::portal_url_with_context('organize', ['project_id' => $project_id]); if ($outline_url) { echo '

'.esc_html(self::t('write.create_first_chapter','Create your first chapter')).'

'; } else { echo '

Create chapters using [aiaas_outline].

'; } echo ''; return ob_get_clean(); } // Pick a chapter if not provided if (!$chapter_id) { $chapter_id = (int)$chapters[0]->ID; } // Build prev/next $chapter_ids_nav = array_map(function($c){ return (int)$c->ID; }, $chapters); $cur_idx = $chapter_id ? array_search((int)$chapter_id, $chapter_ids_nav, true) : false; $prev_id = ($cur_idx !== false && $cur_idx > 0) ? $chapter_ids_nav[$cur_idx - 1] : 0; $next_id = ($cur_idx !== false && $cur_idx < (count($chapter_ids_nav)-1)) ? $chapter_ids_nav[$cur_idx + 1] : 0; $project = get_post($project_id); $ptitle = $project ? $project->post_title : self::t('common.untitled_project','Untitled Project'); self::render_project_bar_frontend($project_id, ['chapter_id' => true]); // Helpful links $outline_url = self::portal_url_with_context('organize', ['project_id' => $project_id]); // High-level project navigation (portal tabs) // Chapter navigation within Write // Chapter toolbar (two rows): // Row 1: Current chapter + Rename + Language // Row 2: Action buttons (left aligned) echo '
'; echo '
'; // Current chapter selector echo '
'; foreach (\AIAAS\Request::query_args(['chapter_id']) as $k => $v) { echo ''; } echo ''; echo ''; echo '
'; // Rename field (saved with main Save) echo '
' . ''.esc_html(self::t('write.rename','Rename:')).'' . '' . '
'; // Chapter language selector on Write was confusing and is removed from this screen. if (false && self::can_language_ui()) { $enabled_langs = self::get_enabled_languages(); $langs_map = self::allowed_portal_languages(); $cur_ch_lang = self::get_chapter_lang((int)$chapter_id); $return_url_lang = add_query_arg(['project_id'=>$project_id,'chapter_id'=>$chapter_id], self::current_url()) . '#aiaas-versions'; // Language control (Studio): keep it on the same top row. echo '
'; wp_nonce_field('aiaas_set_chapter_lang'); echo ''; echo ''; echo ''; echo ''; echo ''.esc_html(self::t('common.language','Language')).':'; echo ''; echo '
'; } echo '
'; // top row // Row 2: buttons (left) echo '
'; // Removed: + New Chapter button (requested). Replace with a stable Organize button. $organize_url = self::portal_url_with_context('outlines', ['project_id' => $project_id]); echo ''.esc_html(self::t('nav.outlines','← Outlines')).''; if ($prev_id) { $purl = add_query_arg(['project_id' =>$project_id,'chapter_id'=>$prev_id], self::current_url()); echo ''.esc_html(self::t('common.prev','← Previous')).''; } if ($next_id) { $nurl = add_query_arg(['project_id' =>$project_id,'chapter_id'=>$next_id], self::current_url()); echo ''.esc_html(self::t('common.next','Next →')).''; } echo '
'; echo '
'; // chapterbar // Versions UI moved into Editor Tools → Versions. if (\AIAAS\Entitlements::has_feature('versioning')) { // Versions UI moved into Editor Tools; keep Write page clean. } $current = get_post($chapter_id); if (!$current || $current->post_type !== 'aiaas_chapter' || (int)$current->post_parent !== (int)$project_id) { // fallback to first chapter $chapter_id = (int)$chapters[0]->ID; $current = get_post($chapter_id); } $notes = (string)get_post_meta($chapter_id, '_aiaas_notes', true); $nonce = wp_create_nonce('aiaas_autosave_chapter'); // Safety-net: some environments bypass the admin-post handler even though the page reloads. // If we just returned from a successful save, ensure at least one rolling history entry exists. $msg = (string) \AIAAS\Request::get('aiaas_msg', 'key', ''); $existing = self::get_chapter_history_events($chapter_id); // If we just saved and for some reason the rolling history is empty (host caching / blocked admin-post), // seed a manual-save entry from the current post content so the panel is never blank. if ($msg === 'chapter_saved' && empty($existing)) { $cur_post = get_post($chapter_id); $cur_content = $cur_post ? (string)$cur_post->post_content : ''; self::record_chapter_history_event($chapter_id, 'manual_save', 'Manual Save', $cur_content, $notes, true); $existing = self::get_chapter_history_events($chapter_id); } // Safety net: if a chapter already has content/notes but history is empty, create a single "Initial Capture". if (empty($existing)) { $cur_post = get_post($chapter_id); $cur_content = $cur_post ? (string)$cur_post->post_content : ''; if (trim($cur_content) !== '' || trim((string)$notes) !== '') { self::record_chapter_history_event($chapter_id, 'manual_save', 'Initial Capture', $cur_content, $notes, true); } } // Editor form echo '
'; // IMPORTANT: front-end portal forms must post to admin-post.php so admin_post hooks fire. // Posting to the portal URL can silently fail under some themes/caching layers. echo '
'; wp_nonce_field('aiaas_save_chapter'); echo ''; echo ''; echo ''; // Auto-snapshot toggle (Write page). The visible checkbox lives in the Snapshots panel (outside the form), // so we mirror its state into this hidden field on submit. echo ''; echo ''; // Writing area + notes (notes below writing area) echo '
'; echo '
'; echo ''; echo '
'; // Fullscreen toggle + Save (use the same icon/button styling as the lower fullscreen control) echo ''; echo ''; echo '
'; echo '
'; // Write-page note editing behavior: attach it to the already-enqueued frontend handle // instead of printing literal