Skip to content

Commit

Permalink
Merge branch 'main' of github.com:cdek-it/wordpress
Browse files Browse the repository at this point in the history
  • Loading branch information
vermorag committed Jun 18, 2024
2 parents 5de183e + 8bcb9de commit 90a2a4c
Show file tree
Hide file tree
Showing 21 changed files with 1,474 additions and 305 deletions.
58 changes: 58 additions & 0 deletions src/Actions/Schedule/CollectOrders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace {
defined('ABSPATH') or exit;
}

namespace Cdek\Actions\Schedule {

use Cdek\Contracts\TaskContract;
use Cdek\Model\TaskOutputData;

class CollectOrders extends TaskContract
{
private const ORDERS_LIMIT = 10000;

public static function getName(): string
{
return 'collect-orphaned-orders';
}

public function start(): void
{
$query = new \WC_Order_Query(
[
'orderby' => 'id',
'order' => 'ASC',
'paginate' => true,
'limit' => self::ORDERS_LIMIT,
'return' => 'ids',
],
);

for ($page = 1, $maxPages = 1; $page <= $maxPages; $page++) {
$query->set('page', $page);
$result = $query->get_orders();

$maxPages = $result->max_num_pages;

$response = $this->cdekCoreApi->sendTaskData(
$this->taskId,
new TaskOutputData(
'success',
[
'orders' => array_map(
static fn($order) => (string)$order,
$result->orders,
)
],
$page,
$maxPages
)
);

$this->initData($response);
}
}
}
}
61 changes: 61 additions & 0 deletions src/Actions/Schedule/ReindexOrders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php


namespace {

defined('ABSPATH') or exit;
}

namespace Cdek\Actions\Schedule {

use Cdek\Contracts\TaskContract;
use Cdek\Exceptions\CdekApiException;
use Cdek\Exceptions\CdekScheduledTaskException;
use Cdek\Model\TaskOutputData;
use Cdek\Model\OrderMetaData;
use Cdek\Model\Validate;

class ReindexOrders extends TaskContract
{
public function __construct(string $taskId)
{
parent::__construct($taskId);
$this->initTaskData();
}

public static function getName(): string
{
return 'restore-order-uuids';
}

/**
* @return void
* @throws CdekApiException
* @throws CdekScheduledTaskException
* @throws \JsonException
*/
public function start(): void
{
if (empty($this->getTaskMeta())) {
throw new CdekScheduledTaskException(
'[CDEKDelivery] Failed to get orders meta info',
'cdek_error.core.data',
);
}

foreach ($this->getTaskMeta() as $arOrder) {
OrderMetaData::updateMetaByOrderId(
$arOrder['external_id'],
[
'order_uuid' => $arOrder['id'],
],
);
}

$this->initData($this->cdekCoreApi->sendTaskData(
$this->taskId,
new TaskOutputData('success'),
));
}
}
}
67 changes: 67 additions & 0 deletions src/Cache/FileCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace {
defined('ABSPATH') or exit;
}

namespace Cdek\Cache
{

use Cdek\Exceptions\CdekApiException;
use Cdek\Loader;

class FileCache
{
private const CACHE_FILE_NAME = '.cache.php';
private static array $store;

public static function getVars(): ?array
{
if(!file_exists(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME)){
return null;
}

return self::$store[self::CACHE_FILE_NAME] ?? self::$store[self::CACHE_FILE_NAME] = require_once(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME);
}

/**
* @param array|null $vars
*
* @return void
* @throws CdekApiException
*/
public static function putVars(?array $vars): void
{
if($vars === null){
return;
}

if(file_exists(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME)){
if(!is_writable(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME)){
throw new CdekApiException('[CDEKDelivery] Failed check file rights',
'cdek_error.cache.rights',
['path' => Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME],
true);
}
}else{
if(!is_writable(Loader::getPluginPath())){
throw new CdekApiException('[CDEKDelivery] Failed check directory rights',
'cdek_error.cache.rights',
['path' => Loader::getPluginPath()],
true);
}
}


$logFile = fopen( Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME, 'w+');

fwrite($logFile, '<?php return ' . var_export($vars, true) . ';' . PHP_EOL);
fclose($logFile);
}

public static function clear(): void
{
unlink(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME);
}
}
}
Loading

0 comments on commit 90a2a4c

Please sign in to comment.