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