PluginProbe
Sendy / 3.2.4
Sendy v3.2.4
3.4.6 3.4.7 trunk 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 All 32 releases
sendy / src / Plugin.php

Plugin.php in Sendy 3.2.4, at src/Plugin.php

188 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\ProcessInBackground;
11 use Sendy\WooCommerce\Modules\Orders\OrderList;
12 use Sendy\WooCommerce\Modules\Orders\Single;
13 use Sendy\WooCommerce\Modules\ShippingMethodsSynchronizer;
14 use Sendy\WooCommerce\Modules\Webhooks;
15 use Sendy\WooCommerce\ShippingMethods\PickupPointDelivery;
16 use Sendy\WooCommerce\ShippingMethods\StandardDelivery;
17 use WC_Shipping_Method;
18
19 class Plugin
20 {
21 public const VERSION = '3.2.4';
22
23 public const SETTINGS_ID = 'sendy';
24
25 private static Plugin $instance;
26
27 private array $modules = [];
28
29 private function __construct()
30 {
31 add_action('init', [$this, 'initialize_plugin'], 0);
32 add_action('before_woocommerce_init', [$this, 'declare_wc_hpos_compatibility'], 10);
33 add_action('before_woocommerce_init', [$this, 'declare_checkout_blocks_incompatibility'], 10);
34 }
35
36 public static function instance(): Plugin
37 {
38 return self::$instance ??= new self();
39 }
40
41 public function initialize_plugin(): void
42 {
43 $this->set_default_values_for_settings();
44 $this->define_constants();
45 $this->init_hooks();
46 $this->init_internationalization();
47 $this->init_cron();
48 }
49
50 public function declare_wc_hpos_compatibility(): void
51 {
52 if (class_exists('Automattic\WooCommerce\Utilities\FeaturesUtil')) {
53 FeaturesUtil::declare_compatibility('custom_order_tables', SENDY_WC_PLUGIN_BASENAME);
54 }
55 }
56
57 public function declare_checkout_blocks_incompatibility(): void
58 {
59 if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
60 FeaturesUtil::declare_compatibility('cart_checkout_blocks', SENDY_WC_PLUGIN_BASENAME, false);
61 }
62 }
63
64 private function define_constants(): void
65 {
66 $upload_dir = wp_upload_dir();
67
68 define('SENDY_WC_PLUGIN_DIR_PATH', untrailingslashit(plugin_dir_path(SENDY_WC_PLUGIN_FILE)));
69 define('SENDY_WC_PLUGIN_DIR_URL', untrailingslashit(plugins_url('/', SENDY_WC_PLUGIN_FILE)));
70 define('SENDY_WC_VERSION', self::VERSION);
71 define('SENDY_SETTINGS_ID', self::SETTINGS_ID);
72 define('SENDY_LOG_DIR', $upload_dir['basedir'] . '/sendy-logs');
73 define('SENDY_UPLOAD_DIR', $upload_dir['basedir'] . '/sendy');
74 }
75
76 private function init_hooks(): void
77 {
78 add_action('woocommerce_shipping_methods', [$this, 'add_shipping_methods']);
79
80 add_action('init', [$this, 'initialize_modules']);
81
82 if (is_admin()) {
83 add_action('admin_notices', [$this, 'display_admin_notices']);
84 }
85 }
86
87 private function init_internationalization(): void
88 {
89 // This filter is added in order to override the translations from the WordPress translations directory if a
90 // translation is provided by the plug-in.
91 add_filter('load_textdomain_mofile', function (string $moFile, string $domain) {
92 $basePath = WP_CONTENT_DIR . '/plugins/sendy/languages/';
93
94 if ($domain === 'sendy' && str_starts_with($moFile, WP_LANG_DIR . '/plugins/') !== false) {
95 $locale = apply_filters('plugin_locale', determine_locale(), $domain);
96
97 $filename = $basePath . '/sendy-' . $locale . '.mo';
98
99 if (is_readable($filename)) {
100 return $filename;
101 }
102 }
103
104 return $moFile;
105 }, 10, 2);
106
107 load_plugin_textdomain('sendy', false, untrailingslashit(dirname(SENDY_WC_PLUGIN_BASENAME)) . '/languages');
108 }
109
110 private function init_cron(): void
111 {
112 if (!wp_next_scheduled('sendy_cron')) {
113 wp_schedule_event(time(), 'hourly', 'sendy_cron');
114 }
115 }
116
117 public function initialize_modules(): void
118 {
119 $this->modules['oauth'] = new OAuth();
120 $this->modules['admin_settings'] = new Settings();
121
122 if (sendy_is_authenticated()) {
123 $this->modules['orders_bulk_actions'] = new BulkActions();
124 $this->modules['orders_list'] = new OrderList();
125 $this->modules['orders_single'] = new Single();
126 $this->modules['checkout'] = new Checkout();
127 $this->modules['webhooks'] = new Webhooks();
128 $this->modules['orders_sendy'] = new ProcessInBackground();
129 $this->modules['shipping_methods_synchronizer'] = new ShippingMethodsSynchronizer();
130 }
131 }
132
133 /**
134 * @param array<string,WC_Shipping_Method> $shippingMethods
135 * @return array<string,WC_Shipping_Method>
136 */
137 public function add_shipping_methods(array $shippingMethods): array
138 {
139 $shippingMethods[StandardDelivery::ID] = StandardDelivery::class;
140 $shippingMethods[PickupPointDelivery::ID] = PickupPointDelivery::class;
141
142 return $shippingMethods;
143 }
144
145 public function display_admin_notices(): void
146 {
147 if (is_array(get_option('sendy_flash_admin_messages'))) {
148 $messages = get_option('sendy_flash_admin_messages');
149
150 foreach ($messages as $message) {
151 printf('<div class="notice notice-%s">%s</div>', esc_html($message['type']), esc_html($message['message']));
152 }
153
154 delete_option('sendy_flash_admin_messages');
155 }
156 }
157
158 public function set_default_values_for_settings(): void
159 {
160 $defaultValues = [
161 'sendy_import_weight' => true,
162 'sendy_import_products' => true,
163 'sendy_mark_order_as_completed' => 'after-shipment-created',
164 'sendy_processing_method' => 'woocommerce',
165 'sendy_processable_status' => 'processing',
166 ];
167
168 foreach ($defaultValues as $option => $defaultValue) {
169 if (get_option($option) === false) {
170 update_option($option, $defaultValue);
171 }
172 }
173 }
174
175 public function deactivate(): void
176 {
177 if (wp_next_scheduled('sendy_cron')) {
178 wp_clear_scheduled_hook('sendy_cron');
179 }
180
181 foreach ($this->modules as $module) {
182 if (method_exists($module, 'deactivate')) {
183 $module->deactivate();
184 }
185 }
186 }
187 }
188