| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\CLI; |
| 4 |
|
| 5 |
use FluentSupport\App\Modules\StatModule; |
| 6 |
use FluentSupport\App\Services\Tickets\Importer\MigratorService; |
| 7 |
use FluentSupport\App\Models\Meta; |
| 8 |
|
| 9 |
class FluentCli |
| 10 |
{ |
| 11 |
private $domain; |
| 12 |
private $accessToken; |
| 13 |
|
| 14 |
public function stats($args, $assoc_args) |
| 15 |
{ |
| 16 |
$overallStats = StatModule::getOverAllStats(); |
| 17 |
$format = \WP_CLI\Utils\get_flag_value($assoc_args, 'format', 'table'); |
| 18 |
|
| 19 |
\WP_CLI\Utils\format_items( |
| 20 |
$format, |
| 21 |
$overallStats, |
| 22 |
['title', 'count'] |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
public function activate_license($args, $assoc_args) |
| 27 |
{ |
| 28 |
if (empty($assoc_args['key'])) { |
| 29 |
\WP_CLI::line('use --key=LICENSE_KEY to activate the license'); |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$licenseKey = trim(sanitize_text_field($assoc_args['key'])); |
| 34 |
|
| 35 |
if (!class_exists('\FluentSupportPro\App\Services\PluginManager\LicenseManager')) { |
| 36 |
\WP_CLI::line('FluentSupport Pro is required'); |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
\WP_CLI::line('Validating License, Please wait'); |
| 41 |
|
| 42 |
$licenseManager = new \FluentSupportPro\App\Services\PluginManager\LicenseManager(); |
| 43 |
$response = $licenseManager->activateLicense($licenseKey); |
| 44 |
|
| 45 |
if (is_wp_error($response)) { |
| 46 |
\WP_CLI::error($response->get_error_message()); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
\WP_CLI::line('Your license key has been successfully updated'); |
| 51 |
\WP_CLI::line('Your License Status: ' . $response['status']); |
| 52 |
\WP_CLI::line('Expire Date: ' . $response['expires']); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
public function license_status() |
| 57 |
{ |
| 58 |
|
| 59 |
if (!class_exists('\FluentSupportPro\App\Services\PluginManager\LicenseManager')) { |
| 60 |
\WP_CLI::line('FluentSupport Pro is required'); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
\WP_CLI::line('Fetching License details, Please wait'); |
| 65 |
|
| 66 |
$licenseManager = new \FluentSupportPro\App\Services\PluginManager\LicenseManager(); |
| 67 |
$licenseManager->verifyRemoteLicense(true); |
| 68 |
$response = $licenseManager->getLicenseDetails(); |
| 69 |
|
| 70 |
\WP_CLI::line('Your License Status: ' . $response['status']); |
| 71 |
\WP_CLI::line('Expires: ' . $response['expires']); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
public function freshdesk_ticket_import($args, $assoc_args) |
| 76 |
{ |
| 77 |
try { |
| 78 |
if (!defined('FLUENTSUPPORTPRO_PLUGIN_VERSION')) { |
| 79 |
\WP_CLI::error('Fluent Support Pro is required for this command.'); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if (!$this->validateAndExtractParams($assoc_args)) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$startPage = $this->checkPreviousMigration(); |
| 88 |
|
| 89 |
$this->processMigration($startPage); |
| 90 |
|
| 91 |
} finally { |
| 92 |
$this->domain = null; |
| 93 |
$this->accessToken = null; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Validates and extracts parameters from CLI arguments. |
| 99 |
* |
| 100 |
* @param array $assoc_args Associative array of options. |
| 101 |
* @return bool True if validation succeeded, false otherwise |
| 102 |
*/ |
| 103 |
private function validateAndExtractParams($assoc_args) |
| 104 |
{ |
| 105 |
$this->domain = \WP_CLI\Utils\get_flag_value($assoc_args, 'domain'); |
| 106 |
if (empty($this->domain)) { |
| 107 |
$this->domain = \cli\prompt('Enter your support domain (e.g. https://yourcompany.freshdesk.com)'); |
| 108 |
} |
| 109 |
|
| 110 |
$this->accessToken = \WP_CLI\Utils\get_flag_value($assoc_args, 'access_token'); |
| 111 |
if (empty($this->accessToken)) { |
| 112 |
$this->accessToken = \cli\prompt('Enter your access token'); |
| 113 |
} |
| 114 |
|
| 115 |
if (empty($this->accessToken) || empty($this->domain)) { |
| 116 |
\WP_CLI::error('Both domain and access token are required to proceed.'); |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check for previous migration data and determine the starting page. |
| 125 |
* |
| 126 |
* @return int Page number to start from |
| 127 |
*/ |
| 128 |
private function checkPreviousMigration() |
| 129 |
{ |
| 130 |
$metadata = Meta::where('object_type', '_fs_freshdesk_migration_info')->first(); |
| 131 |
|
| 132 |
if (empty($metadata)) { |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
$previouslyImported = maybe_unserialize($metadata->value ?? []); |
| 137 |
$previousDomain = $metadata->key ?? ''; |
| 138 |
|
| 139 |
if ($previousDomain === $this->domain && |
| 140 |
!empty($previouslyImported['has_more']) && |
| 141 |
$previouslyImported['has_more'] === true && |
| 142 |
!empty($previouslyImported['next_page'])) { |
| 143 |
|
| 144 |
\WP_CLI::line("A previous incomplete migration was found for this domain:"); |
| 145 |
\WP_CLI::line(" - Last imported page: " . ($previouslyImported['imported_page'] ?? 'N/A')); |
| 146 |
\WP_CLI::line(" - Tickets imported so far: " . ($previouslyImported['completed'] ?? 0)); |
| 147 |
|
| 148 |
$choice = \cli\prompt('Do you want to resume from the last incomplete page? (yes/no)', 'yes'); |
| 149 |
|
| 150 |
if (strtolower($choice) === 'yes') { |
| 151 |
$startPage = (int) $previouslyImported['next_page']; |
| 152 |
\WP_CLI::line("Resuming from page $startPage..."); |
| 153 |
return $startPage; |
| 154 |
} |
| 155 |
|
| 156 |
\WP_CLI::line("Starting over from page 1..."); |
| 157 |
} |
| 158 |
|
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Process the migration by fetching and importing tickets page by page. |
| 164 |
* |
| 165 |
* @param int $startPage Page number to start from |
| 166 |
*/ |
| 167 |
private function processMigration($startPage = 1) |
| 168 |
{ |
| 169 |
try { |
| 170 |
$page = $startPage; |
| 171 |
$migrator = new MigratorService; |
| 172 |
$totalImported = 0; |
| 173 |
$totalSkipped = 0; |
| 174 |
|
| 175 |
while (true) { |
| 176 |
\WP_CLI::line("Processing page: $page"); |
| 177 |
|
| 178 |
$result = $migrator->handleImport($page, 'freshdesk', [ |
| 179 |
'access_token' => $this->accessToken, |
| 180 |
'domain' => $this->domain |
| 181 |
]); |
| 182 |
|
| 183 |
if (empty($result) || !isset($result['completed'])) { |
| 184 |
\WP_CLI::warning("Invalid response received for page $page. Stopping migration."); |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
$imported = count($result['insert_ids'] ?? []); |
| 189 |
$skipped = intval($result['skips'] ?? 0); |
| 190 |
|
| 191 |
$totalImported += $imported; |
| 192 |
$totalSkipped += $skipped; |
| 193 |
|
| 194 |
\WP_CLI::success(sprintf( |
| 195 |
"Page %d processed: %d ticket(s) imported, %d skipped", |
| 196 |
$page, |
| 197 |
$imported, |
| 198 |
$skipped |
| 199 |
)); |
| 200 |
|
| 201 |
if (empty($result['has_more'])) { |
| 202 |
break; |
| 203 |
} |
| 204 |
|
| 205 |
$page = intval($result['next_page'] ?? ($page + 1)); |
| 206 |
} |
| 207 |
|
| 208 |
\WP_CLI::success(sprintf( |
| 209 |
"Migration complete. Total: %d imported, %d skipped.", |
| 210 |
$totalImported, |
| 211 |
$totalSkipped |
| 212 |
)); |
| 213 |
} catch (\Exception $e) { |
| 214 |
\WP_CLI::error("Migration failed: " . $e->getMessage()); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|