Changeset 3088300
- Timestamp:
- 05/17/2024 12:20:44 PM (23 months ago)
- Location:
- gholab/trunk
- Files:
-
- 9 edited
-
gholab.php (modified) (1 diff)
-
src/Controllers/MenuAndPageController.php (modified) (6 diffs)
-
src/Data/LogRepo.php (modified) (2 diffs)
-
src/Gholab.php (modified) (4 diffs)
-
src/Handlers/CronHandler.php (modified) (1 diff)
-
src/Helpers/Helpers.php (modified) (2 diffs)
-
src/Services/GholabService.php (modified) (6 diffs)
-
src/Services/LogService.php (modified) (1 diff)
-
uninstall.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
gholab/trunk/gholab.php
r3017916 r3088300 32 32 define('GHOLAB_SYNC_BULK', 20); 33 33 define('GHOLAB_CRON_DELETE_LOGS_INTERVAL', 'weekly'); 34 define('GHOLAB_LOGS_TABLE_NAME', 'gholab_logs'); 34 35 35 36 require_once __DIR__ . '/src/Gholab.php'; -
gholab/trunk/src/Controllers/MenuAndPageController.php
r3017916 r3088300 74 74 $lastSyncLog = $this->logRepo->getLastSyncLog(); 75 75 if (null !== $lastSyncLog) { 76 $lastSync = JDateTime::dateTime($lastSyncLog->c omment_date);77 $lastSyncLog = $lastSyncLog-> comment_content;76 $lastSync = JDateTime::dateTime($lastSyncLog->created_at); 77 $lastSyncLog = $lastSyncLog->log_content; 78 78 } 79 79 80 80 $lastApiLog = $this->logRepo->getLastApiLog(); 81 81 if (null !== $lastApiLog) { 82 $lastApiCall = JDateTime::dateTime($lastApiLog->c omment_date);83 $lastApiLog = $lastApiLog-> comment_content;82 $lastApiCall = JDateTime::dateTime($lastApiLog->created_at); 83 $lastApiLog = $lastApiLog->log_content; 84 84 } 85 85 … … 95 95 96 96 if ('POST' === $_SERVER['REQUEST_METHOD']) { 97 check_admin_referer('gholab_welcome_nonce'); 98 $error = false; 97 99 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; 111 102 if (is_null($gholabWelcomeAccept) || 1 != $gholabWelcomeAccept) { 112 103 add_settings_error( … … 118 109 $error = true; 119 110 } 120 121 if (empty($gholabApiKey)) { 111 if (empty($gholabApiKey) || strlen($gholabApiKey) > GHOLAB_MAX_LEN_API_KEY || strlen($gholabApiKey) < GHOLAB_MIN_LEN_API_KEY) { 122 112 add_settings_error( 123 113 'gholab_api_key', … … 129 119 } 130 120 131 $ result = $this->gholabService->checkGholabToken($gholabApiKey);132 if ( !$result) {121 $gholabSiteResponse = $this->gholabService->sendPluginStatusToGholab($gholabApiKey, true); 122 if (false == $gholabSiteResponse) { 133 123 add_settings_error( 134 124 'gholab_api_key', … … 140 130 } 141 131 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 145 137 wp_redirect(admin_url('admin.php?page=gholab')); 146 138 return; … … 152 144 global $gholab_api_key; 153 145 $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'); 156 148 Helpers::renderPartial(__DIR__ . '/../Partials/Welcome.phtml'); 157 149 return; -
gholab/trunk/src/Data/LogRepo.php
r3017916 r3088300 12 12 public function getLastApiLog() 13 13 { 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; 26 22 } 27 23 … … 31 27 public function getLastSyncLog() 32 28 { 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; 45 37 } 46 38 } -
gholab/trunk/src/Gholab.php
r3017916 r3088300 73 73 } 74 74 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 75 94 /** 76 95 * @return void … … 79 98 { 80 99 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(); 87 102 88 103 // register cron jobs 89 104 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', []); 91 106 } 92 107 if (!wp_next_scheduled('gholab_cron_delete_logs_hook')) { … … 95 110 96 111 // 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 } 105 117 }); 106 118 } … … 117 129 118 130 // 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 } 129 136 }); 130 137 } -
gholab/trunk/src/Handlers/CronHandler.php
r3017916 r3088300 75 75 { 76 76 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(); 81 78 } catch (Exception $e) { 82 79 $this->logService->info([ -
gholab/trunk/src/Helpers/Helpers.php
r3017916 r3088300 7 7 public static function dd($arg) 8 8 { 9 echo '<pre >';9 echo '<pre style="direction: ltr;">'; 10 10 var_dump($arg); 11 11 echo '</pre>'; … … 284 284 $productData = self::mapToGholabUpdate($productData); 285 285 $productData['Availability'] = $product->get_status() !== 'publish' ? 0 : $productData['Availability']; 286 $productData['IsSecondHand'] = false; 286 287 return $productData; 287 288 } -
gholab/trunk/src/Services/GholabService.php
r3017916 r3088300 5 5 use Gholab\Gholab\Container; 6 6 use Gholab\Gholab\Data\SettingsRepo; 7 use Gholab\Gholab\Helpers\Helpers; 7 8 8 9 class GholabService … … 14 15 15 16 /** 17 * @var LogService $logService 18 */ 19 protected $logService; 20 21 /** 16 22 * @param Container $container 17 23 */ … … 19 25 { 20 26 $this->settingsRepo = $container->get(SettingsRepo::class); 27 $this->logService = $container->get(LogService::class); 21 28 } 22 29 … … 62 69 } 63 70 64 public function sendPluginStatusToGholab($ status)71 public function sendPluginStatusToGholab($apiKey, $status) 65 72 { 66 73 $result = wp_remote_post('https://api.gholab.ir/api/v1/Provider/Product/Status', [ … … 70 77 'Content-Type' => 'application/json', 71 78 'Accept' => 'application/json', 72 'ApiKey' => $ this->settingsRepo->getOption('gholab_api_key')79 'ApiKey' => $apiKey 73 80 ], 74 81 'body' => json_encode([ … … 77 84 ]); 78 85 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; 80 97 } 81 98 } -
gholab/trunk/src/Services/LogService.php
r3017916 r3088300 7 7 public function info($content, $type = 'gholab_log') 8 8 { 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) 12 15 ]); 13 16 } 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 } 14 25 } -
gholab/trunk/uninstall.php
r3017916 r3088300 15 15 $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_type LIKE %s", 'gholab_%') 16 16 ); 17 18 $tableName = $wpdb->prefix . GHOLAB_LOGS_TABLE_NAME; 19 $wpdb->query("DROP TABLE IF EXISTS {$tableName}"); 17 20 } 18 21 gholab_uninstall();
Note: See TracChangeset
for help on using the changeset viewer.