Fix PHPcs errors

This commit is contained in:
Narek Zakarian 2022-08-11 17:47:29 +04:00
parent cd75b08e9e
commit a4cb280fe3
2 changed files with 293 additions and 290 deletions

View file

@ -39,21 +39,21 @@ return array(
return new MetaBoxRenderer( return new MetaBoxRenderer(
$container->get( 'order-tracking.endpoint.controller' ), $container->get( 'order-tracking.endpoint.controller' ),
$container->get( 'order-tracking.allowed-shipping-statuses' ), $container->get( 'order-tracking.allowed-shipping-statuses' ),
$container->get( 'order-tracking.available-carriers' ), $container->get( 'order-tracking.available-carriers' )
); );
}, },
'order-tracking.allowed-shipping-statuses' => static function ( ContainerInterface $container ): array { 'order-tracking.allowed-shipping-statuses' => static function ( ContainerInterface $container ): array {
return array('SHIPPED', 'ON_HOLD', 'DELIVERED', 'CANCELLED'); return array( 'SHIPPED', 'ON_HOLD', 'DELIVERED', 'CANCELLED' );
}, },
'order-tracking.allowed-carriers' => static function ( ContainerInterface $container ): array { 'order-tracking.allowed-carriers' => static function ( ContainerInterface $container ): array {
return require __DIR__ . '/carriers.php'; return require __DIR__ . '/carriers.php';
}, },
'order-tracking.available-carriers' => static function ( ContainerInterface $container ): array { 'order-tracking.available-carriers' => static function ( ContainerInterface $container ): array {
$wc_default_country = get_option( 'woocommerce_default_country' ); $wc_default_country = get_option( 'woocommerce_default_country' );
$has_state = strpos($wc_default_country, ':'); $has_state = strpos( $wc_default_country, ':' );
$selected_address = $has_state ? substr($wc_default_country, 0, $has_state) : $wc_default_country; $selected_address = $has_state ? substr( $wc_default_country, 0, $has_state ) : $wc_default_country;
$allowed_carriers = $container->get('order-tracking.allowed-carriers'); $allowed_carriers = $container->get( 'order-tracking.allowed-carriers' );
$selected_country_carriers = $allowed_carriers[$selected_address] ?? []; $selected_country_carriers = $allowed_carriers[ $selected_address ] ?? array();
return array( return array(
$selected_country_carriers, $selected_country_carriers,
@ -62,18 +62,18 @@ return array(
'name' => 'Other', 'name' => 'Other',
'items' => array( 'items' => array(
'OTHER' => _x( 'Other', 'Name of carrier', 'woocommerce-paypal-payments' ), 'OTHER' => _x( 'Other', 'Name of carrier', 'woocommerce-paypal-payments' ),
) ),
), ),
); );
}, },
'order-tracking.is-paypal-order-edit-page' => static function ( ContainerInterface $container ): bool { 'order-tracking.is-paypal-order-edit-page' => static function ( ContainerInterface $container ): bool {
$orderId = isset( $_GET['post'] ) ? (int)$_GET['post'] : ''; $order_id = isset( $_GET['post'] ) ? (int) $_GET['post'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if (empty($orderId)) { if ( empty( $order_id ) ) {
return false; return false;
} }
$meta = get_post_meta($orderId, PayPalGateway::ORDER_ID_META_KEY, true); $meta = get_post_meta( $order_id, PayPalGateway::ORDER_ID_META_KEY, true );
return !empty($meta); return ! empty( $meta );
}, },
); );

View file

