PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.2
Elementor Website Builder – more than just a page builder v3.13.2
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.2, at modules/ai/module.php

254 lines 6.3 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 add_action( 'elementor/editor/after_enqueue_styles', function() {
61 wp_enqueue_style(
62 'elementor-ai',
63 $this->get_css_assets_url( 'modules/ai/editor' ),
64 [],
65 ELEMENTOR_VERSION
66 );
67 } );
68 }
69
70 public function ajax_ai_get_user_information( $data ) {
71 $app = $this->get_ai_app();
72
73 if ( ! $app->is_connected() ) {
74 return [
75 'is_connected' => false,
76 'connect_url' => $app->get_admin_url( 'authorize', [
77 'utm_source' => 'ai-popup',
78 'utm_campaign' => 'connect-account',
79 'utm_medium' => 'wp-dash',
80 'source' => 'generic',
81 ] ),
82 ];
83 }
84
85 $user_usage = wp_parse_args( $app->get_usage(), [
86 'hasAiSubscription' => false,
87 'usedQuota' => 0,
88 'quota' => 100,
89 ] );
90
91 return [
92 'is_connected' => true,
93 'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
94 'usage' => $user_usage,
95 ];
96 }
97
98 private function verify_permissions( $editor_post_id ) {
99 $document = Plugin::$instance->documents->get( $editor_post_id );
100
101 if ( ! $document ) {
102 throw new \Exception( 'Document not found' );
103 }
104
105 if ( ! $document->is_built_with_elementor() || ! $document->is_editable_by_current_user() ) {
106 throw new \Exception( 'Access denied' );
107 }
108 }
109
110 public function ajax_ai_get_completion_text( $data ) {
111 $this->verify_permissions( $data['editor_post_id'] );
112
113 $app = $this->get_ai_app();
114
115 if ( empty( $data['prompt'] ) ) {
116 throw new \Exception( 'Missing prompt' );
117 }
118
119 if ( ! $app->is_connected() ) {
120 throw new \Exception( 'not_connected' );
121 }
122
123 $result = $app->get_completion_text( $data['prompt'] );
124 if ( is_wp_error( $result ) ) {
125 throw new \Exception( $result->get_error_message() );
126 }
127
128 return [
129 'text' => $result['text'],
130 'response_id' => $result['responseId'],
131 'usage' => $result['usage'],
132 ];
133 }
134
135 private function get_ai_app() : Ai {
136 return Plugin::$instance->common->get_component( 'connect' )->get_app( 'ai' );
137 }
138
139 public function ajax_ai_get_edit_text( $data ) {
140 $this->verify_permissions( $data['editor_post_id'] );
141
142 $app = $this->get_ai_app();
143
144 if ( empty( $data['input'] ) ) {
145 throw new \Exception( 'Missing input' );
146 }
147
148 if ( empty( $data['instruction'] ) ) {
149 throw new \Exception( 'Missing instruction' );
150 }
151
152 if ( ! $app->is_connected() ) {
153 throw new \Exception( 'not_connected' );
154 }
155
156 $result = $app->get_edit_text( $data['input'], $data['instruction'] );
157 if ( is_wp_error( $result ) ) {
158 throw new \Exception( $result->get_error_message() );
159 }
160
161 return [
162 'text' => $result['text'],
163 'response_id' => $result['responseId'],
164 'usage' => $result['usage'],
165 ];
166 }
167
168 public function ajax_ai_get_custom_code( $data ) {
169 $app = $this->get_ai_app();
170
171 if ( empty( $data['prompt'] ) ) {
172 throw new \Exception( 'Missing prompt' );
173 }
174
175 if ( empty( $data['language'] ) ) {
176 throw new \Exception( 'Missing language' );
177 }
178
179 if ( ! $app->is_connected() ) {
180 throw new \Exception( 'not_connected' );
181 }
182
183 $result = $app->get_custom_code( $data['prompt'], $data['language'] );
184 if ( is_wp_error( $result ) ) {
185 throw new \Exception( $result->get_error_message() );
186 }
187
188 return [
189 'text' => $result['text'],
190 'response_id' => $result['responseId'],
191 'usage' => $result['usage'],
192 ];
193 }
194
195 public function ajax_ai_get_custom_css( $data ) {
196 $this->verify_permissions( $data['editor_post_id'] );
197
198 $app = $this->get_ai_app();
199
200 if ( empty( $data['prompt'] ) ) {
201 throw new \Exception( 'Missing prompt' );
202 }
203
204 if ( empty( $data['html_markup'] ) ) {
205 $data['html_markup'] = '';
206 }
207
208 if ( empty( $data['element_id'] ) ) {
209 throw new \Exception( 'Missing element_id' );
210 }
211
212 if ( ! $app->is_connected() ) {
213 throw new \Exception( 'not_connected' );
214 }
215
216 $result = $app->get_custom_css( $data['prompt'], $data['html_markup'], $data['element_id'] );
217 if ( is_wp_error( $result ) ) {
218 throw new \Exception( $result->get_error_message() );
219 }
220
221 return [
222 'text' => $result['text'],
223 'response_id' => $result['responseId'],
224 'usage' => $result['usage'],
225 ];
226 }
227
228 public function ajax_ai_set_get_started( $data ) {
229 $app = $this->get_ai_app();
230
231 User::set_introduction_viewed( [
232 'introductionKey' => 'ai_get_started',
233 ] );
234
235 return $app->set_get_started();
236 }
237
238 public function ajax_ai_set_status_feedback( $data ) {
239 if ( empty( $data['response_id'] ) ) {
240 throw new \Exception( 'Missing response_id' );
241 }
242
243 $app = $this->get_ai_app();
244
245 if ( ! $app->is_connected() ) {
246 throw new \Exception( 'not_connected' );
247 }
248
249 $app->set_status_feedback( $data['response_id'] );
250
251 return [];
252 }
253 }
254