Skip to content

Commit

Permalink
fix: upgrader process
Browse files Browse the repository at this point in the history
  • Loading branch information
vermorag committed Dec 10, 2024
1 parent a74c262 commit c7aeb75
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Config
public const DELIVERY_NAME = 'official_cdek';
public const ORDER_META_BOX_KEY = 'official_cdek_order';
public const ORDER_AUTOMATION_HOOK_NAME = 'cdekdelivery_automation';
public const UPGRADE_HOOK_NAME = 'cdekdelivery_upgrade';
public const TASK_MANAGER_HOOK_NAME = 'cdekdelivery_task_manager';
public const API_CORE_URL = 'https://api.cdek.ru/';
public const API_URL = 'https://api.cdek.ru/v2/';
Expand Down
91 changes: 45 additions & 46 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cdek\Actions\ProcessWoocommerceCreateShippingAction;
use Cdek\Actions\RecalculateShippingAction;
use Cdek\Actions\SaveCustomCheckoutFieldsAction;
use Cdek\Blocks\AdminOrderBox;
use Cdek\Blocks\CheckoutMapBlock;
use Cdek\Controllers\CallbackController;
use Cdek\Controllers\IntakeController;
Expand All @@ -30,9 +31,9 @@
use Cdek\UI\CdekWidget;
use Cdek\UI\CheckoutMap;
use Cdek\UI\Frontend;
use Cdek\Blocks\AdminOrderBox;
use Cdek\Validator\CheckoutValidator;
use RuntimeException;
use WP_Upgrader;

class Loader
{
Expand Down Expand Up @@ -62,31 +63,6 @@ class Loader
private static string $pluginName;
private static string $pluginMainFile;

public static function debug(): bool
{
return self::$debug;
}

public static function getPluginVersion(): string
{
return self::$pluginVersion;
}

public static function getPluginName(): string
{
return self::$pluginName;
}

public static function getPluginUrl(?string $path = null): string
{
return plugin_dir_url(self::$pluginMainFilePath).($path !== null ? ltrim($path, '/') : '');
}

public static function getPluginFile(): string
{
return self::$pluginMainFile;
}

/**
* @throws RuntimeException
*/
Expand All @@ -97,7 +73,7 @@ public static function activate(): void
}

self::checkRequirements();
self::upgrade(true);
self::upgrade();
}

/**
Expand Down Expand Up @@ -135,27 +111,29 @@ public static function deactivate(): void
TaskManager::cancelExecution();
}

/**
* @param \WP_Upgrader|true $up
* @param array $options
*
* @return void
* @noinspection MissingParameterTypeDeclarationInspection
*/
public static function upgrade($up, array $options = []): void
public static function debug(): bool
{
if ($up !== true &&
($options['action'] !== 'update' ||
$options['type'] !== 'plugin' ||
!in_array(self::$pluginMainFile, $options['plugins'] ?? [], true))) {
return;
}
return self::$debug;
}

TaskManager::scheduleExecution();
public static function getPluginFile(): string
{
return self::$pluginMainFile;
}

foreach (self::MIGRATORS as $migrator) {
(new $migrator)();
}
public static function getPluginName(): string
{
return self::$pluginName;
}

public static function getPluginUrl(?string $path = null): string
{
return plugin_dir_url(self::$pluginMainFilePath).($path !== null ? ltrim($path, '/') : '');
}

public static function getPluginVersion(): string
{
return self::$pluginVersion;
}

public static function getTemplate(string $name): string
Expand All @@ -168,6 +146,26 @@ public static function getPluginPath(?string $path = null): string
return plugin_dir_path(self::$pluginMainFilePath).($path !== null ? ltrim($path, DIRECTORY_SEPARATOR) : '');
}

public static function scheduleUpgrade(WP_Upgrader $_wp, array $options): void
{
if (($options['action'] !== 'update' ||
$options['type'] !== 'plugin' ||
!in_array(self::$pluginMainFile, $options['plugins'] ?? [], true))) {
return;
}

as_schedule_single_action(time() + 60, Config::UPGRADE_HOOK_NAME, [], 'cdekdelivery', true);
}

public static function upgrade(): void
{
TaskManager::scheduleExecution();

foreach (self::MIGRATORS as $migrator) {
(new $migrator)();
}
}

public function __invoke(string $pluginMainFile): void
{
self::$pluginMainFilePath = $pluginMainFile;
Expand All @@ -192,7 +190,7 @@ public function __invoke(string $pluginMainFile): void
add_action("activate_".self::$pluginMainFile, [__CLASS__, 'activate']);
add_action("deactivate_".self::$pluginMainFile, [__CLASS__, 'deactivate']);

add_action('upgrader_process_complete', [__CLASS__, 'upgrade'], 30, 2);
add_action('upgrader_process_complete', [__CLASS__, 'scheduleUpgrade'], 30, 2);

self::declareCompatibility();

Expand Down Expand Up @@ -257,6 +255,7 @@ public function __invoke(string $pluginMainFile): void

add_action(Config::ORDER_AUTOMATION_HOOK_NAME, OrderCreateAction::new(), 10, 2);
add_action(Config::TASK_MANAGER_HOOK_NAME, new TaskManager, 20);
add_action(Config::UPGRADE_HOOK_NAME, [__CLASS__, 'upgrade']);

CdekWidget::new()();
Admin::new()();
Expand Down
1 change: 1 addition & 0 deletions src/TaskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __invoke(): void
as_enqueue_async_action(
$hookName,
[$task['id']],
'cdekdelivery',
);

continue;
Expand Down

0 comments on commit c7aeb75

Please sign in to comment.