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.1.6 1.6.86 0.1.7 1.6.87 0.1.8 1.6.88 0.1.9 1.6.89 0.2.0 1.6.90
ai-engine / classes / core.php
ai-engine / classes Last commit date
engines 1 year ago modules 1 year ago queries 1 year ago admin.php 2 years ago api.php 1 year ago core.php 1 year ago discussion.php 1 year ago init.php 2 years ago logging.php 1 year ago reply.php 1 year ago rest.php 1 year ago
core.php
1312 lines
1 <?php
2
3 require_once( MWAI_PATH . '/vendor/autoload.php' );
4 require_once( MWAI_PATH . '/constants/init.php' );
5
6 define( 'MWAI_IMG_WAND', MWAI_URL . '/images/wand.png' );
7 define( 'MWAI_IMG_WAND_HTML', "<img style='height: 22px; margin-bottom: -5px; margin-right: 8px;'
8 src='" . MWAI_IMG_WAND . "' alt='AI Wand' />" );
9 define( 'MWAI_IMG_WAND_HTML_XS', "<img style='height: 16px; margin-bottom: -2px;'
10 src='" . MWAI_IMG_WAND . "' alt='AI Wand' />" );
11
12 class Meow_MWAI_Core
13 {
14 public $admin = null;
15 public $is_rest = false;
16 public $is_cli = false;
17 public $site_url = null;
18 public $files = null;
19 public $tasks = null;
20 public $magicWand = null;
21 private $options = null;
22 private $option_name = 'mwai_options';
23 private $themes_option_name = 'mwai_themes';
24 private $chatbots_option_name = 'mwai_chatbots';
25 private $nonce = null;
26
27 public $chatbot = null;
28 public $discussions = null;
29
30 public function __construct() {
31 Meow_MWAI_Logging::init( 'mwai_options', 'AI Engine' );
32 $this->site_url = get_site_url();
33 $this->is_rest = MeowCommon_Helpers::is_rest();
34 $this->is_cli = defined( 'WP_CLI' );
35 $this->files = new Meow_MWAI_Modules_Files( $this );
36 $this->tasks = new Meow_MWAI_Modules_Tasks( $this );
37
38 add_action( 'plugins_loaded', array( $this, 'init' ) );
39 add_action( 'wp_register_script', array( $this, 'register_scripts' ) );
40 add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
41 add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
42 }
43
44 #region Init & Scripts
45 function init() {
46 global $mwai;
47 $this->chatbot = null;
48 $this->discussions = null;
49 new Meow_MWAI_Modules_Security( $this );
50
51 // REST API
52 if ( $this->is_rest ) {
53 new Meow_MWAI_Rest( $this );
54 }
55
56 // WP Admin
57 if ( is_admin() ) {
58 new Meow_MWAI_Admin( $this );
59 }
60
61 // GDPR Module
62 if ( $this->get_option( 'chatbot_gdpr_consent' ) ) {
63 new Meow_MWAI_Modules_GDPR( $this );
64 }
65
66 // Suggestions Module
67 if ( $this->get_option( 'module_suggestions' ) && ( is_admin() || $this->is_rest ) ) {
68 $this->magicWand = new Meow_MWAI_Modules_Wand( $this );
69 }
70
71 // Administrator in WP Admin
72 if ( is_admin() && current_user_can( 'manage_options' ) ) {
73 $module_advisor = $this->get_option( 'module_advisor' );
74 if ( $module_advisor ) {
75 new Meow_MWAI_Modules_Advisor( $this );
76 }
77 }
78
79 // Chatbots & Discussions
80 if ( $this->get_option( 'module_chatbots' ) ) {
81 $this->chatbot = new Meow_MWAI_Modules_Chatbot();
82 $this->discussions = new Meow_MWAI_Modules_Discussions();
83 }
84
85 // Advanced Core
86 if ( class_exists( 'MeowPro_MWAI_Core' ) ) {
87 new MeowPro_MWAI_Core( $this );
88 }
89
90 // Simple API
91 $mwai = new Meow_MWAI_API( $this->chatbot, $this->discussions );
92 }
93
94 public function register_scripts() {
95 // Register Highlight.js
96 wp_register_script( 'mwai_highlight', MWAI_URL . 'vendor/highlightjs/highlight.min.js', [], '11.7', false );
97 // Register CSS for the themes
98 $themes = $this->get_themes();
99 foreach ( $themes as $theme ) {
100 if ( $theme['type'] === 'internal' ) {
101 $themeId = $theme['themeId'];
102 $filename = $themeId . '.css';
103 $physical_file = trailingslashit( MWAI_PATH ) . 'themes/' . $filename;
104 $cache_buster = file_exists( $physical_file ) ? filemtime( $physical_file ) : MWAI_VERSION;
105 wp_register_style( 'mwai_chatbot_theme_' . $themeId, trailingslashit( MWAI_URL )
106 . 'themes/' . $filename, [], $cache_buster );
107 }
108 }
109 }
110
111 public function enqueue_themes() {
112 // TODO: We should optimize and only load the themes that are used.
113 $themes = $this->get_themes();
114 foreach ( $themes as $theme ) {
115 if ( $theme['type'] === 'internal' ) {
116 $themeId = $theme['themeId'];
117 wp_enqueue_style( "mwai_chatbot_theme_$themeId" );
118 }
119 }
120 }
121
122 #endregion
123
124 #region Roles & Capabilities
125 function can_start_session() {
126 return apply_filters( 'mwai_allow_session', true );
127 }
128
129 function can_access_settings() {
130 return apply_filters( 'mwai_allow_setup', current_user_can( 'manage_options' ) );
131 }
132
133 function can_access_features() {
134 $editor_or_admin = current_user_can( 'editor' ) || current_user_can( 'administrator' );
135 return apply_filters( 'mwai_allow_usage', $editor_or_admin );
136 }
137
138 function can_access_public_api( $feature, $extra ) {
139 $logged_in = is_user_logged_in();
140 return apply_filters( 'mwai_allow_public_api', $logged_in, $feature, $extra );
141 }
142
143 #endregion
144
145 #region AI-Related Helpers
146 function run_query( $query, $streamCallback = null, $markdown = false ) {
147
148 // Allow to modify the query before it is sent.
149 // Embedding and Feedback queries are not allowed to be modified.
150 if ( !( $query instanceof Meow_MWAI_Query_Embed ) && !( $query instanceof Meow_MWAI_Query_Feedback ) ) {
151 $query = apply_filters( 'mwai_ai_query', $query );
152 }
153
154 // Let's check the default environment and model.
155 $engine = $this->validate_env_model( $query );
156
157 // Let's run the query.
158 $reply = $engine->run( $query, $streamCallback );
159
160 // Let's allow to modify the reply before it is sent.
161 if ( $markdown ) {
162 if ( $query instanceof Meow_MWAI_Query_Image ) {
163 $reply->result = "";
164 foreach ( $reply->results as $result ) {
165 $reply->result .= "![Image]($result)\n";
166 }
167 }
168 }
169
170 return $reply;
171 }
172
173 public function validate_env_model( $query ) {
174 $envId = !empty( $query->envId ) ? $query->envId : null;
175 $engine = Meow_MWAI_Engines_Factory::get( $this, $envId );
176 if ( !$envId || !$engine->retrieve_model_info( $query->model ) ) {
177 if ( $query instanceof Meow_MWAI_Query_Text ) {
178 $this->set_default_env_and_model( $query, 'ai_default_env', 'ai_default_model' );
179 }
180 if ( $query instanceof Meow_MWAI_Query_Embed ) {
181 $this->set_default_env_and_model( $query, 'ai_embeddings_default_env', 'ai_embeddings_default_model' );
182 }
183 else if ( $query instanceof Meow_MWAI_Query_Image ) {
184 $this->set_default_env_and_model( $query, 'ai_images_default_env', 'ai_images_default_model' );
185 }
186 else if ( $query instanceof Meow_MWAI_Query_Transcribe ) {
187 $this->set_default_env_and_model( $query, 'ai_audio_default_env', 'ai_audio_default_model' );
188 }
189 $engine = Meow_MWAI_Engines_Factory::get( $this, $query->envId );
190 }
191 return $engine;
192 }
193
194 private function set_default_env_and_model( $query, $envOption, $modelOption ) {
195 $defaultEnv = $this->get_option( $envOption );
196 $defaultModel = $this->get_option( $modelOption );
197 if ( empty( $defaultEnv ) || empty( $defaultModel ) ) {
198 throw new Exception( 'AI Engine: The default environment and model are not set.' );
199 }
200 $query->set_env_id( $defaultEnv );
201 $query->set_model( $defaultModel );
202 }
203 #endregion
204
205 #region Text-Related Helpers
206
207 // Clean the text perfectly, resolve shortcodes, etc, etc.
208 function clean_text( $rawText = "" ) {
209 $text = html_entity_decode( $rawText );
210 $text = wp_strip_all_tags( $text );
211 $text = preg_replace( '/[\r\n]+/', "\n", $text );
212 $text = preg_replace( '/\n+/', "\n", $text );
213 $text = preg_replace( '/\t+/', "\t", $text );
214 return $text . " ";
215 }
216
217 // Make sure there are no duplicate sentences, and keep the length under a maximum length.
218 function clean_sentences( $text, $maxLength = null ) {
219 // Step 1: Identify URLs and replace them with a placeholder.
220 $urlPattern = '/\bhttps?:\/\/[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/))/';
221 preg_match_all($urlPattern, $text, $urls);
222 $urlPlaceholders = array();
223 foreach ($urls[0] as $index => $url) {
224 $placeholder = "{urlPlaceholder" . $index . "}";
225 $text = str_replace($url, $placeholder, $text);
226 $urlPlaceholders[$placeholder] = $url;
227 }
228
229 $maxLength = (int)($maxLength ? $maxLength : $this->get_option( 'context_max_length', 4096 ));
230 $sentences = preg_split('/(?<=[.?!。.!?])\s+/u', $text, -1, PREG_SPLIT_NO_EMPTY);
231 $hashes = array();
232 $uniqueSentences = array();
233 $total = 0;
234
235 foreach ( $sentences as $sentence ) {
236 $sentence = preg_replace( '/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $sentence );
237 $hash = md5( $sentence );
238 if ( !in_array( $hash, $hashes ) ) {
239 $length = mb_strlen( $sentence, 'UTF-8' );
240 if ( $total + $length > $maxLength ) {
241 continue;
242 }
243 $hashes[] = $hash;
244 $uniqueSentences[] = $sentence;
245 $total += $length;
246 }
247 }
248
249 $freshText = implode( " ", $uniqueSentences );
250
251 // Step 3: Restore URLs in the final text.
252 foreach ($urlPlaceholders as $placeholder => $url) {
253 $freshText = str_replace($placeholder, $url, $freshText);
254 }
255
256 $freshText = preg_replace( '/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $freshText );
257 return $freshText;
258 }
259
260 function get_post_content( $postId ) {
261 $post = get_post( $postId );
262 if ( !$post ) {
263 return false;
264 }
265 $text = apply_filters( 'mwai_pre_post_content', $post->post_content, $postId );
266 $pattern = '/\[mwai_.*?\]/';
267 $text = preg_replace( $pattern, '', $text );
268 if ( $this->get_option( 'resolve_shortcodes' ) ) {
269 $text = apply_filters( 'the_content', $text );
270 }
271 else {
272 $pattern = "/\[[^\]]+\]/";
273 $text = preg_replace( $pattern, '', $text );
274 $pattern = "/<!--\s*\/?wp:[^\>]+-->/";
275 $text = preg_replace( $pattern, '', $text );
276 }
277 $text = $this->clean_text( $text );
278 $text = $this->clean_sentences( $text );
279 $text = apply_filters( 'mwai_post_content', $text, $postId );
280 return $text;
281 }
282
283 function markdown_to_html( $content ) {
284 $Parsedown = new Parsedown();
285 $content = $Parsedown->text( $content );
286 return $content;
287 }
288
289 function get_post_language( $postId ) {
290 $locale = get_locale();
291 $code = strtolower( substr( $locale, 0, 2 ) );
292 $humanLanguage = strtr( $code, MWAI_ALL_LANGUAGES );
293 $lang = apply_filters( 'wpml_post_language_details', null, $postId );
294 if ( !empty( $lang ) ) {
295 $locale = $lang['locale'];
296 $humanLanguage = $lang['display_name'];
297 }
298 return strtolower( "$locale ($humanLanguage)" );
299 }
300
301 function do_placeholders( $text ) {
302 $defaultPlaceholders = [];
303 $dataPlaceholders = $this->get_user_data();
304 if ( !empty( $dataPlaceholders ) ) {
305 $defaultPlaceholders = array_merge( $defaultPlaceholders, $dataPlaceholders );
306 }
307 $placeholders = apply_filters( 'mwai_placeholders', $defaultPlaceholders );
308 foreach ( $placeholders as $key => $value ) {
309 $text = str_replace( '{' . $key . '}', $value, $text );
310 }
311 return $text;
312 }
313 #endregion
314
315 #region Image-Related Helpers
316 static function is_image( $file ) {
317 $mimeType = Meow_MWAI_Core::get_mime_type( $file );
318 if ( strpos( $mimeType, 'image' ) !== false ) {
319 return true;
320 }
321 return false;
322 }
323
324 static function get_image_resolution( $url ) {
325 if ( empty( $url ) ) {
326 return null;
327 }
328 $headers = get_headers( $url, 1 );
329 if ( strpos( $headers[0], '200' ) === false ) {
330 return null;
331 }
332 $image_info = getimagesize( $url );
333 if ( $image_info === false ) {
334 return null;
335 }
336 return [
337 'width' => $image_info[0],
338 'height' => $image_info[1]
339 ];
340 }
341
342 static function get_mime_type( $file ) {
343 $mimeType = null;
344
345 // Let's try to use mime_content_type if the function exists
346 $isUrl = filter_var( $file, FILTER_VALIDATE_URL );
347 if ( !$isUrl && function_exists( 'mime_content_type' ) ) {
348 $mimeType = mime_content_type( $file );
349 }
350
351 // Otherwise, let's check the file extension (which can actually also be an URL)
352 if ( !$mimeType ) {
353 $extension = pathinfo( $file, PATHINFO_EXTENSION );
354 $extension = strtolower( $extension );
355 $mimeTypes = [
356 'jpg' => 'image/jpeg',
357 'jpeg' => 'image/jpeg',
358 'png' => 'image/png',
359 'gif' => 'image/gif',
360 'webp' => 'image/webp',
361 'bmp' => 'image/bmp',
362 'tiff' => 'image/tiff',
363 'tif' => 'image/tiff',
364 'svg' => 'image/svg+xml',
365 'ico' => 'image/x-icon',
366 'pdf' => 'application/pdf',
367 ];
368 $mimeType = isset( $mimeTypes[$extension] ) ? $mimeTypes[$extension] : null;
369 }
370
371 return $mimeType;
372 }
373
374 function download_image( $url ) {
375 $args = array( 'timeout' => 60, );
376 $response = wp_safe_remote_get( $url, $args );
377 if ( is_wp_error( $response ) ) {
378 throw new Exception( $response->get_error_message() );
379 }
380 $output = wp_remote_retrieve_body( $response );
381 if ( is_wp_error( $output ) ) {
382 throw new Exception( $output->get_error_message() );
383 }
384 return $output;
385 }
386
387 /**
388 * Add an image from a URL to the Media Library.
389 * @param string $url The URL of the image to be downloaded.
390 * @param string $filename The filename of the image, if not set, it will be the basename of the URL.
391 * @param string $title The title of the image.
392 * @param string $description The description of the image.
393 * @param string $caption The caption of the image.
394 * @param string $alt The alt text of the image.
395 * @return int The attachment ID of the image.
396 */
397 public function add_image_from_url( $url, $filename = null, $title = null, $description = null, $caption = null, $alt = null, $attachedPost = null ) {
398 $path_parts = pathinfo( parse_url( $url, PHP_URL_PATH ) );
399 $url_filename = $path_parts['basename'];
400 $file_type = wp_check_filetype( $url_filename, null );
401 $allowed_types = get_allowed_mime_types();
402 if ( !$file_type || !in_array( $file_type['type'], $allowed_types ) ) {
403 throw new Exception( 'Invalid file type from URL.' );
404 }
405
406 // Initial extension from URL file name
407 $extension = $file_type['ext'];
408
409 if ( !empty( $filename ) ) {
410 $custom_file_type = wp_check_filetype( $filename, null );
411 if ( !$custom_file_type || !in_array( $custom_file_type['type'], $allowed_types ) ) {
412 throw new Exception( 'Invalid custom file type.' );
413 }
414 // Use the extension from the custom filename if valid
415 $extension = $custom_file_type['ext'];
416 }
417
418 $image_data = $this->download_image( $url );
419 if ( !$image_data ) {
420 throw new Exception( 'Could not download the image.' );
421 }
422 $upload_dir = wp_upload_dir();
423
424 // Filename handling including 'generated_' prefix scenario
425 if ( empty( $filename ) ) {
426 $filename = sanitize_file_name( $url_filename );
427 if ( empty( $extension ) ) { // This condition might now be redundant
428 $extension = $file_type['ext'];
429 }
430 // Filename length check and prepend if conditions met
431 if ( strlen( $filename ) > 32 || strlen( $filename ) < 4 || strpos( $filename, 'generated_' ) === 0 ) {
432 $filename = $this->get_random_id( 16 ) . '.' . $extension;
433 }
434 if ( strpos( $filename, '.' ) === false ) {
435 $filename .= '.' . $extension;
436 }
437 }
438
439 // Directory and file path handling
440 if ( wp_mkdir_p( $upload_dir['path'] ) ) {
441 $file = $upload_dir['path'] . '/' . $filename;
442 }
443 else {
444 $file = $upload_dir['basedir'] . '/' . $filename;
445 }
446
447 // Ensure file name uniqueness in the directory
448 $i = 1;
449 $parts = pathinfo( $file );
450 while ( file_exists( $file ) ) {
451 $file = $parts['dirname'] . '/' . $parts['filename'] . '-' . $i . '.' . $parts['extension'];
452 $i++;
453 }
454
455 // Writing the file to disk
456 file_put_contents( $file, $image_data );
457
458 // Attachment and metadata handling in WP
459 $attachment = [
460 'post_mime_type' => $file_type['type'],
461 'post_title' => $title ?? '',
462 'post_content' => $description ?? '',
463 'post_excerpt' => $caption ?? '',
464 'post_status' => 'inherit'
465 ];
466 $attachmentId = wp_insert_attachment( $attachment, $file );
467 require_once( ABSPATH . 'wp-admin/includes/image.php' );
468 $attachment_data = wp_generate_attachment_metadata( $attachmentId, $file );
469 wp_update_attachment_metadata( $attachmentId, $attachment_data );
470 update_post_meta( $attachmentId, '_wp_attachment_image_alt', $alt );
471
472 // Attach the image to a post if needed
473 if ( $attachedPost ) {
474 wp_update_post( [ 'ID' => $attachmentId, 'post_parent' => $attachedPost ] );
475 }
476
477 return $attachmentId;
478 }
479 #endregion
480
481 #region Context-Related Helpers
482 function retrieve_context( $params, $query ) {
483 $contextMaxLength = $params['contextMaxLength'] ?? $this->get_option( 'context_max_length', 4096 );
484 $embeddingsEnvId = $params['embeddingsEnvId'] ?? null;
485 $context = apply_filters( 'mwai_context_search', [], $query, [
486 'embeddingsEnvId' => $embeddingsEnvId
487 ]);
488 if ( empty( $context ) ) {
489 return null;
490 }
491 else if ( !isset( $context['content'] ) ) {
492 Meow_MWAI_Logging::warn( "A context without content was returned." );
493 return null;
494 }
495 $context['content'] = $this->clean_sentences( $context['content'], $contextMaxLength );
496 $context['length'] = strlen( $context['content'] );
497 return $context;
498 }
499 #endregion
500
501 #region Users/Sessions Helpers
502
503 function get_nonce( $force = false ) {
504 if ( !$force && !is_user_logged_in() ) {
505 return null;
506 }
507 if ( isset( $this->nonce ) ) {
508 return $this->nonce;
509 }
510 $this->nonce = wp_create_nonce( 'wp_rest' );
511 return $this->nonce;
512 }
513
514 // This is a bit hacky, but chatId needs to be retrieved or generated.
515 // Maybe we can clean this up later.
516 function fix_chat_id( $query, $params ) {
517 if ( isset( $query->chatId ) && $query->chatId !== 'N/A' ) {
518 return $query->chatId;
519 }
520 $chatId = isset( $params['chatId'] ) ? $params['chatId'] : $query->session;
521 if ( $chatId === 'N/A' ) {
522 $chatId = $this->get_random_id( 8 );
523 }
524 $query->set_chat_id( $chatId );
525 return $chatId;
526 }
527
528 function get_session_id() {
529 if ( isset( $_COOKIE['mwai_session_id'] ) ) {
530 return $_COOKIE['mwai_session_id'];
531 }
532 return "N/A";
533 }
534
535 // Get the UserID from the data, or from the current user
536 function get_user_id( $data = null ) {
537 // TODO: Not sure if that's the best way, but we should probably use an admin user as a fallback for CRON.
538 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
539 $admin = get_users( [ 'role' => 'administrator' ] );
540 if ( !empty( $admin ) ) {
541 return $admin[0]->ID;
542 }
543 }
544 if ( isset( $data ) && isset( $data['userId'] ) ) {
545 return (int)$data['userId'];
546 }
547 if ( is_user_logged_in() ) {
548 $current_user = wp_get_current_user();
549 if ( $current_user->ID > 0 ) {
550 return $current_user->ID;
551 }
552 }
553 return null;
554 }
555
556 function get_admin_user() {
557 $admin = get_users( [ 'role' => 'administrator' ] );
558 if ( !empty( $admin ) ) {
559 return $admin[0];
560 }
561 return null;
562 }
563
564 function get_user_data() {
565 $user = wp_get_current_user();
566 if ( empty( $user ) || empty( $user->ID ) ) {
567 return null;
568 }
569 $placeholders = array(
570 'FIRST_NAME' => get_user_meta( $user->ID, 'first_name', true ),
571 'LAST_NAME' => get_user_meta( $user->ID, 'last_name', true ),
572 'USER_LOGIN' => isset( $user ) && isset($user->data) && isset( $user->data->user_login ) ?
573 $user->data->user_login : null,
574 'DISPLAY_NAME' => isset( $user ) && isset( $user->data ) && isset( $user->data->display_name ) ?
575 $user->data->display_name : null,
576 'AVATAR_URL' => get_avatar_url( get_current_user_id() ),
577 );
578 return $placeholders;
579 }
580
581 function get_ip_address( $params = null ) {
582 $ip = '127.0.0.1';
583 $headers = [
584 'HTTP_TRUE_CLIENT_IP',
585 'HTTP_CF_CONNECTING_IP',
586 'HTTP_X_REAL_IP',
587 'HTTP_CLIENT_IP',
588 'HTTP_X_FORWARDED_FOR',
589 'HTTP_X_FORWARDED',
590 'HTTP_X_CLUSTER_CLIENT_IP',
591 'HTTP_FORWARDED_FOR',
592 'HTTP_FORWARDED',
593 'REMOTE_ADDR',
594 ];
595
596 if ( isset( $params ) && isset( $params[ 'ip' ] ) ) {
597 $ip = ( string )$params[ 'ip' ];
598 } else {
599 foreach ( $headers as $header ) {
600 if ( array_key_exists( $header, $_SERVER ) && !empty( $_SERVER[ $header ] && $_SERVER[ $header ] != '::1' ) ) {
601 $address_chain = explode( ',', wp_unslash( $_SERVER [ $header ] ) );
602 $ip = filter_var( trim( $address_chain[ 0 ] ), FILTER_VALIDATE_IP );
603 break;
604 }
605 }
606 }
607
608 return filter_var( apply_filters( 'mwai_get_ip_address', $ip ), FILTER_VALIDATE_IP );
609 }
610
611 #endregion
612
613 #region Sanitization
614 function sanitize_sort( &$sort, $default_accessor = 'created', $default_order = 'DESC',
615 $allowed_columns = array( 'created', 'updated', 'name', 'id', 'time', 'units', 'price' )) {
616
617 // Ensure $sort is an array
618 if ( !is_array( $sort ) ) {
619 $sort = [ "accessor" => $default_accessor, "by" => $default_order ];
620 }
621 // Extract and sanitize the accessor
622 $sort_accessor = isset( $sort['accessor'] ) ? $sort['accessor'] : $default_accessor;
623 if ( !in_array( $sort_accessor, $allowed_columns ) ) {
624 Meow_MWAI_Logging::error( "This sort accessor is not allowed ($sort_accessor)." );
625 $sort_accessor = $default_accessor;
626 }
627 // Extract and sanitize the sort order
628 $sort_by = isset( $sort['by'] ) ? strtoupper( $sort['by'] ) : $default_order;
629 if ( $sort_by !== 'ASC' && $sort_by !== 'DESC' ) {
630 Meow_MWAI_Logging::error( "This sort order is not allowed ($sort_by)." );
631 $sort_by = $default_order;
632 }
633 // Update the sort array with sanitized values
634 $sort['accessor'] = $sort_accessor;
635 $sort['by'] = $sort_by;
636 }
637 #endregion
638
639 #region Other Helpers
640 function safe_strlen( $string, $encoding = 'UTF-8' ) {
641 if ( function_exists( 'mb_strlen' ) ) {
642 return mb_strlen( $string, $encoding );
643 }
644 else {
645 // Fallback implementation for environments without mbstring extension
646 return preg_match_all( '/./u', $string, $matches );
647 }
648 }
649
650 public function check_rest_nonce( $request ) {
651 $nonce = $request->get_header( 'X-WP-Nonce' );
652 $rest_nonce = wp_verify_nonce( $nonce, 'wp_rest' );
653 return apply_filters( 'mwai_rest_authorized', $rest_nonce, $request );
654 }
655
656 function get_random_id( $length = 8, $excludeIds = [] ) {
657 $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
658 $charactersLength = strlen( $characters );
659 $randomId = '';
660 for ( $i = 0; $i < $length; $i++ ) {
661 $randomId .= $characters[rand( 0, $charactersLength - 1 )];
662 }
663 if ( in_array( $randomId, $excludeIds ) ) {
664 return $this->get_random_id( $length, $excludeIds );
665 }
666 return $randomId;
667 }
668
669 function is_url( $url ) {
670 return strpos( $url, 'http' ) === 0 ? true : false;
671 }
672
673 function get_post_types() {
674 $excluded = array( 'attachment', 'revision', 'nav_menu_item' );
675 $post_types = array();
676 $types = get_post_types( [], 'objects' );
677
678 // Let's get the Post Types that are enabled for Embeddings Sync
679 $embeddingsSettings = $this->get_option( 'embeddings' );
680 $syncPostTypes = isset( $embeddingsSettings['syncPostTypes'] ) ? $embeddingsSettings['syncPostTypes'] : [];
681
682 foreach ( $types as $type ) {
683 $forced = in_array( $type->name, $syncPostTypes );
684 // Should not be excluded.
685 if ( !$forced && in_array( $type->name, $excluded ) ) {
686 continue;
687 }
688 // Should be public.
689 if ( !$forced && !$type->public ) {
690 continue;
691 }
692 $post_types[] = array(
693 'name' => $type->labels->name,
694 'type' => $type->name,
695 );
696 }
697
698 // Let's get the Post Types that are enabled for Embeddings Sync
699 $embeddingsSettings = $this->get_option( 'embeddings' );
700 $syncPostTypes = isset( $embeddingsSettings['syncPostTypes'] ) ? $embeddingsSettings['syncPostTypes'] : [];
701
702 return $post_types;
703 }
704
705 function get_post( $post ) {
706 if ( is_numeric( $post ) ) {
707 $post = get_post( $post );
708 }
709 if ( is_object( $post ) ) {
710 $post = (array)$post;
711 }
712 if ( !is_array( $post ) ) {
713 return null;
714 }
715 $language = $this->get_post_language( $post['ID'] );
716 $content = $this->get_post_content( $post['ID'] );
717 $title = $post['post_title'];
718 $excerpt = $post['post_excerpt'];
719 $url = get_permalink( $post['ID'] );
720 $checksum = wp_hash( $content . $title . $url );
721 return [
722 'postId' => (int)$post['ID'],
723 'title' => $title,
724 'content' => $content,
725 'excerpt' => $excerpt,
726 'url' => $url,
727 'language' => $language ?? 'english',
728 'checksum' => $checksum,
729 ];
730 }
731 #endregion
732
733 #region Usage & Costs
734
735 // Quick and dirty token estimation
736 // Let's keep this synchronized with Helpers in JS
737 static function estimate_tokens( ...$args ): int {
738 $text = "";
739 foreach ( $args as $arg ) {
740 if ( is_array( $arg ) ) {
741 foreach ( $arg as $message ) {
742 $text .= isset( $message['content']['text'] ) ? $message['content']['text'] : "";
743 $text .= isset( $message['content'] ) && is_string( $message['content'] ) ? $message['content'] : "";
744 }
745 }
746 else if ( is_string( $arg ) ) {
747 $text .= $arg;
748 }
749 }
750 $averageTokenLength = 4;
751 $words = preg_split( '/\s+/', trim( $text ) );
752 $tokenCount = 0;
753 foreach ( $words as $word ) {
754 $tokenCount += ceil( strlen( $word ) / $averageTokenLength );
755 }
756 return apply_filters( 'mwai_estimate_tokens', $tokenCount, $text );
757 }
758
759 public function record_tokens_usage( $model, $in_tokens, $out_tokens = 0, $returned_price = null ) {
760 if ( !is_numeric( $in_tokens ) ) {
761 throw new Exception( 'AI Engine: in_tokens must be a number.' );
762 }
763 if ( !is_numeric( $out_tokens ) ) {
764 $out_tokens = 0;
765 }
766 if ( !$model ) {
767 throw new Exception( 'AI Engine: model is required.' );
768 }
769 $usage = $this->get_option( 'ai_models_usage' );
770 $month = date( 'Y-m' );
771 if ( !isset( $usage[$month] ) ) {
772 $usage[$month] = array();
773 }
774 if ( !isset( $usage[$month][$model] ) ) {
775 $usage[$month][$model] = array( 'prompt_tokens' => 0, 'completion_tokens' => 0, 'total_tokens' => 0 );
776 }
777 $usage[$month][$model]['prompt_tokens'] += $in_tokens;
778 $usage[$month][$model]['completion_tokens'] += $out_tokens;
779 $usage[$month][$model]['total_tokens'] += $in_tokens + $out_tokens;
780 $this->update_option( 'ai_models_usage', $usage );
781 $usageInfo = [
782 'prompt_tokens' => $in_tokens,
783 'completion_tokens' => $out_tokens,
784 'total_tokens' => $in_tokens + $out_tokens,
785 ];
786 if ( $returned_price !== null ) {
787 $usageInfo['price'] = $returned_price;
788 }
789 return $usageInfo;
790 }
791
792 public function record_audio_usage( $model, $seconds ) {
793 if ( !is_numeric( $seconds ) ) {
794 throw new Exception( 'AI Engine: seconds must be a number.' );
795 }
796 if ( !$model ) {
797 throw new Exception( 'AI Engine: model is required.' );
798 }
799 $usage = $this->get_option( 'ai_models_usage' );
800 $month = date( 'Y-m' );
801 if ( !isset( $usage[$month] ) ) {
802 $usage[$month] = array();
803 }
804 if ( !isset( $usage[$month][$model] ) ) {
805 $usage[$month][$model] = array( 'seconds' => 0 );
806 }
807 $usage[$month][$model]['seconds'] += $seconds;
808 $this->update_option( 'ai_models_usage', $usage );
809 return [ 'seconds' => $seconds ];
810 }
811
812 public function record_images_usage( $model, $resolution, $images ) {
813 if ( !$model || !$resolution || !$images ) {
814 throw new Exception( 'Missing parameters for record_image_usage.' );
815 }
816 $usage = $this->get_option( 'ai_models_usage' );
817 $month = date( 'Y-m' );
818 if ( !isset( $usage[$month] ) ) {
819 $usage[$month] = array();
820 }
821 if ( !isset( $usage[$month][$model] ) ) {
822 $usage[$month][$model] = array( 'resolution' => array(), 'images' => 0 );
823 }
824 if ( !isset( $usage[$month][$model]['resolution'][$resolution] ) ) {
825 $usage[$month][$model]['resolution'][$resolution] = 0;
826 }
827 $usage[$month][$model]['resolution'][$resolution] += $images;
828 $usage[$month][$model]['images'] += $images;
829 $this->update_option( 'ai_models_usage', $usage );
830 return [ 'resolution' => $resolution, 'images' => $images ];
831 }
832
833 #endregion
834
835 #region Streaming
836 public function stream_push( $data, $query = null ) {
837 $data = apply_filters( 'mwai_stream_push', $data, $query );
838 $out = "data: " . json_encode( $data );
839 echo $out;
840 echo "\n\n";
841 if ( ob_get_level() > 0 ) {
842 ob_end_flush();
843 }
844 flush();
845 }
846 #endregion
847
848 #region Options
849 function get_themes() {
850 $themes = get_option( $this->themes_option_name, [] );
851 $themes = empty( $themes ) ? [] : $themes;
852
853 $internalThemes = [
854 'chatgpt' => [
855 'type' => 'internal','name' => 'ChatGPT', 'themeId' => 'chatgpt',
856 'settings' => [], 'style' => ""
857 ],
858 'messages' => [
859 'type' => 'internal', 'name' => 'Messages', 'themeId' => 'messages',
860 'settings' => [], 'style' => ""
861 ],
862 'timeless' => [
863 'type' => 'internal', 'name' => 'Timeless', 'themeId' => 'timeless',
864 'settings' => [], 'style' => ""
865 ],
866 ];
867 $customThemes = [];
868 foreach ( $themes as $theme ) {
869 if ( isset( $internalThemes[$theme['themeId']] ) ) {
870 $internalThemes[$theme['themeId']] = $theme;
871 continue;
872 }
873 $customThemes[] = $theme;
874 }
875 return array_merge(array_values($internalThemes), $customThemes);
876 }
877
878 function update_themes( $themes ) {
879 update_option( $this->themes_option_name, $themes );
880 return $themes;
881 }
882
883 function get_chatbots() {
884 $chatbots = get_option( $this->chatbots_option_name, [] );
885 $hasChanges = false;
886 if ( empty( $chatbots ) ) {
887 $chatbots = [ array_merge( MWAI_CHATBOT_DEFAULT_PARAMS, ['name' => 'Default', 'botId' => 'default' ] ) ];
888 }
889 $hasDefault = false;
890 foreach ( $chatbots as &$chatbot ) {
891 if ( $chatbot['botId'] === 'default' ) {
892 $hasDefault = true;
893 }
894 foreach ( MWAI_CHATBOT_DEFAULT_PARAMS as $key => $value ) {
895 // Use default value if not set.
896 if ( !isset( $chatbot[$key] ) ) {
897 $chatbot[$key] = $value;
898 }
899 }
900
901 /*
902 This is the best section to rename fields.
903 We did this in 2024 for context to instructions, and fileUpload to fileSearch. fileSearch is for assistant file search, and fileUpload is now for chatbot file upload (similar to vision, but for files instead of images).
904 */
905
906 // if ( isset( $chatbot['context'] ) ) {
907 // $chatbot['instructions'] = $chatbot['context'];
908 // unset( $chatbot['context'] );
909 // $hasChanges = true;
910 // }
911 }
912 if ( !$hasDefault ) {
913 $defaultBot = array_merge( MWAI_CHATBOT_DEFAULT_PARAMS, ['name' => 'Default', 'botId' => 'default' ] );
914 array_unshift( $chatbots, $defaultBot );
915 $hasChanges = true;
916 }
917 if ( $hasChanges ) {
918 update_option( $this->chatbots_option_name, $chatbots );
919 }
920 return $chatbots;
921 }
922
923 function get_chatbot( $botId ) {
924 $chatbots = $this->get_chatbots();
925 foreach ( $chatbots as $chatbot ) {
926 if ( $chatbot['botId'] === (string)$botId ) {
927 return $chatbot;
928 }
929 }
930 return null;
931 }
932
933 function get_embeddings_env( $envId ) {
934 $envs = $this->get_option( 'embeddings_envs' );
935 foreach ( $envs as $env ) {
936 if ( $env['id'] === $envId ) {
937 return $env;
938 }
939 }
940 return null;
941 }
942
943 function get_ai_env( $envId ) {
944 $envs = $this->get_option( 'ai_envs' );
945 foreach ( $envs as $env ) {
946 if ( $env['id'] === $envId ) {
947 return $env;
948 }
949 }
950 return null;
951 }
952
953 function get_assistant( $envId, $assistantId ) {
954 $env = $this->get_ai_env( $envId );
955 if ( !$env ) {
956 return null;
957 }
958 $assistants = $env['assistants'];
959 foreach ( $assistants as $assistant ) {
960 if ( $assistant['id'] === $assistantId ) {
961 return $assistant;
962 }
963 }
964 return null;
965 }
966
967 function get_theme( $themeId ) {
968 $themes = $this->get_themes();
969 foreach ( $themes as $theme ) {
970 if ( $theme['themeId'] === $themeId ) {
971 return $theme;
972 }
973 }
974 return null;
975 }
976
977 function update_chatbots( $chatbots ) {
978 $deprecatedFields = [ 'env', 'embeddingsIndex', 'embeddingsNamespace', 'service' ];
979 // TODO: I think some HTML fields are missing, guestName, maybe others.
980 $htmlFields = [ 'instructions', 'textCompliance', 'aiName', 'userName', 'startSentence' ];
981 $keepLineReturnsFields = [ 'instructions' ];
982 $whiteSpacedFields = [ 'context' ];
983 foreach ( $chatbots as &$chatbot ) {
984 foreach ( $chatbot as $key => &$value ) {
985 if ( in_array( $key, $deprecatedFields ) ) {
986 unset( $chatbot[$key] );
987 continue;
988 }
989 if ( in_array( $key, $htmlFields ) ) {
990 $value = wp_kses_post( $value );
991 }
992 else if ( in_array( $key, $whiteSpacedFields ) ) {
993 $value = sanitize_textarea_field( $value );
994 }
995 else if ( $key === 'functions' ) {
996 $functions = [];
997 foreach ( $value as $function ) {
998 if ( isset( $function['id'] ) && isset( $function['type'] ) ) {
999 $functions[] = [
1000 'id' => sanitize_text_field( $function['id'] ),
1001 'type' => sanitize_text_field( $function['type'] ),
1002 ];
1003 }
1004 }
1005 $value = $functions;
1006 }
1007 else {
1008 if ( in_array( $key, $keepLineReturnsFields ) ) {
1009 $value = preg_replace( '/\r\n/', "[==LINE_RETURN==]", $value );
1010 $value = preg_replace( '/\n/', "[==LINE_RETURN==]", $value );
1011 }
1012 $value = sanitize_text_field( $value );
1013 if ( in_array( $key, $keepLineReturnsFields ) ) {
1014 $value = preg_replace( '/\[==LINE_RETURN==\]/', "\n", $value );
1015 }
1016 }
1017 }
1018 }
1019 if ( !update_option( $this->chatbots_option_name, $chatbots ) ) {
1020 Meow_MWAI_Logging::warn( 'Could not update chatbots.' );
1021 $chatbots = get_option( $this->chatbots_option_name, [] );
1022 return $chatbots;
1023 }
1024 return $chatbots;
1025 }
1026
1027 function populate_dynamic_options( $options ) {
1028 // Languages
1029 $options['languages'] = apply_filters( 'mwai_languages', MWAI_LANGUAGES );
1030
1031 // Consolidate the Engines and their Models
1032 // PS: We should ABSOLUTELY AVOID to use ai_models directly (except for saving)
1033 // Engine Example: [ 'name' => 'Ollama', 'type' => 'ollama', inputs => ['apikey', 'endpoint'], models => [] ]
1034 $options['ai_engines'] = apply_filters( 'mwai_engines', MWAI_ENGINES );
1035 foreach ( $options['ai_engines'] as &$engine ) {
1036 if ( $engine['type'] === 'openai' ) {
1037 $engine['models'] = apply_filters( 'mwai_openai_models',
1038 Meow_MWAI_Engines_OpenAI::get_models_static()
1039 );
1040 }
1041 else if ( $engine['type'] === 'anthropic' ) {
1042 $engine['models'] = apply_filters( 'mwai_anthropic_models',
1043 Meow_MWAI_Engines_Anthropic::get_models_static()
1044 );
1045 }
1046 else if ( $engine['type'] === 'perplexity' ) {
1047 $engine['models'] = apply_filters( 'mwai_perplexity_models',
1048 Meow_MWAI_Engines_Perplexity::get_models_static()
1049 );
1050 }
1051 else {
1052 $engine['models'] = [];
1053 foreach ( $options['ai_models'] as $model ) {
1054 if ( $model['type'] === $engine['type'] ) {
1055 $engine['models'][] = $model;
1056 }
1057 }
1058 }
1059 }
1060
1061 // Functions via Code Engine (or custom code)
1062 $json = [];
1063 $functions = apply_filters( 'mwai_functions_list', [] );
1064 foreach ( $functions as $function ) {
1065 $json[] = Meow_MWAI_Query_Function::toJson( $function );
1066 }
1067 $options['functions'] = $json;
1068
1069 // Addons
1070 $options['addons'] = apply_filters( 'mwai_addons', [
1071 [
1072 "slug" => "mwai-notifications",
1073 "name" => "Notifications",
1074 "description" => "Get real-time alerts for new discussions in your chatbot, so you never miss a chance to engage.",
1075 "install_url" => "https://meowapps.com/products/mwai-notifications/",
1076 "settings_url" => null,
1077 "stars" => 4,
1078 "enabled" => false
1079 ],
1080 [
1081 "slug" => "mwai-ollama",
1082 "name" => "Ollama",
1083 "description" => "Leverage local LLM integration through Ollama; refresh and use your own models for a flexible, cost-free approach.",
1084 "install_url" => "https://meowapps.com/products/mwai-ollama/",
1085 "settings_url" => null,
1086 "stars" => 3,
1087 "enabled" => false
1088 ],
1089 [
1090 "slug" => "mwai-deepseek",
1091 "name" => "DeepSeek",
1092 "description" => "Support for DeepSeek, a Chinese AI company that provides extremely powerful LLM models.",
1093 "install_url" => "https://meowapps.com/products/mwai-deepseek/",
1094 "settings_url" => null,
1095 "stars" => 3,
1096 "enabled" => false
1097 ],
1098 [
1099 "slug" => "mwai-websearch",
1100 "name" => "Web Search",
1101 "description" => "Enhance chatbot responses by pulling context from Google and Tavily, delivering more accurate answers.",
1102 "install_url" => "https://meowapps.com/products/mwai-websearch/",
1103 "settings_url" => null,
1104 "stars" => 5,
1105 "enabled" => false
1106 ],
1107 [
1108 "slug" => "mwai-better-links",
1109 "name" => "Better Links",
1110 "description" => "Validate internal and external links and map specific terms to custom URLs, ensuring smoother navigation and references.",
1111 "install_url" => "https://meowapps.com/products/mwai-better-links/",
1112 "settings_url" => null,
1113 "stars" => 3,
1114 "enabled" => false
1115 ],
1116 [
1117 "slug" => "mwai-woo-basics",
1118 "name" => "Woo Basics",
1119 "description" => "Access essential WooCommerce data so your chatbot can understand products, orders, and more for a richer shopping experience.",
1120 "install_url" => "https://meowapps.com/products/mwai-woo-basics/",
1121 "settings_url" => null,
1122 "stars" => 2,
1123 "enabled" => false
1124 ],
1125 [
1126 "slug" => "mwai-quick-actions",
1127 "name" => "Quick Actions",
1128 "description" => "Enable dynamic quick actions at chat start or during events, helping users find what they need faster.",
1129 "install_url" => "https://meowapps.com/products/mwai-quick-actions/",
1130 "settings_url" => null,
1131 "stars" => 3,
1132 "enabled" => false
1133 ],
1134 [
1135 "slug" => "mwai-content-parser",
1136 "name" => "Content Parser",
1137 "description" => "Parse complex website content, including ACF fields and page builders, for more precise embeddings and knowledge retrieval.",
1138 "install_url" => "https://meowapps.com/products/mwai-content-parser/",
1139 "settings_url" => null,
1140 "stars" => 2,
1141 "enabled" => false
1142 ],
1143 [
1144 "slug" => "mwai-visitor-form",
1145 "name" => "Visitor Form",
1146 "description" => "Add a customizable form triggered by specific events in your chatbot to collect key visitor information seamlessly.",
1147 "install_url" => "https://meowapps.com/products/mwai-visitor-form/",
1148 "settings_url" => null,
1149 "stars" => 2,
1150 "enabled" => false
1151 ],
1152 [
1153 "slug" => "mwai-dynamic-keys",
1154 "name" => "Dynamic Keys",
1155 "description" => "Rotate multiple API keys dynamically for any environment, balancing usage and ensuring smooth performance.",
1156 "install_url" => "https://meowapps.com/products/mwai-dynamic-keys/",
1157 "settings_url" => null,
1158 "stars" => 1,
1159 "enabled" => false
1160 ],
1161 ] );
1162
1163 return $options;
1164 }
1165
1166 function get_all_options( $force = false, $sanitize = false ) {
1167 if ( $force || is_null( $this->options ) ) {
1168 $options = get_option( $this->option_name, [] );
1169 $init_mode = empty( $options );
1170 foreach ( MWAI_OPTIONS as $key => $value ) {
1171 if ( !isset( $options[$key] ) ) {
1172 $options[$key] = $value;
1173 }
1174 }
1175 $options['chatbot_defaults'] = MWAI_CHATBOT_DEFAULT_PARAMS;
1176 $options['default_limits'] = MWAI_LIMITS;
1177 if ( $sanitize || $init_mode ) {
1178 $options = $this->sanitize_options( $options );
1179 }
1180 $this->options = $options;
1181 }
1182 $options = $this->populate_dynamic_options( $this->options );
1183 return $options;
1184 }
1185
1186 // Sanitize options when we update the plugin or perform some updates
1187 // if we change the structure of the options.
1188 function sanitize_options( $options ) {
1189 $needs_update = false;
1190
1191 // Removing old options of options renaming should be done here, as it was done before.
1192 // Check version 2.6.8 for an example.
1193
1194 // Avoid the logs_path to be a PHP file.
1195 if ( isset( $options['logs_path'] ) ) {
1196 $logs_path = $options['logs_path'];
1197 if ( substr( $logs_path, -4 ) !== '.log' ) {
1198 $options['logs_path'] = '';
1199 $needs_update = true;
1200 }
1201 }
1202
1203 // The IDs for the embeddings environments are generated here.
1204 // TODO: We should handle this more gracefully via an option in the Embeddings Settings.
1205 $embeddings_default_exists = false;
1206 if ( isset( $options['embeddings_envs'] ) ) {
1207 foreach ( $options['embeddings_envs'] as &$env ) {
1208 if ( !isset( $env['id'] ) ) {
1209 $env['id'] = $this->get_random_id();
1210 $needs_update = true;
1211 }
1212 if ( $env['id'] === $options['embeddings_default_env'] ) {
1213 $embeddings_default_exists = true;
1214 }
1215 }
1216 }
1217 if ( !$embeddings_default_exists ) {
1218 $options['embeddings_default_env'] = $options['embeddings_envs'][0]['id'] ?? null;
1219 $needs_update = true;
1220 }
1221
1222 // The IDs for the AI environments are generated here.
1223 $allEnvIds = [];
1224 $ai_default_exists = false;
1225 if ( isset( $options['ai_envs'] ) ) {
1226 foreach ( $options['ai_envs'] as &$env ) {
1227 if ( !isset( $env['id'] ) ) {
1228 $env['id'] = $this->get_random_id();
1229 $needs_update = true;
1230 }
1231 if ( $env['id'] === $options['ai_default_env'] ) {
1232 $ai_default_exists = true;
1233 }
1234 $allEnvIds[] = $env['id'];
1235 }
1236 }
1237 if ( !$ai_default_exists ) {
1238 $options['ai_default_env'] = $options['ai_envs'][0]['id'] ?? null;
1239 $needs_update = true;
1240 }
1241
1242 // All the models with an envId that does not exist anymore are removed.
1243 if ( isset( $options['ai_models'] ) ) {
1244 $options['ai_models'] = array_values( array_filter( $options['ai_models'],
1245 function( $model ) use ( $allEnvIds, &$needs_update ) {
1246 if ( isset( $model['envId'] ) && !in_array( $model['envId'], $allEnvIds ) ) {
1247 $needs_update = true;
1248 return false;
1249 }
1250 return true;
1251 }
1252 ) );
1253 }
1254
1255 if ( $needs_update ) {
1256 ksort( $options );
1257 update_option( $this->option_name, $options, false );
1258 }
1259
1260 return $options;
1261 }
1262
1263 function update_options( $options ) {
1264 if ( !update_option( $this->option_name, $options, false ) ) {
1265 return false;
1266 }
1267 $options = $this->get_all_options( true, true );
1268 return $options;
1269 }
1270
1271 function update_option( $option, $value ) {
1272 $options = $this->get_all_options( true );
1273 $options[$option] = $value;
1274 return $this->update_options( $options );
1275 }
1276
1277 function get_option( $option, $default = null ) {
1278 $options = $this->get_all_options();
1279 return $options[$option] ?? $default;
1280 }
1281
1282 function update_ai_env( $env_id, $option, $value ) {
1283 $options = $this->get_all_options( true );
1284 foreach ( $options['ai_envs'] as &$env ) {
1285 if ( $env['id'] === $env_id ) {
1286 $env[$option] = $value;
1287 break;
1288 }
1289 }
1290 return $this->update_options( $options );
1291 }
1292
1293 function get_engine_models( $engineType ) {
1294 $engines = $this->get_option( 'ai_engines' );
1295 foreach ( $engines as $engine ) {
1296 if ( $engine['type'] === $engineType ) {
1297 return isset( $engine['models'] ) ? $engine['models'] : [];
1298 }
1299 }
1300 return [];
1301 }
1302
1303 function reset_options() {
1304 delete_option( $this->themes_option_name );
1305 delete_option( $this->chatbots_option_name );
1306 delete_option( $this->option_name );
1307 return $this->get_all_options( true );
1308 }
1309 #endregion
1310 }
1311
1312 ?>