-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerfex_API_Form_Action.php
152 lines (124 loc) · 4.83 KB
/
Perfex_API_Form_Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
class Perfex_API_Form_Action extends \ElementorPro\Modules\Forms\Classes\Action_Base {
public function get_name() {return 'PerfexAPI';}
public function get_label() {return __( 'PerfexAPI', 'text-domain' );}
/**
* register elementor forms
* @param type $widget
* @return type
*/
public function register_settings_section( $widget ) {
$widget->start_controls_section(
'section_perfex_api',
[
'label' => __( 'PerfexAPI', 'text-domain' ),
'condition' => [
'submit_actions' => $this->get_name(),
],
]
);
$widget->add_control(
'perfex_endpoit_url',
[
'label' => __( 'End Poist Url *', 'text-domain' ),
'type' => \Elementor\Controls_Manager::URL,
'placeholder' => 'http://yourmauticurl.com/',
'label_block' => true,
'separator' => 'before',
'description' => __( 'Enter the ENDPOINT where you have API_Perfex installed', 'text-domain' ),
]
);
$widget->add_control(
'authtoken',
[
'label' => __('Token *', 'text-domain'),
'type' => \Elementor\Controls_Manager::TEXT,
'placeholder' => '99',
'label_block' => true,
'separator' => 'before',
'description' => __( 'Fill with your token API', 'text-domain' ),
]
);
$widget->end_controls_section();
}
public function run( $record, $ajax_handler ) {
$settings = $record->get( 'form_settings' );
// Make sure that there is a Mautic url
if ( empty( $settings['perfex_endpoit_url'] ) ) {
return;
}
// Get sumitetd Form data
$raw_fields = $record->get( 'fields' );
// Normalize the Form Data
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
// Caso queira add mais fields, e so colocar ai
// $fields = array("company"=>"OLA TEST OK", "phonenumber"=>"8596859001");
//
//Transforma os dados no Formato -> multipart/form-data aaaaaaaahhhhhh que chato do carai!!!!
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$data = '';
$eol = "\r\n";
foreach ($fields as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
. $content . $eol;
}
$data .= "--" . $delimiter . "--".$eol;
$post_data = $data;
//FIM do Transforma os dados no Formato -> multipart/form-data aaaaaaaahhhhhh que chato do carai!!!!
//Inicio do Envio dos dados transformados!
$url = rtrim($settings['perfex_endpoit_url']['url']);
$authtoken = $settings['authtoken'];
$options = [
'body' => $post_data,
'headers' => [
'Content-Type' => 'multipart/form-data; boundary=' . $delimiter,
'authtoken' => $authtoken
],
'timeout' => 30,
'redirection' => 5,
'blocking' => true,
'httpversion' => '1.0',
'sslverify' => false,
'data_format' => 'body',
];
$resp = wp_remote_post( $url, $options );
$body = wp_remote_retrieve_body( $resp );
$response_code = wp_remote_retrieve_response_code( $resp );
//Fim do Envio dos dados transformados!
//=====================> Start TESTE <======================================
$endpoint = "https://atendimento1.uatiz.app/uatizs/send-message";
$TOKEN= "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZF93cCI6IjEiLCJlbWFpbF93cCI6InkubS5saW1hMTlAZ21haWwuY29tIiwiaWF0IjoxNjM5NjIyODE2LCJleHAiOjE2NDQ4MDY4MTZ9.y5xFGQhYFcDtvWdXFi1LDUUHTLOJGJGvk8UHDrbpX0I";
$message = $body;
// $message = wp_json_encode( $body );
$body = [
'number' => '558596286998',
'message' => $message
];
$body = wp_json_encode( $body );
$options = [
'body' => $body,
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $TOKEN
],
'timeout' => 30,
'redirection' => 5,
'blocking' => true,
'httpversion' => '1.0',
'sslverify' => false,
'data_format' => 'body',
];
wp_remote_post( $endpoint, $options );
//=====================> End TESTE <======================================
// $message = preg_match('/<div class=\"well text-center\">(.*?)<\/div>/s', $response['body'], $match);
// echo trim(strip_tags($match[1]));
}
public function on_export( $element ) {
return $element;
}
}