PluginProbe
Sendy / 3.4.3
Sendy v3.4.3
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 / lib / Plugin.php

Plugin.php in Sendy 3.4.3, at lib/Plugin.php

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