container = $container; } public function init() { // Hook into WooCommerce product save/update add_action('woocommerce_update_product', [$this, 'on_product_updated'], 10, 2); add_action('woocommerce_new_product', [$this, 'on_product_updated'], 10, 2); // Hook into product deletion add_action('before_delete_post', [$this, 'on_product_deleted']); // Register async indexer hook add_action('awas_index_product_async', [$this, 'process_async_indexing']); // Clear caches when terms update add_action('edited_term', [$this, 'clear_search_transients'], 10, 3); } /** * Triggered when a product is created or updated. * * @param int $product_id * @param \WC_Product $product */ public function on_product_updated($product_id, $product) { $settings = get_option('awas_settings', []); if (!empty($settings['performance']['background']) && function_exists('as_enqueue_async_action')) { // Background Indexing using Action Scheduler as_enqueue_async_action('awas_index_product_async', ['product_id' => $product_id]); } else { // Synchronous indexing fallback $indexer = $this->container->get(Indexer::class); $indexer->index_product($product_id); } $this->clear_search_transients(); } /** * Background worker for indexing */ public function process_async_indexing($product_id) { $indexer = $this->container->get(Indexer::class); $indexer->index_product($product_id); } /** * Triggered before a post is deleted. * * @param int $post_id */ public function on_product_deleted($post_id) { if (get_post_type($post_id) === 'product') { $indexer = $this->container->get(Indexer::class); $indexer->remove_product($post_id); $this->clear_search_transients(); } } /** * Clears all AWAS search transients */ public function clear_search_transients() { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_awas_search_%' OR option_name LIKE '_transient_timeout_awas_search_%'"); } }