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>
174 lines
4.6 KiB
PHP
174 lines
4.6 KiB
PHP
<?php
|
|
/**
|
|
* Cron Scheduler for WooCommerce Business Central Integration
|
|
*
|
|
* @package WooBusinessCentral
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class WBC_Cron
|
|
*
|
|
* Handles scheduled tasks for syncing with Business Central.
|
|
*/
|
|
class WBC_Cron {
|
|
|
|
/**
|
|
* Cron event name
|
|
*/
|
|
const SYNC_EVENT = 'wbc_product_sync_event';
|
|
|
|
/**
|
|
* Cleanup event name
|
|
*/
|
|
const CLEANUP_EVENT = 'wbc_log_cleanup_event';
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct() {
|
|
// Register cleanup cron if not scheduled
|
|
if ( ! wp_next_scheduled( self::CLEANUP_EVENT ) ) {
|
|
wp_schedule_event( time(), 'daily', self::CLEANUP_EVENT );
|
|
}
|
|
|
|
// Hook the cleanup handler
|
|
add_action( self::CLEANUP_EVENT, array( $this, 'run_log_cleanup' ) );
|
|
}
|
|
|
|
/**
|
|
* Run scheduled product sync
|
|
*/
|
|
public function run_scheduled_sync() {
|
|
WBC_Logger::info( 'Cron', 'Starting scheduled product sync' );
|
|
|
|
$product_sync = new WBC_Product_Sync();
|
|
$result = $product_sync->run_sync();
|
|
|
|
if ( isset( $result['success'] ) && $result['success'] ) {
|
|
WBC_Logger::info( 'Cron', 'Scheduled product sync completed', $result );
|
|
} else {
|
|
WBC_Logger::error( 'Cron', 'Scheduled product sync failed', $result );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Run log cleanup
|
|
*/
|
|
public function run_log_cleanup() {
|
|
$deleted = WBC_Logger::cleanup_old_logs( 30 );
|
|
WBC_Logger::info( 'Cron', 'Log cleanup completed', array( 'deleted_count' => $deleted ) );
|
|
}
|
|
|
|
/**
|
|
* Reschedule the sync event with a new frequency
|
|
*
|
|
* @param string $frequency New frequency (hourly, twice_daily, daily).
|
|
*/
|
|
public static function reschedule_sync( $frequency ) {
|
|
// Clear existing schedule
|
|
$timestamp = wp_next_scheduled( self::SYNC_EVENT );
|
|
if ( $timestamp ) {
|
|
wp_unschedule_event( $timestamp, self::SYNC_EVENT );
|
|
}
|
|
|
|
// Schedule with new frequency
|
|
wp_schedule_event( time(), $frequency, self::SYNC_EVENT );
|
|
|
|
WBC_Logger::info( 'Cron', 'Sync schedule updated', array( 'frequency' => $frequency ) );
|
|
}
|
|
|
|
/**
|
|
* Get next scheduled sync time
|
|
*
|
|
* @return int|false Timestamp of next scheduled sync or false if not scheduled.
|
|
*/
|
|
public static function get_next_sync_time() {
|
|
return wp_next_scheduled( self::SYNC_EVENT );
|
|
}
|
|
|
|
/**
|
|
* Get next scheduled sync time formatted
|
|
*
|
|
* @return string Formatted date/time string or 'Not scheduled'.
|
|
*/
|
|
public static function get_next_sync_formatted() {
|
|
$next = self::get_next_sync_time();
|
|
|
|
if ( ! $next ) {
|
|
return __( 'Not scheduled', 'woo-business-central' );
|
|
}
|
|
|
|
return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $next );
|
|
}
|
|
|
|
/**
|
|
* Get last sync time
|
|
*
|
|
* @return string|false Last sync time or false if never synced.
|
|
*/
|
|
public static function get_last_sync_time() {
|
|
return get_option( 'wbc_last_sync_time', false );
|
|
}
|
|
|
|
/**
|
|
* Get last sync time formatted
|
|
*
|
|
* @return string Formatted date/time string or 'Never'.
|
|
*/
|
|
public static function get_last_sync_formatted() {
|
|
$last = self::get_last_sync_time();
|
|
|
|
if ( ! $last ) {
|
|
return __( 'Never', 'woo-business-central' );
|
|
}
|
|
|
|
return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $last ) );
|
|
}
|
|
|
|
/**
|
|
* Update last sync time
|
|
*/
|
|
public static function update_last_sync_time() {
|
|
update_option( 'wbc_last_sync_time', current_time( 'mysql' ) );
|
|
}
|
|
|
|
/**
|
|
* Run sync now (manual trigger)
|
|
*
|
|
* @return array Sync result.
|
|
*/
|
|
public static function run_sync_now() {
|
|
WBC_Logger::info( 'Cron', 'Manual sync triggered' );
|
|
|
|
$product_sync = new WBC_Product_Sync();
|
|
$result = $product_sync->run_sync();
|
|
|
|
// Update last sync time
|
|
self::update_last_sync_time();
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Clear all scheduled events
|
|
*/
|
|
public static function clear_all_schedules() {
|
|
// Clear sync event
|
|
$sync_timestamp = wp_next_scheduled( self::SYNC_EVENT );
|
|
if ( $sync_timestamp ) {
|
|
wp_unschedule_event( $sync_timestamp, self::SYNC_EVENT );
|
|
}
|
|
|
|
// Clear cleanup event
|
|
$cleanup_timestamp = wp_next_scheduled( self::CLEANUP_EVENT );
|
|
if ( $cleanup_timestamp ) {
|
|
wp_unschedule_event( $cleanup_timestamp, self::CLEANUP_EVENT );
|
|
}
|
|
}
|
|
}
|