settings = $settings; // Get the post ID from the URL $post_id = isset( $_GET[ 'post' ] ) ? intval( $_GET[ 'post' ] ) : 0; // phpcs:ignore if ( ! empty( $_GET[ 'post_type' ] ) ) { // phpcs:ignore $post_type = $_GET[ 'post_type' ]; // phpcs:ignore } elseif ( $post_id > 0 ) { $post_type = get_post_type( $post_id ); } else { $post_type = ''; } if ( ! empty( $this->isEnabledWriteWithAI() ) && 'docs' == $post_type ) { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_ai_edit_assets' ) ); } // Legacy AJAX handler kept for back-compat; the redesigned modal uses the REST route. add_action( 'wp_ajax_generate_openai_content', array( $this, 'generate_openai_content_callback' ) ); } public function enqueue_ai_edit_assets( $hook ) { if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) { return; } global $post_type; if ( 'docs' !== $post_type ) { return; } $api_key = $this->get_api_key(); $has_key = ! empty( $api_key ); // Write with AI — loads even without a key so the modal can show its // "add an API key" banner (matches the legacy inline form behavior). betterdocs()->assets->enqueue( 'betterdocs-write-with-ai', 'blocks/write-with-ai.js' ); betterdocs()->assets->enqueue( 'betterdocs-write-with-ai-style', 'blocks/write-with-ai-style.css' ); wp_localize_script( 'betterdocs-write-with-ai', 'betterdocsWriteWithAI', array( 'rest_url' => esc_url_raw( rest_url( 'betterdocs/v1/write-with-ai' ) ), 'rest_nonce' => wp_create_nonce( 'wp_rest' ), 'post_id' => get_the_ID(), 'has_key' => $has_key, // Term-suggestion (Docs AI Suite) wiring reused by the Write-with-AI // preview step. Endpoint + gate mirror REST\DocsAISuite / Core\DocsAISuite. 'rest_suggest_url' => esc_url_raw( rest_url( 'betterdocs/v1/ai-suggest-terms' ) ), 'suggest_terms_enabled' => (bool) $this->settings->get( 'enable_docs_ai_suite', true ), 'model' => $this->settings->get( 'write_with_ai_model', 'gpt-4o-mini' ), 'max_token' => (int) $this->settings->get( 'ai_autowrite_max_token', 2500 ), 'settings_url' => esc_url( admin_url( 'admin.php?page=betterdocs-settings#betterdocs-ai' ) ), 'woo_active' => class_exists( 'WooCommerce' ), 'is_multilingual_active' => Helper::is_multilingual_active(), 'language_options' => Helper::get_active_languages(), 'instructions' => $this->get_instruction_choices(), // From Git is a Pro feature. Pro is detected here; Git enabled/auth // status is filled in by Pro via the filter below (default: off). 'is_pro_active' => betterdocs()->is_pro_active(), 'git' => apply_filters( 'betterdocs_write_with_ai_git', array( 'enabled' => false, 'connected' => false, 'settings_url' => admin_url( 'admin.php?page=betterdocs-settings#git-sync' ), ) ), ) ); // AI Edit — only meaningful with a key configured. if ( ! $has_key ) { return; } betterdocs()->assets->enqueue( 'betterdocs-ai-edit', 'blocks/ai-edit.js' ); betterdocs()->assets->enqueue( 'betterdocs-ai-edit-style', 'blocks/ai-edit-style.css' ); wp_localize_script( 'betterdocs-ai-edit', 'betterdocsAIEdit', array( 'rest_url' => esc_url_raw( rest_url( 'betterdocs/v1/ai-edit' ) ), 'rest_nonce' => wp_create_nonce( 'wp_rest' ), 'post_id' => get_the_ID(), 'instructions' => $this->get_instruction_choices(), 'actions' => AIEdit::get_localized_actions() ) ); } public function isEnabledWriteWithAI() { $isEnableAutoWrite = $this->settings->get( 'enable_write_with_ai', true ); return $isEnableAutoWrite; } public function isValidAPIKey( $apiKey ) { if ( empty( $apiKey ) ) { $api_response[ 'valid' ] = false; $api_response[ 'message' ] = 'Please Insert your OpenAI API Key to use this Write with AI feature.'; return $api_response; } $api_response = array(); $response = wp_safe_remote_get( 'https://api.openai.com/v1/engines', array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $apiKey, ), 'timeout' => 15, ) ); if ( is_wp_error( $response ) ) { $api_response[ 'valid' ] = false; $api_response[ 'message' ] = $response->get_error_message(); return $api_response; } $httpCode = (int) wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); if ( 200 === $httpCode ) { $api_response[ 'valid' ] = true; $api_response[ 'message' ] = 'Valid API Key'; } else { $responseData = json_decode( $body, true ); $messageData = ! empty( $responseData[ 'error' ] ) ? $responseData[ 'error' ] : array(); $api_response[ 'valid' ] = false; $api_response[ 'message' ] = ! empty( $messageData[ 'message' ] ) ? $messageData[ 'message' ] : 'Invalid API Key'; } // print_r($response); return $api_response; } public function get_api_key() { $api_key = $this->settings->get( 'ai_autowrite_api_key', '' ); return $api_key; } /** * The built-in "Default" instruction body. * * Single source of truth shared by {@see Settings::get_default()} (which seeds * the editable "Default" instruction set) and {@see self::get_system_prompt()} * (the fallback when no saved Default content exists). * * @return string */ public static function default_instruction_content() { return <<<'PROMPT' You are a Senior Technical Writer specializing in comprehensive, high-quality documentation. Your goal is to produce documentation that scores 100/100 on clarity, completeness, and structure. ## Output format Return only HTML body content. Never wrap output in ``, ``, `
`, ``, or markdown code fences (no ```html ... ```). Use semantic, Gutenberg-friendly tags only: - Headings: `` for prose - Lists: `
`
- Code blocks: `...
` for multi-line code or commands
- Quotes: ``
- Tables: `` with ``, ``, ``, ``, ` `
- Images: `
`
Apply `key term` to important topic terms inside headings and to the first occurrence of each keyword in body text. Use it sparingly — never wrap whole sentences or wrap text inside `href`, `src`, `alt`, or other attributes.
Do not emit `