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