get_var( $wpdb->prepare( "SELECT webhook_id FROM %i WHERE topic = %s AND delivery_url = %s LIMIT 1", $wpdb->prefix . 'wc_webhooks', $topic, $delivery_url ) ); return $id ? (int) $id : 0; } /** * Install or update WooCommerce webhooks for wein.cc */ function weincc_install_webhooks(): void { if (!weincc_is_woocommerce_active()) { return; } $merchant_id = weincc_get_merchant_id(); $secret = weincc_get_secret(); if ($merchant_id === '' || $secret === '') { return; } weincc_load_webhook_class(); if (!class_exists('WC_Webhook')) { return; } $endpoint = defined('WEINCC_PRODUCT_ENDPOINT') ? WEINCC_PRODUCT_ENDPOINT : ''; if (empty($endpoint)) { return; } foreach (weincc_topics() as $topic) { $existing_id = weincc_find_webhook_id($topic, $endpoint); if ($existing_id > 0) { $webhook = new WC_Webhook($existing_id); $webhook->set_status('active'); $webhook->set_secret($secret); $webhook->save(); } else { $webhook = new WC_Webhook(); $webhook->set_name('wein.cc - ' . $topic); $webhook->set_topic($topic); $webhook->set_delivery_url($endpoint); $webhook->set_secret($secret); $webhook->set_status('active'); $webhook->set_user_id(get_current_user_id()); $webhook->save(); } } } /** * Remove WooCommerce webhooks for wein.cc (used in uninstall.php) */ function weincc_remove_webhooks(): void { if (!weincc_is_woocommerce_active()) { return; } $endpoint = defined('WEINCC_PRODUCT_ENDPOINT') ? WEINCC_PRODUCT_ENDPOINT : ''; if (empty($endpoint)) { return; } weincc_load_webhook_class(); foreach (weincc_topics() as $topic) { $webhook_id = weincc_find_webhook_id($topic, $endpoint); if ($webhook_id > 0 && class_exists('WC_Webhook')) { $webhook = new WC_Webhook($webhook_id); $webhook->delete(true); } } }