PluginProbe
seQura / 4.1.0
seQura v4.1.0
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Controllers / Hooks / Order / class-order-controller.php

class-order-controller.php in seQura 4.1.0, at src/Controllers/Hooks/Order/class-order-controller.php

189 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Log\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( $wp_query_args, $query_vars, $order_data_store ) {
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 * @param int $order_id Order ID.
76 * @param string $old_status Old status.
77 * @param string $new_status New status.
78 * @param WC_Order $order Order object.
79 *
80 * @return void
81 */
82 public function handle_order_status_changed( $order_id, $old_status, $new_status, $order ) {
83 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
84 try {
85 $this->order_service->update_sequra_order_status( $order, $old_status, $new_status );
86 } catch ( Throwable $e ) {
87 $this->logger->log_throwable(
88 $e,
89 __FUNCTION__,
90 __CLASS__,
91 array(
92 new LogContextData( 'order_id', $order_id ),
93 new LogContextData( 'old_status', $old_status ),
94 new LogContextData( 'new_status', $new_status ),
95 )
96 );
97
98 \set_transient(
99 $this->get_notices_transient( $order_id ),
100 array(
101 array(
102 'notice' => __( 'An error occurred while updating the order data in seQura.', 'sequra' ), // TODO: improve message with link to simba.
103 'type' => 'error',
104 'dismissible' => true,
105 ),
106 ),
107 0
108 );
109 }
110 }
111
112 /**
113 * Get the transient key for notices related to an order
114 *
115 * @param int $order_id Order ID.
116 */
117 private function get_notices_transient( $order_id ): string {
118 return 'sequra_notices_order_' . $order_id;
119 }
120
121 /**
122 * Display notices related to an order
123 *
124 * @return void
125 */
126 public function display_notices() {
127 // phpcs:ignore WordPress.Security.NonceVerification
128 if ( ! \is_admin() || ! isset( $_GET['post'], $_GET['action'] ) || 'edit' !== $_GET['action'] ) {
129 return;
130 }
131
132 $order = wc_get_order( \absint( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
133 if ( ! $order instanceof WC_Order ) {
134 return;
135 }
136
137 $transient_key = $this->get_notices_transient( $order->get_id() );
138 $notices = (array) \get_transient( $transient_key );
139 if ( ! empty( $notices ) ) {
140 \delete_transient( $transient_key );
141 }
142 foreach ( $notices as $notice ) {
143 ob_start();
144 \wc_get_template( 'admin/notice.php', $notice, '', $this->templates_path );
145 echo ob_get_clean(); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
146 }
147 }
148
149 /**
150 * Show a link to the seQura back office in the order details page
151 *
152 * @param WC_Order $order Order object.
153 * @return void
154 */
155 public function show_link_to_sequra_back_office( $order ): void {
156 $args = array(
157 'sequra_link' => $this->order_service->get_link_to_sequra_back_office( $order ),
158 );
159 \wc_get_template( 'admin/order_details.php', $args, '', $this->templates_path );
160 }
161
162 /**
163 * Cleanup orders
164 *
165 * @return void
166 */
167 public function cleanup_orders() {
168 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
169 $this->order_service->cleanup_orders();
170 }
171
172 /**
173 * Respond to the sequra_order_migration_add_indexes hook
174 *
175 * @param string $hook Hook name that triggered the action.
176 */
177 public function migrate_orders_to_use_indexes( $hook ) {
178 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
179
180 if ( $this->order_service->is_migration_complete() ) {
181 $this->logger->log_debug( 'Indexing process is done. Job will be unscheduled', __FUNCTION__, __CLASS__ );
182 \wp_clear_scheduled_hook( $hook, array( $hook ) );
183 return;
184 }
185
186 $this->order_service->migrate_data();
187 }
188 }
189