| @@ -14,9 +14,9 @@ | ||
| 14 | 14 | use SeQura\WC\Controllers\Hooks\Payment\Interface_Payment_Controller; |
| 15 | 15 | use SeQura\WC\Controllers\Hooks\Process\Interface_Async_Process_Controller; |
| 16 | 16 | use SeQura\WC\Controllers\Hooks\Settings\Interface_Settings_Controller; |
| 17 | 17 | use SeQura\WC\Controllers\Rest\REST_Controller; |
| 18 | -use SeQura\WC\Services\Interface_Constants; | |
| 18 | +use SeQura\WC\Services\Constants\Interface_Constants; | |
| 19 | 19 | use SeQura\WC\Services\Migration\Interface_Migration_Manager; |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * The core plugin class. |
| @@ -65,8 +65,9 @@ | ||
| 65 | 65 | */ |
| 66 | 66 | private $migration_manager; |
| 67 | 67 | |
| 68 | 68 | private const HOOK_DELIVERY_REPORT = 'sequra_delivery_report'; |
| 69 | + private const HOOK_CLEANUP_ORDERS = 'sequra_cleanup_orders'; | |
| 69 | 70 | |
| 70 | 71 | /** |
| 71 | 72 | * Construct the plugin and bind hooks with controllers. |
| 72 | 73 | */ |
| @@ -138,8 +139,10 @@ | ||
| 138 | 139 | |
| 139 | 140 | // Order Update. |
| 140 | 141 | \add_action( 'woocommerce_order_status_changed', array( $order_controller, 'handle_order_status_changed' ), 10, 4 ); |
| 141 | 142 | \add_action( 'woocommerce_admin_order_data_after_order_details', array( $order_controller, 'show_link_to_sequra_back_office' ) ); |
| 143 | + \add_action( self::HOOK_CLEANUP_ORDERS, array( $order_controller, 'cleanup_orders' ) ); | |
| 144 | + \add_action( $constants->get_hook_add_order_indexes(), array( $order_controller, 'migrate_orders_to_use_indexes' ) ); | |
| 142 | 145 | |
| 143 | 146 | // WooCommerce Compat. |
| 144 | 147 | \add_action( 'before_woocommerce_init', array( $this, 'declare_woocommerce_compatibility' ) ); |
| 145 | 148 | } |
| @@ -168,19 +171,31 @@ | ||
| 168 | 171 | * Handle deactivation of the plugin. |
| 169 | 172 | */ |
| 170 | 173 | public function deactivate(): void { |
| 171 | 174 | \wp_clear_scheduled_hook( self::HOOK_DELIVERY_REPORT ); |
| 175 | + \wp_clear_scheduled_hook( self::HOOK_CLEANUP_ORDERS ); | |
| 172 | 176 | } |
| 173 | 177 | |
| 174 | 178 | /** |
| 179 | + * Get a random time between 2AM and 8AM. | |
| 180 | + * | |
| 181 | + * @return int | |
| 182 | + */ | |
| 183 | + private function get_random_midnight_time() { | |
| 184 | + $random_offset = \wp_rand( 0, 25200 ); // 60*60*7 seconds from 2AM to 8AM. | |
| 185 | + $tomorrow = gmdate( 'Y-m-d 02:00', strtotime( 'tomorrow' ) ); | |
| 186 | + return $random_offset + strtotime( $tomorrow ); | |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 175 | 190 | * Execute the installation process if needed. |
| 176 | 191 | */ |
| 177 | 192 | public function install(): void { |
| 178 | 193 | if ( ! \wp_next_scheduled( self::HOOK_DELIVERY_REPORT ) ) { |
| 179 | - $random_offset = \wp_rand( 0, 25200 ); // 60*60*7 seconds from 2AM to 8AM. | |
| 180 | - $tomorrow = gmdate( 'Y-m-d 02:00', strtotime( 'tomorrow' ) ); | |
| 181 | - $time = $random_offset + strtotime( $tomorrow ); | |
| 182 | - \wp_schedule_event( $time, 'daily', self::HOOK_DELIVERY_REPORT ); | |
| 194 | + \wp_schedule_event( $this->get_random_midnight_time(), 'daily', self::HOOK_DELIVERY_REPORT ); | |
| 195 | + } | |
| 196 | + if ( ! \wp_next_scheduled( self::HOOK_CLEANUP_ORDERS ) ) { | |
| 197 | + \wp_schedule_event( $this->get_random_midnight_time(), 'daily', self::HOOK_CLEANUP_ORDERS ); | |
| 183 | 198 | } |
| 184 | 199 | $this->migration_manager->run_install_migrations(); |
| 185 | 200 | } |
| 186 | 201 | |