Changeset 2442105
- Timestamp:
- 12/18/2020 09:10:03 AM (5 years ago)
- Location:
- officegest/trunk
- Files:
-
- 1 added
- 11 edited
-
officegest.php (modified) (4 diffs)
-
readme.txt (modified) (2 diffs)
-
src/Controllers/SyncEcoautoParts.php (modified) (1 diff)
-
src/Controllers/SyncOfficeGestQueue.php (modified) (1 diff)
-
src/Controllers/SyncOfficegestArticleImages.php (added)
-
src/OfficeGestCurl.php (modified) (1 diff)
-
src/OfficeGestDBModel.php (modified) (7 diffs)
-
src/OfficegestProduct.php (modified) (4 diffs)
-
src/Plugin.php (modified) (2 diffs)
-
src/Templates/Containers/Settings/Articles.php (modified) (3 diffs)
-
src/Templates/Containers/Utils.php (modified) (2 diffs)
-
src/Templates/DEBUG.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
officegest/trunk/officegest.php
r2433102 r2442105 5 5 * Plugin URI: https://www.officegest.com/woocommerce 6 6 * Description: A forma mais fácil de ligar a sua loja online com a sua faturação. 7 * Version: 1.0.3 67 * Version: 1.0.37 8 8 * Author: OfficeGest 9 9 * Author URI: https://www.officegest.com … … 260 260 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'); 261 261 262 $wpdb->query("CREATE TABLE IF NOT EXISTS `officegest_article_photos` ( 263 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 264 `component_id` varchar(255) DEFAULT NULL, 265 `attach_num` varchar(255) DEFAULT NULL, 266 `main` int(11) DEFAULT NULL, 267 `photo` varchar(255) DEFAULT NULL, 268 `woo_attach_id` bigint(20) NOT NULL DEFAULT '0', 269 KEY `photo` (`photo`) 270 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"); 271 262 272 $wpdb->flush(); 263 273 $row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '". DB_NAME ."' AND table_name = 'officegest_articles' AND column_name = 'woo_id'" ); … … 276 286 if(empty($row)){ 277 287 $wpdb->query("ALTER TABLE officegest_articles ADD updated_at TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"); 288 } 289 $row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '". DB_NAME ."' AND table_name = 'officegest_articles' AND column_name = 'photos'" ); 290 if(empty($row)){ 291 $wpdb->query("ALTER TABLE officegest_articles ADD photos VARCHAR(255) DEFAULT NULL"); 292 } 293 $row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '". DB_NAME ."' AND table_name = 'officegest_articles' AND column_name = 'photo'" ); 294 if(empty($row)){ 295 $wpdb->query("ALTER TABLE officegest_articles ADD photo VARCHAR(255) DEFAULT NULL"); 296 } 297 $row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '". DB_NAME ."' AND table_name = 'officegest_articles' AND column_name = 'photos_imported'" ); 298 if(empty($row)){ 299 $wpdb->query("ALTER TABLE officegest_articles ADD photos_imported int(11) DEFAULT 0"); 278 300 } 279 301 $row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '". DB_NAME ."' AND table_name = 'officegest_eco_parts' AND column_name = 'created_at'" ); … … 359 381 define('TABLE_OFFICEGEST_CRON_JOBS','officegest_cron_jobs'); 360 382 } 383 if (!defined('TABLE_OFFICEGEST_ARTICLE_PHOTOS')){ 384 define('TABLE_OFFICEGEST_ARTICLE_PHOTOS','officegest_article_photos'); 385 } 361 386 } 362 387 -
officegest/trunk/readme.txt
r2433102 r2442105 4 4 Requires at least: 4.6 5 5 Tested up to: 5.5 6 Requires PHP: 5.67 Stable tag: 1.0.3 66 Requires PHP: 7.3 7 Stable tag: 1.0.37 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 33 33 34 34 == Changelog == 35 = 1.0.37 = 36 Added option to personalize article name 37 Added articles image sync 38 35 39 = 1.0.36 = 36 40 Fix images sync -
officegest/trunk/src/Controllers/SyncEcoautoParts.php
r2420124 r2442105 7 7 use OfficeGest\Log; 8 8 use OfficeGest\OfficeGestDBModel; 9 use OfficeGest\Tools; 9 10 10 11 class SyncEcoautoParts -
officegest/trunk/src/Controllers/SyncOfficeGestQueue.php
r2420184 r2442105 40 40 }elseif($jobToRun['cron_type'] == 'articles') { 41 41 OfficeGestDBModel::generateArticlesToWoo($items); 42 }elseif($jobToRun['cron_type'] == 'parts_images') {42 }elseif($jobToRun['cron_type'] == 'parts_images') { 43 43 OfficeGestDBModel::generateEcoautoPartsImages($items); 44 }elseif($jobToRun['cron_type'] == 'article_images'){ 45 OfficeGestDBModel::generateOfficeGestArticlesImages($items); 44 46 }else{ 45 47 Log::write("Tipo de cron não encontrada"); -
officegest/trunk/src/OfficeGestCurl.php
r2420007 r2442105 1107 1107 return json_decode( json_encode( $curl->response ), true ); 1108 1108 } 1109 1110 /** 1111 * Get all officegest article images 1112 * /api/ 1113 * 1114 * @param $article 1115 * @return mixed 1116 */ 1117 public static function getAllOfficeGestArticlePhotos($article){ 1118 $tokensRow = OfficeGestDBModel::getTokensRow(); 1119 $photo = $tokensRow['domain'].'/download/?aib='.base64_encode($article['id']); 1120 return $photo; 1121 } 1109 1122 } -
officegest/trunk/src/OfficeGestDBModel.php
r2426732 r2442105 1515 1515 1516 1516 /** 1517 * Create article name 1518 * 1519 * @param $data 1520 * @param $brandName 1521 * @return string|string[]|null 1522 */ 1523 public static function createArticleName($data,$brandName) 1524 { 1525 $part = self::getOption('article_name'); 1526 if ( $part == '' ){ 1527 $final = $data['description'] .' '. $data['id'].' '. 1528 $brandName; 1529 } 1530 else{ 1531 $final = str_replace( array( 1532 '{descricao}', 1533 '{codigo_artigo}', 1534 '{marca}' 1535 ), array( 1536 trim($data['description']), 1537 trim($data['id']), 1538 $brandName 1539 ), $part ); 1540 } 1541 return preg_replace('/\s+/', ' ',$final); 1542 } 1543 1544 /** 1545 * Create article description 1546 * 1547 * @param $data 1548 * @param $brandName 1549 * @return string|string[]|null 1550 */ 1551 public static function createArticleDescription($data,$brandName) 1552 { 1553 $part = self::getOption('article_description'); 1554 if ( $part == '' ){ 1555 $final = $data['description'] .' '. $data['id'].' '. 1556 $brandName; 1557 } 1558 else{ 1559 $final = str_replace( array( 1560 '{descricao}', 1561 '{codigo_artigo}', 1562 '{marca}' 1563 ), array( 1564 trim($data['description']), 1565 trim($data['id']), 1566 $brandName 1567 ), $part ); 1568 } 1569 return preg_replace('/\s+/', ' ',$final); 1570 } 1571 1572 /** 1517 1573 * Create ecoauto part description 1518 1574 * … … 1980 2036 1981 2037 /** 2038 * Create ecoauto parts images in officegest tables 2039 * 2040 * @param $article 2041 */ 2042 public static function getAllOfficeGestArticlesPhotosDB($article) { 2043 global $wpdb; 2044 try{ 2045 $response = OfficeGestCurl::getAllOfficeGestArticlePhotos($article); 2046 2047 if ($response) { 2048 $insert_data = [ 2049 'component_id'=>$article['id'], 2050 'attach_num'=>$article['id'], 2051 'main'=>1, 2052 'photo'=>$response 2053 ]; 2054 $id = self::getSingleOfficeGestArticlePhotoID($insert_data['component_id'],$insert_data['attach_num']); 2055 if (empty($id)){ 2056 $wpdb->insert(TABLE_OFFICEGEST_ARTICLE_PHOTOS,$insert_data); 2057 } 2058 else{ 2059 $wpdb->update(TABLE_OFFICEGEST_ARTICLE_PHOTOS,$insert_data,$id); 2060 } 2061 } 2062 }catch(\Exception $e){ 2063 Log::write("[OfficeGestDBModel: getAllOfficeGestArticlesPhotosDB] Error: ".$e->getMessage()); 2064 } 2065 } 2066 2067 /** 1982 2068 * Get ecoauto part photo from id 1983 2069 * … … 2005 2091 2006 2092 /** 2093 * Get officegest article photo from id 2094 * 2095 * @param $component_id 2096 * @param $attach_num 2097 * @return array 2098 */ 2099 public static function getSingleOfficeGestArticlePhotoID($component_id,$attach_num){ 2100 global $wpdb; 2101 try{ 2102 $query = 'SELECT * FROM ' . TABLE_OFFICEGEST_ARTICLE_PHOTOS. ' WHERE component_id="'.$component_id.'" AND attach_num="'.$attach_num.'"'; 2103 $row = $wpdb->get_row($query,ARRAY_A); 2104 if (empty($row)){ 2105 return []; 2106 } 2107 return [ 2108 'component_id'=>$row['component_id'], 2109 'attach_num'=>$row['attach_num'], 2110 ]; 2111 }catch(\Exception $e){ 2112 Log::write("[OfficeGestDBModel: getSingleOfficeGestArticlePhotoID] Error: ".$e->getMessage()); 2113 return []; 2114 } 2115 } 2116 2117 /** 2007 2118 * Set woo id to ecoauto part attach's 2008 2119 * … … 2015 2126 ]; 2016 2127 $wpdb->update(TABLE_OFFICEGEST_ECO_PARTS,$data,['id'=>$product]); 2128 } 2129 2130 /** 2131 * Set woo id to officegest article attach's 2132 * 2133 * @param $product 2134 */ 2135 public static function setWooIDArticleAttachs($product) { 2136 global $wpdb; 2137 $data=[ 2138 'photos_imported'=>1 2139 ]; 2140 $wpdb->update(TABLE_OFFICEGEST_ARTICLES,$data,['id'=>$product]); 2017 2141 } 2018 2142 … … 2070 2194 2071 2195 /** 2196 * Create officegest article images in WooCommerce based on cron job array; 2197 * 2198 * @param $items 2199 */ 2200 public static function generateOfficeGestArticlesImages($items) { 2201 global $wpdb; 2202 try{ 2203 foreach ($items as $a => $item){ 2204 $partsQuery = "SELECT * FROM ".TABLE_OFFICEGEST_ARTICLES." WHERE id = '".$item."'"; 2205 $itemToRun = $wpdb->get_results($partsQuery,ARRAY_A); 2206 if(!empty($itemToRun[0])){ 2207 $id = OfficegestProduct::updateOfficeGestArticleImage($itemToRun[0]); 2208 } 2209 } 2210 }catch(\Exception $e){ 2211 Log::write("[OfficeGestDBModel: generateOfficeGestArticlesImages] Error: ".$e->getMessage()); 2212 } 2213 } 2214 2215 /** 2072 2216 * Get photo 2073 2217 * … … 2078 2222 global $wpdb; 2079 2223 $query = "SELECT woo_attach_id from ".TABLE_OFFICEGEST_ECO_PHOTOS." where woo_attach_id>0 and photo='".$name."'"; 2224 $single = $wpdb->get_row($query,ARRAY_A); 2225 if (!empty($single)){ 2226 return $single['woo_attach_id']; 2227 } 2228 return 0; 2229 } 2230 2231 /** 2232 * Get photo 2233 * 2234 * @param $name 2235 * @return int|mixed 2236 */ 2237 public static function singleArticlePhoto($name){ 2238 global $wpdb; 2239 $query = "SELECT woo_attach_id FROM ".TABLE_OFFICEGEST_ARTICLE_PHOTOS." WHERE woo_attach_id>0 AND photo='".$name."'"; 2080 2240 $single = $wpdb->get_row($query,ARRAY_A); 2081 2241 if (!empty($single)){ … … 2102 2262 return $wpdb->update(TABLE_OFFICEGEST_ECO_PHOTOS,$data,$where); 2103 2263 } 2264 2265 /** 2266 * Find article photo 2267 * 2268 * @param $name 2269 * @param $thumbid 2270 * @return bool|int 2271 */ 2272 public static function photoArticleFind($id,$thumbid){ 2273 global $wpdb; 2274 $where = [ 2275 'component_id'=>$id 2276 ]; 2277 $data=[ 2278 'woo_attach_id'=>$thumbid 2279 ]; 2280 return $wpdb->update(TABLE_OFFICEGEST_ARTICLE_PHOTOS,$data,$where); 2281 } 2104 2282 } -
officegest/trunk/src/OfficegestProduct.php
r2433102 r2442105 299 299 300 300 $product_data = [ 301 'name' => $data['description'],301 'name' => OfficeGestDBModel::createArticleName($data,$brandyName), 302 302 'slug' => Tools::slugify($data['description']), 303 303 'type' => 'simple', … … 314 314 $product = new WC_Product_Simple($id); 315 315 $product->set_name($product_data['name']); 316 $product->set_description( $product_data['description']?? '');316 $product->set_description(OfficeGestDBModel::createArticleDescription($data,$brandyName) ?? ''); 317 317 $product->set_slug($product_data['slug']); 318 318 $product->set_sku($product_data['sku']); … … 576 576 577 577 /** 578 * Update officegest article images 579 * 580 * @param $data 581 * @return int|null 582 */ 583 public static function updateOfficeGestArticleImage($data){ 584 global $wpdb; 585 try { 586 $id = wc_get_product_id_by_sku($data['id']); 587 $photos = $wpdb->get_results('SELECT * FROM ' . TABLE_OFFICEGEST_ARTICLE_PHOTOS. ' WHERE component_id="'.$data['id'].'"',ARRAY_A); 588 $images = []; 589 foreach ($photos as $key=>$vis){ 590 $images[]=[ 591 'src'=>$vis['photo'] 592 ]; 593 } 594 595 foreach ($images as $ki=>$vi){ 596 self::addArticleImages($id,$vi,$data['id']); 597 } 598 OfficeGestDBModel::setWooIDArticleAttachs($data['id']); 599 600 return $id; 601 }catch(\Exception $e){ 602 Log::write("[OfficeGestProduct: updateOfficeGestArticleImage] Error: ".$e->getMessage()); 603 return null; 604 } 605 } 606 607 /** 578 608 * Add image file 579 609 * … … 613 643 set_post_thumbnail($post_id, $singlePhoto); 614 644 } 645 646 /** 647 * Add article image file 648 * 649 * @param $post_id 650 * @param $images 651 */ 652 private static function addArticleImages($post_id,$images,$id){ 653 require_once(ABSPATH . 'wp-admin/includes/file.php'); 654 require_once(ABSPATH . 'wp-admin/includes/media.php'); 655 require_once(ABSPATH . 'wp-admin/includes/image.php'); 656 $thumb_url = $images['src']; 657 $singlePhoto = OfficeGestDBModel::singleArticlePhoto($thumb_url); 658 if ($singlePhoto===0){ 659 // Download file to temp location 660 $tmp = download_url($thumb_url); 661 662 $get = wp_remote_get( $thumb_url ); 663 664 $type = wp_remote_retrieve_header( $get, 'content-type' ); 665 666 $sType = '.jpg'; 667 if($type == 'image/png'){ 668 $sType = '.png'; 669 } elseif($type == 'image/jpg'){ 670 $sType = '.jpg'; 671 } elseif($type == 'image/jpeg'){ 672 $sType = '.jpg'; 673 } 674 675 // Set variables for storage 676 // fix file name for query strings 677 preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches); 678 $file_array['name'] = base64_encode($id).$sType; 679 $file_array['tmp_name'] = $tmp; 680 681 // If error storing temporarily, unlink 682 if ( is_wp_error( $tmp ) ) { 683 @unlink($file_array['tmp_name']); 684 $file_array['tmp_name'] = ''; 685 } 686 687 //use media_handle_sideload to upload img: 688 $thumbid = media_handle_sideload( $file_array, $post_id, 'gallery desc' ); 689 690 if ( is_wp_error($thumbid) ) { 691 @unlink($file_array['tmp_name']); 692 } 693 694 OfficeGestDBModel::photoArticleFind($id,$thumbid); 695 $singlePhoto = $thumbid; 696 } 697 set_post_thumbnail($post_id, $singlePhoto); 698 } 615 699 } -
officegest/trunk/src/Plugin.php
r2426605 r2442105 198 198 case 'forcesyncarticles': 199 199 $this->forceSyncArticles(); 200 break; 201 case 'syncarticleimages': 202 $this->syncArticleImages(); 200 203 break; 201 204 … … 526 529 } 527 530 531 /** 532 * Force to run officegest articles sync 533 */ 534 private function syncArticleImages() { 535 ini_set( 'memory_limit',-1); 536 ini_set( 'max_execution_time',-1); 537 $forceSyncArticleImagess = (new Controllers\SyncOfficegestArticleImages())->run(); 538 if ($forceSyncArticleImagess->countFoundRecord() > 0) { 539 add_settings_error('officegest', 'officegest-sync-pecas-woo-updated', __('Foram criados ' . $forceSyncArticleImagess->countFoundRecord() . ' imagens.' ), 'updated'); 540 } 541 } 528 542 } -
officegest/trunk/src/Templates/Containers/Settings/Articles.php
r2426605 r2442105 17 17 $pontos_de_recolha = OfficeGestDBModel::getOption('pontos_de_recolha'); 18 18 $articles_sync_limit = OfficeGestDBModel::getOption('articles_sync_limit'); 19 $article_name = OfficeGestDBModel::getOption('article_name'); 20 $article_description = OfficeGestDBModel::getOption('article_description'); 21 $articles_sync_images_limit = OfficeGestDBModel::getOption('articles_sync_images_limit'); 22 $sync_article_images = OfficeGestDBModel::getOption('sync_article_images'); 19 23 20 24 ?> … … 123 127 </td> 124 128 </tr> 125 <?php } ?> 129 130 <tr> 131 <th scope="row" class="titledesc"> 132 <label for="article_name"><?= __("Nome do Artigo") ?></label> 133 </th> 134 <td class="forminp"> 135 <fieldset> 136 <legend class="screen-reader-text"><span>Titulo do Artigo</span></legend> 137 <textarea rows="3" cols="20" class="input-text wide-input " type="textarea" name="opt[article_name]" id="article_name" style="" placeholder=""><?=$article_name?></textarea> 138 <p class='description'><?= __("Tags Disponiveis: {descricao} {codigo_artigo} {marca}") ?></p> 139 </fieldset> 140 </td> 141 </tr> 142 <tr> 143 <th scope="row" class="titledesc"> 144 <label for="article_description"><?= __("Descrição") ?></label> 145 </th> 146 <td class="forminp"> 147 <fieldset> 148 <legend class="screen-reader-text"><span>Texto Genérico da Descrição</span></legend> 149 <textarea rows="3" cols="20" class="input-text wide-input " type="textarea" name="opt[article_description]" id="article_description" style="" placeholder=""><?=$article_description?></textarea> 150 <p class='description'><?= __("Tags Disponiveis: {descricao} {codigo_artigo} {marca}") ?></p> 151 </fieldset> 152 </td> 153 </tr> 126 154 <tr> 127 155 <th scope="row" class="titledesc"> … … 144 172 </td> 145 173 </tr> 174 175 <tr> 176 <th scope="row" class="titledesc"> 177 <label for="sync_article_images"><?= __("Sincronizar imagens de artigos") ?></label> 178 </th> 179 <td> 180 <select id="sync_article_images" name='opt[sync_article_images]' class='officegest_select2 inputOut'> 181 <option value='0' <?= $sync_article_images == '0' ? 'selected' : '0' ?>>Não</option> 182 <option value='1' <?= $sync_article_images == '1' ? 'selected' : '1' ?>>Sim</option> 183 </select> 184 <p class='description'><?= __('Sincronizar imagens de artigos') ?></p> 185 </td> 186 </tr> 187 188 <?php if ($sync_article_images >0) {?> 189 <tr> 190 <th scope="row" class="titledesc"> 191 <label for="articles_sync_images_limit"><?= __("Limite de imagens de artigos por sincronização") ?></label> 192 </th> 193 <td> 194 <select id="articles_sync_images_limit" name='opt[articles_sync_images_limit]' class='officegest_select2 inputOut'> 195 <option value='20' <?= $articles_sync_images_limit == '20' ? 'selected' : '20' ?>>20</option> 196 <option value='40' <?= $articles_sync_images_limit == '40' ? 'selected' : '40' ?>>40</option> 197 <option value='60' <?= $articles_sync_images_limit == '60' ? 'selected' : '60' ?>>60</option> 198 <option value='80' <?= $articles_sync_images_limit == '80' ? 'selected' : '80' ?>>80</option> 199 <option value='100' <?= $articles_sync_images_limit == '100' ? 'selected' : '100' ?>>100</option> 200 <option value='120' <?= $articles_sync_images_limit == '120' ? 'selected' : '120' ?>>120</option> 201 <option value='140' <?= $articles_sync_images_limit == '140' ? 'selected' : '140' ?>>140</option> 202 <option value='160' <?= $articles_sync_images_limit == '160' ? 'selected' : '160' ?>>160</option> 203 <option value='180' <?= $articles_sync_images_limit == '180' ? 'selected' : '180' ?>>180</option> 204 <option value='200' <?= $articles_sync_images_limit == '200' ? 'selected' : '200' ?>>200</option> 205 </select> 206 <p class='description'><?= __('Limite de imagens de artigos por sincronização') ?></p> 207 </td> 208 </tr> 209 <?php } ?> 210 <?php } ?> 146 211 </tbody> 147 212 </table> -
officegest/trunk/src/Templates/Containers/Utils.php
r2420007 r2442105 4 4 use OfficeGest\OfficeGestDBModel; 5 5 $configuracao = OfficeGestDBModel::getOption('general_configuration'); 6 $sync_article_images = OfficeGestDBModel::getOption('sync_article_images'); 6 7 ?> 7 8 <br> … … 81 82 </td> 82 83 </tr> 84 <?php if ($sync_article_images >0) {?> 85 <tr> 86 <th style="padding: 2rem"> 87 <strong class="name"><?= __('Imagens de Artigos') ?></strong> 88 <p class='description'><?= __('Força a importacao das Imagens dos Artigos') ?></p> 89 </th> 90 <td class="run-tool" style="padding: 2rem; text-align: right"> 91 <a class="button button-large" 92 href='<?= admin_url('admin.php?page=officegest&tab=utils&action=syncarticleimages') ?>'> 93 <?= __('Importação das Imagens') ?> 94 </a> 95 </td> 96 </tr> 97 <?php } ?> 83 98 <?php } ?> 84 99 </tbody> -
officegest/trunk/src/Templates/DEBUG.php
r2420184 r2442105 19 19 ini_set( 'memory_limit',-1); 20 20 ini_set( 'max_execution_time',-1); 21 22 Tools::debug(\OfficeGest\Controllers\SyncOfficeGestQueue::run());
Note: See TracChangeset
for help on using the changeset viewer.