includes();
$this->init_hooks();
}
/**
* Include required files.
*/
private function includes() {
// Include core classes.
require_once ANFOWO_PLUGIN_DIR . 'includes/class-loader.php';
ANFOWO_Loader::load_classes();
}
/**
* Hook into actions and filters.
*/
private function init_hooks() {
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) );
// Declare HPOS (High-Performance Order Storage) compatibility (WC 7.1+).
add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );
}
/**
* Declare compatibility with WooCommerce HPOS (Custom Order Tables).
* Required for WooCommerce 7.1+ to avoid compatibility warnings in the admin.
*/
public function declare_hpos_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
'custom_order_tables',
__FILE__,
true
);
}
}
/**
* Run when all plugins are loaded.
*/
public function on_plugins_loaded() {
if ( ! class_exists( 'WooCommerce' ) ) {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
return;
}
// Verify minimum WooCommerce version (7.0).
if ( version_compare( WC_VERSION, '7.0', '<' ) ) {
add_action( 'admin_notices', array( $this, 'woocommerce_version_notice' ) );
return;
}
// Initialize REST API, Admin Menu, etc.
if ( is_admin() ) {
new ANFOWO_Admin_Menu();
new ANFOWO_Enqueue();
new ANFOWO_AJAX_Handler();
}
add_action( 'rest_api_init', array( 'ANFOWO_REST_API', 'register_routes' ) );
// Handle Exports via AJAX action.
add_action( 'wp_ajax_anfowo_export_csv', array( 'ANFOWO_CSV_Export', 'handle_download' ) );
}
/**
* Display notice when WooCommerce is missing.
*/
public function woocommerce_missing_notice() {
?>
200,
'status' => $custom_statuses,
'orderby' => 'date',
'order' => 'DESC',
)
);
// Sync the orders immediately.
foreach ( $orders as $order ) {
anfowo_sync_order_stats_realtime( $order->get_id(), true );
}
}
// Real-time synchronization hooks for immediate analytics updates on the overview/sales evolution graphs.
add_action( 'woocommerce_new_order', 'anfowo_sync_order_stats_realtime', 20, 1 );
add_action( 'woocommerce_update_order', 'anfowo_sync_order_stats_realtime', 20, 1 );
add_action( 'woocommerce_order_status_changed', 'anfowo_sync_order_status_changed_realtime', 20, 3 );
add_action( 'woocommerce_trash_order', 'anfowo_delete_order_stats_realtime', 20, 1 );
add_action( 'woocommerce_delete_order', 'anfowo_delete_order_stats_realtime', 20, 1 );
/**
* Instantly syncs order stats to database when order is created or updated.
*
* @param int $order_id Order ID.
*/
function anfowo_sync_order_stats_realtime( $order_id, $force_immediate = false ) {
static $synced_orders = array();
if ( ! $order_id ) {
return;
}
if ( is_object( $order_id ) && method_exists( $order_id, 'get_id' ) ) {
$order_id = $order_id->get_id();
}
$order_id = (int) $order_id;
if ( ! $order_id ) {
return;
}
if ( isset( $synced_orders[ $order_id ] ) && ! $force_immediate ) {
return;
}
$synced_orders[ $order_id ] = true;
$sync_fn = function() use ( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order || ! $order->get_date_created() ) {
return; // Avoid fatal error if order date is missing or null
}
try {
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore::sync_order( $order_id );
}
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Products\DataStore' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Products\DataStore::sync_order_products( $order_id );
}
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore::sync_order_coupons( $order_id );
}
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore::sync_order_taxes( $order_id );
}
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::sync_order_customer( $order_id );
}
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Cache' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Cache::invalidate();
}
} catch ( \Throwable $t ) {
// Catch any errors during sync to avoid breaking site runtime
}
};
if ( $force_immediate ) {
$sync_fn();
} else {
// Defer to PHP shutdown to guarantee that all line items and order meta are saved to DB.
add_action( 'shutdown', $sync_fn );
}
}
/**
* Instantly syncs order stats to database when order status changes.
*
* @param int $order_id Order ID.
* @param string $old_status Old order status.
* @param string $new_status New order status.
*/
function anfowo_sync_order_status_changed_realtime( $order_id, $old_status, $new_status ) {
anfowo_sync_order_stats_realtime( $order_id );
}
/**
* Instantly removes order stats from database when order is trashed or deleted.
*
* @param int $order_id Order ID.
*/
function anfowo_delete_order_stats_realtime( $order_id ) {
static $deleted_orders = array();
if ( ! $order_id ) {
return;
}
if ( is_object( $order_id ) && method_exists( $order_id, 'get_id' ) ) {
$order_id = $order_id->get_id();
}
$order_id = (int) $order_id;
if ( ! $order_id || isset( $deleted_orders[ $order_id ] ) ) {
return;
}
$deleted_orders[ $order_id ] = true;
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore::delete_order( $order_id );
}
if ( class_exists( 'Automattic\WooCommerce\Admin\API\Reports\Cache' ) ) {
\Automattic\WooCommerce\Admin\API\Reports\Cache::invalidate();
}
}