id = $data; }
return $this->id;
}
function fecha( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->fecha = $data; }
return $this->fecha;
}
function plugin_version( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->plugin_version = $data; }
return $this->plugin_version;
}
function generador( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->generador = $data; }
return $this->generador;
}
function modelo( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->modelo = $data; }
return $this->modelo;
}
function idioma( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->idioma = $data; }
return $this->idioma;
}
function longitud_solicitada( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->longitud_solicitada = $data; }
return $this->longitud_solicitada;
}
function tono( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->tono = $data; }
return $this->tono;
}
function mensaje_sistema( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->mensaje_sistema = $data; }
return $this->mensaje_sistema;
}
function contexto( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->contexto = $data; }
return $this->contexto;
}
function titulo( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->titulo = $data; }
return $this->titulo;
}
function directrices_adicionales( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->directrices_adicionales = $data; }
return $this->directrices_adicionales;
}
function prompt( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->prompt = $data; }
return $this->prompt;
}
function solicitud( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->solicitud = $data; }
return $this->solicitud;
}
function respuesta( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->respuesta = $data; }
return $this->respuesta;
}
function error( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->error = $data; }
return $this->error;
}
function contenido( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->contenido = $data; }
return $this->contenido;
}
function meta_description( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->meta_description = $data; }
return $this->meta_description;
}
function meta_keywords( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->meta_keywords = $data; }
return $this->meta_keywords;
}
function valida( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->valida = $data; }
return $this->valida;
}
function validacion_errores( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->validacion_errores = $data; }
return $this->validacion_errores;
}
function validacion_advertencias( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->validacion_advertencias = $data; }
return $this->validacion_advertencias;
}
function validacion_notificaciones( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->validacion_notificaciones = $data; }
return $this->validacion_notificaciones;
}
function validada_por( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->validada_por = $data; }
return $this->validada_por;
}
function validada_fecha( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->validada_fecha = $data; }
return $this->validada_fecha;
}
function autor( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->autor = $data; }
return $this->autor;
}
function publicada_post_id( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->publicada_post_id = $data; }
return $this->publicada_post_id;
}
function publicable_slug( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->publicable_slug = $data; }
return $this->publicable_slug;
}
function publicada_fecha( $data = NULL ) {
if ( ! is_null( $data ) ) { $this->publicada_fecha = $data; }
return $this->publicada_fecha;
}
function __construct() {
$this->limpiar();
}
public function limpiar() {
$this->id = null;
$this->fecha = '0000-00-00 00:00:00';
$this->plugin_version = '';
$this->generador = '';
$this->modelo = '';
$this->idioma = '';
$this->longitud_solicitada = 0;
$this->tono = '';
$this->mensaje_sistema = '';
$this->contexto = '';
$this->titulo = '';
$this->directrices_adicionales = '';
$this->prompt = '';
$this->solicitud = '';
$this->respuesta = '';
$this->error = 0;
$this->contenido = '';
$this->meta_description = '';
$this->meta_keywords = '';
$this->valida = 0;
$this->validacion_errores = '';
$this->validacion_advertencias = '';
$this->validacion_notificaciones = '';
$this->validada_por = '';
$this->validada_fecha = '0000-00-00 00:00:00';
$this->autor = '';
$this->publicada_post_id = 0;
$this->publicable_slug = '';
$this->publicada_fecha = '0000-00-00 00:00:00';
}
public function grabar() {
// Método para grabar las propiedades a la base de datos
global $wpdb; // Objeto global de WordPress para acceder a la base de datos
// Retira la clave de API de la solicitud por seguridad
$solicitud_anonimizada = $this->solicitud;
$solicitud_anonimizada['headers']['Authorization'] = 'Bearer sk-my-hidden-api-key';
$datos = array(
'fecha' => $this->fecha,
'plugin_version' => $this->plugin_version,
'generador' => $this->generador,
'modelo' => $this->modelo,
'idioma' => $this->idioma,
'longitud_solicitada' => $this->longitud_solicitada,
'tono' => $this->tono,
'mensaje_sistema' => $this->mensaje_sistema,
'contexto' => $this->contexto,
'titulo' => $this->titulo,
'directrices_adicionales' => $this->directrices_adicionales,
'prompt' => $this->prompt,
'solicitud' => wp_json_encode( $solicitud_anonimizada ),
'respuesta' => wp_json_encode( $this->respuesta ),
'error' => $this->error,
'contenido' => $this->contenido,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'valida' => $this->valida,
'validacion_errores' => wp_json_encode( $this->validacion_errores ),
'validacion_advertencias' => wp_json_encode( $this->validacion_advertencias ),
'validacion_notificaciones' => wp_json_encode( $this->validacion_notificaciones ),
'validada_por' => $this->validada_por,
'validada_fecha' => $this->validada_fecha,
'autor' => $this->autor,
'publicada_post_id' => $this->publicada_post_id,
'publicable_slug' => $this->publicable_slug,
'publicada_fecha' => $this->publicada_fecha
);
$tabla = aicc_tabla_generaciones();
if ( $this->id ) {
$resultado = $wpdb->update( $tabla, $datos, array( 'id' => $this->id ) );
} else {
$resultado = $wpdb->insert( $tabla, $datos );
$this->id = $wpdb->insert_id;
}
return ($resultado !== false);
}
public function cargar( $id ) {
// Método para cargar las propiedades desde la base de datos
global $wpdb;
// Si no se indica id, se carga el de la instancia
if ( ! $id ) {
$id = $this->id;
}
//$tabla = $wpdb->prefix . 'mi_tabla'; // Reemplaza 'mi_tabla' con el nombre de tu tabla
$tabla = aicc_tabla_generaciones();
// Preparamos la sentencia SQL para obtener las propiedades de la fila con el id correspondiente
$sql = $wpdb->prepare( "SELECT * FROM $tabla WHERE id = %d", $id );
// Ejecutamos la consulta
$fila = $wpdb->get_row( $sql );
// Si la consulta no devuelve resultados, devolvemos false
if ( is_null( $fila ) ) {
return false;
}
// Asignamos los valores recuperados a las propiedades del objeto
// https://www.php.net/manual/es/function.json-decode.php Observar parámetros
$this->id = $fila->id;
$this->fecha = $fila->fecha;
$this->plugin_version = $fila->plugin_version;
$this->generador = $fila->generador;
$this->modelo = $fila->modelo;
$this->idioma = $fila->idioma;
$this->longitud_solicitada = $fila->longitud_solicitada;
$this->tono = $fila->tono;
$this->mensaje_sistema = $fila->mensaje_sistema;
$this->contexto = $fila->contexto;
$this->titulo = $fila->titulo;
$this->directrices_adicionales = $fila->directrices_adicionales;
$this->prompt = $fila->prompt;
$this->solicitud = json_decode( $fila->solicitud, true );
$this->respuesta = json_decode( $fila->respuesta, true );
$this->error = $fila->error;
$this->contenido = $fila->contenido;
$this->meta_description = $fila->meta_description;
$this->meta_keywords = $fila->meta_keywords;
$this->valida = $fila->valida;
$this->validacion_errores = json_decode( $fila->validacion_errores, true );
$this->validacion_advertencias = json_decode( $fila->validacion_advertencias, true );
$this->validacion_notificaciones = json_decode( $fila->validacion_notificaciones, true );
$this->validada_por = $fila->validada_por;
$this->validada_fecha = $fila->validada_fecha;
$this->autor = $fila->autor;
$this->publicada_post_id = $fila->publicada_post_id;
$this->publicable_slug = $fila->publicable_slug;
$this->publicada_fecha = $fila->publicada_fecha;
return true;
}
public function url_editar_post() {
// URL de WP para editar el borrador
$url = '';
if ( $this->publicada_post_id ) {
$url = get_edit_post_link( $this->publicada_post_id );
}
return $url;
}
public function crear_borrador() {
// Crear un borrador con la generación
//$post_category = array( 'category1', 'category2' );
//$post_tags = 'etiqueta1, etiqueta2';
$post_category = array();
$post_tags = '';
$new_post = array(
'post_title' => $this->titulo,
'post_content' => wp_kses_post( $this->contenido ),
'post_status' => 'draft',
'post_category' => $post_category,
'tags_input' => $post_tags
);
$post_id = wp_insert_post( $new_post );
if ( $post_id > 0 ) {
$this->publicada_post_id = $post_id;
$this->publicada_fecha = current_time( 'Y-m-d H:i:s' );
$this->grabar();
}
return $post_id;
}
public function borrar( $id ) {
// Borrar una fila dado el id
global $wpdb;
// Si no se indica id, se borra el de la instancia
if ( ! $id ) {
$id = $this->id;
}
$tabla = aicc_tabla_generaciones();
$result = $wpdb->delete( $tabla, array( 'id' => $id ) );
if ( $result !== false ) {
$this->id = null;
return true;
} else {
return false;
}
}
public function ver_html() {
// Ver el contenido de un artículo
$html = esc_html( sprintf( __( 'Created: %s by %s', 'ai-content-creator' ), $this->fecha_hora_corta( $this->fecha ), $this->modelo ) );
if ( $this->generador ) {
$html .= esc_html( ' (' . $this->generador . ')' );
}
$html .= "
\n";
$html .= '
// y hace diveros h2 con variaciones del título... sin cerrar el
*/ $this->valida = aicc_validar_contenido_auto( $this->contenido ); // Toma la lista de errores de validación $this->validacion_errores = aicc_errores_validacion_contenido( $this->contenido ); $this->validacion_advertencias = aicc_advertencias_validacion_contenido( $this->contenido ); $this->validacion_notificaciones = aicc_notificaciones_validacion_contenido( $this->contenido ); $this->validada_por = aicc_validador_auto_actual(); $this->validada_fecha = current_time( 'Y-m-d H:i:s' ); return $this->valida; } public function error_proceso_api() { // Texto del posible error en el proceso $error_proceso_api = ''; if ( is_wp_error( $this->respuesta ) ) { // Típicamente errores en la comunicación HTTP $error_proceso_api = $this->respuesta->get_error_message(); } return $error_proceso_api; } public function error_respuesta_ai() { // Errores indicados como respuesta de la IA $error_respuesta_ai = ''; if ( ! is_wp_error( $this->respuesta ) ) { $body = wp_remote_retrieve_body( $this->respuesta ); $respuesta_body = json_decode( $body, true ); if ( isset ( $respuesta_body['error'] ) ) { $error_respuesta_ai = $respuesta_body['error']['message']; // Algunos errores de la AI, siendo estándar y conocidos, los puedo traducir como constantes $error_respuesta_ai = aicc_traduce_error_externo( $error_respuesta_ai ); } } return $error_respuesta_ai; } public function error_finalizacion_ai() { // Errores indicados como causa de finalización de la IA $error_finalizacion_ai = ''; $body = wp_remote_retrieve_body( $this->respuesta ); $response_body = json_decode( $body, true ); $error_finalizacion_ai = aicc_causa_fin_respuesta( $this->generador, $response_body ); if ( 'stop' == $error_finalizacion_ai ) { // stop indica la terminación normal $error_finalizacion_ai = ''; } return $error_finalizacion_ai; } public function iframe_contenido_html() { // Uso de sandbox admitiendo tan solo allow-same-origin para poder redimensionar la altura del marco. $html = '' . "\n"; // Script para ajustar la altura del frame en el contenedor padre. $html .= "\n"; return $html; } public function cabecera_tabla_lista_html() { $html = '
| ' . __( 'ID', 'ai-content-creator' ) . " | \n"; $html .= '' . __( 'Date', 'ai-content-creator' ) . " | \n"; $html .= '' . __( 'Model', 'ai-content-creator' ) . " | \n"; $html .= '' . __( 'Title', 'ai-content-creator' ) . " | \n"; $html .= '' . __( 'View', 'ai-content-creator' ) . " | \n"; $html .= '' . __( 'Status', 'ai-content-creator' ) . " | \n"; $html .= '' . __( 'Action', 'ai-content-creator' ) . " | \n"; $html .= '' . __( 'Delete', 'ai-content-creator' ) . " | \n"; $html .= "
|---|---|---|---|---|---|---|---|
| '; $html .= esc_html( $this->id ); $html .= " | \n"; $html .= ''; $html .= esc_html( $this->fecha_hora_corta( $this->fecha ) ); $html .= " | \n"; $html .= ''; $html .= esc_html( aicc_denominacion_modelo( $this->modelo ) ); $html .= " | \n"; $html .= ''; if ( $this->titulo ) { $html .= esc_html( $this->titulo ); } else { $html .= '[' . esc_html( __( 'Untitled', 'ai-content-creator' ) ) . ']'; } $html .= " | \n"; // Ver $html .= ''; $html .= ' '; $html .= $this->dashicon_html( 'dashicons-visibility', __( 'View', 'ai-content-creator' ), 'text-decoration:none;' ); $html .= ''; $html .= " | \n"; // Estado $html .= ''; $html .= $this->icono_estado_html(); $html .= " | \n"; // Acción. En función del estado del artículo y del posprocesaror, indica la acción recomendada $html .= ''; $html .= $this->icono_accion_html(); $html .= " | \n"; // Borrar $html .= ''; //$tooltip = 'No borrable, hay un post creado basado en este artículo.'; if ( ! $this->publicada_post_id ) { $tooltip = __( 'Delete', 'ai-content-creator' ); $html .= ' '; $html .= $this->dashicon_html( 'dashicons-trash', $tooltip, 'text-decoration:none;' ); $html .= ''; } $html .= " | \n"; $html .= "
' . __( 'You have a different version of the articles post-production system than the one initially used.', 'ai-content-creator' ); $html_contenido .= ' ' . __( 'That system is responsible for the basic validation of the content received from the AI and a subsequent process to facilitate its publication.', 'ai-content-creator' ) . "
\n"; $html_contenido .= '' . __( 'You can repeat post-production in search of a possible better result.', 'ai-content-creator' ) . "
\n"; if ( $this->publicada_post_id ) { $html_contenido .= '' . __( 'The reprocessing does not affect the previously created draft.', 'ai-content-creator' ) . "
\n"; } $html_boton = $this->formulario_operacion_html( 'reprocesar', __( 'Reprocess', 'ai-content-creator' ), 'dashicons-update' ); $html .= $this->bloque_formulario_operacion_html( $html_contenido, $html_boton ); } // Regenerar $html_contenido = '' . __( 'You can go back to the beginning to create a new article based on the request used in this one.', 'ai-content-creator' ) . "
\n"; $html_contenido .= '' . __( 'The new article will not overwrite the data of this one', 'ai-content-creator' ); if ( $this->publicada_post_id ) { $html_contenido .= ' '; $html_contenido .= __( 'nor will affect the previously created draft', 'ai-content-creator' ); } $html_contenido .= ".
\n"; // Alternativas: dashicons-welcome-add-page, dashicons-controls-repeat, dashicons-redo, dashicons-editor-break $html_boton = $this->formulario_operacion_html( 'regenerar', __( 'Create another', 'ai-content-creator' ), 'dashicons-welcome-add-page' ); $html .= $this->bloque_formulario_operacion_html( $html_contenido, $html_boton ); // Editar post + borrar generación if ( $this->publicada_post_id ) { $html_contenido = '' . __( 'You can', 'ai-content-creator' ) . ' '; //$html_contenido .= ''; $html_contenido .= __( 'edit the article', 'ai-content-creator' ); //$html_contenido .= ''; $html_contenido .= ' '; $html_contenido .= __( "with your WordPress's usual system", 'ai-content-creator' ) . ".
\n"; // Enlace como falso botón $html_boton = '' . __( 'If you are sure that you will not need any revision of how it was generated anymore, you can delete it. This change is not reversible. The draft or published article will not be affected.', 'ai-content-creator' ) . "
\n"; $html_boton = $this->formulario_operacion_html( 'borrar', __( 'Delete', 'ai-content-creator' ), 'dashicons-trash' ); $html .= $this->bloque_formulario_operacion_html( $html_contenido, $html_boton ); } else { if ( current_user_can( 'edit_posts' ) ) { if ( $this->valida ) { // Crear borrador $html_contenido = '' . __( 'Create a draft using the generated content to edit and finalize its publishing.', 'ai-content-creator' ) . "
\n"; // Alternativa: dashicons-migrate $html_boton = $this->formulario_operacion_html( 'borrador', __( 'Create draft', 'ai-content-creator' ), 'dashicons-external' ); $html .= $this->bloque_formulario_operacion_html( $html_contenido, $html_boton ); } else { // Crear borrador aun con error if ( $this->contenido ) { $html_contenido = '' . __( 'Although errors have been detected in the generated content, if you want to take advantage of it, you can create a draft to edit and finalize its publishing.', 'ai-content-creator' ) . "
\n"; $texto_confirmacion = __( 'I understand that the content of the draft may be defective.', 'ai-content-creator' ); $html_contenido .= '' . $texto_confirmacion . "
\n"; $html_boton = $this->formulario_operacion_html( 'borrador', __( 'Create draft', 'ai-content-creator' ), 'dashicons-external' ); $html .= $this->bloque_formulario_operacion_html( $html_contenido, $html_boton ); $html .= ''; // Llama inicialmente para deshabilitar, así uso el código estándar con el botón habilitado en origen $html .= ''; //javascript que deshabilite el botón 'borrador' directamente //javascript que habilite el botón 'borrador' al hacer click en la casilla //onchange="isChecked(this, 'borrador')" } } } else { // No tiene permisos para crear ni editar borradores // En principio no debería llegar aquí tampoco si no los tiene. $html .= '' . __( "You don't have permission to create drafts.", 'ai-content-creator' ) . "
\n"; } // Borrar $html_contenido = '' . __( 'If the content is not useful and you will not need any revision of how it was generated or create a draft with it, you can delete it. This change is not reversible.', 'ai-content-creator' ) . "
\n"; $html_boton = $this->formulario_operacion_html( 'borrar', __( 'Delete', 'ai-content-creator' ), 'dashicons-trash' ); $html .= $this->bloque_formulario_operacion_html( $html_contenido, $html_boton ); } // Otros dashicons útiles: dashicons-editor-help, dashicons-download, dashicons-upload // https://developer.wordpress.org/resource/dashicons/ return $html; } public function bloque_formulario_operacion_html( $html_contenido, $html_boton ) { $html = '