| @@ -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. |
| @@ -29,8 +30,22 @@ | ||
| 29 | 30 | */ |
| 30 | 31 | private $data; |
| 31 | 32 | |
| 32 | 33 | /** |
| 34 | + * The WordPress version | |
| 35 | + * | |
| 36 | + * @var string | |
| 37 | + */ | |
| 38 | + private $wp_version; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * The WooCommerce version | |
| 42 | + * | |
| 43 | + * @var string | |
| 44 | + */ | |
| 45 | + private $wc_version; | |
| 46 | + | |
| 47 | + /** | |
| 33 | 48 | * The plugin base name. |
| 34 | 49 | * |
| 35 | 50 | * @var string |
| 36 | 51 | */ |
| @@ -36,8 +51,15 @@ | ||
| 36 | 51 | */ |
| 37 | 52 | private $base_name; |
| 38 | 53 | |
| 39 | 54 | /** |
| 55 | + * The plugin file path. | |
| 56 | + * | |
| 57 | + * @var string | |
| 58 | + */ | |
| 59 | + private $file_path; | |
| 60 | + | |
| 61 | + /** | |
| 40 | 62 | * Migration manager. |
| 41 | 63 | * |
| 42 | 64 | * @var Interface_Migration_Manager |
| 43 | 65 | */ |
| @@ -43,15 +65,15 @@ | ||
| 43 | 65 | */ |
| 44 | 66 | private $migration_manager; |
| 45 | 67 | |
| 46 | 68 | private const HOOK_DELIVERY_REPORT = 'sequra_delivery_report'; |
| 69 | + private const HOOK_CLEANUP_ORDERS = 'sequra_cleanup_orders'; | |
| 47 | 70 | |
| 48 | 71 | /** |
| 49 | 72 | * Construct the plugin and bind hooks with controllers. |
| 50 | 73 | */ |
| 51 | 74 | public function __construct( |
| 52 | - array $data, | |
| 53 | - string $base_name, | |
| 75 | + Interface_Constants $constants, | |
| 54 | 76 | Interface_Migration_Manager $migration_manager, |
| 55 | 77 | Interface_I18n_Controller $i18n_controller, |
| 56 | 78 | Interface_Assets_Controller $assets_controller, |
| 57 | 79 | Interface_Settings_Controller $settings_controller, |
| @@ -63,59 +85,67 @@ | ||
| 63 | 85 | Interface_Product_Controller $product_controller, |
| 64 | 86 | Interface_Async_Process_Controller $async_process_controller, |
| 65 | 87 | Interface_Order_Controller $order_controller |
| 66 | 88 | ) { |
| 67 | - $this->data = $data; | |
| 68 | - $this->base_name = $base_name; | |
| 89 | + $this->data = $constants->get_plugin_data(); | |
| 90 | + $this->wp_version = $constants->get_environment_data()['wp_version'] ?? ''; | |
| 91 | + $this->wc_version = $constants->get_woocommerce_data()['Version'] ?? ''; | |
| 92 | + $this->file_path = $constants->get_plugin_file_path(); | |
| 93 | + $this->base_name = $constants->get_plugin_basename(); | |
| 69 | 94 | $this->migration_manager = $migration_manager; |
| 70 | 95 | |
| 71 | - add_action( 'plugins_loaded', array( $this, 'install' ) ); | |
| 96 | + \add_action( 'plugins_loaded', array( $this, 'install' ) ); | |
| 72 | 97 | |
| 73 | 98 | // I18n. |
| 74 | - add_action( 'plugins_loaded', array( $i18n_controller, 'load_text_domain' ) ); | |
| 99 | + \add_action( 'plugins_loaded', array( $i18n_controller, 'load_text_domain' ) ); | |
| 75 | 100 | |
| 76 | 101 | // Assets hooks. |
| 77 | - add_action( 'admin_enqueue_scripts', array( $assets_controller, 'enqueue_admin' ) ); | |
| 78 | - add_action( 'wp_enqueue_scripts', array( $assets_controller, 'enqueue_front' ) ); | |
| 102 | + \add_action( 'admin_enqueue_scripts', array( $assets_controller, 'enqueue_admin' ) ); | |
| 103 | + \add_action( 'wp_enqueue_scripts', array( $assets_controller, 'enqueue_front' ) ); | |
| 79 | 104 | |
| 80 | 105 | // Settings hooks. |
| 81 | - add_action( 'admin_menu', array( $settings_controller, 'register_page' ) ); | |
| 82 | - add_filter( "plugin_action_links_{$base_name}", array( $settings_controller, 'add_action_link' ), 10, 4 ); | |
| 83 | - add_filter( 'sequra_settings_page_url', array( $settings_controller, 'get_settings_page_url' ) ); | |
| 84 | - add_filter( 'admin_footer_text', array( $settings_controller, 'remove_footer_admin' ) ); | |
| 85 | - add_filter( 'plugin_row_meta', array( $settings_controller, 'add_plugin_row_meta' ), 10, 2 ); | |
| 106 | + \add_action( 'admin_menu', array( $settings_controller, 'register_page' ) ); | |
| 107 | + \add_filter( "plugin_action_links_{$this->base_name}", array( $settings_controller, 'add_action_link' ), 10, 4 ); | |
| 108 | + \add_filter( 'sequra_settings_page_url', array( $settings_controller, 'get_settings_page_url' ) ); | |
| 109 | + \add_filter( 'admin_footer_text', array( $settings_controller, 'remove_footer_admin' ) ); | |
| 110 | + \add_filter( 'plugin_row_meta', array( $settings_controller, 'add_plugin_row_meta' ), 10, 2 ); | |
| 86 | 111 | |
| 87 | 112 | // REST Controllers. |
| 88 | - add_action( 'rest_api_init', array( $rest_settings_controller, 'register_routes' ) ); | |
| 89 | - add_action( 'rest_api_init', array( $rest_onboarding_controller, 'register_routes' ) ); | |
| 90 | - add_action( 'rest_api_init', array( $rest_payment_controller, 'register_routes' ) ); | |
| 91 | - add_action( 'rest_api_init', array( $rest_log_controller, 'register_routes' ) ); | |
| 113 | + \add_action( 'rest_api_init', array( $rest_settings_controller, 'register_routes' ) ); | |
| 114 | + \add_action( 'rest_api_init', array( $rest_onboarding_controller, 'register_routes' ) ); | |
| 115 | + \add_action( 'rest_api_init', array( $rest_payment_controller, 'register_routes' ) ); | |
| 116 | + \add_action( 'rest_api_init', array( $rest_log_controller, 'register_routes' ) ); | |
| 92 | 117 | |
| 93 | 118 | // Payment hooks. |
| 94 | - add_filter( 'woocommerce_payment_gateways', array( $payment_controller, 'register_gateway_classes' ) ); | |
| 95 | - add_action( 'woocommerce_blocks_loaded', array( $payment_controller, 'register_gateway_gutenberg_block' ) ); | |
| 119 | + \add_filter( 'woocommerce_payment_gateways', array( $payment_controller, 'register_gateway_classes' ) ); | |
| 120 | + \add_action( 'woocommerce_blocks_loaded', array( $payment_controller, 'register_gateway_gutenberg_block' ) ); | |
| 96 | 121 | |
| 97 | - add_filter( 'woocommerce_thankyou_order_received_text', array( $payment_controller, 'order_received_text' ), 10, 2 ); | |
| 98 | - add_filter( 'woocommerce_order_get_payment_method_title', array( $payment_controller, 'order_get_payment_method_title' ), 10, 2 ); | |
| 122 | + \add_filter( 'woocommerce_thankyou_order_received_text', array( $payment_controller, 'order_received_text' ), 10, 2 ); | |
| 123 | + \add_filter( 'woocommerce_order_get_payment_method_title', array( $payment_controller, 'order_get_payment_method_title' ), 10, 2 ); | |
| 99 | 124 | |
| 100 | 125 | // Product widget. |
| 101 | - add_action( 'woocommerce_after_main_content', array( $product_controller, 'add_widget_shortcode_to_page' ) ); | |
| 102 | - add_action( 'wp_footer', array( $product_controller, 'add_widget_shortcode_to_page' ) ); | |
| 126 | + \add_action( 'woocommerce_after_main_content', array( $product_controller, 'add_widget_shortcode_to_page' ) ); | |
| 127 | + \add_action( 'wp_footer', array( $product_controller, 'add_widget_shortcode_to_page' ) ); | |
| 103 | 128 | |
| 104 | - add_shortcode( 'sequra_widget', array( $product_controller, 'do_widget_shortcode' ) ); | |
| 105 | - add_shortcode( 'sequra_cart_widget', array( $product_controller, 'do_cart_widget_shortcode' ) ); | |
| 106 | - add_shortcode( 'sequra_product_listing_widget', array( $product_controller, 'do_product_listing_widget_shortcode' ) ); | |
| 129 | + \add_shortcode( 'sequra_widget', array( $product_controller, 'do_widget_shortcode' ) ); | |
| 130 | + \add_shortcode( 'sequra_cart_widget', array( $product_controller, 'do_cart_widget_shortcode' ) ); | |
| 131 | + \add_shortcode( 'sequra_product_listing_widget', array( $product_controller, 'do_product_listing_widget_shortcode' ) ); | |
| 107 | 132 | |
| 108 | - add_action( 'add_meta_boxes', array( $product_controller, 'add_meta_boxes' ) ); | |
| 109 | - add_action( 'woocommerce_process_product_meta', array( $product_controller, 'save_product_meta' ) ); | |
| 133 | + \add_action( 'add_meta_boxes', array( $product_controller, 'add_meta_boxes' ) ); | |
| 134 | + \add_action( 'woocommerce_process_product_meta', array( $product_controller, 'save_product_meta' ) ); | |
| 110 | 135 | |
| 111 | 136 | // Delivery report. |
| 112 | - add_action( self::HOOK_DELIVERY_REPORT, array( $async_process_controller, 'send_delivery_report' ) ); | |
| 113 | - add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', array( $order_controller, 'handle_custom_query_vars' ), 10, 3 ); | |
| 137 | + \add_action( self::HOOK_DELIVERY_REPORT, array( $async_process_controller, 'send_delivery_report' ) ); | |
| 138 | + \add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', array( $order_controller, 'handle_custom_query_vars' ), 10, 3 ); | |
| 114 | 139 | |
| 115 | 140 | // Order Update. |
| 116 | - add_action( 'woocommerce_order_status_changed', array( $order_controller, 'handle_order_status_changed' ), 10, 4 ); | |
| 117 | - add_action( 'woocommerce_admin_order_data_after_order_details', array( $order_controller, 'show_link_to_sequra_back_office' ) ); | |
| 141 | + \add_action( 'woocommerce_order_status_changed', array( $order_controller, 'handle_order_status_changed' ), 10, 4 ); | |
| 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' ) ); | |
| 145 | + | |
| 146 | + // WooCommerce Compat. | |
| 147 | + \add_action( 'before_woocommerce_init', array( $this, 'declare_woocommerce_compatibility' ) ); | |
| 118 | 148 | } |
| 119 | 149 | |
| 120 | 150 | /** |
| 121 | 151 | * Handle activation of the plugin. |
| @@ -121,21 +151,20 @@ | ||
| 121 | 151 | * Handle activation of the plugin. |
| 122 | 152 | */ |
| 123 | 153 | public function activate(): void { |
| 124 | 154 | if ( version_compare( PHP_VERSION, $this->data['RequiresPHP'], '<' ) ) { |
| 125 | - deactivate_plugins( $this->base_name ); | |
| 126 | - wp_die( esc_html( 'This plugin requires PHP ' . $this->data['RequiresPHP'] . ' or greater.' ) ); | |
| 155 | + \deactivate_plugins( $this->base_name ); | |
| 156 | + \wp_die( \esc_html( 'This plugin requires PHP ' . $this->data['RequiresPHP'] . ' or greater.' ) ); | |
| 127 | 157 | } |
| 128 | 158 | |
| 129 | - global $wp_version; | |
| 130 | - if ( version_compare( $wp_version, $this->data['RequiresWP'], '<' ) ) { | |
| 131 | - deactivate_plugins( $this->base_name ); | |
| 132 | - wp_die( esc_html( 'This plugin requires WordPress ' . $this->data['RequiresWP'] . ' or greater.' ) ); | |
| 159 | + if ( version_compare( $this->wp_version, $this->data['RequiresWP'], '<' ) ) { | |
| 160 | + \deactivate_plugins( $this->base_name ); | |
| 161 | + \wp_die( \esc_html( 'This plugin requires WordPress ' . $this->data['RequiresWP'] . ' or greater.' ) ); | |
| 133 | 162 | } |
| 134 | 163 | |
| 135 | - if ( ! defined( 'WC_VERSION' ) || version_compare( WC_VERSION, $this->data['RequiresWC'], '<' ) ) { | |
| 136 | - deactivate_plugins( $this->base_name ); | |
| 137 | - wp_die( esc_html( 'This plugin requires WooCommerce ' . $this->data['RequiresWC'] . ' or greater.' ) ); | |
| 164 | + if ( version_compare( $this->wc_version, $this->data['RequiresWC'], '<' ) ) { | |
| 165 | + \deactivate_plugins( $this->base_name ); | |
| 166 | + \wp_die( \esc_html( 'This plugin requires WooCommerce ' . $this->data['RequiresWC'] . ' or greater.' ) ); | |
| 138 | 167 | } |
| 139 | 168 | } |
| 140 | 169 | |
| 141 | 170 | /** |
| @@ -141,20 +170,41 @@ | ||
| 141 | 170 | /** |
| 142 | 171 | * Handle deactivation of the plugin. |
| 143 | 172 | */ |
| 144 | 173 | public function deactivate(): void { |
| 145 | - wp_clear_scheduled_hook( self::HOOK_DELIVERY_REPORT ); | |
| 174 | + \wp_clear_scheduled_hook( self::HOOK_DELIVERY_REPORT ); | |
| 175 | + \wp_clear_scheduled_hook( self::HOOK_CLEANUP_ORDERS ); | |
| 146 | 176 | } |
| 147 | 177 | |
| 148 | 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 | + /** | |
| 149 | 190 | * Execute the installation process if needed. |
| 150 | 191 | */ |
| 151 | 192 | public function install(): void { |
| 152 | - if ( ! wp_next_scheduled( self::HOOK_DELIVERY_REPORT ) ) { | |
| 153 | - $random_offset = wp_rand( 0, 25200 ); // 60*60*7 seconds from 2AM to 8AM. | |
| 154 | - $tomorrow = gmdate( 'Y-m-d 02:00', strtotime( 'tomorrow' ) ); | |
| 155 | - $time = $random_offset + strtotime( $tomorrow ); | |
| 156 | - wp_schedule_event( $time, 'daily', self::HOOK_DELIVERY_REPORT ); | |
| 193 | + if ( ! \wp_next_scheduled( self::HOOK_DELIVERY_REPORT ) ) { | |
| 194 | + \wp_schedule_event( $this->get_random_midnight_time(), 'daily', self::HOOK_DELIVERY_REPORT ); | |
| 157 | 195 | } |
| 196 | + if ( ! \wp_next_scheduled( self::HOOK_CLEANUP_ORDERS ) ) { | |
| 197 | + \wp_schedule_event( $this->get_random_midnight_time(), 'daily', self::HOOK_CLEANUP_ORDERS ); | |
| 198 | + } | |
| 158 | 199 | $this->migration_manager->run_install_migrations(); |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 203 | + * Declare WooCommerce compatibility. | |
| 204 | + */ | |
| 205 | + public function declare_woocommerce_compatibility() { | |
| 206 | + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { | |
| 207 | + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', $this->file_path, true ); | |
| 208 | + } | |
| 159 | 209 | } |
| 160 | 210 | } |