PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.0-beta1
Elementor Website Builder – more than just a page builder v3.13.0-beta1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / ai / module.php

module.php in Elementor Website Builder – more than just a page builder 3.13.0-beta1, at modules/ai/module.php

243 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\Ai;
3
4 use Elementor\Core\Base\Module as BaseModule;
5 use Elementor\Core\Common\Modules\Connect\Module as ConnectModule;
6 use Elementor\Modules\Ai\Connect\Ai;
7 use Elementor\Plugin;
8 use Elementor\User;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13 class Module extends BaseModule {
14
15 public function get_name() {
16 return 'ai';
17 }
18
19 public function __construct() {
20 parent::__construct();
21
22 add_action( 'elementor/connect/apps/register', function ( ConnectModule $connect_module ) {
23 $connect_module->register_app( 'ai', Ai::get_class_name() );
24 } );
25
26 add_action( 'elementor/ajax/register_actions', function( $ajax ) {
27 $ajax->register_ajax_action( 'ai_get_user_information', [ $this, 'ajax_ai_get_user_information' ] );
28 $ajax->register_ajax_action( 'ai_get_completion_text', [ $this, 'ajax_ai_get_completion_text' ] );
29 $ajax->register_ajax_action( 'ai_get_edit_text', [ $this, 'ajax_ai_get_edit_text' ] );
30 $ajax->register_ajax_action( 'ai_get_custom_code', [ $this, 'ajax_ai_get_custom_code' ] );
31 $ajax->register_ajax_action( 'ai_get_custom_css', [ $this, 'ajax_ai_get_custom_css' ] );
32 $ajax->register_ajax_action( 'ai_set_get_started', [ $this, 'ajax_ai_set_get_started' ] );
33 $ajax->register_ajax_action( 'ai_set_status_feedback', [ $this, 'ajax_ai_set_status_feedback' ] );
34 } );
35
36 add_action( 'elementor/editor/before_enqueue_scripts', function() {
37 wp_enqueue_script(
38 'elementor-ai',
39 $this->get_js_assets_url( 'ai' ),
40 [
41 'elementor-common',
42 'elementor-editor-modules',
43 'elementor-editor-document',
44 'elementor-packages-ui',
45 'elementor-packages-icons',
46 ],
47 ELEMENTOR_VERSION,
48 true
49 );
50
51 wp_localize_script(
52 'elementor-ai',
53 'ElementorAiConfig',
54 [
55 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
56 ]
57 );
58 } );
59 }
60
61 public function ajax_ai_get_user_information( $data ) {
62 $app = $this->get_ai_app();
63
64 if ( ! $app->is_connected() ) {
65 return [
66 'is_connected' => false,
67 'connect_url' => $app->get_admin_url( 'authorize', [
68 'utm_source' => 'ai-popup',
69 'utm_campaign' => 'connect-account',
70 'utm_medium' => 'wp-dash',
71 'source' => 'generic',
72 ] ),
73 ];
74 }
75
76 return [
77 'is_connected' => true,
78 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
79 'usage' => $app->get_usage(),
80 ];
81 }
82
83 private function verify_permissions( $editor_post_id ) {
84 $document = Plugin::$instance->documents->get( $editor_post_id );
85
86 if ( ! $document ) {
87 throw new \Exception( 'Document not found' );
88 }
89
90 if ( ! $document->is_built_with_elementor() || ! $document->is_editable_by_current_user() ) {
91 throw new \Exception( 'Access denied' );
92 }
93 }
94
95 public function ajax_ai_get_completion_text( $data ) {
96 $this->verify_permissions( $data['editor_post_id'] );
97
98 $app = $this->get_ai_app();
99
100 if ( empty( $data['prompt'] ) ) {
101 throw new \Exception( 'Missing prompt' );
102 }
103
104 if ( ! $app->is_connected() ) {
105 throw new \Exception( 'not_connected' );
106 }
107
108 $result = $app->get_completion_text( $data['prompt'] );
109 if ( is_wp_error( $result ) ) {
110 throw new \Exception( $result->get_error_message() );
111 }
112
113 return [
114 'text' => $result['text'],
115 'response_id' => $result['responseId'],
116 'usage' => $result['usage'],
117 ];
118 }
119
120 private function get_ai_app() : Ai {
121 return Plugin::$instance->common->get_component( 'connect' )->get_app( 'ai' );
122 }
123
124 public function ajax_ai_get_edit_text( $data ) {
125 $this->verify_permissions( $data['editor_post_id'] );
126
127 $app = $this->get_ai_app();
128
129 if ( empty( $data['input'] ) ) {
130 throw new \Exception( 'Missing input' );
131 }
132
133 if ( empty( $data['instruction'] ) ) {
134 throw new \Exception( 'Missing instruction' );
135 }
136
137 if ( ! $app->is_connected() ) {
138 throw new \Exception( 'not_connected' );
139 }
140
141 $result = $app->get_edit_text( $data['input'], $data['instruction'] );
142 if ( is_wp_error( $result ) ) {
143 throw new \Exception( $result->get_error_message() );
144 }
145
146 return [
147 'text' => $result['text'],
148 'response_id' => $result['responseId'],
149 'usage' => $result['usage'],
150 ];
151 }
152
153 public function ajax_ai_get_custom_code( $data ) {
154 $app = $this->get_ai_app();
155
156 if ( empty( $data['prompt'] ) ) {
157 throw new \Exception( 'Missing prompt' );
158 }
159
160 if ( empty( $data['language'] ) ) {
161 throw new \Exception( 'Missing language' );
162 }
163
164 if ( ! $app->is_connected() ) {
165 throw new \Exception( 'not_connected' );
166 }
167
168 $result = $app->get_custom_code( $data['prompt'], $data['language'] );
169 if ( is_wp_error( $result ) ) {
170 throw new \Exception( $result->get_error_message() );
171 }
172
173 return [
174 'text' => $result['text'],
175 'response_id' => $result['responseId'],
176 'usage' => $result['usage'],
177 ];
178 }
179
180 public function ajax_ai_get_custom_css( $data ) {
181 $this->verify_permissions( $data['editor_post_id'] );
182
183 $app = $this->get_ai_app();
184
185 if ( empty( $data['prompt'] ) ) {
186 throw new \Exception( 'Missing prompt' );
187 }
188
189 if ( empty( $data['html_markup'] ) ) {
190 throw new \Exception( 'Missing html_markup' );
191 }
192
193 if ( empty( $data['element_id'] ) ) {
194 throw new \Exception( 'Missing element_id' );
195 }
196
197 if ( ! $app->is_connected() ) {
198 throw new \Exception( 'not_connected' );
199 }
200
201 $result = $app->get_custom_css( $data['prompt'], $data['html_markup'], $data['element_id'] );
202 if ( is_wp_error( $result ) ) {
203 throw new \Exception( $result->get_error_message() );
204 }
205
206 return [
207 'text' => $result['text'],
208 'response_id' => $result['responseId'],
209 'usage' => $result['usage'],
210 ];
211 }
212
213 public function ajax_ai_set_get_started( $data ) {
214 $this->verify_permissions( $data['editor_post_id'] );
215
216 $app = $this->get_ai_app();
217
218 User::set_introduction_viewed( [
219 'introductionKey' => 'ai_get_started',
220 ] );
221
222 return $app->set_get_started();
223 }
224
225 public function ajax_ai_set_status_feedback( $data ) {
226 $this->verify_permissions( $data['editor_post_id'] );
227
228 if ( empty( $data['response_id'] ) ) {
229 throw new \Exception( 'Missing response_id' );
230 }
231
232 $app = $this->get_ai_app();
233
234 if ( ! $app->is_connected() ) {
235 throw new \Exception( 'not_connected' );
236 }
237
238 $app->set_status_feedback( $data['response_id'] );
239
240 return [];
241 }
242 }
243