@ -106,17 +106,20 @@ class OrderTrackingEndpoint {
try { try {
$data = $this->request_data->read_request( $this->nonce() ); $data = $this->request_data->read_request( $this->nonce() );
$action = $data['action']; $action = $data['action'];
$request_body = $this->extract_tracking_information($data); $request_body = $this->extract_tracking_information( $data );
$order_id = (int)$data['order_id']; $order_id = (int) $data['order_id'];
$action === 'create' ? $this->add_tracking_information($request_body, $order_id) : $this->update_tracking_information($data, $order_id); $action === 'create' ? $this->add_tracking_information( $request_body, $order_id ) : $this->update_tracking_information( $data, $order_id );
$action_message = $action === 'create' ? 'created' : 'updated'; $action_message = $action === 'create' ? 'created' : 'updated';
$message = __("successfully {$action_message}",'woocommerce-paypal-payments'); $message = sprintf(
wp_send_json_success(array('message' => $message)); // translators: %1$s is the action message (created or updated).
return true; _x( 'successfully %1$s', 'tracking inof success message', 'woocommerce-paypal-payments' ),
esc_html( $action_message )
);
wp_send_json_success( array( 'message' => $message ) );
} catch ( Exception $error ) { } catch ( Exception $error ) {
wp_send_json_error( $error->getMessage(), 500 ); wp_send_json_error( $error->getMessage(), 500 );
return false;
} }
} }
@ -128,11 +131,11 @@ class OrderTrackingEndpoint {
* @param int $order_id The order ID. * @param int $order_id The order ID.
* @throws RuntimeException If problem creating. * @throws RuntimeException If problem creating.
*/ */
public function add_tracking_information(array $data, int $order_id) : void { public function add_tracking_information( array $data, int $order_id ) : void {
$url = trailingslashit( $this->host ) . 'v1/shipping/trackers-batch'; $url = trailingslashit( $this->host ) . 'v1/shipping/trackers-batch';
$body = array( $body = array(
'trackers' => array($data) 'trackers' => array( $data ),
); );
$args = array( $args = array(
@ -179,7 +182,7 @@ class OrderTrackingEndpoint {
throw $error; throw $error;
} }
update_post_meta($order_id, '_ppcp_paypal_tracking_number', $data['tracking_number']); update_post_meta( $order_id, '_ppcp_paypal_tracking_number', $data['tracking_number'] );
} }
/** /**
@ -189,11 +192,11 @@ class OrderTrackingEndpoint {
* @return array The tracking information. * @return array The tracking information.
* @psalm-return TrackingInfo * @psalm-return TrackingInfo
*/ */
public function get_tracking_information(int $wc_order_id) : array { public function get_tracking_information( int $wc_order_id ) : array {
$wc_order = wc_get_order( $wc_order_id ); $wc_order = wc_get_order( $wc_order_id );
$transaction_id = $wc_order->get_transaction_id(); $transaction_id = $wc_order->get_transaction_id();
$tracking_number = get_post_meta($wc_order_id, '_ppcp_paypal_tracking_number', true); $tracking_number = get_post_meta( $wc_order_id, '_ppcp_paypal_tracking_number', true );
$url = trailingslashit( $this->host ) . 'v1/shipping/trackers/'. $this->find_tracker_id($transaction_id, $tracking_number); $url = trailingslashit( $this->host ) . 'v1/shipping/trackers/' . $this->find_tracker_id( $transaction_id, $tracking_number );
$args = array( $args = array(
'method' => 'GET', 'method' => 'GET',
@ -221,7 +224,7 @@ class OrderTrackingEndpoint {
$status_code = (int) wp_remote_retrieve_response_code( $response ); $status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code ) { if ( 200 !== $status_code ) {
return []; return array();
} }
return array( return array(
@ -240,11 +243,11 @@ class OrderTrackingEndpoint {
* @param int $order_id The order ID. * @param int $order_id The order ID.
* @throws RuntimeException If problem updating. * @throws RuntimeException If problem updating.
*/ */
public function update_tracking_information(array $data, int $order_id) : void { public function update_tracking_information( array $data, int $order_id ) : void {
$tracking_info = $this->get_tracking_information($order_id); $tracking_info = $this->get_tracking_information( $order_id );
$transaction_id = $tracking_info['transaction_id'] ?? ''; $transaction_id = $tracking_info['transaction_id'] ?? '';
$tracking_number = $tracking_info['tracking_number'] ?? ''; $tracking_number = $tracking_info['tracking_number'] ?? '';
$url = trailingslashit( $this->host ) . 'v1/shipping/trackers/'. $this->find_tracker_id($transaction_id, $tracking_number); $url = trailingslashit( $this->host ) . 'v1/shipping/trackers/' . $this->find_tracker_id( $transaction_id, $tracking_number );
$args = array( $args = array(
'method' => 'PUT', 'method' => 'PUT',
@ -290,7 +293,7 @@ class OrderTrackingEndpoint {
throw $error; throw $error;
} }
update_post_meta($order_id, '_ppcp_paypal_tracking_number', $data['tracking_number']); update_post_meta( $order_id, '_ppcp_paypal_tracking_number', $data['tracking_number'] );
} }
/** /**
@ -310,7 +313,7 @@ class OrderTrackingEndpoint {
* @return array A map of tracking information keys to values. * @return array A map of tracking information keys to values.
* @psalm-return TrackingInfo * @psalm-return TrackingInfo
*/ */
protected function extract_tracking_information(array $data): array { protected function extract_tracking_information( array $data ): array {
return array( return array(
'transaction_id' => $data['transaction_id'] ?? '', 'transaction_id' => $data['transaction_id'] ?? '',
'tracking_number' => $data['tracking_number'] ?? '', 'tracking_number' => $data['tracking_number'] ?? '',
@ -338,7 +341,7 @@ class OrderTrackingEndpoint {
* @param string $tracking_number The tracking number. * @param string $tracking_number The tracking number.
* @return string The tracker ID. * @return string The tracker ID.
*/ */
protected function find_tracker_id(string $transaction_id, string $tracking_number): string { protected function find_tracker_id( string $transaction_id, string $tracking_number ): string {
return !empty($tracking_number) ? "{$transaction_id}-{$tracking_number}" : "{$transaction_id}-NOTRACKER"; return ! empty( $tracking_number ) ? "{$transaction_id}-{$tracking_number}" : "{$transaction_id}-NOTRACKER";
} }
} }