Manual and scheduled sync previously processed every WooCommerce product inline in a single PHP request/AJAX call, with several sequential BC API calls per product. At real catalog sizes this exceeds PHP's max_execution_time and the proxy's read timeout, surfacing as a 502 Bad Gateway on "Sync Now". Adds WBC_Batch_Sync, which queues the catalog in chunks of 25 via Action Scheduler (bundled with WooCommerce) and processes them asynchronously. The manual sync AJAX call now returns immediately after queuing, and the admin UI polls for progress instead of waiting on one long request. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
170 lines
5.0 KiB
PHP
170 lines
5.0 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: WooCommerce Business Central Integration
|
|
* Plugin URI: https://example.com/woo-business-central
|
|
* Description: Syncs stock, pricing, orders, and customers between WooCommerce and Microsoft Dynamics 365 Business Central.
|
|
* Version: 1.0.0
|
|
* Author: Your Name
|
|
* Author URI: https://example.com
|
|
* License: GPL-2.0+
|
|
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
|
* Text Domain: woo-business-central
|
|
* Domain Path: /languages
|
|
* Requires at least: 5.8
|
|
* Requires PHP: 7.4
|
|
* WC requires at least: 5.0
|
|
* WC tested up to: 8.0
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
// Plugin constants
|
|
define( 'WBC_VERSION', '1.0.0' );
|
|
define( 'WBC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
|
|
define( 'WBC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
|
|
define( 'WBC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
|
|
|
|
/**
|
|
* Check if WooCommerce is active
|
|
*/
|
|
function wbc_check_woocommerce() {
|
|
if ( ! class_exists( 'WooCommerce' ) ) {
|
|
add_action( 'admin_notices', 'wbc_woocommerce_missing_notice' );
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Admin notice for missing WooCommerce
|
|
*/
|
|
function wbc_woocommerce_missing_notice() {
|
|
?>
|
|
<div class="notice notice-error">
|
|
<p><?php esc_html_e( 'WooCommerce Business Central Integration requires WooCommerce to be installed and active.', 'woo-business-central' ); ?></p>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Plugin activation hook
|
|
*/
|
|
function wbc_activate() {
|
|
// Check WooCommerce dependency
|
|
if ( ! class_exists( 'WooCommerce' ) ) {
|
|
deactivate_plugins( plugin_basename( __FILE__ ) );
|
|
wp_die(
|
|
esc_html__( 'WooCommerce Business Central Integration requires WooCommerce to be installed and active.', 'woo-business-central' ),
|
|
'Plugin dependency check',
|
|
array( 'back_link' => true )
|
|
);
|
|
}
|
|
|
|
// Create logs table
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-logger.php';
|
|
WBC_Logger::create_table();
|
|
|
|
// Set default options
|
|
$defaults = array(
|
|
'wbc_sync_frequency' => 'daily',
|
|
'wbc_enable_stock_sync' => 'yes',
|
|
'wbc_enable_price_sync' => 'yes',
|
|
'wbc_enable_order_sync' => 'yes',
|
|
'wbc_environment' => 'production',
|
|
);
|
|
|
|
foreach ( $defaults as $option => $value ) {
|
|
if ( get_option( $option ) === false ) {
|
|
add_option( $option, $value );
|
|
}
|
|
}
|
|
|
|
// Schedule cron event
|
|
if ( ! wp_next_scheduled( 'wbc_product_sync_event' ) ) {
|
|
$frequency = get_option( 'wbc_sync_frequency', 'daily' );
|
|
wp_schedule_event( time(), $frequency, 'wbc_product_sync_event' );
|
|
}
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
}
|
|
register_activation_hook( __FILE__, 'wbc_activate' );
|
|
|
|
/**
|
|
* Plugin deactivation hook
|
|
*/
|
|
function wbc_deactivate() {
|
|
// Clear scheduled cron events
|
|
$timestamp = wp_next_scheduled( 'wbc_product_sync_event' );
|
|
if ( $timestamp ) {
|
|
wp_unschedule_event( $timestamp, 'wbc_product_sync_event' );
|
|
}
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
}
|
|
register_deactivation_hook( __FILE__, 'wbc_deactivate' );
|
|
|
|
/**
|
|
* Load plugin text domain
|
|
*/
|
|
function wbc_load_textdomain() {
|
|
load_plugin_textdomain( 'woo-business-central', false, dirname( WBC_PLUGIN_BASENAME ) . '/languages' );
|
|
}
|
|
add_action( 'plugins_loaded', 'wbc_load_textdomain' );
|
|
|
|
/**
|
|
* Initialize the plugin
|
|
*/
|
|
function wbc_init() {
|
|
// Check WooCommerce dependency
|
|
if ( ! wbc_check_woocommerce() ) {
|
|
return;
|
|
}
|
|
|
|
// Load required files
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-loader.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-logger.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-oauth.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-api-client.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-product-sync.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-batch-sync.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-customer-sync.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-order-sync.php';
|
|
require_once WBC_PLUGIN_DIR . 'includes/class-wbc-cron.php';
|
|
|
|
// Load admin files
|
|
if ( is_admin() ) {
|
|
require_once WBC_PLUGIN_DIR . 'admin/class-wbc-admin.php';
|
|
}
|
|
|
|
// Initialize the loader
|
|
$loader = new WBC_Loader();
|
|
$loader->run();
|
|
}
|
|
add_action( 'plugins_loaded', 'wbc_init', 20 );
|
|
|
|
/**
|
|
* Add custom cron schedules
|
|
*/
|
|
function wbc_cron_schedules( $schedules ) {
|
|
$schedules['twice_daily'] = array(
|
|
'interval' => 12 * HOUR_IN_SECONDS,
|
|
'display' => __( 'Twice Daily', 'woo-business-central' ),
|
|
);
|
|
return $schedules;
|
|
}
|
|
add_filter( 'cron_schedules', 'wbc_cron_schedules' );
|
|
|
|
/**
|
|
* HPOS compatibility declaration
|
|
*/
|
|
add_action( 'before_woocommerce_init', function() {
|
|
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
|
|
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
|
|
}
|
|
} );
|