public function rewrite_article_discover_list( $source_html, $language, $tone, $product_names = array(), $title_keyword = '', $source_title = '' ) { $language = (string) $language; $tone = (string) $tone; $title_keyword = (string) $title_keyword; $source_title = (string) $source_title; // Lista rígida de produtos (somente do artigo) $strict_products = array(); if ( is_array( $product_names ) ) { foreach ( $product_names as $p ) { $p = trim( wp_strip_all_tags( (string) $p ) ); if ( '' !== $p ) { $strict_products[] = $p; } } } // BLOCO de diretrizes fornecidas pelo usuário (mantido fiel) $user_block = <<,
ou sugestões de imagem. - Tom: {$tone}. Idioma: {$language}. SEO: - "seo_title": 55–60 caracteres, contendo a keyword principal. - "meta_description": 150–160 caracteres (foco na keyword). - "focus_keyword": use a keyword fornecida. FORMATO DE RESPOSTA (JSON VÁLIDO, sem markdown/code fences): { "title": string, "content_html": string, "seo_title": string, "meta_description": string, "focus_keyword": string, "discover_titles": string[] } RULES; $N = count( $strict_products ); $structure = str_replace( '{N}', (string) $N, $structure ); $tema = array(); if ( $source_title ) { $tema[] = 'TÍTULO ORIGINAL: «' . wp_strip_all_tags( $source_title ) . '» (não reutilizar).'; } if ( $title_keyword ) { $tema[] = 'PALAVRA-CHAVE PARA O TÍTULO: ' . wp_strip_all_tags( $title_keyword ); } $tema_line = implode( "\n", $tema ); $prompt = $user_block . "\n\n" . $tema_line . "\n\n" . $headline_rules . "\n\n" . $products_section . "\n\n" . $structure . "\n\nCONTEÚDO DO ARTIGO (HTML do corpo principal):\n" . (string) $source_html; $body = array( 'model' => $this->model, 'messages' => array( array( 'role' => 'system', 'content' => 'You are an expert editorial SEO writer for Google Discover lists.' ), array( 'role' => 'user', 'content' => $prompt ), ), 'temperature' => $this->temperature, 'max_tokens' => $this->max_tokens, ); $res = $this->post( 'https://api.openai.com/v1/chat/completions', $body ); if ( is_wp_error( $res ) ) { return $this->empty_payload(); } $text = isset( $res['choices'][0]['message']['content'] ) ? (string) $res['choices'][0]['message']['content'] : ''; if ( '' === $text ) { return $this->empty_payload(); } $data = $this->safe_json_from_text( $text ); if ( ! is_array( $data ) ) { return $this->empty_payload(); } if ( ! empty( $title_keyword ) ) { $data['focus_keyword'] = $title_keyword; } return array( 'title' => (string) ( isset( $data['title'] ) ? $data['title'] : '' ), 'content_html' => (string) ( isset( $data['content_html'] ) ? $data['content_html'] : '' ), 'seo_title' => (string) ( isset( $data['seo_title'] ) ? $data['seo_title'] : '' ), 'meta_description' => (string) ( isset( $data['meta_description'] ) ? $data['meta_description'] : '' ), 'focus_keyword' => (string) ( isset( $data['focus_keyword'] ) ? $data['focus_keyword'] : '' ), 'discover_titles' => ( isset( $data['discover_titles'] ) && is_array( $data['discover_titles'] ) ) ? array_values( array_map( 'strval', $data['discover_titles'] ) ) : array(), ); }