| 1 |
<?php |
| 2 |
/** |
| 3 |
* Order controller |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Controllers |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Controllers\Hooks\Order; |
| 10 |
|
| 11 |
use SeQura\Core\Infrastructure\Logger\LogContextData; |
| 12 |
use SeQura\WC\Controllers\Controller; |
| 13 |
use SeQura\WC\Services\Interface_Logger_Service; |
| 14 |
use SeQura\WC\Services\Order\Interface_Order_Service; |
| 15 |
use Throwable; |
| 16 |
use WC_Order; |
| 17 |
|
| 18 |
/** |
| 19 |
* Handle hooks related to order management |
| 20 |
*/ |
| 21 |
class Order_Controller extends Controller implements Interface_Order_Controller { |
| 22 |
|
| 23 |
/** |
| 24 |
* Order service |
| 25 |
* |
| 26 |
* @var Interface_Order_Service |
| 27 |
*/ |
| 28 |
private $order_service; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
*/ |
| 33 |
public function __construct( |
| 34 |
Interface_Logger_Service $logger, |
| 35 |
string $templates_path, |
| 36 |
Interface_Order_Service $order_service |
| 37 |
) { |
| 38 |
parent::__construct( $logger, $templates_path ); |
| 39 |
$this->order_service = $order_service; |
| 40 |
|
| 41 |
add_action( 'admin_notices', array( $this, 'display_notices' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add support to custom meta query vars for the order query |
| 46 |
* |
| 47 |
* @param array $wp_query_args Args for WP_Query. |
| 48 |
* @param array $query_vars Query vars from WC_Order_Query. |
| 49 |
* @param WC_Order_Data_Store_CPT $order_data_store WC_Order_Data_Store instance. |
| 50 |
* @return array modified $query |
| 51 |
*/ |
| 52 |
public function handle_custom_query_vars( array $wp_query_args, array $query_vars, $order_data_store ): array { |
| 53 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 54 |
$custom_query_vars = array( |
| 55 |
$this->order_service->get_sent_to_sequra_meta_key(), |
| 56 |
); |
| 57 |
foreach ( $query_vars as $key => $value ) { |
| 58 |
if ( isset( $value['compare'] ) && in_array( $key, $custom_query_vars, true ) ) { |
| 59 |
$arg = array( |
| 60 |
'key' => $key, |
| 61 |
'compare' => $value['compare'], |
| 62 |
); |
| 63 |
if ( isset( $value['value'] ) ) { |
| 64 |
$arg['value'] = $value['value']; |
| 65 |
} |
| 66 |
$wp_query_args['meta_query'][] = $arg; |
| 67 |
} |
| 68 |
} |
| 69 |
return $wp_query_args; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Trigger the sync of the order status with SeQura |
| 74 |
*/ |
| 75 |
public function handle_order_status_changed( int $order_id, string $old_status, string $new_status, WC_Order $order ): void { |
| 76 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 77 |
try { |
| 78 |
$this->order_service->update_sequra_order_status( $order, $old_status, $new_status ); |
| 79 |
} catch ( Throwable $e ) { |
| 80 |
$this->logger->log_throwable( |
| 81 |
$e, |
| 82 |
__FUNCTION__, |
| 83 |
__CLASS__, |
| 84 |
array( |
| 85 |
new LogContextData( 'order_id', $order_id ), |
| 86 |
new LogContextData( 'old_status', $old_status ), |
| 87 |
new LogContextData( 'new_status', $new_status ), |
| 88 |
) |
| 89 |
); |
| 90 |
|
| 91 |
set_transient( |
| 92 |
$this->get_notices_transient( $order_id ), |
| 93 |
array( |
| 94 |
array( |
| 95 |
'notice' => __( 'An error occurred while updating the order data in seQura.', 'sequra' ), // TODO: improve message with link to simba. |
| 96 |
'type' => 'error', |
| 97 |
'dismissible' => true, |
| 98 |
), |
| 99 |
), |
| 100 |
0 |
| 101 |
); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the transient key for notices related to an order |
| 107 |
*/ |
| 108 |
private function get_notices_transient( int $order_id ): string { |
| 109 |
return 'sequra_notices_order_' . $order_id; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Display notices related to an order |
| 114 |
*/ |
| 115 |
public function display_notices(): void { |
| 116 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 117 |
if ( ! is_admin() || ! isset( $_GET['post'], $_GET['action'] ) || 'edit' !== $_GET['action'] ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
$order = wc_get_order( absint( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 122 |
if ( ! $order instanceof WC_Order ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
$transient_key = $this->get_notices_transient( $order->get_id() ); |
| 127 |
$notices = (array) get_transient( $transient_key ); |
| 128 |
if ( ! empty( $notices ) ) { |
| 129 |
delete_transient( $transient_key ); |
| 130 |
} |
| 131 |
foreach ( $notices as $notice ) { |
| 132 |
ob_start(); |
| 133 |
wc_get_template( 'admin/notice.php', $notice, '', $this->templates_path ); |
| 134 |
echo ob_get_clean(); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Show a link to the seQura back office in the order details page |
| 140 |
*/ |
| 141 |
public function show_link_to_sequra_back_office( WC_Order $order ): void { |
| 142 |
$args = array( |
| 143 |
'sequra_link' => $this->order_service->get_link_to_sequra_back_office( $order ), |
| 144 |
); |
| 145 |
wc_get_template( 'admin/order_details.php', $args, '', $this->templates_path ); |
| 146 |
} |
| 147 |
} |
| 148 |
|