// y hace diversos h2 con variaciones del título... sin cerrar el
*/
// Comprobar que contanga etiquetas HTML o será sospechoso
// Es requisito que tenga contenido
$longitud_minima = 200;
if ( ! $contenido ) {
$errores[] = __( 'No content generated.', 'ai-content-creator' );
} else {
if ( strlen( $contenido ) <= $longitud_minima ) {
$errores[] = __( 'Generated content is unusually short.', 'ai-content-creator' );
}
if ( ! aicc_contiene_html( $contenido ) ) {
$errores[] = __( 'Generated content does not appear to be an HTML document.', 'ai-content-creator' );
/*
} else {
$errores[] = __( 'Generated content appears to be an HTML document.', 'ai-content-creator' );
*/
}
}
if ( aicc_modo_debug() ) {
echo '[modo_debug] Errores:
';
echo '
' . nl2br( esc_html ( var_export( $errores, true ) ) ) . '
';
/*
if ( ! $errores ) {
$errores[] = __( 'No errors generated.', 'ai-content-creator' );
}
*/
}
return $errores;
}
function aicc_advertencias_validacion_contenido( $contenido ) {
// Devuelve un array con la lista de advertencias detectadas si los hubiera
$advertencias = array ();
if ( aicc_menciona_recursos_externos( $contenido ) ) {
$advertencias[] = __( 'The content mentions external resources that have not been validated. Check their functionality manually or configure an external post-production system for validation.', 'ai-content-creator' );
}
if ( aicc_modo_debug() ) {
echo 'Advertencias:
';
echo '' . nl2br( esc_html ( var_export( $advertencias, true ) ) ) . '
';
}
return $advertencias;
}
function aicc_notificaciones_validacion_contenido( $contenido ) {
// Devuelve un array con la lista de notificaciones generadas si los hubiera
$notificaciones = array ();
if ( $contenido ) {
//$notificaciones[] = __( 'There is no external post-production system configured to validate the created HTML code.', 'ai-content-creator' );
}
if ( aicc_modo_debug() ) {
echo 'Notificaciones:
';
echo '' . nl2br( esc_html ( var_export( $notificaciones, true ) ) ) . '
';
}
return $notificaciones;
}
function aicc_validar_contenido_auto( $contenido ) {
// Validar el contenido de un artículo automáticamente
if ( aicc_errores_validacion_contenido( $contenido ) ) {
$valida = 0;
} else {
$valida = 1;
}
return $valida;
}
function aicc_contiene_html( $contenido ) {
$contiene_html = false;
// str_contains requiere PHP 8
if (
str_contains( strtolower( $contenido ), '' ) ||
str_contains( strtolower( $contenido ), '
' ) ||
str_contains( strtolower( $contenido ), '
' ) ||
str_contains( strtolower( $contenido ), '' ) ||
str_contains( strtolower( $contenido ), '' ) ||
str_contains( strtolower( $contenido ), '' )
) {
$contiene_html = true;
}
return $contiene_html;
}
function aicc_menciona_recursos_externos( $contenido ) {
$contiene_enlaces_externos = false;
// str_contains requiere PHP 8
if (
str_contains( strtolower( $contenido ), 'http://' ) ||
str_contains( strtolower( $contenido ), 'https://' )
) {
$contiene_enlaces_externos = true;
}
return $contiene_enlaces_externos;
}
function aicc_procesar_respuesta_auto( $aicc_generacion ) {
// No modifica el contenido del objeto, solo devuelve dos parámetros
// Parte de la respuesta HTTP completa para devolver el texto del artículo,
// siendo el contenido de la seccón con algunos arreglos
$respuesta = $aicc_generacion->respuesta();
$modelo = $aicc_generacion->modelo();
$titulo = $aicc_generacion->titulo();
// La respuesta es un array, toma el cuerpo de la respuesta HTTP
$body = wp_remote_retrieve_body( $respuesta );
$response_body = json_decode( $body, true );
$respuesta_mensaje = aicc_contenido_mensaje_respuesta( $modelo, $response_body );
// Ya se podría considerar contenido el mensaje y empezar a procesarlo
// Presuntamente sería HTML ya
$article_text = aicc_procesa_html_respuesta( $respuesta_mensaje, $titulo );
if ( 'stop' != aicc_causa_fin_respuesta( $modelo, $response_body ) ){
$estado_error = 1;
}
$estado_error = 0;
return array( $article_text, $estado_error );
}
function aicc_causa_fin_respuesta( $modelo, $response_body ) {
$causa_fin_respuesta = '';
if ( 'chat' == aicc_tipo_modelo( $modelo ) ){
if ( isset ( $response_body['choices'][0]['finish_reason'] ) ) {
$causa_fin_respuesta = $response_body['choices'][0]['finish_reason'];
}
} else {
if ( isset ( $response_body['choices'][0]['finish_reason'] ) ) {
$causa_fin_respuesta = $response_body['choices'][0]['finish_reason'];
}
}
return $causa_fin_respuesta;
}
function aicc_contenido_mensaje_respuesta( $modelo, $respuesta_body ) {
$respuesta_mensaje = '';
if ( 'chat' == aicc_tipo_modelo( $modelo ) ){
if ( isset ( $respuesta_body['choices'][0]['message']['content'] ) ) {
$respuesta_mensaje = $respuesta_body['choices'][0]['message']['content'];
}
} else {
if ( isset ( $respuesta_body['choices'][0]['text'] ) ) {
$respuesta_mensaje = $respuesta_body['choices'][0]['text'];
}
}
//$article_text = aicc_procesa_html_respuesta( $respuesta_mensaje );
return $respuesta_mensaje;
}
function aicc_procesa_html_respuesta( $articulo_html, $titulo ) {
// Ejemplo de respuesta Cómo hacer una receta Cómo hacer una receta
...
// Con modelos básicos de GPT-3 era frecuente encabezar diciendo:
// Use the following HTML:
// Ejemplo de respuesta:
// Ejemplo:
//$articulo_html = preg_replace( '/^ejemplo de respuesta/i', '', $articulo_html );
//$articulo_html = preg_replace( '/^use the following html:/i', '', $articulo_html );
// Primeros párrafos eliminables
// En el siguiente ejemplo se muestra.*:
// El siguiente ejemplo muestra.*:
// Puede estar relacionado con el prompt
// Retirar el propio prompt y texto posterior
// Formatear índice
$articulo_html = trim( $articulo_html );
// Retirar todo lo que no conviene
$articulo_html = aicc_sanitiza_articulo_html( $articulo_html, $titulo );
// Tomar solo el contenido de la sección body
$articulo_html = aicc_extrae_body_respuesta( $articulo_html );
return $articulo_html;
}
function aicc_extrae_body_respuesta( $articulo_html ) {
// Ejemplo de respuesta Cómo hacer una receta Cómo hacer una receta
...
$regex = preg_quote ( '', '/' );
$articulo_html = preg_replace( '/^.*' . $regex . '/is', '', $articulo_html, 1 );
/* $articulo_html = preg_replace( '/^.*/is', '', $articulo_html, 1 ); */
$regex = preg_quote ( '', '/' );
$articulo_html = preg_replace( '/^.*' . $regex . '/is', '', $articulo_html, 1 );
// Ojo con la secuencia * y /, finaliza comentarios php, por eso la fracciono forzando el "$"
$regex = preg_quote ( '', '/' );
$articulo_html = preg_replace( '/' . $regex . '.*$/is', '', $articulo_html );
$articulo_html = trim( $articulo_html );
return $articulo_html;
}
function aicc_sanitiza_articulo_html( $articulo_html, $titulo ) {
//Concentra las partes que convienen del artículo y descarta las que no.
// Operaciones de limpieza de cabecera y pie del artículo
$articulo_html = aicc_retira_titulo_html ( $articulo_html, $titulo );
$articulo_html = aicc_retira_seccion_header_vacia_html ( $articulo_html );
// La sección footer podría contener simplemente una conclusión. De momento se mantiene.
//$articulo_html = aicc_retira_seccion_footer_html ( $articulo_html );
// Retirada de detiquetas indeseadas por seguridad
/*
// Este método acaba iniciand con aunque la respuesta no haya sido HTML, confundiendo al control de errores
// Ojo que esas hacen un reproceso que, quizás entre más cosas, sgún cómo se usen pone entidades HTML
// y si falta el contenedor de documento HTML, lo puede poner también
if ( class_exists( 'DOMDocument' ) ) {
$articulo_html = aicc_retira_scripts_html ( $articulo_html );
$articulo_html = aicc_retira_iframes_html ( $articulo_html );
$articulo_html = aicc_retira_objects_html ( $articulo_html );
} else {
$advertencia = __( 'DOMDocument class is not available in your website and some sanity actions can\'t be applied to the created article.', 'ai-content-creator' );
$advertencia .= '
';
$advertencia .= __( 'The DOMDocument class is part of the PHP DOM extension, which is included by default in most PHP installations and should be available on most WP hosting environments.', 'ai-content-creator' );
$advertencia .= ' ';
$advertencia .= __( 'However, some hosting providers disable certain PHP extensions or limit their functionality for security or performance reasons.', 'ai-content-creator' );
$advertencia .= '
';
$advertencia .= __( 'Verify manually there is not malicious content in the code of the created article.', 'ai-content-creator' );
echo aicc_admin_notice( $advertencia, 'warning' );
}
*/
$articulo_html = wp_kses_post( $articulo_html );
$articulo_html = trim( $articulo_html );
return $articulo_html;
}
function aicc_retira_titulo_html ( $articulo_html, $titulo ) {
// Quita el título H1 para evitar que se forme un duplicado en el borrador con el título declarado
$regex = preg_quote ( "
$titulo
", '/' );
$articulo_html = preg_replace( '/' . $regex . '/i', '', $articulo_html, 1 );
// Quita el título inicial aun sin H1, pese a ser indicador de no haber creado página HTML
// para evitar que se forme un duplicado en el borrador con el título declarado
$regex = preg_quote ( "$titulo", '/' );
$articulo_html = preg_replace( '/^' . $regex . '/i', '', $articulo_html, 1 );
return $articulo_html;
}
function aicc_retira_seccion_header_vacia_html ( $articulo_html ) {
// Quita una posible sección que haya quedado vacía
$articulo_html = preg_replace( '/^\s*\s*<\/header>/i', '', $articulo_html, 1 );
return $articulo_html;
}
function aicc_retira_seccion_footer_html ( $articulo_html ) {
// Quita una posible sección