Files
WooBC/woo-business-central/includes/class-wbc-loader.php
Malin b64397dcd3 feat: WooCommerce Business Central integration plugin
Native PHP plugin (no Composer) that syncs:
- Product stock and pricing from BC to WooCommerce (scheduled cron)
- Orders from WooCommerce to BC (on payment received)
- Auto-creates customers in BC from WooCommerce billing data

Product matching: WooCommerce SKU → BC Item Number, fallback to GTIN (EAN).
OAuth2 client credentials auth with encrypted secret storage.
Admin settings page with connection test, manual sync, and log viewer.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:59:53 +01:00

141 lines
5.4 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 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_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' );
// 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']
);
}
}
}