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 / api-actions.php

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

124 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API 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 API_Actions
16 */
17 class API_Actions extends Action {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 $this->id = 'api';
24 $this->group = __( 'API', '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 'api_request':
39 return $this->api_request( $action_data, $trigger_data );
40 default:
41 return false;
42 }
43 }
44
45 /**
46 * Make an API request
47 */
48 private function api_request( $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'] : 'GET';
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' => 'API 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 // Prepare Query Params
79 if ( ! empty( $config_values['params'] ) ) {
80 $params_data = $config_values['params'];
81 if ( is_string( $params_data ) ) {
82 $params_data = json_decode( $params_data, true );
83 }
84 if ( is_array( $params_data ) ) {
85 $query_args = array();
86 foreach ( $params_data as $pair ) {
87 if ( ! empty( $pair['key'] ) ) {
88 $query_args[ $pair['key'] ] = $this->parse_tokens( $pair['value'], $trigger_data );
89 }
90 }
91 if ( ! empty( $query_args ) ) {
92 $url = add_query_arg( $query_args, $url );
93 }
94 }
95 }
96
97 $args = array(
98 'method' => $method,
99 'headers' => $headers,
100 'body' => $body,
101 'timeout' => 30,
102 );
103
104 $response = wp_remote_request( $url, $args );
105
106 if ( is_wp_error( $response ) ) {
107 return array(
108 'success' => false,
109 'message' => $response->get_error_message(),
110 );
111 }
112
113 $response_code = wp_remote_retrieve_response_code( $response );
114 $response_body = wp_remote_retrieve_body( $response );
115
116 return array(
117 'success' => $response_code >= 200 && $response_code < 300,
118 'code' => $response_code,
119 'body' => $response_body,
120 'message' => sprintf( 'Response Code: %d', $response_code ),
121 );
122 }
123 }
124