false, 'resultado' => [ 'success' => false, 'mensaje' => 'Parámetros insuficientes para la importación.' ] ], 400); } // Claramente obtenemos el feed origen que tiene la configuración $_source_feed_id = get_post_meta($feed_id, '_source_feed_id', true); $config_general = get_option('aife_settings', []); // Si no existe el _source_feed_id, devolver error claramente if (!$_source_feed_id) { $config_feed = $config_general; }else{ $config_feed = get_post_meta($_source_feed_id); $config_feed_simple = []; foreach ($config_feed as $clave => $valor) { $config_feed_simple[$clave] = maybe_unserialize($valor[0]); } $config_feed = $config_feed_simple; } // Obtener configuración desde el feed origen claramente $usa_config_general = isset($config_feed['_aife_hereda_ajustes']) && $config_feed['_aife_hereda_ajustes'] == 1; // Obtener correctamente post_status desde configuración $post_status = $usa_config_general ? ($config_general['post_status'] ?? 'draft') : ($config_feed['post_status'] ?? 'draft'); //error_log("Usa configuración general: " . var_export($usa_config_general, true)); //error_log("Post Status Final: " . var_export($post_status, true)); $fecha_programada = $usa_config_general ? ($config_general['post_date'] ?? '') : ($config_feed['post_date'] ?? ''); if (is_array($fecha_programada)) { $fecha_programada = reset($fecha_programada); } if (!empty($fecha_programada) && is_string($fecha_programada)) { $post_date = gmdate('Y-m-d H:i:s', strtotime($fecha_programada, current_time('timestamp', true))); } else { $post_date = current_time('mysql'); } // Insertar post principal claramente $post_id = wp_insert_post([ 'post_title' => sanitize_text_field($resultados['titular']['titular'] ?? ''), 'post_excerpt' => sanitize_textarea_field($resultados['resumen']['resumen'] ?? ''), 'post_content' => wp_kses_post($resultados['refrito']['refrito'] ?? ''), 'post_status' => $post_status, 'post_date' => $post_date, 'post_author' => intval($resultados['autor']['autor_id'] ?? 1), ]); if (is_wp_error($post_id)) { return new WP_REST_Response([ 'success' => false, 'resultado' => [ 'success' => false, 'mensaje' => $post_id->get_error_message() ] ], 500); } // Guardar claramente campos adicionales $campos_adicionales = ['subtitulo', 'antetitulo', 'cifras', 'cronologia', 'faq']; foreach ($campos_adicionales as $campo) { if (!empty($resultados[$campo][$campo])) { update_post_meta($post_id, $campo, wp_kses_post($resultados[$campo][$campo])); } } // Imagen destacada claramente (con title y alt) if (!empty($resultados['imagen']['imagen_destacada'])) { $imagen_destacada_id = aife_upload_image_from_url($resultados['imagen']['imagen_destacada'], $post_id); if (!is_wp_error($imagen_destacada_id)) { set_post_thumbnail($post_id, $imagen_destacada_id); update_post_meta($imagen_destacada_id, '_wp_attachment_image_alt', sanitize_text_field($resultados['imagen']['alt'] ?? '')); wp_update_post([ 'ID' => $imagen_destacada_id, 'post_title' => sanitize_text_field($resultados['imagen']['title'] ?? '') ]); } } // Imágenes relevantes claramente if (!empty($resultados['refrito']['imagenes_relevantes'])) { foreach ($resultados['refrito']['imagenes_relevantes'] as $imagen) { if (!empty($imagen['url'])) { $imagen_id = aife_upload_image_from_url($imagen['url'], $post_id); if (!is_wp_error($imagen_id)) { update_post_meta($imagen_id, '_wp_attachment_image_alt', sanitize_text_field($imagen['alt'] ?? '')); wp_update_post(['ID' => $imagen_id, 'post_title' => sanitize_text_field($imagen['title'] ?? '')]); $imagen_url_local = wp_get_attachment_url($imagen_id); $resultados['refrito']['refrito'] = str_replace($imagen['url'], $imagen_url_local, $resultados['refrito']['refrito']); } } } wp_update_post(['ID' => $post_id, 'post_content' => wp_kses_post($resultados['refrito']['refrito'])]); } // Tags claramente if (!empty($resultados['tags']) && is_array($resultados['tags'])) { wp_set_post_tags($post_id, $resultados['tags']); } $categorias_ids = []; if (!empty($resultados['secciones']['secciones']['secciones'])) { foreach ($resultados['secciones']['secciones']['secciones'] as $seccion) { if (!empty($seccion['id']) && is_numeric($seccion['id'])) { $categorias_ids[] = (int) $seccion['id']; } } wp_set_post_categories($post_id, $categorias_ids); } wp_set_post_categories($post_id, $categorias_ids); // Yoast SEO metas claramente if (!empty($resultados['autoseo'])) { update_post_meta($post_id, '_yoast_wpseo_focuskw', sanitize_text_field($resultados['autoseo']['_yoast_wpseo_focuskw'] ?? '')); update_post_meta($post_id, '_yoast_wpseo_title', sanitize_text_field($resultados['autoseo']['_yoast_wpseo_title'] ?? '')); update_post_meta($post_id, '_yoast_wpseo_metadesc', sanitize_textarea_field($resultados['autoseo']['_yoast_wpseo_metadesc'] ?? '')); } // Marcar feed original como procesado claramente update_post_meta($feed_id, '_procesado_en_cola', true); // Defino meta claramente para identificar post de AI Feeds update_post_meta($post_id, '_post_from_ai_feed', $feed_id); // Respuesta ajustada claramente return new WP_REST_Response([ 'success' => true, 'resultado' => [ 'success' => true, 'mensaje' => 'Post importado correctamente.', 'data' => [ 'post_id' => $post_id, 'post_status' => $post_status ] ] ], 200); } function aife_upload_image_from_url($image_url, $post_id = 0) { require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; // Descargar la imagen temporalmente $tmp = download_url($image_url); if (is_wp_error($tmp)) { return $tmp; } // Preparar archivo para WordPress $file_array = [ 'name' => basename(wp_parse_url($image_url, PHP_URL_PATH)), 'tmp_name' => $tmp ]; // Manejar carga $attachment_id = media_handle_sideload($file_array, $post_id); // Limpiar archivo temporal si hubo error if (is_wp_error($attachment_id)) { @wp_delete_file($tmp); } return $attachment_id; }