Files
2026-04-28 15:13:50 +02:00

180 lines
3.8 KiB
PHP

<?php
/**
* Class WPDesk_Paczkomaty_Select_Machines_Via_Ajax
*
* @package WooCommerce InPost
*/
use FS\InPost\PickupPoints\PickupPointsManager;
/**
* Select Machines via AJAX.
*/
class WPDesk_Paczkomaty_Select_Machines_Via_Ajax {
/**
* .
*
* @var int
*/
const MIN_LENGTH = 3;
const AJAX_ACTION = 'paczkomaty_search_machines_via_ajax';
/**
* .
*
* @var WPDesk_Paczkomaty $plugin
*/
protected $plugin = null;
/**
* .
*
* @var array
*/
private $machines;
/**
* .
*
* @var WPDesk_Paczkomaty_Ajax_Request
*/
private $requests;
/**
* .
*
* @var string
*/
private $nonce_action_name;
/**
* @var PickupPointsManager
*/
private $pickup_points_manager;
/**
* WPDesk_Paczkomaty_Select_Machines_Via_Ajax constructor.
*
* @param WPDesk_Paczkomaty $plugin .
* @param string $nonce_action_name .
*/
public function __construct( $plugin, $nonce_action_name, PickupPointsManager $pickup_points_manager ) {
$this->plugin = $plugin;
$this->nonce_action_name = $nonce_action_name;
$this->pickup_points_manager = $pickup_points_manager;
}
/**
* Hooks.
*/
public function hooks() {
add_action(
'wp_ajax_' . self::AJAX_ACTION,
[
$this,
'wp_ajax_paczkomaty_search_machines_via_ajax',
]
);
add_action(
'wp_ajax_nopriv_' . self::AJAX_ACTION,
[
$this,
'wp_ajax_paczkomaty_search_machines_via_ajax',
]
);
}
/**
* Execute AJAX request
*
* @return void
*/
public function wp_ajax_paczkomaty_search_machines_via_ajax(): void {
check_ajax_referer( $this->nonce_action_name, 'security' );
$all_machines = [];
$this->requests = new WPDesk_Paczkomaty_Ajax_Request( $_POST ); // phpcs:ignore
$cod = $this->requests->get_cod() === 'TRUE';
$end_of_week_collection = $this->requests->get_end_of_week_collection() === 'TRUE';
$point_types = WPDesk_Paczkomaty_ShipX_Cache::PARCEL_LOCKER;
if ( $cod || $end_of_week_collection ) {
$point_types = WPDesk_Paczkomaty_ShipX_Cache::PARCEL_LOCKER_ONLY;
}
$this->machines = $this->pickup_points_manager->get_points( $point_types );
if ( $this->requests->get_code() ) {
$all_machines = $this->get_machines_by_code();
} elseif ( $this->requests->get_city() ) {
$all_machines['items'] = $this->get_machines_by_city();
}
wp_send_json( $all_machines );
}
/**
* Filter machines by string.
*
* @param string $search_string .
*/
private function filter_machines_by_string( $search_string ) {
$map_function = 'trim';
if ( function_exists( 'mb_strtolower' ) ) {
$search_string = mb_strtolower( $search_string );
$map_function = 'mb_strtolower';
}
$machines = [];
$results = preg_grep(
'/' . preg_quote( $search_string, '/' ) . '/i',
array_map( $map_function, $this->machines )
);
foreach ( $results as $key => $item ) {
$machines[ $key ] = $this->machines[ $key ];
}
$this->machines = $machines;
}
/**
* Return a list of parcels for city request
*
* @return array
*/
public function get_machines_by_city() {
if ( strlen( $this->requests->get_city() ) >= self::MIN_LENGTH ) {
foreach ( preg_split( '/[\s,.]+/', $this->requests->get_city() ) as $search_string ) {
$this->filter_machines_by_string( trim( $search_string, '.,;\'"' ) );
}
}
$items = [];
foreach ( $this->machines as $key => $item ) {
$items[] = [
'id' => $key,
'text' => $item,
];
}
return $items;
}
/**
* Return a list of machines for code request
*
* @return array
*/
public function get_machines_by_code() {
if ( isset( $this->machines[ $this->requests->getCode() ] ) ) {
return [
'id' => $this->requests->getCode(),
'text' => $this->machines[ $this->requests->getCode() ],
];
}
return [];
}
}