set_default_values_for_settings(); $this->define_constants(); $this->init_hooks(); $this->init_internationalization(); $this->init_cron(); } public function declare_wc_hpos_compatibility(): void { if (class_exists('Automattic\WooCommerce\Utilities\FeaturesUtil')) { FeaturesUtil::declare_compatibility('custom_order_tables', SENDY_WC_PLUGIN_BASENAME); } } private function define_constants(): void { $upload_dir = wp_upload_dir(); define('SENDY_WC_PLUGIN_DIR_PATH', untrailingslashit(plugin_dir_path(SENDY_WC_PLUGIN_FILE))); define('SENDY_WC_PLUGIN_DIR_URL', untrailingslashit(plugins_url('/', SENDY_WC_PLUGIN_FILE))); define('SENDY_WC_VERSION', self::VERSION); define('SENDY_SETTINGS_ID', self::SETTINGS_ID); define('SENDY_LOG_DIR', $upload_dir['basedir'] . '/sendy-logs'); define('SENDY_UPLOAD_DIR', $upload_dir['basedir'] . '/sendy'); } private function init_hooks(): void { add_action('woocommerce_shipping_methods', [$this, 'add_shipping_methods']); add_action('init', [$this, 'initialize_modules']); if (is_admin()) { add_action('admin_notices', [$this, 'display_admin_notices']); } } private function init_internationalization(): void { // This filter is added in order to override the translations from the WordPress translations directory if a // translation is provided by the plug-in. add_filter('load_textdomain_mofile', function (string $moFile, string $domain) { $basePath = WP_CONTENT_DIR . '/plugins/sendy/languages/'; if ($domain === 'sendy' && str_starts_with($moFile, WP_LANG_DIR . '/plugins/') !== false) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound $locale = apply_filters('plugin_locale', determine_locale(), $domain); $filename = $basePath . '/sendy-' . $locale . '.mo'; if (is_readable($filename)) { return $filename; } } return $moFile; }, 10, 2); // phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound load_plugin_textdomain('sendy', false, untrailingslashit(dirname(SENDY_WC_PLUGIN_BASENAME)) . '/languages'); } private function init_cron(): void { if (! wp_next_scheduled('sendy_cron')) { wp_schedule_event(time(), 'hourly', 'sendy_cron'); } } public function initialize_modules(): void { $this->modules['oauth'] = new OAuth(); $this->modules['admin_settings'] = new Settings(); if (sendy_is_authenticated()) { $this->modules['orders_bulk_actions'] = new BulkActions(); $this->modules['orders_list'] = new OrderList(); $this->modules['orders_single'] = new Single(); $this->modules['checkout'] = new Checkout(); $this->modules['webhooks'] = new Webhooks(); $this->modules['orders_sendy'] = new ProcessInBackground(); $this->modules['shipping_methods_synchronizer'] = new ShippingMethodsSynchronizer(); } } /** * @param array $shippingMethods * @return array */ public function add_shipping_methods(array $shippingMethods): array { $shippingMethods[StandardDelivery::ID] = StandardDelivery::class; $shippingMethods[PickupPointDelivery::ID] = PickupPointDelivery::class; return $shippingMethods; } public function display_admin_notices(): void { if (is_array(get_option('sendy_flash_admin_messages'))) { $messages = get_option('sendy_flash_admin_messages'); foreach ($messages as $message) { printf('
%s
', esc_html($message['type']), esc_html($message['message'])); } delete_option('sendy_flash_admin_messages'); } } public function set_default_values_for_settings(): void { $defaultValues = [ 'sendy_import_weight' => true, 'sendy_import_products' => true, 'sendy_mark_order_as_completed' => 'after-shipment-created', 'sendy_processing_method' => 'woocommerce', 'sendy_processable_status' => 'processing', ]; foreach ($defaultValues as $option => $defaultValue) { if (get_option($option) === false) { update_option($option, $defaultValue); } } } public function deactivate(): void { if (wp_next_scheduled('sendy_cron')) { wp_clear_scheduled_hook('sendy_cron'); } foreach ($this->modules as $module) { if (method_exists($module, 'deactivate')) { $module->deactivate(); } } } }