Plugin Directory

Changeset 3088300


Ignore:
Timestamp:
05/17/2024 12:20:44 PM (23 months ago)
Author:
weboneco
Message:

some changes from github repo (from f77c206 to 8af51e1)

  • refactor gholab activation / deactivation functionality
  • refactor log functionality
  • improve dd helper function
  • add IsSecondHand parameter in update product request
Location:
gholab/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • gholab/trunk/gholab.php

    r3017916 r3088300  
    3232define('GHOLAB_SYNC_BULK', 20);
    3333define('GHOLAB_CRON_DELETE_LOGS_INTERVAL', 'weekly');
     34define('GHOLAB_LOGS_TABLE_NAME', 'gholab_logs');
    3435
    3536require_once __DIR__ . '/src/Gholab.php';
  • gholab/trunk/src/Controllers/MenuAndPageController.php

    r3017916 r3088300  
    7474        $lastSyncLog = $this->logRepo->getLastSyncLog();
    7575        if (null !== $lastSyncLog) {
    76             $lastSync = JDateTime::dateTime($lastSyncLog->comment_date);
    77             $lastSyncLog = $lastSyncLog->comment_content;
     76            $lastSync = JDateTime::dateTime($lastSyncLog->created_at);
     77            $lastSyncLog = $lastSyncLog->log_content;
    7878        }
    7979
    8080        $lastApiLog = $this->logRepo->getLastApiLog();
    8181        if (null !== $lastApiLog) {
    82             $lastApiCall = JDateTime::dateTime($lastApiLog->comment_date);
    83             $lastApiLog = $lastApiLog->comment_content;
     82            $lastApiCall = JDateTime::dateTime($lastApiLog->created_at);
     83            $lastApiLog = $lastApiLog->log_content;
    8484        }
    8585
     
    9595
    9696        if ('POST' === $_SERVER['REQUEST_METHOD']) {
     97            check_admin_referer('gholab_welcome_nonce');
     98            $error = false;
    9799
    98             check_admin_referer('gholab_welcome_nonce');
    99 
    100             $gholabWelcomeAccept = null;
    101             if (isset($_POST['gholab_welcome_accept'])) {
    102                 $gholabWelcomeAccept = sanitize_text_field($_POST['gholab_welcome_accept']);
    103             }
    104            
    105             $gholabApiKey = null;
    106             if (isset($_POST['gholab_api_key'])) {
    107                 $gholabApiKey = sanitize_text_field($_POST['gholab_api_key']);
    108             }
    109 
    110             $error = false;
     100            $gholabWelcomeAccept = isset($_POST['gholab_welcome_accept']) ? sanitize_text_field($_POST['gholab_welcome_accept']) : null;
     101            $gholabApiKey        = isset($_POST['gholab_api_key'])        ? sanitize_text_field($_POST['gholab_api_key'])        : null;
    111102            if (is_null($gholabWelcomeAccept) || 1 != $gholabWelcomeAccept) {
    112103                add_settings_error(
     
    118109                $error = true;
    119110            }
    120 
    121             if (empty($gholabApiKey)) {
     111            if (empty($gholabApiKey) || strlen($gholabApiKey) > GHOLAB_MAX_LEN_API_KEY || strlen($gholabApiKey) < GHOLAB_MIN_LEN_API_KEY) {
    122112                add_settings_error(
    123113                    'gholab_api_key',
     
    129119            }
    130120
    131             $result = $this->gholabService->checkGholabToken($gholabApiKey);
    132             if (!$result) {
     121            $gholabSiteResponse = $this->gholabService->sendPluginStatusToGholab($gholabApiKey, true);
     122            if (false == $gholabSiteResponse) {
    133123                add_settings_error(
    134124                    'gholab_api_key',
     
    140130            }
    141131
    142             if (!$error) {
    143                 $this->settingsRepo->updateOption('gholab_welcome_accept', 1);
    144                 $this->settingsRepo->updateOption('gholab_api_key', $gholabApiKey);
     132            if (false == $error) {
     133                update_option('gholab_welcome_accept', 1);
     134                update_option('gholab_api_key'       , $gholabApiKey);
     135                update_option('gholab_live_sync'     , 1);
     136
    145137                wp_redirect(admin_url('admin.php?page=gholab'));
    146138                return;
     
    152144        global $gholab_api_key;
    153145        $gholab_welcome_nonce = wp_create_nonce('gholab_welcome_nonce');
    154         $gholab_welcome_accept = $this->settingsRepo->getOption('gholab_welcome_accept');
    155         $gholab_api_key = $this->settingsRepo->getOption('gholab_api_key');
     146        $gholab_welcome_accept = get_option('gholab_welcome_accept');
     147        $gholab_api_key = get_option('gholab_api_key');
    156148        Helpers::renderPartial(__DIR__ . '/../Partials/Welcome.phtml');
    157149        return;
  • gholab/trunk/src/Data/LogRepo.php

    r3017916 r3088300  
    1212    public function getLastApiLog()
    1313    {
    14         $log = get_comments([
    15             'type'    => GHOLAB_LOG_API,
    16             'orderby' => 'comment_date',
    17             'order'   => 'DESC',
    18             'number'  => 1
    19         ]);
    20 
    21         if (empty($log)) {
    22             return null;
    23         }
    24 
    25         return $log[0];
     14        global $wpdb;
     15       
     16        $tableName = $wpdb->prefix . GHOLAB_LOGS_TABLE_NAME;
     17        $log = $wpdb->get_row(
     18            sprintf("SELECT * FROM {$tableName} WHERE `log_type` = '%s' ORDER BY `created_at` DESC LIMIT 1", GHOLAB_LOG_API)
     19        );
     20       
     21        return $log;
    2622    }
    2723
     
    3127    public function getLastSyncLog()
    3228    {
    33         $log = get_comments([
    34             'type'    => GHOLAB_LOG_SYNC,
    35             'orderby' => 'comment_date',
    36             'order'   => 'DESC',
    37             'number'  => 1
    38         ]);
    39 
    40         if (empty($log)) {
    41             return null;
    42         }
    43 
    44         return $log[0];
     29        global $wpdb;
     30       
     31        $tableName = $wpdb->prefix . GHOLAB_LOGS_TABLE_NAME;
     32        $log = $wpdb->get_row(
     33            sprintf("SELECT * FROM {$tableName} WHERE `log_type` = '%s' ORDER BY `created_at` DESC LIMIT 1", GHOLAB_LOG_SYNC)
     34        );
     35       
     36        return $log;
    4537    }
    4638}
  • gholab/trunk/src/Gholab.php

    r3017916 r3088300  
    7373    }
    7474
     75    protected function createLogsTable()
     76    {
     77        global $wpdb;
     78
     79        $collate = $wpdb->has_cap('collation') ? $wpdb->get_charset_collate() : '';
     80        $tableName = $wpdb->prefix . GHOLAB_LOGS_TABLE_NAME;
     81        $sql = "CREATE TABLE IF NOT EXISTS `{$tableName}` (
     82            `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
     83            `log_type` VARCHAR(255) NOT NULL,
     84            `log_content` TEXT NOT NULL,
     85            `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
     86            PRIMARY KEY (`id`)
     87            ) {$collate};";
     88
     89        // $wpdb->query($sql);
     90        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     91        dbDelta($sql);
     92    }
     93
    7594    /**
    7695     * @return void
     
    7998    {
    8099        register_activation_hook(GHOLAB_PLUGIN_FILE, function () {
    81             // default live sync checked
    82             $settingsRepo = $this->container->get(SettingsRepo::class);
    83             $gholabService= $this->container->get(GholabService::class);
    84             $logService = $this->container->get(LogService::class);
    85 
    86             $settingsRepo->updateOption('gholab_live_sync', 1);
     100            // create table in database for logs
     101            $this->createLogsTable();
    87102
    88103            // register cron jobs
    89104            if (!wp_next_scheduled('gholab_cron_sync_hook')) {
    90                 wp_schedule_event(time(), $settingsRepo->getOption('gholab_cron_sync_interval', 'daily'), 'gholab_cron_sync_hook', []);
     105                wp_schedule_event(time(), get_option('gholab_cron_sync_interval', 'daily'), 'gholab_cron_sync_hook', []);
    91106            }
    92107            if (!wp_next_scheduled('gholab_cron_delete_logs_hook')) {
     
    95110
    96111            // notify plugin status to gholab
    97             $result = $gholabService->sendPluginStatusToGholab(true);
    98             $logService->info([
    99                 'type'     => 'plugin_status_active',
    100                 'request'  => [
    101                     'Status' => true
    102                 ],
    103                 'response' => $result
    104             ], GHOLAB_LOG_API);
     112            $gholabApiKey = get_option('gholab_api_key');
     113            if (! empty($gholabApiKey)) {
     114                $gholabService = $this->container->get(GholabService::class);
     115                $gholabService->sendPluginStatusToGholab($gholabApiKey, true);
     116            }
    105117        });
    106118    }
     
    117129
    118130            // notify plugin status to gholab
    119             $gholabService = $this->container->get(GholabService::class);
    120             $logService = $this->container->get(LogService::class);
    121             $result = $gholabService->sendPluginStatusToGholab(false);
    122             $logService->info([
    123                 'type'     => 'plugin_status_deactive',
    124                 'request'  => [
    125                     'Status' => false
    126                 ],
    127                 'response' => $result
    128             ], GHOLAB_LOG_API);
     131            $gholabApiKey = get_option('gholab_api_key');
     132            if (! empty($gholabApiKey)) {
     133                $gholabService = $this->container->get(GholabService::class);
     134                $gholabService->sendPluginStatusToGholab($gholabApiKey, false);
     135            }
    129136        });
    130137    }
  • gholab/trunk/src/Handlers/CronHandler.php

    r3017916 r3088300  
    7575    {
    7676        try {
    77             global $wpdb;
    78             $wpdb->query(
    79                 $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_type LIKE %s", 'gholab_%')
    80             );
     77            $this->logService->deleteAllLogs();
    8178        } catch (Exception $e) {
    8279            $this->logService->info([
  • gholab/trunk/src/Helpers/Helpers.php

    r3017916 r3088300  
    77    public static function dd($arg)
    88    {
    9         echo '<pre>';
     9        echo '<pre style="direction: ltr;">';
    1010        var_dump($arg);
    1111        echo '</pre>';
     
    284284        $productData = self::mapToGholabUpdate($productData);
    285285        $productData['Availability'] = $product->get_status() !== 'publish' ? 0 : $productData['Availability'];
     286        $productData['IsSecondHand'] = false;
    286287        return $productData;
    287288    }
  • gholab/trunk/src/Services/GholabService.php

    r3017916 r3088300  
    55use Gholab\Gholab\Container;
    66use Gholab\Gholab\Data\SettingsRepo;
     7use Gholab\Gholab\Helpers\Helpers;
    78
    89class GholabService
     
    1415
    1516    /**
     17     * @var LogService $logService
     18     */
     19    protected $logService;
     20
     21    /**
    1622     * @param Container $container
    1723     */
     
    1925    {
    2026        $this->settingsRepo = $container->get(SettingsRepo::class);
     27        $this->logService   = $container->get(LogService::class);
    2128    }
    2229
     
    6269    }
    6370
    64     public function sendPluginStatusToGholab($status)
     71    public function sendPluginStatusToGholab($apiKey, $status)
    6572    {
    6673        $result = wp_remote_post('https://api.gholab.ir/api/v1/Provider/Product/Status', [
     
    7077                'Content-Type' => 'application/json',
    7178                'Accept'       => 'application/json',
    72                 'ApiKey'       => $this->settingsRepo->getOption('gholab_api_key')
     79                'ApiKey'       => $apiKey
    7380            ],
    7481            'body'    => json_encode([
     
    7784        ]);
    7885
    79         return $result;
     86        $this->logService->info([
     87            'sync_type' => $status ? 'plugin_status_active' : 'plugin_status_inactive',
     88            'request'   => ['Status' => $status],
     89            'response'  => $result
     90        ], GHOLAB_LOG_API);
     91
     92        if (is_wp_error($result) || 200 != wp_remote_retrieve_response_code($result)) {
     93            return false;
     94        }
     95
     96        return true;
    8097    }
    8198}
  • gholab/trunk/src/Services/LogService.php

    r3017916 r3088300  
    77    public function info($content, $type = 'gholab_log')
    88    {
    9         wp_insert_comment([
    10             'comment_content' => json_encode($content, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE),
    11             'comment_type'    => $type
     9        global $wpdb;
     10
     11        $tableName = $wpdb->prefix . GHOLAB_LOGS_TABLE_NAME;
     12        $wpdb->insert($tableName, [
     13            'log_type'    => $type,
     14            'log_content' => json_encode($content, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)
    1215        ]);
    1316    }
     17
     18    public function deleteAllLogs()
     19    {
     20        global $wpdb;
     21
     22        $tableName = $wpdb->prefix . GHOLAB_LOGS_TABLE_NAME;
     23        $wpdb->query("TRUNCATE TABLE {$tableName}");
     24    }
    1425}
  • gholab/trunk/uninstall.php

    r3017916 r3088300  
    1515        $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_type LIKE %s", 'gholab_%')
    1616    );
     17
     18    $tableName = $wpdb->prefix . GHOLAB_LOGS_TABLE_NAME;
     19    $wpdb->query("DROP TABLE IF EXISTS {$tableName}");
    1720}
    1821gholab_uninstall();
Note: See TracChangeset for help on using the changeset viewer.