first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
{
"name": "wpdesk\/wp-wpdesk-pickup-points",
"authors": [
{
"name": "Sebastian Pisula",
"email": "sebastian.pisula@flexibleshipping.com"
}
],
"config": {
"sort-packages": true,
"platform": {
"php": "7.0"
}
},
"require": {
"php": ">=7.0",
"psr\/log": "^1.1",
"wpdesk\/wp-builder": "^1.4|^2"
},
"require-dev": {
"10up\/wp_mock": "*",
"mockery\/mockery": "*",
"phpunit\/phpunit": "<7",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"wimg\/php-compatibility": "^8"
},
"autoload": {
"psr-4": {
"DPDVendor\\WPDesk\\PickupPoints\\": "src\/"
}
},
"autoload-dev": {},
"extra": {
"text-domain": "wp-wpdesk-pickup-points",
"translations-folder": "lang",
"po-files": {
"pl_PL": "pl_PL.po"
}
},
"scripts": {
"phpcs": "phpcs",
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-html build-coverage",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace DPDVendor\WPDesk\PickupPoints;
use DPDVendor\WPDesk\PickupPoints\Cache\CacheManager;
abstract class AbstractPickupPointsManager implements \DPDVendor\WPDesk\PickupPoints\PickupPointsManager
{
/**
* @var CacheManager
*/
protected $cache_manager;
/**
* @param CacheManager $pickup_points .
*/
public function __construct(\DPDVendor\WPDesk\PickupPoints\Cache\CacheManager $pickup_points)
{
$this->pickup_points = $pickup_points;
}
public abstract function get_points(bool $cod_only = \false) : array;
public abstract function get_point_details(string $point_id) : \DPDVendor\WPDesk\PickupPoints\PickupPointDetails;
}

View File

@@ -0,0 +1,86 @@
<?php
namespace DPDVendor\WPDesk\PickupPoints;
abstract class AbstractPointDetails implements \DPDVendor\WPDesk\PickupPoints\PickupPointDetails
{
/**
* @var string
*/
private $point_id;
/**
* @var string
*/
private $street;
/**
* @var string
*/
private $zipcode;
/**
* @var string
*/
private $city;
/**
* @var string
*/
private $country;
/**
* @var array
*/
private $meta_data;
/**
* @var bool
*/
private $cod;
/**
* @param string $point_id .
* @param string $street .
* @param string $zipcode .
* @param string $city .
* @param string $country .
* @param array $meta_data .
* @param bool $cod .
*/
public function __construct(string $point_id, string $street, string $zipcode, string $city, string $country, bool $cod = \false, array $meta_data = [])
{
$this->point_id = $point_id;
$this->street = $street;
$this->zipcode = $zipcode;
$this->city = $city;
$this->country = $country;
$this->meta_data = $meta_data;
$this->cod = $cod;
}
public function get_id() : string
{
return $this->point_id;
}
public function get_street() : string
{
return $this->street;
}
public function get_zipcode() : string
{
return $this->zipcode;
}
public function get_city() : string
{
return $this->city;
}
public function get_country() : string
{
return $this->country;
}
public function get_label() : string
{
return $this->get_street() . ', ' . $this->get_zipcode() . ' ' . $this->get_city();
}
public function get_meta_data() : array
{
return $this->meta_data;
}
public function is_cod() : bool
{
return $this->cod;
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace DPDVendor\WPDesk\PickupPoints;
class Ajax
{
const AJAX_ACTION_SUFFIX = '_collection_point_ajax';
/**
* @var PickupPointsManager
*/
private $pickup_points_manager;
/**
* @var string
*/
private $integration;
public function __construct(\DPDVendor\WPDesk\PickupPoints\PickupPointsManager $pickup_points_manager, string $integration)
{
$this->pickup_points_manager = $pickup_points_manager;
$this->integration = $integration;
}
/**
* Hooks.
*/
public function hooks()
{
\add_action('wp_ajax_' . $this->get_ajax_action(), array($this, 'ajax_collection_points'));
\add_action('wp_ajax_nopriv_' . $this->get_ajax_action(), array($this, 'ajax_collection_points'));
}
public function get_ajax_action() : string
{
return $this->integration . self::AJAX_ACTION_SUFFIX;
}
/**
* Creates nonce.
*
* @return string
*/
public function create_nonce()
{
return \wp_create_nonce($this->get_ajax_action());
}
/**
* @param array $collection_points .
*
* @return array
*/
private function convert_points_for_select2($collection_points)
{
$elements = array('items');
foreach ($collection_points as $id => $text) {
$elements['items'][] = array('id' => $id, 'text' => $text);
}
return $elements;
}
/**
* Handles AJAX request.
*
* @internal
*/
public function ajax_collection_points()
{
\check_ajax_referer($this->get_ajax_action(), 'security');
$cod_only = isset($_REQUEST['cod']) && \sanitize_key(\wp_unslash($_REQUEST['cod'])) === 'yes';
$query = \sanitize_text_field($_REQUEST['query'] ?? '');
try {
$collection_points = $this->filter_collection_points($this->pickup_points_manager->get_points($cod_only), $query);
} catch (\Exception $e) {
$collection_points = array('error' => \sprintf(\__('Error while creating pickup points list: %1$s', 'woocommerce-dpd'), $e->getMessage()));
}
\wp_send_json($this->convert_points_for_select2($collection_points));
}
/**
* Filter collection point.
*
* @param array $collection_points
* @param string $query
*
* @return array
*/
private function filter_collection_points(array $collection_points, $query)
{
$query_parts = \explode(' ', \trim(\str_replace(',', ' ', $query)));
foreach ($query_parts as $query_part) {
$query_part = $this->str_to_upper(\trim($query_part));
if ($query_part) {
$collection_points = \array_filter($collection_points, function ($elem) use($query_part) {
$elem = $this->str_to_upper($elem);
return \strpos($elem, $query_part) !== \false;
});
}
}
return $collection_points;
}
/**
* Str to upper.
*
* @param $str
*
* @return string
*/
private function str_to_upper($str)
{
if (\function_exists('mb_strtoupper')) {
return \mb_strtoupper($str);
} else {
return \strtoupper($str);
}
}
}

View File

@@ -0,0 +1,134 @@
<?php
/**
* Class CacheManager
*
* @package WPDesk\PickupPoints\Cache
*/
namespace DPDVendor\WPDesk\PickupPoints\Cache;
/**
* Cache manager.
*/
class CacheManager
{
const TIMEOUT_SUFFIX = '_timeout';
const AUTOLOAD_VALUE = 'no';
const OPTION_NAME_PREFIX = 'pickup_points_data_';
/**
* @var ObjectDataInterface
*/
private $object_data;
/**
* @var string
*/
private $name;
/**
* @var int
*/
private $expiration;
/**
* @param ObjectDataInterface $object_data .
* @param string|null $name .
* @param int $expiration .
*/
public function __construct(\DPDVendor\WPDesk\PickupPoints\Cache\ObjectDataInterface $object_data, string $name = null, int $expiration = 86400)
{
$this->object_data = $object_data;
$this->name = $name ?: \get_class($object_data);
$this->expiration = $expiration;
}
/**
* @return mixed
* @throws GetDataException
*/
public function get_data(bool $force = \false, $default_value = null)
{
$cached_data = $this->get_cached_data();
if (!$force && $cached_data !== null) {
return $cached_data;
}
try {
return $this->set_cached_data($this->object_data->get());
} catch (\Throwable $e) {
if ($force) {
throw $e;
}
}
return $cached_data ?: $default_value;
}
/**
* @return void
*/
public function clear_cache()
{
\delete_option($this->get_option_name());
\delete_option($this->get_option_name_timeout());
}
/**
* @return int
* @codeCoverageIgnore
*/
protected function get_current_time()
{
return \time();
}
/**
* @return bool
*/
private function is_expired() : bool
{
$expiration = (int) \get_option($this->get_option_name_timeout(), 0);
return $this->get_current_time() > $expiration;
}
/**
* @return mixed
*/
private function get_cached_data()
{
$data = \get_option($this->get_option_name(), null);
if (\is_string($data)) {
$json_data = \json_decode($data, \true);
if (\json_last_error() === \JSON_ERROR_NONE) {
return $json_data;
}
}
return $data;
}
/**
* @param mixed $cached_data .
*
* @return mixed
* @throws GetDataException
*/
private function set_cached_data($cached_data)
{
global $wpdb;
$wpdb->show_errors();
\update_option($this->get_option_name_timeout(), $this->get_current_time() + $this->expiration, self::AUTOLOAD_VALUE);
$encoded_cached_data = \json_encode($cached_data, \JSON_UNESCAPED_UNICODE);
if ($encoded_cached_data !== \get_option($this->get_option_name(), '')) {
if (\false === \update_option($this->get_option_name(), $encoded_cached_data, self::AUTOLOAD_VALUE)) {
if ($wpdb->last_error) {
throw new \DPDVendor\WPDesk\PickupPoints\Cache\GetDataException($wpdb->last_error);
}
throw new \DPDVendor\WPDesk\PickupPoints\Cache\GetDataException(\__('Unknown error while updating option.', 'woocommerce-dpd'));
}
}
return $cached_data;
}
/**
* @return string
*/
private function get_option_name()
{
return self::OPTION_NAME_PREFIX . $this->name;
}
/**
* @return string
*/
private function get_option_name_timeout()
{
return $this->get_option_name() . self::TIMEOUT_SUFFIX;
}
}

View File

@@ -0,0 +1,16 @@
<?php
/**
* Class GetDataException
*
* @package WPDesk\PickupPoints\Cache
*/
namespace DPDVendor\WPDesk\PickupPoints\Cache;
use RuntimeException;
/**
* Exception get data.
*/
class GetDataException extends \RuntimeException
{
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* Interface ObjectDataInterface
*
* @package WPDesk\PickupPoints\Cache
*/
namespace DPDVendor\WPDesk\PickupPoints\Cache;
/**
* Object data.
*/
interface ObjectDataInterface
{
/**
* @return mixed
* @throws GetDataException
*/
public function get();
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* Class CronAction
*
* @package WPDesk\PickupPoints
*/
namespace DPDVendor\WPDesk\PickupPoints;
use DPDVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Cron action refresh.
*/
class CronAction implements \DPDVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
const HOOK_NAME = 'refresh_pickup_points';
/**
* @var string
*/
private $service;
/**
* @var RefreshAction
*/
private $refresh_action;
/**
* @param string $service .
* @param RefreshAction $refresh_action .
*/
public function __construct(string $service, \DPDVendor\WPDesk\PickupPoints\RefreshAction $refresh_action)
{
$this->service = $service;
$this->refresh_action = $refresh_action;
}
/**
* @return void
*/
public function hooks()
{
\add_action(self::HOOK_NAME, [$this, 'refresh_pickup_points']);
}
/**
* @param string $service .
*
* @return void
*/
public function refresh_pickup_points(string $service)
{
\set_time_limit(0);
if ($this->service !== $service) {
return;
}
$this->refresh_action->start();
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Class LastRefreshTime
*
* @package WPDesk\PickupPoints
*/
namespace DPDVendor\WPDesk\PickupPoints;
/**
* Pickup Refresh data.
*/
class LastRefreshTime
{
const LAST_REFRESH_PICKUP_POINTS_TIME_FIELD = 'pickup_points_last_refresh_time';
const LAST_REFRESH_PICKUP_POINTS_DATA_FIELD = 'pickup_points_last_refresh_data';
const STATUS = 'status';
const OK = 'ok';
const ERROR = 'error';
const MESSAGE = 'message';
/**
* @var string
*/
private $service;
/**
* @param string $service .
*/
public function __construct(string $service)
{
$this->service = $service;
}
/**
* @return int
*/
public function get_last_refresh_time() : int
{
return (int) \get_option(self::LAST_REFRESH_PICKUP_POINTS_TIME_FIELD . '_' . $this->service, 0);
}
public function get_last_refresh_status() : string
{
$data = $this->get_last_refresh_status_data();
return $data[self::STATUS] ?? self::OK;
}
public function get_last_refresh_message() : string
{
$data = $this->get_last_refresh_status_data();
return $data[self::MESSAGE] ?? '';
}
private function get_last_refresh_status_data() : array
{
$data = \get_option(self::LAST_REFRESH_PICKUP_POINTS_DATA_FIELD . '_' . $this->service, []);
$data = \is_array($data) ? $data : [];
return $data;
}
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* Class ManualAction
*
* @package WPDesk\PickupPoints
*/
namespace DPDVendor\WPDesk\PickupPoints;
use WC_Admin_Settings;
use DPDVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Manual action for refresh
*/
class ManualAction implements \DPDVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
const ACTION = 'refresh-pickup-points';
const SERVICE = 'service';
const STATUS = 'status';
const NONCE = 'security';
/**
* @var string
*/
private $service;
/**
* @var RefreshAction
*/
private $refresh_action;
/**
* @param string $service .
* @param RefreshAction $refresh_action .
*/
public function __construct(string $service, \DPDVendor\WPDesk\PickupPoints\RefreshAction $refresh_action)
{
$this->service = $service;
$this->refresh_action = $refresh_action;
}
/**
* @return void
*/
public function hooks()
{
\add_action('admin_init', [$this, 'add_message']);
\add_action('admin_post_' . self::ACTION, [$this, 'refresh_pickup_points']);
}
/**
* @return void
*/
public function add_message()
{
$status = $this->filter_input(\INPUT_GET, self::STATUS);
$service = $this->filter_input(\INPUT_GET, self::ACTION);
if ($service !== $this->service) {
return;
}
if ('success' === $status) {
$this->add_wc_settings_message(\__('Clearing the cache and refreshing the list of reception points successfully completed.', 'woocommerce-dpd'));
} elseif ('error' === $status) {
$this->add_wc_settings_error(\__('An error occurred while clearing the cache and refreshing the list of pickup points. Please try again.', 'woocommerce-dpd'));
}
}
/**
* @return void
*/
public function refresh_pickup_points()
{
\check_admin_referer(self::NONCE);
$service = $this->filter_input(\INPUT_GET, self::SERVICE);
if ($service !== $this->service) {
return;
}
if ($this->refresh_action->start()) {
\wp_safe_redirect($this->get_redirect_url('success'));
} else {
\wp_safe_redirect($this->get_redirect_url('error'));
}
$this->end_request();
}
/**
* @return void
* @codeCoverageIgnore
*/
protected function end_request()
{
die;
}
/**
* @param string $message_text .
*
* @return void
* @codeCoverageIgnore
*/
protected function add_wc_settings_message(string $message_text)
{
\WC_Admin_Settings::add_message($message_text);
}
/**
* @param string $message_text .
*
* @return void
* @codeCoverageIgnore
*/
protected function add_wc_settings_error(string $message_text)
{
\WC_Admin_Settings::add_error($message_text);
}
/**
* @param int $type .
* @param string $var_name .
*
* @return mixed
* @codeCoverageIgnore
*/
protected function filter_input(int $type, string $var_name)
{
return \filter_input($type, $var_name);
}
/**
* @param string $status .
*
* @return string
*/
private function get_redirect_url(string $status) : string
{
return \add_query_arg([self::ACTION => $this->service, self::STATUS => $status], \wp_get_referer());
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace DPDVendor\WPDesk\PickupPoints;
interface PickupPointDetails
{
public function get_id() : string;
public function get_street() : string;
public function get_zipcode() : string;
public function get_city() : string;
public function get_country() : string;
public function get_label() : string;
public function get_meta_data() : array;
public function is_cod() : bool;
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Class PickupPoints
*
* @package WPDesk\PickupPoints
*/
namespace DPDVendor\WPDesk\PickupPoints;
use DPDVendor\Psr\Log\LoggerInterface;
use DPDVendor\WPDesk\PluginBuilder\Plugin\HookableCollection;
use DPDVendor\WPDesk\PluginBuilder\Plugin\HookableParent;
/**
* Main library class.
*/
class PickupPoints implements \DPDVendor\WPDesk\PluginBuilder\Plugin\HookableCollection
{
use HookableParent;
/**
* @var string
*/
private $service;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param string $service .
* @param LoggerInterface $logger .
*/
public function __construct(string $service, \DPDVendor\Psr\Log\LoggerInterface $logger)
{
$this->service = $service;
$this->logger = $logger;
}
/**
* @return void
*/
public function hooks()
{
$refresh_action = new \DPDVendor\WPDesk\PickupPoints\RefreshAction($this->service, $this->logger);
$this->add_hookable(new \DPDVendor\WPDesk\PickupPoints\RegisterCron($this->service));
$this->add_hookable(new \DPDVendor\WPDesk\PickupPoints\CronAction($this->service, $refresh_action));
$this->add_hookable(new \DPDVendor\WPDesk\PickupPoints\ManualAction($this->service, $refresh_action));
$this->hooks_on_hookable_objects();
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace DPDVendor\WPDesk\PickupPoints;
interface PickupPointsManager
{
public function get_points(bool $cod_only = \false) : array;
public function get_point_details(string $point_id) : \DPDVendor\WPDesk\PickupPoints\PickupPointDetails;
}

View File

@@ -0,0 +1,61 @@
<?php
/**
* Class RefreshAction
*
* @package WPDesk\PickupPoints
*/
namespace DPDVendor\WPDesk\PickupPoints;
use Exception;
use DPDVendor\Psr\Log\LoggerInterface;
/**
* Refresh action.
*/
class RefreshAction
{
/**
* @var string
*/
private $service;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param string $service .
* @param LoggerInterface $logger .
*/
public function __construct(string $service, \DPDVendor\Psr\Log\LoggerInterface $logger)
{
$this->service = $service;
$this->logger = $logger;
}
/**
* @return bool
*/
public function start() : bool
{
\set_time_limit(0);
$status_data = [\DPDVendor\WPDesk\PickupPoints\LastRefreshTime::STATUS => \DPDVendor\WPDesk\PickupPoints\LastRefreshTime::OK];
try {
$this->do_action();
return \true;
} catch (\Throwable $e) {
$status_data[\DPDVendor\WPDesk\PickupPoints\LastRefreshTime::STATUS] = \DPDVendor\WPDesk\PickupPoints\LastRefreshTime::ERROR;
$status_data[\DPDVendor\WPDesk\PickupPoints\LastRefreshTime::MESSAGE] = $e->getMessage();
$this->logger->error($e->getMessage());
return \false;
} finally {
\update_option(\DPDVendor\WPDesk\PickupPoints\LastRefreshTime::LAST_REFRESH_PICKUP_POINTS_TIME_FIELD . '_' . $this->service, \current_time('timestamp', \true), 'no');
\update_option(\DPDVendor\WPDesk\PickupPoints\LastRefreshTime::LAST_REFRESH_PICKUP_POINTS_DATA_FIELD . '_' . $this->service, $status_data);
}
}
/**
* @codeCoverageIgnore
*/
protected function do_action()
{
\do_action('pickup-points/refresh/' . $this->service);
}
}

View File

@@ -0,0 +1,58 @@
<?php
/**
* Class RegisterCron
*
* @package WPDesk\PickupPoints
*/
namespace DPDVendor\WPDesk\PickupPoints;
use DPDVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Register cron action.
*/
class RegisterCron implements \DPDVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
const CRON_TIMEOUT = DAY_IN_SECONDS;
/**
* @var string
*/
private $service;
/**
* @param string $service .
*/
public function __construct(string $service)
{
$this->service = $service;
}
/**
* @return void
*/
public function hooks()
{
\add_action('init', [$this, 'register_cron_action']);
}
/**
* @return void
*/
public function register_cron_action()
{
if ($this->can_register_cron()) {
\as_schedule_recurring_action(\time() - self::CRON_TIMEOUT - 10, self::CRON_TIMEOUT, \DPDVendor\WPDesk\PickupPoints\CronAction::HOOK_NAME, $this->get_cron_args());
}
}
/**
* @return bool
*/
private function can_register_cron() : bool
{
return \function_exists('as_schedule_recurring_action') && !\as_next_scheduled_action(\DPDVendor\WPDesk\PickupPoints\CronAction::HOOK_NAME, $this->get_cron_args());
}
/**
* @return array<string, string>
*/
private function get_cron_args() : array
{
return ['service' => $this->service];
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace DPDVendor\WPDesk\PickupPoints;
class Select2Script
{
/**
* @var Ajax
*/
private $ajax;
/**
* @var string
*/
private $field_id;
/**
* @var string
*/
private $action;
/**
* @var string
*/
private $ajax_url;
public function __construct(\DPDVendor\WPDesk\PickupPoints\Ajax $ajax, string $field_id, string $action, string $ajax_url)
{
$this->ajax = $ajax;
$this->field_id = $field_id;
$this->action = $action;
$this->ajax_url = $ajax_url;
}
public function get_script()
{
\ob_start();
$nonce = $this->ajax->create_nonce();
$field_id = $this->field_id;
$action = $this->action;
$ajax_url = $this->ajax_url;
include __DIR__ . '/views/select2-script.php';
return \ob_get_clean();
}
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* Trait RefreshPickupPointsTrait
*
* @package WPDesk\PickupPoints\WooCommerceSettings\Fields
*/
namespace DPDVendor\WPDesk\PickupPoints\WooCommerceSettings\Fields;
use DPDVendor\WPDesk\PickupPoints\Cache\CacheManager;
use DPDVendor\WPDesk\PickupPoints\ManualAction;
use DPDVendor\WPDesk\PickupPoints\LastRefreshTime;
/**
* Render refresh pickup points field.
*/
class RefreshPickupPointsField
{
/**
* @var array .
*/
private $data;
/**
* @param array $data .
*/
public function __construct(array $data)
{
$this->data = $data;
}
/**
* @return string
*/
public function render() : string
{
$data = $this->get_prepared_data();
$refresh_data = $this->get_refresh_data($data['service']);
$last_refresh_time_message = $this->get_last_refresh_time_message($refresh_data->get_last_refresh_time());
$refresh_url = $this->get_refresh_pickup_points_url($data['service']);
$error = $refresh_data->get_last_refresh_status() === \DPDVendor\WPDesk\PickupPoints\LastRefreshTime::ERROR;
$last_error_message = $refresh_data->get_last_refresh_message();
\ob_start();
include __DIR__ . '/views/html-settings-field-refresh-pickup-points.php';
$output = \ob_get_clean();
return \trim($output ? $output : \__('Error rendering field.', 'woocommerce-dpd'));
}
/**
* @codeCoverageIgnore
*
* @param string $service .
*
* @return LastRefreshTime
*/
protected function get_refresh_data(string $service) : \DPDVendor\WPDesk\PickupPoints\LastRefreshTime
{
return new \DPDVendor\WPDesk\PickupPoints\LastRefreshTime($service);
}
/**
* @return array
*/
private function get_prepared_data() : array
{
return \wp_parse_args($this->data, $this->get_defaults());
}
/**
* @return string[]
*/
private function get_defaults() : array
{
return ['service' => '', 'title' => \__('Pickup points cache', 'woocommerce-dpd'), 'label' => \__('Clear cache and refresh the list of pickup points', 'woocommerce-dpd'), 'desc_trip' => \__('The list of pickup points is automatically updated once a day from the API. Use the Cache button if you notice outdated pickup points in the list.', 'woocommerce-dpd')];
}
/**
* @param int $last_refresh .
*
* @return string
*/
private function get_last_refresh_time_message(int $last_refresh) : string
{
if ($last_refresh > 0) {
$humann_time_diff = \human_time_diff(\time(), $last_refresh) . ' ' . \__('ago', 'woocommerce-dpd');
} else {
$humann_time_diff = \__('Never', 'woocommerce-dpd');
}
return \__('Recent refresh: ', 'woocommerce-dpd') . \sprintf("%s%s%s.", '<strong>', $humann_time_diff, '</strong>');
}
/**
* @param string $service .
*
* @return string
*/
private function get_refresh_pickup_points_url(string $service) : string
{
$url = \admin_url('admin-post.php');
$url = \add_query_arg('action', \DPDVendor\WPDesk\PickupPoints\ManualAction::ACTION, $url);
$url = \add_query_arg('service', $service, $url);
return \wp_nonce_url($url, \DPDVendor\WPDesk\PickupPoints\ManualAction::NONCE);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace DPDVendor;
/**
* @var array $data .
* @var string $last_refresh_time_message .
* @var string $refresh_url .
* @var bool $error .
* @var string $last_error_message .
*/
$label_value = \wp_kses_post($data['title']);
if ($data['desc_trip']) {
$label_value .= \wc_help_tip($data['desc_trip']);
}
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label><?php
echo \wp_kses_post($label_value);
?></label>
</th>
<td class="forminp">
<a href="<?php
echo \esc_url($refresh_url);
?>"
class="button-secondary"><?php
echo \wp_kses_post($data['label']);
?></a>
<p class="description"><?php
echo \wp_kses_post($last_refresh_time_message);
?></p>
<?php
if ($error) {
?>
<p class="description"><?php
echo \esc_html__('Recent refresh error:', 'woocommerce-dpd');
?> <strong style="color: red;"><?php
echo \wp_kses_post($last_error_message);
?></strong></p>
<?php
}
?>
</td>
</tr>
<?php

View File

@@ -0,0 +1,26 @@
<?php
/**
* Trait RefreshPickupPointsTrait
*
* @package WPDesk\PickupPoints\WooCommerceSettings
*/
namespace DPDVendor\WPDesk\PickupPoints\WooCommerceSettings;
use DPDVendor\WPDesk\PickupPoints\WooCommerceSettings\Fields\RefreshPickupPointsField;
/**
* Helper for rendering field.
*/
trait RefreshPickupPointsTrait
{
/**
* @param string $key .
* @param array $data .
*
* @return string
*/
public function generate_refresh_pickup_points_html(string $key, array $data) : string
{
return (new \DPDVendor\WPDesk\PickupPoints\WooCommerceSettings\Fields\RefreshPickupPointsField($data))->render();
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace DPDVendor;
/**
* @var string $field_id
* @var string $action
* @var string $nonce
* @var string $ajax_url
*/
?><script type="text/javascript">
jQuery( function ( $ ) {
const ajax_nonce = '<?php
echo \esc_attr($nonce);
?>';
const $field = $('#<?php
echo \esc_attr($field_id);
?>');
var cod = $field.data( 'cod' );
let options = {
ajax: {
url: '<?php
echo \esc_attr($ajax_url);
?>',
dataType: 'json',
delay: 300,
type: 'POST',
data: function (params) {
return {
action: '<?php
echo \esc_attr($action);
?>',
query: params.term,
security: ajax_nonce,
cod: cod,
};
},
processResults: function (data, params) {
return {
results: data.items,
};
},
cache: true,
},
allowClear: true,
minimumInputLength: 3,
width: 'resolve',
dropdownAutoWidth: true,
dropdownParent: jQuery('body'),
language: {
inputTooShort: function (args) {
var remainingChars = args.minimum - args.input.length;
return '<?php
echo \esc_attr(\__('Enter minimum % characters', 'woocommerce-dpd'));
?>'.replace('%', remainingChars);
},
loadingMore: function () {
return '<?php
echo \esc_attr(\__('Loading more...', 'woocommerce-dpd'));
?>'
},
noResults: function () {
return '<?php
echo \esc_attr(\__('No pickup points.', 'woocommerce-dpd'));
?>'
},
searching: function () {
return '<?php
echo \esc_attr(\__('Searching for pickup points...', 'woocommerce-dpd'));
?>'
},
errorLoading: function () {
return '<?php
echo \esc_attr(\__('Error while loading pickup points.', 'woocommerce-dpd'));
?>'
},
},
};
if ($.fn.selectWoo) {
$field.selectWoo(options);
} else if ($.fn.select2) {
$field.select2(options);
}
} );
</script>
<?php