Changeset 3329687
- Timestamp:
- 07/17/2025 12:28:40 PM (7 months ago)
- Location:
- presta-products/trunk
- Files:
-
- 1 added
- 7 edited
-
assets/javascript/ppgbo-admin.js (modified) (4 diffs)
-
includes/tabs/cache.php (modified) (3 diffs)
-
includes/tabs/categories.php (modified) (1 diff)
-
includes/tabs/used.php (modified) (1 diff)
-
presta-products.php (modified) (7 diffs)
-
readme.txt (modified) (2 diffs)
-
scripts/cron/crontab-presta-products.php (modified) (4 diffs)
-
scripts/cron/logs (added)
Legend:
- Unmodified
- Added
- Removed
-
presta-products/trunk/assets/javascript/ppgbo-admin.js
r3183727 r3329687 15 15 16 16 jQuery(function($) { 17 $('# exportCSV').on('click', function(e) {17 $('#ppgbo_exportCSV').on('click', function(e) { 18 18 $('#usedTable').tableExport( 19 19 { … … 26 26 27 27 // Bouton de MAJ du cache 28 $('# majCache').on('click', function(e) {28 $('#ppgbo_majCache').on('click', function(e) { 29 29 var params = { 30 'action': ' maj',30 'action': 'ppgbo_maj', 31 31 }; 32 32 … … 55 55 56 56 // Bouton de RAZ du cache 57 $('# razCache').on('click', function(e) {57 $('#ppgbo_razCache').on('click', function(e) { 58 58 var params = { 59 'action': ' raz',59 'action': 'ppgbo_raz', 60 60 }; 61 61 … … 75 75 76 76 // Bouton de suppression d'une correspondance 77 $('. deleteCorrespondance').on('click', function(e) {77 $('.ppgbo_deleteCorrespondance').on('click', function(e) { 78 78 var params = { 79 79 'action': 'deleteCorrespondance', -
presta-products/trunk/includes/tabs/cache.php
r2809664 r3329687 45 45 <div class="mt-4 mb-0 alert alert-primary"> 46 46 <?php echo __( 'Vous pouvez ajouter une ligne dans votre crontab si vous souhaitez que la tâche soit exécutée automatiquement. Voici un exemple :', 'presta-products' ); ?><br> 47 <?php echo sprintf(__( '12 1 * * * /usr/local/bin/php %s > /dev/null 2>&1', 'presta-products' ), $script_cron); ?>47 <?php echo sprintf(__( '12 1 * * * /usr/local/bin/php %s > %s 2>&1', 'presta-products' ), $script_cron, $logs_cron); ?> 48 48 </div> 49 49 <i class="small"><?php echo __( 'Vous pouvez trouver un tutoriel de la création d\'une tâche cron sur votre serveur en suivant <a href="https://www.guillaume-bouaud.fr/blog/wordpress/ajouter-une-routine-dans-la-crontab" target="_blank">ce lien</a>.', 'presta-products' ); ?></i> … … 64 64 <div class="row mt-4"> 65 65 <div class="col-sm-2 offset-sm-3"> 66 <button type="button" class="btn btn-primary btn-lg" id=" majCache"><?php echo __( 'Mettre à jour le cache', 'presta-products' ); ?></button>66 <button type="button" class="btn btn-primary btn-lg" id="ppgbo_majCache"><?php echo __( 'Mettre à jour le cache', 'presta-products' ); ?></button> 67 67 </div> 68 68 <div class="col-sm-2 offset-sm-2"> 69 <button type="button" class="btn btn-danger btn-lg" id=" razCache"><?php echo __( 'Supprimer le cache', 'presta-products' ); ?></button>69 <button type="button" class="btn btn-danger btn-lg" id="ppgbo_razCache"><?php echo __( 'Supprimer le cache', 'presta-products' ); ?></button> 70 70 </div> 71 71 </div> … … 125 125 <div class="row mt-4"> 126 126 <div class="col-sm-2 offset-sm-5"> 127 <button type="button" class="btn btn-danger btn-lg" id=" razCache"><?php echo __( 'Supprimer le cache', 'presta-products' ); ?></button>127 <button type="button" class="btn btn-danger btn-lg" id="ppgbo_razCache"><?php echo __( 'Supprimer le cache', 'presta-products' ); ?></button> 128 128 </div> 129 129 </div> -
presta-products/trunk/includes/tabs/categories.php
r3183727 r3329687 105 105 echo '<td>' . esc_html(get_the_category_by_ID($element->id_cat_wordpress)) . '</td>'; 106 106 echo '<td>' . esc_html($element->id_cat_prestashop) . '</td>'; 107 echo '<td><a class="btn btn-primary btn-sm" href="?page=presta-products&tab=categories&id=' . $element->id . '"><span class="dashicons dashicons-edit"></span></a> <a class="btn btn-danger btn-sm deleteCorrespondance" href="javascript:;" data-id="' . $element->id . '"><span class="dashicons dashicons-trash"></span></a></td>';107 echo '<td><a class="btn btn-primary btn-sm" href="?page=presta-products&tab=categories&id=' . $element->id . '"><span class="dashicons dashicons-edit"></span></a> <a class="btn btn-danger btn-sm ppgbo_deleteCorrespondance" href="javascript:;" data-id="' . $element->id . '"><span class="dashicons dashicons-trash"></span></a></td>'; 108 108 echo '</tr>'; 109 109 } -
presta-products/trunk/includes/tabs/used.php
r2853481 r3329687 7 7 </div> 8 8 <div class="col-sm-2 text-end"> 9 <button type="button" class="btn btn-success btn" id=" exportCSV"><?php echo __( 'Exporter en CSV', 'presta-products' ); ?></button>9 <button type="button" class="btn btn-success btn" id="ppgbo_exportCSV"><?php echo __( 'Exporter en CSV', 'presta-products' ); ?></button> 10 10 </div> 11 11 </h3> -
presta-products/trunk/presta-products.php
r3183727 r3329687 4 4 * Plugin URI: https://www.guillaume-bouaud.fr/produit/presta-products-for-wordpress 5 5 * Description: Affichage des produits Prestashop sur le site internet généré par Wordpress. 6 * Version: 1.2. 06 * Version: 1.2.1 7 7 * Requires at least: 5.2 8 8 * Requires PHP: 7.2 … … 519 519 $sql = $wpdb->prepare("TRUNCATE TABLE %i", $myTable); 520 520 $wpdb->query($sql) or wp_die(__( 'Problème de réinitialisation du cache.', 'presta-products' )); 521 521 522 522 echo __( 'Données supprimées.', 'presta-products' ); 523 523 … … 526 526 } 527 527 528 add_action( 'wp_ajax_ raz', 'ppgbo_raz_cache' );528 add_action( 'wp_ajax_ppgbo_raz', 'ppgbo_raz_cache' ); 529 529 530 530 // MAJ du cache … … 548 548 // Lire le contenu du fichier functions.php 549 549 $functions_content[] = (object) array('post_content' => file_get_contents($functions_file)); 550 //echo '<pre>'; var_dump(array_merge($posts, $pages, $functions_content)); die; 550 551 551 552 // V1.1.27 : ajout de $functions_content … … 592 593 // Si problème d'enregistrement (false = problem, 0 peut être le nombre de lignes créées (ignore into)) 593 594 if ($result === false) { 594 wp_die(__( 'Problème d\'enregistrement de la donnée.', 'presta-products' ));595 echo sprintf(__( 'Problème d\'enregistrement du shortcode : %s.', 'presta-products' ), json_encode($params)) . "<br>"; 595 596 } 596 597 } … … 605 606 } 606 607 607 add_action( 'wp_ajax_ maj', 'ppgbo_maj_cache' );608 add_action( 'wp_ajax_ppgbo_maj', 'ppgbo_maj_cache' ); 608 609 609 610 // Fonctions de tri … … 1167 1168 $dots_carrousel_2 = plugins_url( 'assets/images/circle.svg', __FILE__ ); 1168 1169 $script_cron = plugin_dir_path( __FILE__ ) . plugin_dir_path( 'scripts/cron/crontab-presta-products.php' ) . 'crontab-presta-products.php'; 1170 $logs_cron = plugin_dir_path( __FILE__ ) . plugin_dir_path( 'scripts/cron/crontab-presta-products.php' ) . 'logs/presta-products_$(env date +\%Y-\%m-\%d-\%H-\%M).log'; 1169 1171 1170 1172 // Gestion du message d'information -
presta-products/trunk/readme.txt
r3200138 r3329687 5 5 Requires at least: 4.7 6 6 Requires PHP: 5.6 7 Tested up to: 6. 7.18 Stable tag: 1.2. 07 Tested up to: 6.8.2 8 Stable tag: 1.2.1 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 64 64 == Changelog == 65 65 66 = 1.2.1 = (17/07/2025) 67 * Testée jusqu’à la version 6.8.2 de WordPress 68 * Fixed: Traitement de compatibilité inter modules 69 * Fixed: Amélioration du lancement en cron du cache (système d'erreur sur shortcode en log) (PREMIUM) 70 * Nouveau: Ajout d'un système de log avec fichiers lors de la mise en cache en cron (PREMIUM) 71 66 72 = 1.2.0 = (07/11/2024) 67 * Testée jusqu’à la version 6.6. 2de WordPress73 * Testée jusqu’à la version 6.6.1 de WordPress 68 74 * Nouveau: Gestion de matching des catégories Prestashop avec des catégories WP (PREMIUM) 69 75 * Fixed: Amélioration des flux API -
presta-products/trunk/scripts/cron/crontab-presta-products.php
r3042581 r3329687 22 22 23 23 // Récupération des articles et pages 24 $posts = get_posts(array('posts_per_page' => -1)); 25 $pages = get_pages(); 24 $posts = get_posts(array('posts_per_page' => -1, 'post_status' => array('publish', 'draft', 'pending'))); 25 $pages = get_pages(array('post_status' => array('publish', 'draft', 'pending'))); 26 27 // V1.1.27 : ajout 28 // Chemin du fichier functions.php du thème en cours 29 $functions_file = get_template_directory() . '/functions.php'; 30 31 // V1.1.27 : ajout 32 // Lire le contenu du fichier functions.php 33 $functions_content[] = (object) array('post_content' => file_get_contents($functions_file)); 26 34 27 35 // Parcours pour obtenir les shortcodes 28 foreach (array_merge($posts, $pages ) as $post) {36 foreach (array_merge($posts, $pages, $functions_content) as $post) { 29 37 // Si le shortcode est trouvé 30 38 if (preg_match_all('/^(.*)(\[ppgbo[a-zA-Z0-9=",\s]+\])(.*)$/misU', $post->post_content, $matched)) { … … 45 53 $params[$param[0]] = preg_replace('/]/', '', preg_replace('/"/', '', $param[1])); 46 54 } 47 48 // Récupération des produits 49 $datas = $this->ppgbo_get_products_from_prestashop_cron($params); 50 51 foreach ($datas as $data) { 52 $products[] = array('url' => $data->url_rewrite, 'name' => $data->product->name); 53 } 54 55 // Requête SQL d'ajout 56 $sql = $wpdb->prepare("INSERT IGNORE INTO {$wpdb->prefix}ppgbo_cache (shortcode, datas, products) VALUES ('%s', '%s', '%s')", json_encode($params), json_encode($datas), json_encode($products)); 57 $wpdb->query($sql) or wp_die(__( 'Problème d\'enregistrement de la donnée.', 'presta-products' )); 55 56 // Vérification qu'il n'est pas déjà présent en base 57 $myTable = $wpdb->prefix.'ppgbo_cache'; 58 $sql0 = $wpdb->prepare("SELECT id FROM $myTable WHERE shortcode = '%s'", json_encode($params)); 59 $elements = $wpdb->get_results($sql0); 60 61 // Si non présent, on ajoute 62 if (count($elements) == 0) { 63 // Récupération des produits 64 $datas = $this->ppgbo_get_products_from_prestashop_cron($params); 65 66 foreach ($datas as $data) { 67 $products[] = array('url' => $data->url_rewrite, 'name' => $data->product->name); 68 } 69 70 // Requête SQL d'ajout 71 $myTable = $wpdb->prefix.'ppgbo_cache'; 72 $sql1 = $wpdb->prepare("INSERT IGNORE INTO $myTable (shortcode, datas, products) VALUES ('%s', '%s', '%s')", json_encode($params), json_encode($datas), json_encode($products)); 73 $result = $wpdb->query($sql1); 74 75 // Si problème d'enregistrement (false = problem, 0 peut être le nombre de lignes créées (ignore into)) 76 if ($result === false) { 77 echo sprintf(__( 'Problème d\'enregistrement du shortcode : %s.', 'presta-products' ), json_encode($params)) . PHP_EOL; 78 } 79 } 58 80 } 59 81 } … … 65 87 // Fonction de récupération des produits depuis le site prestashop 66 88 function ppgbo_get_products_from_prestashop_cron($atts) { 67 $prestashop_url = get_option( 'ppgbo_prestashop_url' ); 68 $webservice_key = get_option( 'ppgbo_prestashop_webservice' ); 69 $hide_inactive = get_option( 'ppgbo_hide_inactive' ); 70 $hide_available = get_option( 'ppgbo_hide_available' ); 71 $hide_visibility = get_option( 'ppgbo_hide_visibility' ); 72 73 // Limitation du nombre de produits 74 $limit = (isset($atts['counter'])) ? $atts['counter'] : 5; 75 76 // Initialisation du tableau des produits 77 $products = array(); 78 79 // GBO - 1.1.21 80 // Gestion Bestsellers 81 if (isset($atts['bestsellers'])) { 82 // Nombre de jours en arrière 83 $days = (isset($atts['bestsellers']) && intval($atts['bestsellers'])) ? $atts['bestsellers'] : 10; 84 $today = date('Y-m-d'); 85 $back = date('Y-m-d', time() - ($days * 24 * 60 * 60)); 86 87 // Appel HTTP API 88 $link = $prestashop_url . '/api/orders/?filter[date_add]=[' . $back . ',' . $today . ']&display=full&date=1&ws_key=' . $webservice_key . '&output_format=JSON'; 89 $response = wp_remote_get( $link ); 90 $body = wp_remote_retrieve_body( $response ); 91 $http_code = wp_remote_retrieve_response_code( $response ); 92 93 if ($http_code == '200') { 94 // Produit (format JSON > Array) 95 $result = json_decode($body); 96 $_products = array(); 97 98 // Parcours des lignes 99 foreach ($result->orders as $element) { 100 foreach ($element->associations->order_rows as $row) { 101 $_products[$row->product_id] = intval($_products[$row->product_id]) + 1; 102 } 103 } 104 105 // Fonctions de traitement du tableau 106 arsort($_products); 107 //$_products = array_keys($_products); 108 109 // Si un 2ème paramètre en plus de bestsellers 110 if (isset($atts['product'])) { 111 if (array_key_exists($atts['product'], $_products)) { 112 $url[] = $prestashop_url . '/api/products/' . $atts['product'] . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 113 } 114 } 115 else if (isset($atts['products'])) { 116 $ids = explode(',', $atts['products']); 117 118 foreach ($ids as $id) { 119 if (array_key_exists($id, $_products)) { 120 $url[] = $prestashop_url . '/api/products/' . $id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 121 } 122 } 123 } 124 else if (isset($atts['category'])) { 125 // Appel HTTP API 126 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $atts['category'] . ']&ws_key=' . $webservice_key . '&output_format=JSON'; 127 $response = wp_remote_get( $link ); 128 $body = wp_remote_retrieve_body( $response ); 129 $http_code = wp_remote_retrieve_response_code( $response ); 130 131 if ($http_code == '200') { 132 // Produit (format JSON > Array) 133 $result = json_decode($body); 134 135 foreach ($result->products as $element) { 136 if (array_key_exists($element->id, $_products)) { 137 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 138 } 139 } 140 } 141 } 142 else if (isset($atts['categories'])) { 143 $ids = explode(',', $atts['categories']); 144 145 foreach ($ids as $id) { 146 // Appel HTTP API 147 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $id . ']&ws_key=' . $webservice_key . '&output_format=JSON'; 148 $response = wp_remote_get( $link ); 149 $body = wp_remote_retrieve_body( $response ); 150 $http_code = wp_remote_retrieve_response_code( $response ); 151 152 if ($http_code == '200') { 153 // Produit (format JSON > Array) 154 $result = json_decode($body); 155 156 foreach ($result->products as $element) { 157 if (array_key_exists($element->id, $_products)) { 158 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 159 } 160 } 161 } 162 } 163 } 164 else { 165 // Fonctions de traitement du tableau 166 array_splice($_products, $limit); 167 168 // Récupération produit 169 foreach ($_products as $element) { 170 $url[] = $prestashop_url . '/api/products/' . $element . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 171 } 172 } 173 174 // Fonctions de traitement du tableau 175 array_splice($url, $limit); 176 } 177 } 178 // 1 produit 179 else if (isset($atts['product'])) { 180 $url[] = $prestashop_url . '/api/products/' . $atts['product'] . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 181 } 182 // Plusieurs produits 183 else if (isset($atts['products'])) { 184 $ids = explode(',', $atts['products']); 185 186 foreach ($ids as $id) { 187 $url[] = $prestashop_url . '/api/products/' . $id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 188 } 189 } 190 // Catégorie 191 else if (isset($atts['category'])) { 192 // Appel HTTP API 193 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $atts['category'] . ']&limit=' . $limit . '&ws_key=' . $webservice_key . '&output_format=JSON'; 194 $response = wp_remote_get( $link ); 195 $body = wp_remote_retrieve_body( $response ); 196 $http_code = wp_remote_retrieve_response_code( $response ); 197 198 if ($http_code == '200') { 199 // Produit (format JSON > Array) 200 $result = json_decode($body); 201 202 foreach ($result->products as $element) { 203 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 204 } 205 } 206 } 207 // Plusieurs catégories 208 else if (isset($atts['categories'])) { 209 $ids = explode(',', $atts['categories']); 210 211 foreach ($ids as $id) { 89 $prestashop_url = get_option( 'ppgbo_prestashop_url' ); 90 $webservice_key = get_option( 'ppgbo_prestashop_webservice' ); 91 $hide_inactive = get_option( 'ppgbo_hide_inactive' ); 92 $hide_available = get_option( 'ppgbo_hide_available' ); 93 $hide_visibility = get_option( 'ppgbo_hide_visibility' ); 94 $hide_stock = get_option( 'ppgbo_hide_stock' ); 95 96 // Limitation du nombre de produits 97 $limit = (isset($atts['counter'])) ? $atts['counter'] : 5; 98 99 // Initialisation du tableau des produits 100 $products = array(); 101 102 // GBO - 1.1.21 103 // Gestion Bestsellers 104 if (isset($atts['bestsellers'])) { 105 // Nombre de jours en arrière 106 $days = (isset($atts['bestsellers']) && intval($atts['bestsellers'])) ? $atts['bestsellers'] : 10; 107 $today = date('Y-m-d'); 108 $back = date('Y-m-d', time() - ($days * 24 * 60 * 60)); 109 212 110 // Appel HTTP API 213 $link = $prestashop_url . '/api/ products/?filter[id_category_default]=[' . $id . ']&limit=' . $limit . '&ws_key=' . $webservice_key . '&output_format=JSON';111 $link = $prestashop_url . '/api/orders/?filter[date_add]=[' . $back . ',' . $today . ']&display=full&date=1&ws_key=' . $webservice_key . '&output_format=JSON'; 214 112 $response = wp_remote_get( $link ); 215 113 $body = wp_remote_retrieve_body( $response ); 216 114 $http_code = wp_remote_retrieve_response_code( $response ); 115 116 if ($http_code == '200') { 117 // Produit (format JSON > Array) 118 $result = json_decode($body); 119 $_products = array(); 120 121 // Parcours des lignes 122 foreach ($result->orders as $element) { 123 foreach ($element->associations->order_rows as $row) { 124 $_products[$row->product_id] = intval($_products[$row->product_id]) + 1; 125 } 126 } 127 128 // Fonctions de traitement du tableau 129 arsort($_products); 130 //$_products = array_keys($_products); 131 132 // Si un 2ème paramètre en plus de bestsellers 133 if (isset($atts['product'])) { 134 if (array_key_exists($atts['product'], $_products)) { 135 $url[] = $prestashop_url . '/api/products/' . $atts['product'] . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 136 } 137 } 138 else if (isset($atts['products'])) { 139 $ids = explode(',', $atts['products']); 140 141 foreach ($ids as $id) { 142 if (array_key_exists($id, $_products)) { 143 $url[] = $prestashop_url . '/api/products/' . $id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 144 } 145 } 146 } 147 else if (isset($atts['auto_category'])) { 148 $id_category = $this::getCorrespondance(get_the_category()[0]->cat_ID); 149 150 // Appel HTTP API 151 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $id_category . ']&ws_key=' . $webservice_key . '&output_format=JSON'; 152 $response = wp_remote_get( $link ); 153 $body = wp_remote_retrieve_body( $response ); 154 $http_code = wp_remote_retrieve_response_code( $response ); 155 156 if ($http_code == '200') { 157 // Produit (format JSON > Array) 158 $result = json_decode($body); 159 160 foreach ($result->products as $element) { 161 if (array_key_exists($element->id, $_products)) { 162 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 163 } 164 } 165 } 166 } 167 else if (isset($atts['category'])) { 168 // Appel HTTP API 169 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $atts['category'] . ']&ws_key=' . $webservice_key . '&output_format=JSON'; 170 $response = wp_remote_get( $link ); 171 $body = wp_remote_retrieve_body( $response ); 172 $http_code = wp_remote_retrieve_response_code( $response ); 173 174 if ($http_code == '200') { 175 // Produit (format JSON > Array) 176 $result = json_decode($body); 177 178 foreach ($result->products as $element) { 179 if (array_key_exists($element->id, $_products)) { 180 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 181 } 182 } 183 } 184 } 185 else if (isset($atts['categories'])) { 186 $ids = explode(',', $atts['categories']); 187 188 foreach ($ids as $id) { 189 // Appel HTTP API 190 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $id . ']&ws_key=' . $webservice_key . '&output_format=JSON'; 191 $response = wp_remote_get( $link ); 192 $body = wp_remote_retrieve_body( $response ); 193 $http_code = wp_remote_retrieve_response_code( $response ); 194 195 if ($http_code == '200') { 196 // Produit (format JSON > Array) 197 $result = json_decode($body); 198 199 foreach ($result->products as $element) { 200 if (array_key_exists($element->id, $_products)) { 201 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 202 } 203 } 204 } 205 } 206 } 207 else { 208 // Fonctions de traitement du tableau 209 array_splice($_products, $limit); 210 211 // Récupération produit 212 foreach ($_products as $element) { 213 $url[] = $prestashop_url . '/api/products/' . $element . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 214 } 215 } 216 217 // Fonctions de traitement du tableau 218 array_splice($url, $limit); 219 } 220 } 221 // 1 produit 222 else if (isset($atts['product'])) { 223 $url[] = $prestashop_url . '/api/products/' . $atts['product'] . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 224 } 225 // Plusieurs produits 226 else if (isset($atts['products'])) { 227 $ids = explode(',', $atts['products']); 228 229 foreach ($ids as $id) { 230 $url[] = $prestashop_url . '/api/products/' . $id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 231 } 232 } 233 // Correspondance Catégories 234 else if (isset($atts['auto_category'])) { 235 $id_category = getCorrespondance(get_the_category()[0]->cat_ID); 236 237 // Appel HTTP API 238 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $id_category . ']&limit=' . $limit . '&ws_key=' . $webservice_key . '&output_format=JSON'; 239 $response = wp_remote_get( $link ); 240 $body = wp_remote_retrieve_body( $response ); 241 $http_code = wp_remote_retrieve_response_code( $response ); 217 242 218 243 if ($http_code == '200') { … … 224 249 } 225 250 } 226 } 227 } 251 } 252 // Catégorie 253 else if (isset($atts['category'])) { 254 // Appel HTTP API 255 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $atts['category'] . ']&limit=' . $limit . '&ws_key=' . $webservice_key . '&output_format=JSON'; 256 $response = wp_remote_get( $link ); 257 $body = wp_remote_retrieve_body( $response ); 258 $http_code = wp_remote_retrieve_response_code( $response ); 259 260 if ($http_code == '200') { 261 // Produit (format JSON > Array) 262 $result = json_decode($body); 263 264 foreach ($result->products as $element) { 265 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 266 } 267 } 268 } 269 // Plusieurs catégories 270 else if (isset($atts['categories'])) { 271 $ids = explode(',', $atts['categories']); 272 273 foreach ($ids as $id) { 274 // Appel HTTP API 275 $link = $prestashop_url . '/api/products/?filter[id_category_default]=[' . $id . ']&limit=' . $limit . '&ws_key=' . $webservice_key . '&output_format=JSON'; 276 $response = wp_remote_get( $link ); 277 $body = wp_remote_retrieve_body( $response ); 278 $http_code = wp_remote_retrieve_response_code( $response ); 279 280 if ($http_code == '200') { 281 // Produit (format JSON > Array) 282 $result = json_decode($body); 283 284 foreach ($result->products as $element) { 285 $url[] = $prestashop_url . '/api/products/' . $element->id . '?price[my_price][use_tax]=1&price[old_price][only_reduction]=1&ws_key=' . $webservice_key . '&output_format=JSON'; 286 } 287 } 288 } 289 } 228 290 229 // V1.1.19 - BEGIN - Ajout de la gestion WPML 230 // WPML : Récupération des langues de Prestashop. Nécessite le Webservice "languages" 231 $wpml_languages = apply_filters( 'wpml_active_languages', null ); 232 if (isset($wpml_languages) && !is_null($wpml_languages) && !empty($wpml_languages)) { 233 foreach($wpml_languages as $languages) { 234 if ($languages['active']) { 235 $langue_actuel = $languages['language_code'];//default_locale']; 236 break; 237 } 238 } 239 $link = $prestashop_url . '/api/languages/?display=[id,iso_code]&ws_key=' . $webservice_key . '&output_format=JSON'; 240 $response = wp_remote_get( $link ); 241 $body = wp_remote_retrieve_body( $response ); 242 $http_code = wp_remote_retrieve_response_code( $response ); 291 // V1.1.19 - BEGIN - Ajout de la gestion WPML 292 // WPML : Récupération des langues de Prestashop. Nécessite le Webservice "languages" 293 $wpml_languages = apply_filters( 'wpml_active_languages', null ); 294 if (isset($wpml_languages) && !is_null($wpml_languages) && !empty($wpml_languages)) { 295 foreach($wpml_languages as $languages) { 296 if ($languages['active']) { 297 $langue_actuel = $languages['language_code'];//default_locale']; 298 break; 299 } 300 } 301 $link = $prestashop_url . '/api/languages/?display=[id,iso_code]&ws_key=' . $webservice_key . '&output_format=JSON'; 302 $response = wp_remote_get( $link ); 303 $body = wp_remote_retrieve_body( $response ); 304 $http_code = wp_remote_retrieve_response_code( $response ); 305 306 if ($http_code == '200') { 307 $prest_languages = json_decode($body); 308 } 309 } 310 // V1.1.19 - END 311 312 // Polylang : Récupération des langues de Prestashop. Nécessite le Webservice "languages" 313 if (function_exists('pll_current_language')) { 314 $langue_actuel = pll_current_language('slug'); 315 $link = $prestashop_url . '/api/languages/?display=[id,iso_code]&ws_key=' . $webservice_key . '&output_format=JSON'; 316 $response = wp_remote_get( $link ); 317 $body = wp_remote_retrieve_body( $response ); 318 $http_code = wp_remote_retrieve_response_code( $response ); 319 320 if ($http_code == '200') { 321 $prest_languages = json_decode($body); 322 } 323 } 243 324 244 if ($http_code == '200') { 245 $prest_languages = json_decode($body); 325 // Si l'utilisateur force la langue 326 if (isset($atts['language'])) { 327 $langue_actuel = $atts['language']; 328 $link = $prestashop_url . '/api/languages/?display=[id,iso_code]&ws_key=' . $webservice_key . '&output_format=JSON'; 329 $response = wp_remote_get( $link ); 330 $body = wp_remote_retrieve_body( $response ); 331 $http_code = wp_remote_retrieve_response_code( $response ); 332 333 if ($http_code == '200') { 334 $prest_languages = json_decode($body); 335 } 246 336 } 247 } 248 // V1.1.19 - END 249 250 // Polylang : Récupération des langues de Prestashop. Nécessite le Webservice "languages" 251 if (function_exists('pll_current_language')) { 252 $langue_actuel = pll_current_language('slug'); 253 $link = $prestashop_url . '/api/languages/?display=[id,iso_code]&ws_key=' . $webservice_key . '&output_format=JSON'; 254 $response = wp_remote_get( $link ); 255 $body = wp_remote_retrieve_body( $response ); 256 $http_code = wp_remote_retrieve_response_code( $response ); 257 258 if ($http_code == '200') { 259 $prest_languages = json_decode($body); 260 } 261 } 262 263 // Si l'utilisateur force la langue 264 if (isset($atts['language'])) { 265 $langue_actuel = $atts['language']; 266 $link = $prestashop_url . '/api/languages/?display=[id,iso_code]&ws_key=' . $webservice_key . '&output_format=JSON'; 267 $response = wp_remote_get( $link ); 268 $body = wp_remote_retrieve_body( $response ); 269 $http_code = wp_remote_retrieve_response_code( $response ); 270 271 if ($http_code == '200') { 272 $prest_languages = json_decode($body); 273 } 274 } 275 276 // Récupération des produits 277 foreach ($url as $link) { 278 // Appel HTTP API 279 $response = wp_remote_get( $link ); 280 $body = wp_remote_retrieve_body( $response ); 281 $http_code = wp_remote_retrieve_response_code( $response ); 282 283 if ($http_code == '200') { 284 // Produit (format JSON > Array) 285 $product = json_decode($body); 286 287 // Si le produit est trouvé, on l'ajoute au tableau des produits 288 if (isset($product->product)) { 289 // Si on veut cacher des produits 290 if (isset($product->product->active) && $product->product->active == 0 && $hide_inactive) { 291 continue; 292 } 293 else if (isset($product->product->available_for_order) && $product->product->available_for_order == 0 && $hide_available) { 294 continue; 295 } 296 else if (isset($product->product->visibility) && $product->product->visibility == 'none' && $hide_visibility) { 297 continue; 298 } 299 else if (isset($product->product->quantity) && $product->product->quantity <= 0 && $hide_stock) { 300 continue; 301 } 337 338 // Récupération des produits 339 foreach ($url as $link) { 340 // Appel HTTP API 341 $response = wp_remote_get( $link ); 342 $body = wp_remote_retrieve_body( $response ); 343 $http_code = wp_remote_retrieve_response_code( $response ); 344 345 if ($http_code == '200') { 346 // Produit (format JSON > Array) 347 $product = json_decode($body); 302 348 303 // Génération de l'URL produit 304 $url_rewrite = $prestashop_url . '/index.php?controller=product&id_product=' . $product->product->id; 305 $url_rewrite_element = wp_remote_head($url_rewrite); 306 307 // Récupération de la redirection par les méthodes WP 308 if (is_array($url_rewrite_element) && isset($url_rewrite_element['headers']) && isset($url_rewrite_element['headers']['location'])) { 309 $product->url_rewrite = $url_rewrite_element['headers']['location']; 310 } 311 312 // Récupération de la redirection par les fonctions PHP (si la précédente n'a pas marché) 313 if (empty($product->url_rewrite) || !isset($product->url_rewrite)) { 314 $get_headers = get_headers($url_rewrite, 1); 315 316 if (isset($get_headers['Location'])) { 317 $product->url_rewrite = $get_headers['Location']; 318 } 319 } 320 321 // Si les fonctions WP et PHP ne fonctionnent pas, on essaie le curl 322 if (empty($product->url_rewrite) || !isset($product->url_rewrite)) { 323 // Initialisation du curl 324 $ch = curl_init(); 325 326 // Paramètres du curl 327 curl_setopt($ch, CURLOPT_URL, $url_rewrite); 328 curl_setopt($ch, CURLOPT_HEADER, true); 329 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 330 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 331 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 332 curl_setopt($ch, CURLOPT_TIMEOUT, 15); 333 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 334 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 335 349 // Si le produit est trouvé, on l'ajoute au tableau des produits 350 if (isset($product->product)) { 351 // Si on veut cacher des produits 352 if (isset($product->product->active) && $product->product->active == 0 && $hide_inactive) { 353 continue; 354 } 355 else if (isset($product->product->available_for_order) && $product->product->available_for_order == 0 && $hide_available) { 356 continue; 357 } 358 else if (isset($product->product->visibility) && $product->product->visibility == 'none' && $hide_visibility) { 359 continue; 360 } 361 else if (isset($product->product->quantity) && $product->product->quantity <= 0 && $hide_stock) { 362 continue; 363 } 336 364 337 // Exécution du curl338 $ a = curl_exec($ch);339 $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);365 // Génération de l'URL produit 366 $url_rewrite = $prestashop_url . '/index.php?controller=product&id_product=' . $product->product->id; 367 $url_rewrite_element = wp_remote_head($url_rewrite); 340 368 341 // Récupération de l'url 342 $product->url_rewrite = $url; 343 } 344 345 // Gestion de la langue par défaut 346 $id_lang = 1; 347 348 // Gestion de la langue avec Polylang OU WPML 349 if (isset($prest_languages) and !empty($prest_languages) and is_array($prest_languages->languages) and count($prest_languages->languages) > 0) { 350 foreach($prest_languages->languages as $key => $prest_language) { 351 if ($prest_language->iso_code == $langue_actuel) { 352 $id_lang = (int) $prest_language->id; 353 break; 354 } 355 } 356 } 357 358 if (is_array($product->product->name) && count($product->product->name) > 0) { 359 foreach ($product->product->name as $key => $name) { 360 if ($name->id == $id_lang) { 361 $keyToSave = $key; 362 break; 363 } 364 } 365 366 $product->product->name = $product->product->name[$keyToSave]->value; 367 $product->product->description = $product->product->description[$keyToSave]->value; 368 $product->product->description_short = $product->product->description_short[$keyToSave]->value; 369 // V1.1.19 - BEGIN - Gestion du lien produit dans la langue également 370 $product->product->link_rewrite = (isset($product->product->link_rewrite) && !empty($keyToSave) && isset($product->product->link_rewrite[$keyToSave])) ? $product->product->link_rewrite[$keyToSave]->value : ''; 371 $product->product->url_rewrite = (isset($product->product->url_rewrite) && !empty($keyToSave) && isset($product->product->url_rewrite[$keyToSave])) ? $product->product->url_rewrite[$keyToSave]->value : ''; 372 // V1.1.19 - END 373 } 374 375 // URL par défaut (si aucune redirection n'est trouvée) 376 if (empty($product->url_rewrite) || !isset($product->url_rewrite) || isset($atts['language'])) { // !empty($product->product->link_rewrite) 377 if (is_array($product->product->link_rewrite) && count($product->product->link_rewrite) > 0) { 378 $link_rewrite = $product->product->link_rewrite[0]->value; 379 } 380 else { 381 $link_rewrite = $product->product->link_rewrite; 382 } 383 384 $product->url_rewrite = $prestashop_url . '/' . (($atts['language']) ? $atts['language'] . '/' : '') . $product->product->id . ((isset($product->product->id_default_combination) && !empty($product->product->id_default_combination)) ? '-' . $product->product->id_default_combination : '' ) . '-' . $link_rewrite .'.html'; 385 } 386 387 $products[] = $product; 388 } 389 } 390 } 391 392 return $products; 369 // Récupération de la redirection par les méthodes WP 370 if (is_array($url_rewrite_element) && isset($url_rewrite_element['headers']) && isset($url_rewrite_element['headers']['location'])) { 371 $product->url_rewrite = $url_rewrite_element['headers']['location']; 372 } 373 374 // Récupération de la redirection par les fonctions PHP (si la précédente n'a pas marché) 375 if (empty($product->url_rewrite) || !isset($product->url_rewrite)) { 376 $get_headers = get_headers($url_rewrite, 1); 377 378 if (isset($get_headers['Location'])) { 379 $product->url_rewrite = $get_headers['Location']; 380 } 381 } 382 383 // Si les fonctions WP et PHP ne fonctionnent pas, on essaie le curl 384 if (empty($product->url_rewrite) || !isset($product->url_rewrite)) { 385 // Initialisation du curl 386 $ch = curl_init(); 387 388 // Paramètres du curl 389 curl_setopt($ch, CURLOPT_URL, $url_rewrite); 390 curl_setopt($ch, CURLOPT_HEADER, true); 391 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 392 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 393 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 394 curl_setopt($ch, CURLOPT_TIMEOUT, 15); 395 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 396 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 397 398 // Exécution du curl 399 $a = curl_exec($ch); 400 $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 401 402 // Récupération de l'url 403 $product->url_rewrite = $url; 404 } 405 406 // Gestion de la langue par défaut 407 $id_lang = 1; 408 409 // Gestion de la langue avec Polylang OU WPML 410 if (isset($prest_languages) and !empty($prest_languages) and is_array($prest_languages->languages) and count($prest_languages->languages) > 0) { 411 foreach($prest_languages->languages as $key => $prest_language) { 412 if ($prest_language->iso_code == $langue_actuel) { 413 $id_lang = (int) $prest_language->id; 414 break; 415 } 416 } 417 } 418 419 if (is_array($product->product->name) && count($product->product->name) > 0) { 420 foreach ($product->product->name as $key => $name) { 421 if ($name->id == $id_lang) { 422 $keyToSave = $key; 423 break; 424 } 425 } 426 427 $product->product->name = $product->product->name[$keyToSave]->value; 428 $product->product->description = $product->product->description[$keyToSave]->value; 429 $product->product->description_short = $product->product->description_short[$keyToSave]->value; 430 // V1.1.19 - BEGIN - Gestion du lien produit dans la langue également 431 $product->product->link_rewrite = (isset($product->product->link_rewrite) && !empty($keyToSave) && isset($product->product->link_rewrite[$keyToSave])) ? $product->product->link_rewrite[$keyToSave]->value : ''; 432 $product->product->url_rewrite = (isset($product->product->url_rewrite) && !empty($keyToSave) && isset($product->product->url_rewrite[$keyToSave])) ? $product->product->url_rewrite[$keyToSave]->value : ''; 433 // V1.1.19 - END 434 } 435 436 // URL par défaut (si aucune redirection n'est trouvée) 437 if (empty($product->url_rewrite) || !isset($product->url_rewrite) || isset($atts['language'])) { // !empty($product->product->link_rewrite) 438 if (is_array($product->product->link_rewrite) && count($product->product->link_rewrite) > 0) { 439 $link_rewrite = $product->product->link_rewrite[0]->value; 440 } 441 else { 442 $link_rewrite = $product->product->link_rewrite; 443 } 444 445 $product->url_rewrite = $prestashop_url . '/' . (($atts['language']) ? $atts['language'] . '/' : '') . $product->product->id . ((isset($product->product->id_default_combination) && !empty($product->product->id_default_combination)) ? '-' . $product->product->id_default_combination : '' ) . '-' . $link_rewrite .'.html'; 446 } 447 448 $products[] = $product; 449 } 450 } 451 } 452 453 return $products; 393 454 } 394 455 }
Note: See TracChangeset
for help on using the changeset viewer.