| @@ -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 | */ |
| @@ -80,8 +81,9 @@ | ||
| 80 | 81 | REST_Controller $rest_settings_controller, |
| 81 | 82 | REST_Controller $rest_onboarding_controller, |
| 82 | 83 | REST_Controller $rest_payment_controller, |
| 83 | 84 | REST_Controller $rest_log_controller, |
| 85 | + REST_Controller $rest_store_integration_controller, | |
| 84 | 86 | Interface_Product_Controller $product_controller, |
| 85 | 87 | Interface_Async_Process_Controller $async_process_controller, |
| 86 | 88 | Interface_Order_Controller $order_controller |
| 87 | 89 | ) { |
| @@ -91,9 +93,9 @@ | ||
| 91 | 93 | $this->file_path = $constants->get_plugin_file_path(); |
| 92 | 94 | $this->base_name = $constants->get_plugin_basename(); |
| 93 | 95 | $this->migration_manager = $migration_manager; |
| 94 | 96 | |
| 95 | - \add_action( 'plugins_loaded', array( $this, 'install' ) ); | |
| 97 | + \add_action( 'init', array( $this, 'install' ) ); | |
| 96 | 98 | |
| 97 | 99 | // I18n. |
| 98 | 100 | \add_action( 'plugins_loaded', array( $i18n_controller, 'load_text_domain' ) ); |
| 99 | 101 | |
| @@ -112,8 +114,9 @@ | ||
| 112 | 114 | \add_action( 'rest_api_init', array( $rest_settings_controller, 'register_routes' ) ); |
| 113 | 115 | \add_action( 'rest_api_init', array( $rest_onboarding_controller, 'register_routes' ) ); |
| 114 | 116 | \add_action( 'rest_api_init', array( $rest_payment_controller, 'register_routes' ) ); |
| 115 | 117 | \add_action( 'rest_api_init', array( $rest_log_controller, 'register_routes' ) ); |
| 118 | + \add_action( 'rest_api_init', array( $rest_store_integration_controller, 'register_routes' ) ); | |
| 116 | 119 | |
| 117 | 120 | // Payment hooks. |
| 118 | 121 | \add_filter( 'woocommerce_payment_gateways', array( $payment_controller, 'register_gateway_classes' ) ); |
| 119 | 122 | \add_action( 'woocommerce_blocks_loaded', array( $payment_controller, 'register_gateway_gutenberg_block' ) ); |
| @@ -138,11 +141,16 @@ | ||
| 138 | 141 | |
| 139 | 142 | // Order Update. |
| 140 | 143 | \add_action( 'woocommerce_order_status_changed', array( $order_controller, 'handle_order_status_changed' ), 10, 4 ); |
| 141 | 144 | \add_action( 'woocommerce_admin_order_data_after_order_details', array( $order_controller, 'show_link_to_sequra_back_office' ) ); |
| 145 | + \add_action( self::HOOK_CLEANUP_ORDERS, array( $order_controller, 'cleanup_orders' ) ); | |
| 146 | + \add_action( $constants->get_hook_add_order_indexes(), array( $order_controller, 'migrate_orders_to_use_indexes' ) ); | |
| 142 | 147 | |
| 143 | 148 | // WooCommerce Compat. |
| 144 | 149 | \add_action( 'before_woocommerce_init', array( $this, 'declare_woocommerce_compatibility' ) ); |
| 150 | + | |
| 151 | + // Cart identifier generation. | |
| 152 | + \add_action( 'woocommerce_init', array( $order_controller, 'ensure_cart_info_exists' ) ); | |
| 145 | 153 | } |
| 146 | 154 | |
| 147 | 155 | /** |
| 148 | 156 | * Handle activation of the plugin. |
| @@ -168,19 +176,31 @@ | ||
| 168 | 176 | * Handle deactivation of the plugin. |
| 169 | 177 | */ |
| 170 | 178 | public function deactivate(): void { |
| 171 | 179 | \wp_clear_scheduled_hook( self::HOOK_DELIVERY_REPORT ); |
| 180 | + \wp_clear_scheduled_hook( self::HOOK_CLEANUP_ORDERS ); | |
| 172 | 181 | } |
| 173 | 182 | |
| 174 | 183 | /** |
| 184 | + * Get a random time between 2AM and 8AM. | |
| 185 | + * | |
| 186 | + * @return int | |
| 187 | + */ | |
| 188 | + private function get_random_midnight_time() { | |
| 189 | + $random_offset = \wp_rand( 0, 25200 ); // 60*60*7 seconds from 2AM to 8AM. | |
| 190 | + $tomorrow = gmdate( 'Y-m-d 02:00', strtotime( 'tomorrow' ) ); | |
| 191 | + return $random_offset + strtotime( $tomorrow ); | |
| 192 | + } | |
| 193 | + | |
| 194 | + /** | |
| 175 | 195 | * Execute the installation process if needed. |
| 176 | 196 | */ |
| 177 | 197 | public function install(): void { |
| 178 | 198 | 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 ); | |
| 199 | + \wp_schedule_event( $this->get_random_midnight_time(), 'daily', self::HOOK_DELIVERY_REPORT ); | |
| 200 | + } | |
| 201 | + if ( ! \wp_next_scheduled( self::HOOK_CLEANUP_ORDERS ) ) { | |
| 202 | + \wp_schedule_event( $this->get_random_midnight_time(), 'daily', self::HOOK_CLEANUP_ORDERS ); | |
| 183 | 203 | } |
| 184 | 204 | $this->migration_manager->run_install_migrations(); |
| 185 | 205 | } |
| 186 | 206 | |