public function rewrite_article_discover_list( string $source_html, string $language, string $tone, array $product_names = array(), string $title_keyword = '', string $source_title = '' ) : array { $headline_guide = <<,
ou sugestões de imagem. - Tom: {$tone}. Idioma: {$language}. SEO: - "seo_title": 55–60 caracteres, com a palavra-chave principal. - "meta_description": 150–160 caracteres, focada na palavra-chave. - "focus_keyword": use a palavra-chave fornecida; se ausente, defina o head term. Formato (JSON apenas): { "title": string, "content_html": string, "seo_title": string, "meta_description": string, "focus_keyword": string, "discover_titles": string[] } RULES; $prompt = $headline_guide . "\n\n" . $tema_line . "\n\n" . $products_hint . "\n\n" . $rules . "\n\nARTIGO DE ORIGEM (HTML):\n" . $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, 'response_format' => array( 'type' => 'json_object' ), ); $res = $this->post( 'https://api.openai.com/v1/chat/completions', $body ); if ( is_wp_error( $res ) ) { return array( 'title' => '', 'content_html' => '', 'seo_title' => '', 'meta_description' => '', 'focus_keyword' => '', 'discover_titles' => array(), ); } $data = json_decode( $res['choices'][0]['message']['content'] ?? '{}', true ); if ( ! is_array( $data ) ) { $data = array(); } if ( ! empty( $title_keyword ) ) { $data['focus_keyword'] = $title_keyword; } return array( 'title' => (string) ( $data['title'] ?? '' ), 'content_html' => (string) ( $data['content_html'] ?? '' ), 'seo_title' => (string) ( $data['seo_title'] ?? '' ), 'meta_description' => (string) ( $data['meta_description'] ?? '' ), 'focus_keyword' => (string) ( $data['focus_keyword'] ?? '' ), 'discover_titles' => isset( $data['discover_titles'] ) && is_array( $data['discover_titles'] ) ? array_values( array_map( 'strval', $data['discover_titles'] ) ) : array(), ); }