| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Ai\Connect; |
| 3 |
|
| 4 |
use Elementor\Core\Common\Modules\Connect\Apps\Library; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
class Ai extends Library { |
| 11 |
const API_URL = 'https://my.elementor.com/api/v2/ai/'; |
| 12 |
|
| 13 |
public function get_title() { |
| 14 |
return esc_html__( 'AI', 'elementor' ); |
| 15 |
} |
| 16 |
|
| 17 |
protected function get_api_url() { |
| 18 |
return static::API_URL . '/'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_usage() { |
| 22 |
return $this->ai_request( |
| 23 |
'POST', |
| 24 |
'status/check', |
| 25 |
[ |
| 26 |
'api_version' => ELEMENTOR_VERSION, |
| 27 |
'site_lang' => get_bloginfo( 'language' ), |
| 28 |
] |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
private function ai_request( $method, $endpoint, $body ) { |
| 33 |
return $this->http_request( |
| 34 |
$method, |
| 35 |
$endpoint, |
| 36 |
[ |
| 37 |
'timeout' => 25, |
| 38 |
'headers' => [ |
| 39 |
'x-elementor-ai-version' => '2', |
| 40 |
], |
| 41 |
'body' => $body, |
| 42 |
], |
| 43 |
[ |
| 44 |
'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 45 |
] |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
public function set_get_started() { |
| 50 |
return $this->ai_request( |
| 51 |
'POST', |
| 52 |
'status/get-started', |
| 53 |
[ |
| 54 |
'api_version' => ELEMENTOR_VERSION, |
| 55 |
'site_lang' => get_bloginfo( 'language' ), |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function set_status_feedback( $response_id ) { |
| 61 |
return $this->ai_request( |
| 62 |
'POST', |
| 63 |
'status/feedback/' . $response_id, |
| 64 |
[ |
| 65 |
'api_version' => ELEMENTOR_VERSION, |
| 66 |
'site_lang' => get_bloginfo( 'language' ), |
| 67 |
] |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
public function get_completion_text( $prompt ) { |
| 72 |
return $this->ai_request( |
| 73 |
'POST', |
| 74 |
'text/completion', |
| 75 |
[ |
| 76 |
'prompt' => $prompt, |
| 77 |
'api_version' => ELEMENTOR_VERSION, |
| 78 |
'site_lang' => get_bloginfo( 'language' ), |
| 79 |
] |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
public function get_edit_text( $input, $instruction ) { |
| 84 |
return $this->ai_request( |
| 85 |
'POST', |
| 86 |
'text/edit', |
| 87 |
[ |
| 88 |
'input' => $input, |
| 89 |
'instruction' => $instruction, |
| 90 |
'api_version' => ELEMENTOR_VERSION, |
| 91 |
'site_lang' => get_bloginfo( 'language' ), |
| 92 |
] |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
public function get_custom_code( $prompt, $language ) { |
| 97 |
return $this->ai_request( |
| 98 |
'POST', |
| 99 |
'text/custom-code', |
| 100 |
[ |
| 101 |
'prompt' => $prompt, |
| 102 |
'language' => $language, |
| 103 |
'api_version' => ELEMENTOR_VERSION, |
| 104 |
'site_lang' => get_bloginfo( 'language' ), |
| 105 |
] |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
public function get_custom_css( $prompt, $html_markup, $element_id ) { |
| 110 |
return $this->ai_request( |
| 111 |
'POST', |
| 112 |
'text/custom-css', |
| 113 |
[ |
| 114 |
'prompt' => $prompt, |
| 115 |
'html_markup' => $html_markup, |
| 116 |
'element_id' => $element_id, |
| 117 |
'api_version' => ELEMENTOR_VERSION, |
| 118 |
'site_lang' => get_bloginfo( 'language' ), |
| 119 |
] |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
protected function init() {} |
| 124 |
} |
| 125 |
|