| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sendy\WooCommerce; |
| 4 |
|
| 5 |
use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 6 |
use Sendy\WooCommerce\Modules\Admin\Settings; |
| 7 |
use Sendy\WooCommerce\Modules\Checkout; |
| 8 |
use Sendy\WooCommerce\Modules\OAuth; |
| 9 |
use Sendy\WooCommerce\Modules\Orders\BulkActions; |
| 10 |
use Sendy\WooCommerce\Modules\Orders\OrderList; |
| 11 |
use Sendy\WooCommerce\Modules\Orders\Single; |
| 12 |
use Sendy\WooCommerce\ShippingMethods\PickupPointDelivery; |
| 13 |
use WC_Shipping_Method; |
| 14 |
|
| 15 |
class Plugin |
| 16 |
{ |
| 17 |
public const VERSION = '3.0'; |
| 18 |
|
| 19 |
public const SETTINGS_ID = 'sendy'; |
| 20 |
|
| 21 |
private static Plugin $instance; |
| 22 |
|
| 23 |
private array $modules = []; |
| 24 |
|
| 25 |
private function __construct() |
| 26 |
{ |
| 27 |
add_action('init', [$this, 'initialize_plugin'], 1); |
| 28 |
add_action('before_woocommerce_init', [$this, 'declare_wc_hpos_compatibility'], 10); |
| 29 |
add_action('before_woocommerce_init', [$this, 'declare_checkout_blocks_incompatibility'], 10); |
| 30 |
|
| 31 |
load_plugin_textdomain('sendy', false, untrailingslashit( dirname( SENDY_WC_PLUGIN_BASENAME ) ) . '/languages'); |
| 32 |
} |
| 33 |
|
| 34 |
public static function instance(): Plugin |
| 35 |
{ |
| 36 |
return self::$instance ??= new self(); |
| 37 |
} |
| 38 |
|
| 39 |
public function initialize_plugin(): void |
| 40 |
{ |
| 41 |
$this->set_default_values_for_settings(); |
| 42 |
$this->define_constants(); |
| 43 |
$this->init_hooks(); |
| 44 |
} |
| 45 |
|
| 46 |
public function declare_wc_hpos_compatibility(): void |
| 47 |
{ |
| 48 |
if (class_exists('Automattic\WooCommerce\Utilities\FeaturesUtil')) { |
| 49 |
FeaturesUtil::declare_compatibility('custom_order_tables', SENDY_WC_PLUGIN_BASENAME); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public function declare_checkout_blocks_incompatibility(): void |
| 54 |
{ |
| 55 |
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) { |
| 56 |
FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', SENDY_WC_PLUGIN_BASENAME, false ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
private function define_constants(): void |
| 61 |
{ |
| 62 |
$upload_dir = wp_upload_dir(); |
| 63 |
|
| 64 |
define('SENDY_WC_PLUGIN_DIR_PATH', untrailingslashit(plugin_dir_path(SENDY_WC_PLUGIN_FILE))); |
| 65 |
define('SENDY_WC_PLUGIN_DIR_URL', untrailingslashit(plugins_url('/', SENDY_WC_PLUGIN_FILE))); |
| 66 |
define('SENDY_WC_VERSION', self::VERSION); |
| 67 |
define('SENDY_SETTINGS_ID', self::SETTINGS_ID); |
| 68 |
define('SENDY_LOG_DIR', $upload_dir['basedir'] . '/sendy-logs'); |
| 69 |
define('SENDY_UPLOAD_DIR', $upload_dir['basedir'] . '/sendy'); |
| 70 |
} |
| 71 |
|
| 72 |
private function init_hooks(): void |
| 73 |
{ |
| 74 |
add_action('woocommerce_shipping_methods', [$this, 'add_shipping_methods']); |
| 75 |
|
| 76 |
add_action('init', [$this, 'initialize_modules']); |
| 77 |
|
| 78 |
if (is_admin()) { |
| 79 |
add_action('admin_notices', [$this, 'display_admin_notices']); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function initialize_modules(): void |
| 84 |
{ |
| 85 |
$this->modules['oauth'] = new OAuth(); |
| 86 |
$this->modules['admin_settings'] = new Settings(); |
| 87 |
|
| 88 |
if (sendy_is_authenticated()) { |
| 89 |
$this->modules['orders_bulk_actions'] = new BulkActions(); |
| 90 |
$this->modules['orders_list'] = new OrderList(); |
| 91 |
$this->modules['orders_single'] = new Single(); |
| 92 |
$this->modules['checkout'] = new Checkout(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param array<string,WC_Shipping_Method> $shippingMethods |
| 98 |
* @return array<string,WC_Shipping_Method> |
| 99 |
*/ |
| 100 |
public function add_shipping_methods(array $shippingMethods): array |
| 101 |
{ |
| 102 |
$shippingMethods[PickupPointDelivery::ID] = PickupPointDelivery::class; |
| 103 |
|
| 104 |
return $shippingMethods; |
| 105 |
} |
| 106 |
|
| 107 |
public function display_admin_notices(): void |
| 108 |
{ |
| 109 |
if (is_array(get_option('sendy_flash_admin_messages'))) { |
| 110 |
$messages = get_option('sendy_flash_admin_messages'); |
| 111 |
|
| 112 |
foreach ($messages as $message) { |
| 113 |
printf('<div class="notice notice-%s">%s</div>', esc_html($message['type']), esc_html($message['message'])); |
| 114 |
} |
| 115 |
|
| 116 |
delete_option('sendy_flash_admin_messages'); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public function set_default_values_for_settings(): void |
| 121 |
{ |
| 122 |
$defaultValues = [ |
| 123 |
'sendy_import_weight' => true, |
| 124 |
'sendy_import_products' => true, |
| 125 |
'sendy_mark_order_as_completed' => 'after-shipment-created', |
| 126 |
]; |
| 127 |
|
| 128 |
foreach ($defaultValues as $option => $defaultValue) { |
| 129 |
if (get_option($option) === false) { |
| 130 |
update_option($option, $defaultValue); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|