$event, 'order_id' => $order_id, 'status' => $order->get_status(), 'total' => $order->get_total(), 'time' => current_time( 'c' ), ]; $vendors = []; foreach ( $order->get_items() as $item ) { $vid = (int) get_post_field( 'post_author', $item->get_product_id() ); if ( ! in_array( $vid, $vendors ) ) $vendors[] = $vid; } foreach ( $vendors as $vendor_id ) { $this->dispatch( $vendor_id, $event, $payload ); } } public function fire_stock( $vendor_id, $product_id ) { $payload = [ 'event' => 'low_stock', 'product_id' => $product_id, 'product' => get_the_title( $product_id ), 'quantity' => (int) get_post_meta( $product_id, '_stock', true ), 'time' => current_time( 'c' ), ]; $this->dispatch( $vendor_id, 'low_stock', $payload ); } private function dispatch( $vendor_id, $event, $payload ) { global $wpdb; $hooks = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}vv_webhooks WHERE vendor_id=%d AND event_type=%s AND is_active=1", $vendor_id, $event ) ); foreach ( $hooks as $hook ) { $json = wp_json_encode( $payload ); $sig = hash_hmac( 'sha256', $json, $hook->secret_key ); $response = wp_remote_post( $hook->url, [ 'timeout' => 5, 'headers' => [ 'Content-Type' => 'application/json', 'X-VendorVerse-Event' => $event, 'X-VV-Signature' => $sig, ], 'body' => $json, ] ); $wpdb->insert( "{$wpdb->prefix}vv_webhook_logs", [ 'webhook_id' => $hook->id, 'event_type' => $event, 'payload' => $json, 'response' => is_wp_error( $response ) ? $response->get_error_message() : wp_remote_retrieve_body( $response ), 'http_code' => is_wp_error( $response ) ? 0 : wp_remote_retrieve_response_code( $response ), ] ); } } }