'', '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('common.select_project_to_edit','Select a project to edit:')).'
'; self::render_project_picker_frontend(); 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 ''; // 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 '