PluginProbe
Getwid – Gutenberg Blocks / 2.0.7
Getwid – Gutenberg Blocks v2.0.7
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / ai / request.php

request.php in Getwid – Gutenberg Blocks 2.0.7, at includes/ai/request.php

98 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid\AI;
4
5 use Exception;
6 use WP_Error;
7 use WP_User;
8
9 final class AIRequest {
10
11 public function stream( $url, $params ) {
12
13 $current_user = wp_get_current_user();
14
15 if ( ! ( $current_user instanceof WP_User ) ) {
16 return rest_ensure_response(
17 new WP_Error(
18 'invalid_user',
19 esc_html__( "Current user must be a valid instance of WP_User.", 'getwid' ),
20 array(
21 'status' => 400
22 )
23 )
24 );
25 }
26
27 if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_setopt' ) ) {
28
29 return rest_ensure_response(
30 new WP_Error(
31 'curl_missing',
32 esc_html__( "cURL support is required, but it can not be found.", 'getwid' ),
33 array(
34 'status' => 500
35 )
36 )
37 );
38 }
39
40 try {
41
42 header('Cache-Control: no-cache, must-revalidate');
43 header('Connection: keep-alive');
44 header('X-Accel-Buffering: no');
45
46 set_time_limit(400);
47
48 $ch = curl_init();
49 curl_setopt( $ch, CURLOPT_URL, $url );
50 curl_setopt( $ch, CURLOPT_POST, 1 );
51 curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
52 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 300 );
53 curl_setopt( $ch, CURLOPT_HTTPHEADER, [
54 'Getwid-Client-Email: ' . $current_user->user_email,
55 'Getwid-AI-API-Version: 1.0'
56 ] );
57 curl_setopt( $ch, CURLOPT_HEADERFUNCTION, function ( $curl, $header ) {
58
59 if ( strpos( $header, 'Content-Type:' ) === 0 ) {
60 header( $header );
61 }
62
63 if ( strpos( $header, "HTTP/" ) === 0 ) {
64 $statusCode = intval( explode( " ", $header )[1] );
65 http_response_code( $statusCode );
66 }
67
68 return strlen( $header );
69 } );
70 curl_setopt( $ch, CURLOPT_WRITEFUNCTION, function ( $curl, $data ) {
71
72 echo $data;
73
74 flush();
75 ob_flush();
76
77 return strlen( $data );
78 });
79 curl_exec( $ch );
80 curl_close( $ch );
81
82 } catch ( Exception $e ) {
83
84 return rest_ensure_response(
85 new WP_Error(
86 'server_error',
87 esc_html__( "An error occurred when making a request to the Getwid AI server.", 'getwid' ),
88 array(
89 'status' => 500
90 )
91 )
92 );
93 }
94
95 die();
96 }
97 }
98