PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.1
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.1
8.8.1 8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 All 537 releases
chatbot / addons / automator / includes / actions / webhook-actions.php

webhook-actions.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.8.1, at addons/automator/includes/actions/webhook-actions.php

129 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Webhook Actions
4 *
5 * @package WPbot_Automator
6 */
7
8 namespace WPbot_Automator\Actions;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class Webhook_Actions
16 */
17 class Webhook_Actions extends Action {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 $this->id = 'webhook';
24 $this->group = __( 'Webhook', 'wpbot-automator' );
25 }
26
27 /**
28 * Execute the action
29 */
30 public function execute( $action_data, $trigger_data ) {
31 $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : '';
32
33 if ( empty( $action_id ) ) {
34 return false;
35 }
36
37 switch ( $action_id ) {
38 case 'outgoing_webhook':
39 return $this->outgoing_webhook( $action_data, $trigger_data );
40 default:
41 return false;
42 }
43 }
44
45 /**
46 * Send outgoing webhook
47 */
48 private function outgoing_webhook( $config, $trigger_data ) {
49 $config_values = isset( $config['config'] ) ? $config['config'] : array();
50
51 $url = isset( $config_values['url'] ) ? $this->parse_tokens( $config_values['url'], $trigger_data ) : '';
52 $method = isset( $config_values['method'] ) ? $config_values['method'] : 'POST';
53 $body = isset( $config_values['body'] ) ? $this->parse_tokens( $config_values['body'], $trigger_data ) : '';
54
55 if ( empty( $url ) ) {
56 return array(
57 'success' => false,
58 'message' => 'Request URL is required.',
59 );
60 }
61
62 // Prepare headers
63 $headers = array();
64 if ( ! empty( $config_values['headers'] ) ) {
65 $headers_data = $config_values['headers'];
66 if ( is_string( $headers_data ) ) {
67 $headers_data = json_decode( $headers_data, true );
68 }
69 if ( is_array( $headers_data ) ) {
70 foreach ( $headers_data as $pair ) {
71 if ( ! empty( $pair['key'] ) ) {
72 $headers[ $pair['key'] ] = $this->parse_tokens( $pair['value'], $trigger_data );
73 }
74 }
75 }
76 }
77
78 // Set default Content-Type if not provided and body is present
79 if ( ! isset( $headers['Content-Type'] ) && ! empty( $body ) ) {
80 $headers['Content-Type'] = 'application/json';
81 }
82
83 // Prepare Query Params
84 if ( ! empty( $config_values['params'] ) ) {
85 $params_data = $config_values['params'];
86 if ( is_string( $params_data ) ) {
87 $params_data = json_decode( $params_data, true );
88 }
89 if ( is_array( $params_data ) ) {
90 $query_args = array();
91 foreach ( $params_data as $pair ) {
92 if ( ! empty( $pair['key'] ) ) {
93 $query_args[ $pair['key'] ] = $this->parse_tokens( $pair['value'], $trigger_data );
94 }
95 }
96 if ( ! empty( $query_args ) ) {
97 $url = add_query_arg( $query_args, $url );
98 }
99 }
100 }
101
102 $args = array(
103 'method' => $method,
104 'headers' => $headers,
105 'body' => $body,
106 'timeout' => 30,
107 );
108
109 $response = wp_remote_request( $url, $args );
110
111 if ( is_wp_error( $response ) ) {
112 return array(
113 'success' => false,
114 'message' => $response->get_error_message(),
115 );
116 }
117
118 $response_code = wp_remote_retrieve_response_code( $response );
119 $response_body = wp_remote_retrieve_body( $response );
120
121 return array(
122 'success' => $response_code >= 200 && $response_code < 300,
123 'code' => $response_code,
124 'body' => $response_body,
125 'message' => sprintf( 'Response Code: %d', $response_code ),
126 );
127 }
128 }
129