Plugin Directory

Changeset 3270560


Ignore:
Timestamp:
04/10/2025 03:33:47 PM (11 months ago)
Author:
perseosoftware
Message:

Correccion sincronizacion de imagenes

Location:
perseo-software/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • perseo-software/trunk/PluginPerseo.php

    r3242181 r3270560  
    44Plugin URI: https://perseo.ec/
    55Description: Este Plugins integra el Sistema Contable Perseo Web y PC con la tienda Woocommerce
    6 Version: 31.0
     6Version: 32.0
    77Author: Perseo Soft S.A. - Ecuador
    88Author URI: https://perseo.ec
     
    1313*/
    1414
    15 $version_Plugin = '30.0';
     15$version_Plugin = '32.0';
    1616define('PERSEO_DIR_PATH', plugin_dir_path(__FILE__));
    1717define('PERSEOCONFIGBASE', get_option('pluginperseo_configuracion'));
  • perseo-software/trunk/PluginPerseoClientes.php

    r3242181 r3270560  
    11<?php
    2 $version_Plugin = '31.0';
     2$version_Plugin = '32.0';
    33//define('PERSEO_DIR_PATH', plugin_dir_path(__FILE__));
    44////////////////////////////////////////////////////////////////
  • perseo-software/trunk/README.md

    r3242181 r3270560  
    11=== PluginPerseo ===
    22
    3 Contributors:  Perseo Soft S.A. - Ecuador
     3Contributors: Perseo Soft S.A. - Ecuador
    44Donate Link: https://perseo.ec/
    55Tags: auto publish, content marketing
     
    77Tested up to: 6.7.2
    88Requires PHP: 7.4
    9 Stable tag: 31.0
     9Stable tag: 32.0
    1010License: GPLv2 or later
    1111License URI: http://www.gnu.org/licenses/gpl-2.0.html
    1212
    13    
    1413This Pluing integrates the Accounting System Perseo Web and PC with the store.
    15    
    1614== Description ==
    1715
     
    3129== Installation ==
    3230
    33 
    3431Contact your Perseo Software support.
    3532
    3633== Frequently Asked Questions ==
    37  
     34
    3835= A question that someone might have =
    39  
     36
    4037An answer to that question.
    41  
     38
    4239= What about foo bar? =
    43  
     40
    4441Answer to foo bar dilemma.
    4542
     
    4744
    48451. perseo-1.jpg
    49 2.  == Changelog ==
     462. == Changelog ==
     47   = 32.0 =
     48
     49- Correcciones de sincronizacion de imagenes
     50
     51== Changelog ==
    5052= 31.0 =
    51 * Correcciones de sincronizacion de categorias
    52 * Recomendaciones: Eliminar categorias para que se vuelvan a sincronizar automaticamente.
    53  
     53
     54- Correcciones de sincronizacion de categorias
     55- Recomendaciones: Eliminar categorias para que se vuelvan a sincronizar automaticamente.
     56
    5457== Changelog ==
    5558= 30.0 =
    56 * Correcciones de optimización y correcciones varias
     59
     60- Correcciones de optimización y correcciones varias
    5761
    5862== Changelog ==
    5963= 29.0 =
    60 * Correccion errores tarifas
    61  
     64
     65- Correccion errores tarifas
     66
    6267== Changelog ==
    6368= 28.0 =
    64 * Actualizar campo fecha_sync
    65  
     69
     70- Actualizar campo fecha_sync
     71
    6672== Changelog ==
    6773= 27.0 =
    68 * Actualizacion iva y parametros.
     74
     75- Actualizacion iva y parametros.
    6976
    7077== Changelog ==
    7178= 26.0 =
    72 * Actualizacion api.
    73  
     79
     80- Actualizacion api.
     81
    7482== Changelog ==
    7583= 25.0 =
    76 * Actualizacion pc.
     84
     85- Actualizacion pc.
    7786
    7887== Changelog ==
    7988= 24.0 =
    80 * Actualizacion pc.
     89
     90- Actualizacion pc.
    8191
    8292== Changelog ==
    8393= 23.0 =
    84 * Actualizacion precios .
     94
     95- Actualizacion precios .
    8596
    8697== Changelog ==
    8798= 22.0 =
    88 * Actualizacion .
     99
     100- Actualizacion .
    89101
    90102== Changelog ==
    91103= 21.0 =
    92 * Actualizacion .
     104
     105- Actualizacion .
    93106
    94107== Changelog ==
    95108= 20.0 =
    96 * Actualizacion .
     109
     110- Actualizacion .
    97111
    98112== Changelog ==
    99113= 17.0 =
    100 * Actualizacion .
     114
     115- Actualizacion .
    101116
    102117= 16.0 =
    103 * Actualizacion .
     118
     119- Actualizacion .
    104120
    105121= 15.0 =
    106 * Actualizacion .
     122
     123- Actualizacion .
    107124
    108125= 14.0 =
    109 * Actualizacion .
     126
     127- Actualizacion .
    110128
    111129= 13.0 =
    112 * Actualizacion .
     130
     131- Actualizacion .
    113132
    114133= 12.0 =
    115 * Actualizacion .
     134
     135- Actualizacion .
    116136
    117137= 11.0 =
    118 * Actualizacion .
     138
     139- Actualizacion .
    119140
    120141= 10.0 =
    121 * Actualizacion .
     142
     143- Actualizacion .
    122144
    123145= 9.0 =
    124 * Actualizacion .
     146
     147- Actualizacion .
    125148
    126149= 8.0 =
    127 * Actualizacion .
     150
     151- Actualizacion .
    128152
    129153= 7.0 =
    130 * Actualizacion .
     154
     155- Actualizacion .
    131156
    132157= 6.0 =
    133 * Actualizacion .
     158
     159- Actualizacion .
    134160
    135161= 5.0 =
    136 * Actualizacion de correo.
     162
     163- Actualizacion de correo.
    137164
    138165= 5.0 =
    139 * Actualizacion de imagenes.
     166
     167- Actualizacion de imagenes.
    140168
    141169= 4.0 =
    142 * Actualizacion de stock.
     170
     171- Actualizacion de stock.
    143172
    144173= 3.0 =
    145 * A change since the previous version.
     174
     175- A change since the previous version.
    146176
    147177= 2.0 =
    148 * A change since the previous version.
    149 * Fixed a pagination bug.
     178
     179- A change since the previous version.
     180- Fixed a pagination bug.
    150181
    151182= 1.1 =
    152 * A change since the previous version.
    153 * Fixed a pagination bug.
     183
     184- A change since the previous version.
     185- Fixed a pagination bug.
    154186
    155187= 1.0 =
    156 * New: Basic functionalities for this plugin.
     188
     189- New: Basic functionalities for this plugin.
    157190
    158191== Upgrade Notice ==
    159192
    160193= 1.1 =
    161 Actualizacion de imagenes 
     194Actualizacion de imagenes
  • perseo-software/trunk/includes/PluginPerseo_cron.php

    r3242181 r3270560  
    12501250    private function procesar_imagenes_producto($productoID, $idPost, $wpdb, $table_prefix, $perseo_urlimagen, $perseo_config)
    12511251    {
     1252        // 🔹 Limpiar cache para evitar inconsistencias al agregar nuevas imágenes
     1253        static $imagenes_cache = [];
     1254        // Forzar la actualización del cache para este producto específico
     1255        unset($imagenes_cache[$productoID]);
     1256
    12521257        // 🔹 Obtener imágenes del producto desde la API en una sola llamada
    1253         static $imagenes_cache = [];
    12541258        if (!isset($imagenes_cache[$productoID])) {
    12551259            $body = ['api_key' => $perseo_config['perseotoken'], 'productosid' => $productoID];
     
    12661270
    12671271            $imagenes_cache[$productoID] = json_decode(wp_remote_retrieve_body($response), true)['productos_imagenes'] ?? [];
     1272
     1273            // Log para depuración
     1274            //error_log("Producto {$productoID}: " . count($imagenes_cache[$productoID]) . " imágenes obtenidas de la API");
    12681275        }
    12691276
     
    12771284        // 🔹 Obtener imágenes ya asociadas al producto
    12781285        $imagenes_existentes = $wpdb->get_results($wpdb->prepare(
    1279             "SELECT ID, guid FROM {$table_prefix}posts WHERE post_parent = %d AND post_type = 'attachment'",
     1286            "SELECT ID, guid, post_title FROM {$table_prefix}posts WHERE post_parent = %d AND post_type = 'attachment'",
    12801287            $idPost
    12811288        ), OBJECT_K);
     
    12851292        wp_mkdir_p($base_upload_path); // Crear el directorio si no existe
    12861293
     1294        // 🔹 Lista para seguimiento de imágenes procesadas
     1295        $imagenes_procesadas = [];
    12871296        $thumbnail_id = null;
    12881297        $gallery_ids = [];
    12891298        $counter = 1;
    12901299
    1291         foreach ($imagenes as $imagen) {
     1300        // Procesar las imágenes de la API
     1301        foreach ($imagenes as $index => $imagen) {
    12921302            if ($imagen["ecommerce"] != 1) {
     1303                //error_log("Imagen {$index} para producto {$productoID} no está habilitada para eCommerce, omitiendo");
    12931304                continue; // Solo procesamos imágenes habilitadas para eCommerce
    12941305            }
    12951306
    1296             $file_name = "{$productoID}_imagen_{$counter}.png";
     1307            // Usar un ID único para cada imagen basado en su índice en el array
     1308            $imagen_index = isset($imagen['imagenid']) ? $imagen['imagenid'] : $counter;
     1309            $file_name = "{$productoID}_imagen_{$imagen_index}.png";
    12971310            $file_path = "{$base_upload_path}/{$file_name}";
    12981311            $file_url = "{$upload_dir['baseurl']}/" . date('Y') . "/" . date('m') . "/{$file_name}";
    12991312
    1300             // 🔹 Evitar insertar imágenes duplicadas
    1301             $duplicada = false;
    1302             foreach ($imagenes_existentes as $img) {
    1303                 if ($img->guid === $file_url) {
    1304                     $duplicada = true;
     1313            // Log para depuración
     1314            //error_log("Procesando imagen {$imagen_index} para producto {$productoID}");
     1315
     1316            // Verificar si ya existe la imagen con el mismo nombre
     1317            $imagen_existente_id = null;
     1318            foreach ($imagenes_existentes as $id => $img) {
     1319                if (basename($img->guid) === $file_name || $img->post_title === sanitize_file_name($file_name)) {
     1320                    $imagen_existente_id = $id;
    13051321                    break;
    13061322                }
    13071323            }
    1308             if ($duplicada) {
    1309                 continue;
    1310             }
    1311 
    1312             // 🔹 Guardar imagen en el sistema de archivos
     1324
     1325            // 🔹 Guardar imagen en el sistema de archivos (siempre, para actualizar contenido)
    13131326            $image_data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $imagen['imagen']));
    13141327            if (file_put_contents($file_path, $image_data) === false) {
     
    13171330            }
    13181331
    1319             // 🔹 Insertar la imagen como adjunto en WordPress
    1320             $attachment = [
    1321                 'post_mime_type' => 'image/png',
    1322                 'post_title' => sanitize_file_name($file_name),
    1323                 'post_content' => '',
    1324                 'post_status' => 'inherit',
    1325                 'guid' => $file_url,
    1326                 'post_parent' => $idPost
    1327             ];
    1328 
    1329             $attachment_id = wp_insert_attachment($attachment, $file_path, $idPost);
    1330             if (is_wp_error($attachment_id)) {
    1331                 error_log("Error al insertar la imagen como adjunto para el producto {$productoID}");
    1332                 continue;
    1333             }
    1334 
    1335             // 🔹 Generar metadatos de la imagen
    1336             require_once ABSPATH . 'wp-admin/includes/image.php';
    1337             $attachment_metadata = wp_generate_attachment_metadata($attachment_id, $file_path);
    1338             wp_update_attachment_metadata($attachment_id, $attachment_metadata);
     1332            $attachment_id = null;
     1333
     1334            // Si ya existe la imagen, actualizamos sus metadatos
     1335            if ($imagen_existente_id) {
     1336                $attachment_id = $imagen_existente_id;
     1337
     1338                // Actualizar metadata de la imagen
     1339                require_once ABSPATH . 'wp-admin/includes/image.php';
     1340                $attachment_metadata = wp_generate_attachment_metadata($attachment_id, $file_path);
     1341                wp_update_attachment_metadata($attachment_id, $attachment_metadata);
     1342            } else {
     1343                // 🔹 Insertar la imagen como nuevo adjunto en WordPress
     1344                $attachment = [
     1345                    'post_mime_type' => 'image/png',
     1346                    'post_title' => sanitize_file_name($file_name),
     1347                    'post_content' => '',
     1348                    'post_status' => 'inherit',
     1349                    'guid' => $file_url,
     1350                    'post_parent' => $idPost
     1351                ];
     1352
     1353                $attachment_id = wp_insert_attachment($attachment, $file_path, $idPost);
     1354                if (is_wp_error($attachment_id)) {
     1355                    error_log("Error al insertar la imagen como adjunto para el producto {$productoID}");
     1356                    continue;
     1357                }
     1358
     1359                // 🔹 Generar metadatos de la imagen
     1360                require_once ABSPATH . 'wp-admin/includes/image.php';
     1361                $attachment_metadata = wp_generate_attachment_metadata($attachment_id, $file_path);
     1362                wp_update_attachment_metadata($attachment_id, $attachment_metadata);
     1363            }
     1364
     1365            // Marcar que esta imagen fue procesada
     1366            $imagenes_procesadas[] = $attachment_id;
    13391367
    13401368            // 🔹 Asignar como imagen destacada o añadir a la galería
    1341             if ($imagen['primera'] == 1 && !$thumbnail_id) {
     1369            if ($imagen['primera'] == 1) {
    13421370                $thumbnail_id = $attachment_id; // Guardar como imagen destacada
    1343             } else {
    1344                 $gallery_ids[] = $attachment_id; // Agregar a la galería
     1371                //error_log("Imagen {$attachment_id} marcada como destacada para producto {$productoID}");
     1372            }
     1373
     1374            // Siempre añadir a la galería (incluyendo la destacada)
     1375            if (!in_array($attachment_id, $gallery_ids)) {
     1376                $gallery_ids[] = $attachment_id;
     1377                //error_log("Imagen {$attachment_id} añadida a la galería para producto {$productoID}");
    13451378            }
    13461379
    13471380            $counter++;
    13481381        }
     1382
     1383        // 🔹 Eliminar imágenes antiguas que ya no existen en la API
     1384        foreach ($imagenes_existentes as $id => $img) {
     1385            if (!in_array($id, $imagenes_procesadas)) {
     1386                //error_log("Eliminando imagen obsoleta ID {$id} para producto {$productoID}");
     1387                wp_delete_attachment($id, true); // Eliminar permanentemente
     1388            }
     1389        }
     1390
     1391        // Log para depuración
     1392        error_log("Producto {$productoID}: Procesadas " . count($imagenes_procesadas) . " imágenes, " . count($gallery_ids) . " en galería");
    13491393
    13501394        // 🔹 Asignar imagen destacada al producto
     
    13531397        }
    13541398
    1355         // 🔹 Asignar galería de imágenes al producto
     1399        // 🔹 Asignar galería de imágenes al producto (siempre actualizar toda la galería)
    13561400        if (!empty($gallery_ids)) {
    13571401            update_post_meta($idPost, '_product_image_gallery', implode(',', $gallery_ids));
    1358         }
     1402        } else {
     1403            delete_post_meta($idPost, '_product_image_gallery');
     1404        }
     1405        // Forzar una actualización completa del caché de WordPress
     1406        wp_cache_flush();
     1407        clean_post_cache($idPost);
     1408
     1409        // Limpiar caché específica de imágenes adjuntas
     1410        foreach ($imagenes_procesadas as $attachment_id) {
     1411            clean_attachment_cache($attachment_id);
     1412        }
     1413
     1414        // Regenerar caché de thumbnails
     1415        update_option('_transient_timeout_wc_product_gallery_' . $idPost, 1);
     1416        delete_transient('wc_product_gallery_' . $idPost);
    13591417    }
    13601418}
Note: See TracChangeset for help on using the changeset viewer.