| @@ -14,8 +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\Constants\Interface_Constants; | |
| 18 | 19 | use SeQura\WC\Services\Migration\Interface_Migration_Manager; |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * The core plugin class. |
| @@ -64,18 +65,15 @@ | ||
| 64 | 65 | */ |
| 65 | 66 | private $migration_manager; |
| 66 | 67 | |
| 67 | 68 | private const HOOK_DELIVERY_REPORT = 'sequra_delivery_report'; |
| 69 | + private const HOOK_CLEANUP_ORDERS = 'sequra_cleanup_orders'; | |
| 68 | 70 | |
| 69 | 71 | /** |
| 70 | 72 | * Construct the plugin and bind hooks with controllers. |
| 71 | 73 | */ |
| 72 | 74 | public function __construct( |
| 73 | - array $data, | |
| 74 | - string $wp_version, | |
| 75 | - string $wc_version, | |
| 76 | - string $file_path, | |
| 77 | - string $base_name, | |
| 75 | + Interface_Constants $constants, | |
| 78 | 76 | Interface_Migration_Manager $migration_manager, |
| 79 | 77 | Interface_I18n_Controller $i18n_controller, |
| 80 | 78 | Interface_Assets_Controller $assets_controller, |
| 81 | 79 | Interface_Settings_Controller $settings_controller, |
| @@ -83,20 +81,21 @@ | ||
| 83 | 81 | REST_Controller $rest_settings_controller, |
| 84 | 82 | REST_Controller $rest_onboarding_controller, |
| 85 | 83 | REST_Controller $rest_payment_controller, |
| 86 | 84 | REST_Controller $rest_log_controller, |
| 85 | + REST_Controller $rest_store_integration_controller, | |
| 87 | 86 | Interface_Product_Controller $product_controller, |
| 88 | 87 | Interface_Async_Process_Controller $async_process_controller, |
| 89 | 88 | Interface_Order_Controller $order_controller |
| 90 | 89 | ) { |
| 91 | - $this->data = $data; | |
| 92 | - $this->wp_version = $wp_version; | |
| 93 | - $this->wc_version = $wc_version; | |
| 94 | - $this->file_path = $file_path; | |
| 95 | - $this->base_name = $base_name; | |
| 90 | + $this->data = $constants->get_plugin_data(); | |
| 91 | + $this->wp_version = $constants->get_environment_data()['wp_version'] ?? ''; | |
| 92 | + $this->wc_version = $constants->get_woocommerce_data()['Version'] ?? ''; | |
| 93 | + $this->file_path = $constants->get_plugin_file_path(); | |
| 94 | + $this->base_name = $constants->get_plugin_basename(); | |
| 96 | 95 | $this->migration_manager = $migration_manager; |
| 97 | 96 | |
| 98 | - \add_action( 'plugins_loaded', array( $this, 'install' ) ); | |
| 97 | + \add_action( 'init', array( $this, 'install' ) ); | |
| 99 | 98 | |
| 100 | 99 | // I18n. |
| 101 | 100 | \add_action( 'plugins_loaded', array( $i18n_controller, 'load_text_domain' ) ); |
| 102 | 101 | |
| @@ -105,9 +104,9 @@ | ||
| 105 | 104 | \add_action( 'wp_enqueue_scripts', array( $assets_controller, 'enqueue_front' ) ); |
| 106 | 105 | |
| 107 | 106 | // Settings hooks. |
| 108 | 107 | \add_action( 'admin_menu', array( $settings_controller, 'register_page' ) ); |
| 109 | - \add_filter( "plugin_action_links_{$base_name}", array( $settings_controller, 'add_action_link' ), 10, 4 ); | |
| 108 | + \add_filter( "plugin_action_links_{$this->base_name}", array( $settings_controller, 'add_action_link' ), 10, 4 ); | |
| 110 | 109 | \add_filter( 'sequra_settings_page_url', array( $settings_controller, 'get_settings_page_url' ) ); |
| 111 | 110 | \add_filter( 'admin_footer_text', array( $settings_controller, 'remove_footer_admin' ) ); |
| 112 | 111 | \add_filter( 'plugin_row_meta', array( $settings_controller, 'add_plugin_row_meta' ), 10, 2 ); |
| 113 | 112 | |
| @@ -115,8 +114,9 @@ | ||
| 115 | 114 | \add_action( 'rest_api_init', array( $rest_settings_controller, 'register_routes' ) ); |
| 116 | 115 | \add_action( 'rest_api_init', array( $rest_onboarding_controller, 'register_routes' ) ); |
| 117 | 116 | \add_action( 'rest_api_init', array( $rest_payment_controller, 'register_routes' ) ); |
| 118 | 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' ) ); | |
| 119 | 119 | |
| 120 | 120 | // Payment hooks. |
| 121 | 121 | \add_filter( 'woocommerce_payment_gateways', array( $payment_controller, 'register_gateway_classes' ) ); |
| 122 | 122 | \add_action( 'woocommerce_blocks_loaded', array( $payment_controller, 'register_gateway_gutenberg_block' ) ); |
| @@ -141,11 +141,16 @@ | ||
| 141 | 141 | |
| 142 | 142 | // Order Update. |
| 143 | 143 | \add_action( 'woocommerce_order_status_changed', array( $order_controller, 'handle_order_status_changed' ), 10, 4 ); |
| 144 | 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' ) ); | |
| 145 | 147 | |
| 146 | 148 | // WooCommerce Compat. |
| 147 | 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' ) ); | |
| 148 | 153 | } |
| 149 | 154 | |
| 150 | 155 | /** |
| 151 | 156 | * Handle activation of the plugin. |
| @@ -171,19 +176,31 @@ | ||
| 171 | 176 | * Handle deactivation of the plugin. |
| 172 | 177 | */ |
| 173 | 178 | public function deactivate(): void { |
| 174 | 179 | \wp_clear_scheduled_hook( self::HOOK_DELIVERY_REPORT ); |
| 180 | + \wp_clear_scheduled_hook( self::HOOK_CLEANUP_ORDERS ); | |
| 175 | 181 | } |
| 176 | 182 | |
| 177 | 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 | + /** | |
| 178 | 195 | * Execute the installation process if needed. |
| 179 | 196 | */ |
| 180 | 197 | public function install(): void { |
| 181 | 198 | if ( ! \wp_next_scheduled( self::HOOK_DELIVERY_REPORT ) ) { |
| 182 | - $random_offset = \wp_rand( 0, 25200 ); // 60*60*7 seconds from 2AM to 8AM. | |
| 183 | - $tomorrow = gmdate( 'Y-m-d 02:00', strtotime( 'tomorrow' ) ); | |
| 184 | - $time = $random_offset + strtotime( $tomorrow ); | |
| 185 | - \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 ); | |
| 186 | 203 | } |
| 187 | 204 | $this->migration_manager->run_install_migrations(); |
| 188 | 205 | } |
| 189 | 206 | |