← All changes
|
src/Controllers/Hooks/Order/class-order-controller.php
+56
-15
3.0.2
→
4.1.0
View file →
| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | namespace SeQura\WC\Controllers\Hooks\Order; |
| 10 | 10 | |
| 11 | 11 | use SeQura\Core\Infrastructure\Logger\LogContextData; |
| 12 | 12 | use SeQura\WC\Controllers\Controller; |
| 13 | -use SeQura\WC\Services\Interface_Logger_Service; | |
| 13 | +use SeQura\WC\Services\Log\Interface_Logger_Service; | |
| 14 | 14 | use SeQura\WC\Services\Order\Interface_Order_Service; |
| 15 | 15 | use Throwable; |
| 16 | 16 | use WC_Order; |
| 17 | 17 | |
| @@ -32,14 +32,14 @@ | ||
| 32 | 32 | */ |
| 33 | 33 | public function __construct( |
| 34 | 34 | Interface_Logger_Service $logger, |
| 35 | 35 | string $templates_path, |
| 36 | - Interface_Order_Service $order_service | |
| 36 | + Interface_Order_Service $order_service | |
| 37 | 37 | ) { |
| 38 | 38 | parent::__construct( $logger, $templates_path ); |
| 39 | 39 | $this->order_service = $order_service; |
| 40 | 40 | |
| 41 | - add_action( 'admin_notices', array( $this, 'display_notices' ) ); | |
| 41 | + \add_action( 'admin_notices', array( $this, 'display_notices' ) ); | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * Add support to custom meta query vars for the order query |
| @@ -48,9 +48,9 @@ | ||
| 48 | 48 | * @param array $query_vars Query vars from WC_Order_Query. |
| 49 | 49 | * @param WC_Order_Data_Store_CPT $order_data_store WC_Order_Data_Store instance. |
| 50 | 50 | * @return array modified $query |
| 51 | 51 | */ |
| 52 | - public function handle_custom_query_vars( array $wp_query_args, array $query_vars, $order_data_store ): array { | |
| 52 | + public function handle_custom_query_vars( $wp_query_args, $query_vars, $order_data_store ) { | |
| 53 | 53 | $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 54 | 54 | $custom_query_vars = array( |
| 55 | 55 | $this->order_service->get_sent_to_sequra_meta_key(), |
| 56 | 56 | ); |
| @@ -70,10 +70,17 @@ | ||
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 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 | |
| 74 | 81 | */ |
| 75 | - public function handle_order_status_changed( int $order_id, string $old_status, string $new_status, WC_Order $order ): void { | |
| 82 | + public function handle_order_status_changed( $order_id, $old_status, $new_status, $order ) { | |
| 76 | 83 | $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 77 | 84 | try { |
| 78 | 85 | $this->order_service->update_sequra_order_status( $order, $old_status, $new_status ); |
| 79 | 86 | } catch ( Throwable $e ) { |
| @@ -87,9 +94,9 @@ | ||
| 87 | 94 | new LogContextData( 'new_status', $new_status ), |
| 88 | 95 | ) |
| 89 | 96 | ); |
| 90 | 97 | |
| 91 | - set_transient( | |
| 98 | + \set_transient( | |
| 92 | 99 | $this->get_notices_transient( $order_id ), |
| 93 | 100 | array( |
| 94 | 101 | array( |
| 95 | 102 | 'notice' => __( 'An error occurred while updating the order data in seQura.', 'sequra' ), // TODO: improve message with link to simba. |
| @@ -103,35 +110,39 @@ | ||
| 103 | 110 | } |
| 104 | 111 | |
| 105 | 112 | /** |
| 106 | 113 | * Get the transient key for notices related to an order |
| 114 | + * | |
| 115 | + * @param int $order_id Order ID. | |
| 107 | 116 | */ |
| 108 | - private function get_notices_transient( int $order_id ): string { | |
| 117 | + private function get_notices_transient( $order_id ): string { | |
| 109 | 118 | return 'sequra_notices_order_' . $order_id; |
| 110 | 119 | } |
| 111 | 120 | |
| 112 | 121 | /** |
| 113 | 122 | * Display notices related to an order |
| 123 | + * | |
| 124 | + * @return void | |
| 114 | 125 | */ |
| 115 | - public function display_notices(): void { | |
| 126 | + public function display_notices() { | |
| 116 | 127 | // phpcs:ignore WordPress.Security.NonceVerification |
| 117 | - if ( ! is_admin() || ! isset( $_GET['post'], $_GET['action'] ) || 'edit' !== $_GET['action'] ) { | |
| 128 | + if ( ! \is_admin() || ! isset( $_GET['post'], $_GET['action'] ) || 'edit' !== $_GET['action'] ) { | |
| 118 | 129 | return; |
| 119 | 130 | } |
| 120 | 131 | |
| 121 | - $order = wc_get_order( absint( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 132 | + $order = wc_get_order( \absint( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 122 | 133 | if ( ! $order instanceof WC_Order ) { |
| 123 | 134 | return; |
| 124 | 135 | } |
| 125 | 136 | |
| 126 | 137 | $transient_key = $this->get_notices_transient( $order->get_id() ); |
| 127 | - $notices = (array) get_transient( $transient_key ); | |
| 138 | + $notices = (array) \get_transient( $transient_key ); | |
| 128 | 139 | if ( ! empty( $notices ) ) { |
| 129 | - delete_transient( $transient_key ); | |
| 140 | + \delete_transient( $transient_key ); | |
| 130 | 141 | } |
| 131 | 142 | foreach ( $notices as $notice ) { |
| 132 | 143 | ob_start(); |
| 133 | - wc_get_template( 'admin/notice.php', $notice, '', $this->templates_path ); | |
| 144 | + \wc_get_template( 'admin/notice.php', $notice, '', $this->templates_path ); | |
| 134 | 145 | echo ob_get_clean(); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 135 | 146 | } |
| 136 | 147 | } |
| 137 | 148 | |
| @@ -136,12 +147,42 @@ | ||
| 136 | 147 | } |
| 137 | 148 | |
| 138 | 149 | /** |
| 139 | 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 | |
| 140 | 154 | */ |
| 141 | - public function show_link_to_sequra_back_office( WC_Order $order ): void { | |
| 155 | + public function show_link_to_sequra_back_office( $order ): void { | |
| 142 | 156 | $args = array( |
| 143 | 157 | 'sequra_link' => $this->order_service->get_link_to_sequra_back_office( $order ), |
| 144 | 158 | ); |
| 145 | - wc_get_template( 'admin/order_details.php', $args, '', $this->templates_path ); | |
| 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(); | |
| 146 | 187 | } |
| 147 | 188 | } |