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>
153 lines
6.1 KiB
PHP
153 lines
6.1 KiB
PHP
<?php
|
|
/**
|
|
* Register all actions and filters for the plugin
|
|
*
|
|
* @package WooBusinessCentral
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class WBC_Loader
|
|
*
|
|
* Maintains lists of all hooks registered throughout the plugin
|
|
* and registers them with WordPress.
|
|
*/
|
|
class WBC_Loader {
|
|
|
|
/**
|
|
* Array of actions registered with WordPress
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $actions = array();
|
|
|
|
/**
|
|
* Array of filters registered with WordPress
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $filters = array();
|
|
|
|
/**
|
|
* Initialize the loader
|
|
*/
|
|
public function __construct() {
|
|
$this->actions = array();
|
|
$this->filters = array();
|
|
}
|
|
|
|
/**
|
|
* Add a new action to the collection
|
|
*
|
|
* @param string $hook The name of the WordPress action.
|
|
* @param object $component A reference to the instance of the object on which the action is defined.
|
|
* @param string $callback The name of the function definition on the $component.
|
|
* @param int $priority Optional. The priority at which the function should be fired. Default 10.
|
|
* @param int $accepted_args Optional. The number of arguments that should be passed to the callback. Default 1.
|
|
*/
|
|
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
|
|
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
|
|
}
|
|
|
|
/**
|
|
* Add a new filter to the collection
|
|
*
|
|
* @param string $hook The name of the WordPress filter.
|
|
* @param object $component A reference to the instance of the object on which the filter is defined.
|
|
* @param string $callback The name of the function definition on the $component.
|
|
* @param int $priority Optional. The priority at which the function should be fired. Default 10.
|
|
* @param int $accepted_args Optional. The number of arguments that should be passed to the callback. Default 1.
|
|
*/
|
|
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
|
|
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
|
|
}
|
|
|
|
/**
|
|
* A utility function for registering hooks into a single collection
|
|
*
|
|
* @param array $hooks The collection of hooks being registered.
|
|
* @param string $hook The name of the WordPress filter being registered.
|
|
* @param object $component A reference to the instance of the object on which the filter is defined.
|
|
* @param string $callback The name of the function definition on the $component.
|
|
* @param int $priority The priority at which the function should be fired.
|
|
* @param int $accepted_args The number of arguments that should be passed to the callback.
|
|
* @return array The collection of actions and filters registered with WordPress.
|
|
*/
|
|
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
|
|
$hooks[] = array(
|
|
'hook' => $hook,
|
|
'component' => $component,
|
|
'callback' => $callback,
|
|
'priority' => $priority,
|
|
'accepted_args' => $accepted_args,
|
|
);
|
|
return $hooks;
|
|
}
|
|
|
|
/**
|
|
* Register the filters and actions with WordPress
|
|
*/
|
|
public function run() {
|
|
// Register cron handler
|
|
$cron = new WBC_Cron();
|
|
$this->add_action( 'wbc_product_sync_event', $cron, 'run_scheduled_sync' );
|
|
|
|
// Register background batch sync handler (Action Scheduler)
|
|
$batch_sync = new WBC_Batch_Sync();
|
|
$batch_sync->register_hooks();
|
|
|
|
// Register order sync handler
|
|
$order_sync = new WBC_Order_Sync();
|
|
$this->add_action( 'woocommerce_order_status_processing', $order_sync, 'sync_order', 10, 1 );
|
|
$this->add_action( 'woocommerce_payment_complete', $order_sync, 'sync_order', 10, 1 );
|
|
|
|
// Register admin handler
|
|
if ( is_admin() ) {
|
|
$admin = new WBC_Admin();
|
|
$this->add_action( 'admin_menu', $admin, 'add_admin_menu' );
|
|
$this->add_action( 'admin_init', $admin, 'register_settings' );
|
|
$this->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_scripts' );
|
|
$this->add_action( 'wp_ajax_wbc_test_connection', $admin, 'ajax_test_connection' );
|
|
$this->add_action( 'wp_ajax_wbc_manual_sync', $admin, 'ajax_manual_sync' );
|
|
$this->add_action( 'wp_ajax_wbc_sync_status', $admin, 'ajax_sync_status' );
|
|
$this->add_action( 'wp_ajax_wbc_clear_logs', $admin, 'ajax_clear_logs' );
|
|
$this->add_action( 'wp_ajax_wbc_get_companies', $admin, 'ajax_get_companies' );
|
|
$this->add_action( 'admin_init', $admin, 'handle_csv_export' );
|
|
|
|
// On-demand push-to-BC (individual product + bulk action)
|
|
$this->add_action( 'add_meta_boxes', $admin, 'add_product_meta_box' );
|
|
$this->add_action( 'wp_ajax_wbc_push_single_to_bc', $admin, 'ajax_push_single_to_bc' );
|
|
$this->add_filter( 'bulk_actions-edit-product', $admin, 'add_bulk_action' );
|
|
$this->add_filter( 'handle_bulk_actions-edit-product', $admin, 'handle_bulk_action', 10, 3 );
|
|
$this->add_action( 'admin_notices', $admin, 'bulk_action_admin_notice' );
|
|
|
|
// Add settings link to plugins page
|
|
$this->add_filter( 'plugin_action_links_' . WBC_PLUGIN_BASENAME, $admin, 'add_settings_link' );
|
|
}
|
|
|
|
// Register all actions
|
|
foreach ( $this->actions as $hook ) {
|
|
add_action(
|
|
$hook['hook'],
|
|
array( $hook['component'], $hook['callback'] ),
|
|
$hook['priority'],
|
|
$hook['accepted_args']
|
|
);
|
|
}
|
|
|
|
// Register all filters
|
|
foreach ( $this->filters as $hook ) {
|
|
add_filter(
|
|
$hook['hook'],
|
|
array( $hook['component'], $hook['callback'] ),
|
|
$hook['priority'],
|
|
$hook['accepted_args']
|
|
);
|
|
}
|
|
}
|
|
}
|