Document Carriers map

This commit is contained in:
Narek Zakarian 2022-08-11 14:54:38 +04:00
parent 7bd2f80c6a
commit 3e47e62f78

View file

@ -10,90 +10,114 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\OrderTracking; namespace WooCommerce\PayPalCommerce\OrderTracking;
use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint; use WooCommerce\PayPalCommerce\OrderTracking\Endpoint\OrderTrackingEndpoint;
use WP_Post;
/** /**
* Class MetaBoxRenderer * Class MetaBoxRenderer
*
* @psalm-type CarrierType = string
* @psalm-type CarrierItemCode = string
* @psalm-type CarrierItemName = string
* @psalm-type Carrier = array{name: string, items: array<CarrierItemCode, CarrierItemName>}
* @psalm-type Carriers = array<CarrierType, Carrier>
*/ */
class MetaBoxRenderer { class MetaBoxRenderer {
public const NAME_PREFIX = 'ppcp-tracking'; public const NAME_PREFIX = 'ppcp-tracking';
/** /**
* The OrderTrackingEndpoint.
*
* @var OrderTrackingEndpoint * @var OrderTrackingEndpoint
*/ */
protected $orderTrackingEndpoint; protected $order_tracking_endpoint;
/** /**
* Allowed shipping statuses.
*
* @var string[] * @var string[]
*/ */
protected $allowedStatuses; protected $allowed_statuses;
/** /**
* Available shipping carriers.
*
* @var array * @var array
* @psalm-var Carriers
*/ */
protected $carriers; protected $carriers;
/**
* MetaBoxRenderer constructor.
*
* @param OrderTrackingEndpoint $order_tracking_endpoint The OrderTrackingEndpoint.
* @param string[] $allowed_statuses Allowed shipping statuses.
* @param array $carriers Available shipping carriers.
* @psalm-param Carriers $carriers
*/
public function __construct( public function __construct(
OrderTrackingEndpoint $orderTrackingEndpoint, OrderTrackingEndpoint $order_tracking_endpoint,
array $allowedStatuses, array $allowed_statuses,
array $carriers array $carriers
) { ) {
$this->orderTrackingEndpoint = $orderTrackingEndpoint; $this->order_tracking_endpoint = $order_tracking_endpoint;
$this->allowedStatuses = $allowedStatuses; $this->allowed_statuses = $allowed_statuses;
$this->carriers = $carriers; $this->carriers = $carriers;
} }
/** /**
* Renders the order tracking MetaBox. * Renders the order tracking MetaBox.
* *
* @param WP_Post $post The post object.
*/ */
public function render( \WP_Post $post) { public function render( WP_Post $post ) {
$wc_order = wc_get_order( $post->ID ); $wc_order = wc_get_order( $post->ID );
$tracking_info = $this->orderTrackingEndpoint->get_tracking_information($post->ID); $tracking_info = $this->order_tracking_endpoint->get_tracking_information( $post->ID );
$tracking_is_not_added = empty( $tracking_info ); $tracking_is_not_added = empty( $tracking_info );
$transaction_id = $tracking_info['transaction_id'] ?? $wc_order->get_transaction_id() ?? ''; $transaction_id = $tracking_info['transaction_id'] ?? $wc_order->get_transaction_id() ?? '';
$tracking_number = $tracking_info['tracking_number'] ?? ''; $tracking_number = $tracking_info['tracking_number'] ?? '';
$statusValue = $tracking_info['status'] ?? 'SHIPPED'; $status_value = $tracking_info['status'] ?? 'SHIPPED';
$carrierValue = $tracking_info['carrier'] ?? ''; $carrier_value = $tracking_info['carrier'] ?? '';
$action = $tracking_is_not_added ? 'create' : 'update'; $action = $tracking_is_not_added ? 'create' : 'update';
?> ?>
<p> <p>
<label for="<?= esc_attr(self::NAME_PREFIX);?>-transaction_id"><?= __('Transaction ID','woocommerce-paypal-payments');?></label> <label for="<?php echo esc_attr( self::NAME_PREFIX ); ?>-transaction_id"><?php echo esc_html__( 'Transaction ID', 'woocommerce-paypal-payments' ); ?></label>
<input type="text" disabled class="<?= esc_attr(self::NAME_PREFIX);?>-transaction_id" id="<?= esc_attr(self::NAME_PREFIX);?>-transaction_id" name="<?= esc_attr(self::NAME_PREFIX);?>[transaction_id]" value="<?= esc_html($transaction_id);?>"/></p> <input type="text" disabled class="<?php echo esc_attr( self::NAME_PREFIX ); ?>-transaction_id" id="<?php echo esc_attr( self::NAME_PREFIX ); ?>-transaction_id" name="<?php echo esc_attr( self::NAME_PREFIX ); ?>[transaction_id]" value="<?php echo esc_html( $transaction_id ); ?>"/></p>
<p> <p>
<label for="<?= esc_attr(self::NAME_PREFIX);?>-tracking_number"><?= __('Tracking Number','woocommerce-paypal-payments');?></label> <label for="<?php echo esc_attr( self::NAME_PREFIX ); ?>-tracking_number"><?php echo esc_html__( 'Tracking Number', 'woocommerce-paypal-payments' ); ?></label>
<input type="text" class="<?= esc_attr(self::NAME_PREFIX);?>-tracking_number" id="<?= esc_attr(self::NAME_PREFIX);?>-tracking_number" name="<?= esc_attr(self::NAME_PREFIX);?>[tracking_number]" value="<?= esc_html($tracking_number);?>"/></p> <input type="text" class="<?php echo esc_attr( self::NAME_PREFIX ); ?>-tracking_number" id="<?php echo esc_attr( self::NAME_PREFIX ); ?>-tracking_number" name="<?php echo esc_attr( self::NAME_PREFIX ); ?>[tracking_number]" value="<?php echo esc_html( $tracking_number ); ?>"/></p>
<p> <p>
<label for="<?= esc_attr(self::NAME_PREFIX);?>-status"><?= __('Status','woocommerce-paypal-payments');?></label> <label for="<?php echo esc_attr( self::NAME_PREFIX ); ?>-status"><?php echo esc_html__( 'Status', 'woocommerce-paypal-payments' ); ?></label>
<select class="<?= esc_attr(self::NAME_PREFIX);?>-status" id="<?= esc_attr(self::NAME_PREFIX);?>-status" name="<?= esc_attr(self::NAME_PREFIX);?>[status]"> <select class="<?php echo esc_attr( self::NAME_PREFIX ); ?>-status" id="<?php echo esc_attr( self::NAME_PREFIX ); ?>-status" name="<?php echo esc_attr( self::NAME_PREFIX ); ?>[status]">
<?php foreach($this->allowedStatuses as $status):?> <?php foreach ( $this->allowed_statuses as $status ) : ?>
<option value="<?= esc_attr($status);?>" <?php selected( $statusValue, $status ); ?>><?= esc_html($status);?></option> <option value="<?php echo esc_attr( $status ); ?>" <?php selected( $status_value, $status ); ?>><?php echo esc_html( $status ); ?></option>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
</p> </p>
<p> <p>
<label for="ppcp-tracking-carrier"><?= __('Carrier','woocommerce-paypal-payments');?></label> <label for="ppcp-tracking-carrier"><?php echo esc_html__( 'Carrier', 'woocommerce-paypal-payments' ); ?></label>
<select class="ppcp-tracking-carrier" id="ppcp-tracking-carrier" name="ppcp-tracking[carrier]"> <select class="ppcp-tracking-carrier" id="ppcp-tracking-carrier" name="ppcp-tracking[carrier]">
<?php foreach($this->carriers as $carrier): <?php
foreach ( $this->carriers as $carrier ) :
$country = $carrier['name'] ?? ''; $country = $carrier['name'] ?? '';
$carriers = $carrier['items'] ?? ''; $carriers = $carrier['items'] ?? '';
?> ?>
<option value=""><?= __('Select Carrier','woocommerce-paypal-payments');?></option> <option value=""><?php echo esc_html__( 'Select Carrier', 'woocommerce-paypal-payments' ); ?></option>
<optgroup label="<?= esc_attr($country);?>"> <optgroup label="<?php echo esc_attr( $country ); ?>">
<?php foreach ( $carriers as $carrier_code => $carrier_name ) : ?> <?php foreach ( $carriers as $carrier_code => $carrier_name ) : ?>
<option value="<?= esc_attr($carrier_code);?>" <?php selected( $carrierValue, $carrier_code ); ?>><?= esc_html($carrier_name);?></option> <option value="<?php echo esc_attr( $carrier_code ); ?>" <?php selected( $carrier_value, $carrier_code ); ?>><?php echo esc_html( $carrier_name ); ?></option>
<?php endforeach; ?> <?php endforeach; ?>
</optgroup> </optgroup>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
</p> </p>
<input type="hidden" class="ppcp-order_id" name="<?= esc_attr(self::NAME_PREFIX);?>[order_id]" value="<?= esc_html($post->ID);?>"/> <input type="hidden" class="ppcp-order_id" name="<?php echo esc_attr( self::NAME_PREFIX ); ?>[order_id]" value="<?php echo esc_html( $post->ID ); ?>"/>
<p> <p>
<button type="button" class="button submit_tracking_info" data-action="<?= esc_attr($action);?>"><?= esc_html(ucfirst($action));?></button></p> <button type="button" class="button submit_tracking_info" data-action="<?php echo esc_attr( $action ); ?>"><?php echo esc_html( ucfirst( $action ) ); ?></button></p>
<?php <?php
} }
} }