Plugin Directory

Changeset 2442105


Ignore:
Timestamp:
12/18/2020 09:10:03 AM (5 years ago)
Author:
officegestpt
Message:

Added option to personalize article name
Added articles image sync

Location:
officegest/trunk
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • officegest/trunk/officegest.php

    r2433102 r2442105  
    55 *   Plugin URI:   https://www.officegest.com/woocommerce
    66 *   Description:  A forma mais fácil de ligar a sua loja online com a sua faturação.
    7  *   Version:      1.0.36
     7 *   Version:      1.0.37
    88 *   Author:       OfficeGest
    99 *   Author URI:   https://www.officegest.com
     
    260260) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
    261261
     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
    262272    $wpdb->flush();
    263273    $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'" );
     
    276286    if(empty($row)){
    277287        $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");
    278300    }
    279301    $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'" );
     
    359381        define('TABLE_OFFICEGEST_CRON_JOBS','officegest_cron_jobs');
    360382    }
     383    if (!defined('TABLE_OFFICEGEST_ARTICLE_PHOTOS')){
     384        define('TABLE_OFFICEGEST_ARTICLE_PHOTOS','officegest_article_photos');
     385    }
    361386}
    362387
  • officegest/trunk/readme.txt

    r2433102 r2442105  
    44Requires at least: 4.6
    55Tested up to: 5.5
    6 Requires PHP: 5.6
    7 Stable tag: 1.0.36
     6Requires PHP: 7.3
     7Stable tag: 1.0.37
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    3333
    3434== Changelog ==
     35= 1.0.37 =
     36Added option to personalize article name
     37Added articles image sync
     38
    3539= 1.0.36 =
    3640Fix images sync
  • officegest/trunk/src/Controllers/SyncEcoautoParts.php

    r2420124 r2442105  
    77use OfficeGest\Log;
    88use OfficeGest\OfficeGestDBModel;
     9use OfficeGest\Tools;
    910
    1011class SyncEcoautoParts
  • officegest/trunk/src/Controllers/SyncOfficeGestQueue.php

    r2420184 r2442105  
    4040            }elseif($jobToRun['cron_type'] == 'articles') {
    4141                OfficeGestDBModel::generateArticlesToWoo($items);
    42             }elseif($jobToRun['cron_type'] == 'parts_images'){
     42            }elseif($jobToRun['cron_type'] == 'parts_images') {
    4343                OfficeGestDBModel::generateEcoautoPartsImages($items);
     44            }elseif($jobToRun['cron_type'] == 'article_images'){
     45                OfficeGestDBModel::generateOfficeGestArticlesImages($items);
    4446            }else{
    4547                Log::write("Tipo de cron não encontrada");
  • officegest/trunk/src/OfficeGestCurl.php

    r2420007 r2442105  
    11071107        return json_decode( json_encode( $curl->response ), true );
    11081108    }
     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    }
    11091122}
  • officegest/trunk/src/OfficeGestDBModel.php

    r2426732 r2442105  
    15151515
    15161516    /**
     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    /**
    15171573     * Create ecoauto part description
    15181574     *
     
    19802036
    19812037    /**
     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    /**
    19822068     * Get ecoauto part photo from id
    19832069     *
     
    20052091
    20062092    /**
     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    /**
    20072118     * Set woo id to ecoauto part attach's
    20082119     *
     
    20152126        ];
    20162127        $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]);
    20172141    }
    20182142
     
    20702194
    20712195    /**
     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    /**
    20722216     * Get photo
    20732217     *
     
    20782222        global $wpdb;
    20792223        $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."'";
    20802240        $single =  $wpdb->get_row($query,ARRAY_A);
    20812241        if (!empty($single)){
     
    21022262        return $wpdb->update(TABLE_OFFICEGEST_ECO_PHOTOS,$data,$where);
    21032263    }
     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    }
    21042282}
  • officegest/trunk/src/OfficegestProduct.php

    r2433102 r2442105  
    299299
    300300            $product_data = [
    301                 'name' => $data['description'],
     301                'name' => OfficeGestDBModel::createArticleName($data,$brandyName),
    302302                'slug' => Tools::slugify($data['description']),
    303303                'type' => 'simple',
     
    314314            $product = new WC_Product_Simple($id);
    315315            $product->set_name($product_data['name']);
    316             $product->set_description($product_data['description'] ?? '');
     316            $product->set_description(OfficeGestDBModel::createArticleDescription($data,$brandyName) ?? '');
    317317            $product->set_slug($product_data['slug']);
    318318            $product->set_sku($product_data['sku']);
     
    576576
    577577    /**
     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    /**
    578608     * Add image file
    579609     *
     
    613643        set_post_thumbnail($post_id, $singlePhoto);
    614644    }
     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    }
    615699}
  • officegest/trunk/src/Plugin.php

    r2426605 r2442105  
    198198                    case 'forcesyncarticles':
    199199                        $this->forceSyncArticles();
     200                        break;
     201                    case 'syncarticleimages':
     202                        $this->syncArticleImages();
    200203                        break;
    201204
     
    526529    }
    527530
     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    }
    528542}
  • officegest/trunk/src/Templates/Containers/Settings/Articles.php

    r2426605 r2442105  
    1717$pontos_de_recolha = OfficeGestDBModel::getOption('pontos_de_recolha');
    1818$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');
    1923
    2024?>
     
    123127            </td>
    124128        </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>
    126154        <tr>
    127155            <th scope="row" class="titledesc">
     
    144172            </td>
    145173        </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 } ?>
    146211    </tbody>
    147212</table>
  • officegest/trunk/src/Templates/Containers/Utils.php

    r2420007 r2442105  
    44use OfficeGest\OfficeGestDBModel;
    55$configuracao = OfficeGestDBModel::getOption('general_configuration');
     6$sync_article_images = OfficeGestDBModel::getOption('sync_article_images');
    67?>
    78<br>
     
    8182            </td>
    8283        </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 } ?>
    8398        <?php } ?>
    8499    </tbody>
  • officegest/trunk/src/Templates/DEBUG.php

    r2420184 r2442105  
    1919ini_set( 'memory_limit',-1);
    2020ini_set( 'max_execution_time',-1);
     21
     22Tools::debug(\OfficeGest\Controllers\SyncOfficeGestQueue::run());
Note: See TracChangeset for help on using the changeset viewer.