PluginProbe
seQura / 4.3.2
seQura v4.3.2
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.3.2, at src/Controllers/Hooks/Order/class-order-controller.php

208 lines 5.6 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\Cart\Interface_Cart_Service;
14 use SeQura\WC\Services\Log\Interface_Logger_Service;
15 use SeQura\WC\Services\Order\Interface_Order_Service;
16 use Throwable;
17 use WC_Order;
18
19 /**
20 * Handle hooks related to order management
21 */
22 class Order_Controller extends Controller implements Interface_Order_Controller {
23
24 /**
25 * Order service
26 *
27 * @var Interface_Order_Service
28 */
29 private $order_service;
30
31 /**
32 * Cart service
33 *
34 * @var Interface_Cart_Service
35 */
36 private $cart_service;
37
38 /**
39 * Constructor
40 */
41 public function __construct(
42 Interface_Logger_Service $logger,
43 string $templates_path,
44 Interface_Order_Service $order_service,
45 Interface_Cart_Service $cart_service
46 ) {
47 parent::__construct( $logger, $templates_path );
48 $this->order_service = $order_service;
49 $this->cart_service = $cart_service;
50
51 \add_action( 'admin_notices', array( $this, 'display_notices' ) );
52 }
53
54 /**
55 * Add support to custom meta query vars for the order query
56 *
57 * @param array $wp_query_args Args for WP_Query.
58 * @param array $query_vars Query vars from WC_Order_Query.
59 * @param WC_Order_Data_Store_CPT $order_data_store WC_Order_Data_Store instance.
60 * @return array modified $query
61 */
62 public function handle_custom_query_vars( $wp_query_args, $query_vars, $order_data_store ) {
63 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
64 $custom_query_vars = array(
65 $this->order_service->get_sent_to_sequra_meta_key(),
66 );
67 foreach ( $query_vars as $key => $value ) {
68 if ( isset( $value['compare'] ) && in_array( $key, $custom_query_vars, true ) ) {
69 $arg = array(
70 'key' => $key,
71 'compare' => $value['compare'],
72 );
73 if ( isset( $value['value'] ) ) {
74 $arg['value'] = $value['value'];
75 }
76 $wp_query_args['meta_query'][] = $arg;
77 }
78 }
79 return $wp_query_args;
80 }
81
82 /**
83 * Trigger the sync of the order status with SeQura
84 *
85 * @param int $order_id Order ID.
86 * @param string $old_status Old status.
87 * @param string $new_status New status.
88 * @param WC_Order $order Order object.
89 *
90 * @return void
91 */
92 public function handle_order_status_changed( $order_id, $old_status, $new_status, $order ) {
93 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
94 try {
95 $this->order_service->update_sequra_order_status( $order, $old_status, $new_status );
96 } catch ( Throwable $e ) {
97 $this->logger->log_throwable(
98 $e,
99 __FUNCTION__,
100 __CLASS__,
101 array(
102 new LogContextData( 'order_id', $order_id ),
103 new LogContextData( 'old_status', $old_status ),
104 new LogContextData( 'new_status', $new_status ),
105 )
106 );
107
108 \set_transient(
109 $this->get_notices_transient( $order_id ),
110 array(
111 array(
112 'notice' => __( 'An error occurred while updating the order data in seQura.', 'sequra' ), // TODO: improve message with link to simba.
113 'type' => 'error',
114 'dismissible' => true,
115 ),
116 ),
117 0
118 );
119 }
120 }
121
122 /**
123 * Get the transient key for notices related to an order
124 *
125 * @param int $order_id Order ID.
126 */
127 private function get_notices_transient( $order_id ): string {
128 return 'sequra_notices_order_' . $order_id;
129 }
130
131 /**
132 * Display notices related to an order
133 *
134 * @return void
135 */
136 public function display_notices() {
137 // phpcs:ignore WordPress.Security.NonceVerification
138 if ( ! \is_admin() || ! isset( $_GET['post'], $_GET['action'] ) || 'edit' !== $_GET['action'] ) {
139 return;
140 }
141
142 $order = wc_get_order( \absint( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
143 if ( ! $order instanceof WC_Order ) {
144 return;
145 }
146
147 $transient_key = $this->get_notices_transient( $order->get_id() );
148 $notices = (array) \get_transient( $transient_key );
149 if ( ! empty( $notices ) ) {
150 \delete_transient( $transient_key );
151 }
152 foreach ( $notices as $notice ) {
153 ob_start();
154 \wc_get_template( 'admin/notice.php', $notice, '', $this->templates_path );
155 echo ob_get_clean(); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
156 }
157 }
158
159 /**
160 * Show a link to the seQura back office in the order details page
161 *
162 * @param WC_Order $order Order object.
163 * @return void
164 */
165 public function show_link_to_sequra_back_office( $order ): void {
166 $args = array(
167 'sequra_link' => $this->order_service->get_link_to_sequra_back_office( $order ),
168 );
169 \wc_get_template( 'admin/order_details.php', $args, '', $this->templates_path );
170 }
171
172 /**
173 * Cleanup orders
174 *
175 * @return void
176 */
177 public function cleanup_orders() {
178 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
179 $this->order_service->cleanup_orders();
180 }
181
182 /**
183 * Respond to the sequra_order_migration_add_indexes hook
184 *
185 * @param string $hook Hook name that triggered the action.
186 */
187 public function migrate_orders_to_use_indexes( $hook ) {
188 $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
189
190 if ( $this->order_service->is_migration_complete() ) {
191 $this->logger->log_debug( 'Indexing process is done. Job will be unscheduled', __FUNCTION__, __CLASS__ );
192 \wp_clear_scheduled_hook( $hook, array( $hook ) );
193 return;
194 }
195
196 $this->order_service->migrate_data();
197 }
198
199 /**
200 * Create the Cart Info object for the current session if it doesn't exist.
201 *
202 * @return void
203 */
204 public function ensure_cart_info_exists() {
205 $this->cart_service->get_cart_info_from_session();
206 }
207 }
208