includes(); $this->maybe_upgrade_db(); $this->logger = new Weship_ACS_Sync_Logger(); $this->api = new Weship_ACS_Sync_Api_Client( $this->logger ); $this->order_sync = new Weship_ACS_Sync_Order_Sync( $this->api, $this->logger ); $this->webhooks = new Weship_ACS_Sync_Webhooks( $this->logger ); $this->print_jobs = new Weship_ACS_Sync_Print_Job_Service(); $this->channel = new Weship_ACS_Sync_Channel_Service( $this->logger ); new Weship_ACS_Sync_Cron( $this->order_sync ); new Weship_ACS_Sync_Checkout_Fields(); if ( is_admin() ) { // Orders_Page registers the top-level admin menu (Orders is its // default/landing page) — must run before Settings so Orders // appears above Settings in the submenu. new Weship_ACS_Sync_Admin_Orders( $this->order_sync ); new Weship_ACS_Sync_Orders_Page( $this->order_sync, $this->logger, $this->print_jobs ); new Weship_ACS_Sync_Settings(); new Weship_ACS_Sync_Print_Jobs_Page( $this->print_jobs ); new Weship_ACS_Sync_Product_Fields(); } } /** * Load includes. */ private function includes() { require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-logger.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-api-client.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-order-sync.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-webhooks.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-cron.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-print-job-service.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-channel-service.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-checkout-fields.php'; require_once WESHIP_ACS_SYNC_PATH . 'admin/class-weship-acs-sync-settings.php'; require_once WESHIP_ACS_SYNC_PATH . 'admin/class-weship-acs-sync-admin-orders.php'; require_once WESHIP_ACS_SYNC_PATH . 'admin/class-weship-acs-sync-orders-page.php'; require_once WESHIP_ACS_SYNC_PATH . 'admin/class-weship-acs-sync-print-jobs-page.php'; require_once WESHIP_ACS_SYNC_PATH . 'admin/class-weship-acs-sync-product-fields.php'; } /** * Create/upgrade the log table schema. Safe to call repeatedly (dbDelta). */ private static function create_log_table() { global $wpdb; $table_name = $wpdb->prefix . 'weship_acs_sync_logs'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$table_name} ( id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, order_id BIGINT(20) UNSIGNED NULL, event VARCHAR(64) NOT NULL, direction VARCHAR(16) NOT NULL DEFAULT 'outbound', url VARCHAR(500) NULL, status_code VARCHAR(16) NULL, message TEXT NULL, request LONGTEXT NULL, response LONGTEXT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY order_id (order_id), KEY event (event), KEY created_at (created_at) ) {$charset_collate};"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } /** * Run the log table schema upgrade if the stored DB version is stale. * Lets existing installs pick up schema changes (e.g. the "url" column) * without requiring deactivate/reactivate. */ private function maybe_upgrade_db() { if ( get_option( 'weship_acs_sync_db_version' ) === WESHIP_ACS_SYNC_DB_VERSION ) { return; } self::migrate_legacy_data(); self::create_log_table(); update_option( 'weship_acs_sync_db_version', WESHIP_ACS_SYNC_DB_VERSION ); } /** * One-time migration from the previous "Advatix OMS - Weship" plugin * (folder advatix-oms-weship; option/meta prefix advatix_weship_ / * _advatix_weship_; tables {prefix}advatix_weship_logs and * {prefix}advatix_weship_print_jobs). Renaming the plugin folder makes * WordPress treat this as a brand new plugin with nothing configured, so * without this, activating it fresh would mean re-entering the API * credentials and losing every order's sync/tag/tracking history. * * Every step checks for the legacy data first and is a no-op if it's * already been migrated (or never existed), so this is safe to call on * every load, not just once. */ private static function migrate_legacy_data() { global $wpdb; if ( false === get_option( 'weship_acs_sync_settings', false ) ) { $legacy_settings = get_option( 'advatix_weship_settings', false ); if ( false !== $legacy_settings ) { update_option( 'weship_acs_sync_settings', $legacy_settings ); } } // Rename tables in place (preserves rows and indexes) rather than // copying, and only once — dbDelta() in create_log_table() / // Print_Job_Service::create_table() then just verifies/upgrades the // schema on the renamed table like it would on any existing one. $table_renames = array( $wpdb->prefix . 'advatix_weship_logs' => $wpdb->prefix . 'weship_acs_sync_logs', $wpdb->prefix . 'advatix_weship_print_jobs' => $wpdb->prefix . 'weship_acs_sync_print_jobs', ); foreach ( $table_renames as $old_table => $new_table ) { $old_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $old_table ) ) === $old_table; $new_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $new_table ) ) === $new_table; if ( $old_exists && ! $new_exists ) { $wpdb->query( "RENAME TABLE `{$old_table}` TO `{$new_table}`" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery } } // Order and product meta: _advatix_weship_* -> _weship_acs_sync_* // (sync status/message/attempts/hash, tags, gift note, cancel // message, tracking, weather hold, fulfillment status, CCP label/ // packing-slip/description, and the per-product CCP fields). $wpdb->query( "UPDATE {$wpdb->postmeta} SET meta_key = REPLACE( meta_key, '_advatix_weship_', '_weship_acs_sync_' ) WHERE meta_key LIKE '\\_advatix\\_weship\\_%'" // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching ); delete_option( 'advatix_weship_db_version' ); } /** * Activation: migrate any legacy data, create the log table and schedule cron. */ public static function activate() { self::migrate_legacy_data(); self::create_log_table(); update_option( 'weship_acs_sync_db_version', WESHIP_ACS_SYNC_DB_VERSION ); require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-print-job-service.php'; Weship_ACS_Sync_Print_Job_Service::create_table(); require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-api-client.php'; require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-cron.php'; // Only schedule if Order Delay is already on (e.g. re-activating with // existing settings); a fresh install defaults it to off. Weship_ACS_Sync_Cron::maybe_schedule_or_clear(); } /** * Deactivation: clear scheduled events. */ public static function deactivate() { require_once WESHIP_ACS_SYNC_PATH . 'includes/class-weship-acs-sync-cron.php'; wp_clear_scheduled_hook( Weship_ACS_Sync_Cron::HOOK ); } /** * Admin notice when WooCommerce is missing. */ public function woocommerce_missing_notice() { echo '

'; esc_html_e( 'WSE ACS Sync for WooCommerce requires WooCommerce to be installed and active.', 'weship-acs-sync-for-woocommerce' ); echo '

'; } }