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
rest.php
980 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Rest |
| 4 | { |
| 5 | private $core = null; |
| 6 | private $namespace = 'mwai/v1'; |
| 7 | |
| 8 | public function __construct( $core ) { |
| 9 | $this->core = $core; |
| 10 | add_action( 'rest_api_init', array( $this, 'rest_init' ) ); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Retrieve the message from the parameters and optionally sanitize it. |
| 15 | * |
| 16 | * @param array &$params The parameters array, passed by reference. |
| 17 | * @param bool $sanitize Whether to sanitize the message using sanitize_text_field. |
| 18 | * @return string The retrieved (and optionally sanitized) message. |
| 19 | */ |
| 20 | function retrieve_message( &$params, $sanitize = false ) : string { |
| 21 | if ( isset( $params['message'] ) ) { |
| 22 | $message = $params['message']; |
| 23 | } |
| 24 | elseif ( isset( $params['prompt'] ) ) { |
| 25 | $message = $params['prompt']; |
| 26 | unset( $params['prompt'] ); |
| 27 | $params['message'] = $message; |
| 28 | $this->core->log( '⚠️ "prompt" is deprecated, please use "message" instead.' ); |
| 29 | } |
| 30 | else { |
| 31 | $message = ""; |
| 32 | } |
| 33 | |
| 34 | if ( $sanitize ) { |
| 35 | $message = sanitize_text_field( $message ); |
| 36 | } |
| 37 | |
| 38 | return $message; |
| 39 | } |
| 40 | |
| 41 | function rest_init() { |
| 42 | try { |
| 43 | // Session Endpoint |
| 44 | register_rest_route( $this->namespace, '/start_session', array( |
| 45 | 'methods' => 'POST', |
| 46 | 'permission_callback' => [ $this->core, 'can_start_session' ], |
| 47 | 'callback' => [ $this, 'rest_start_session' ], |
| 48 | ) ); |
| 49 | |
| 50 | // Settings Endpoints |
| 51 | register_rest_route( $this->namespace, '/settings/update', array( |
| 52 | 'methods' => 'POST', |
| 53 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 54 | 'callback' => [ $this, 'rest_settings_update' ], |
| 55 | ) ); |
| 56 | register_rest_route( $this->namespace, '/settings/options', array( |
| 57 | 'methods' => 'GET', |
| 58 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 59 | 'callback' => [ $this, 'rest_settings_list' ], |
| 60 | ) ); |
| 61 | register_rest_route( $this->namespace, '/settings/reset', array( |
| 62 | 'methods' => 'POST', |
| 63 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 64 | 'callback' => [ $this, 'rest_settings_reset' ], |
| 65 | ) ); |
| 66 | register_rest_route( $this->namespace, '/settings/chatbots', array( |
| 67 | 'methods' => ['GET', 'POST'], |
| 68 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 69 | 'callback' => [ $this, 'rest_settings_chatbots' ], |
| 70 | ) ); |
| 71 | register_rest_route( $this->namespace, '/settings/themes', array( |
| 72 | 'methods' => ['GET', 'POST'], |
| 73 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 74 | 'callback' => [ $this, 'rest_settings_themes' ], |
| 75 | ) ); |
| 76 | |
| 77 | // System Endpoints |
| 78 | register_rest_route( $this->namespace, '/system/logs/list', array( |
| 79 | 'methods' => 'POST', |
| 80 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 81 | 'callback' => [ $this, 'rest_system_logs_list' ], |
| 82 | ) ); |
| 83 | register_rest_route( $this->namespace, '/system/logs/delete', array( |
| 84 | 'methods' => 'POST', |
| 85 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 86 | 'callback' => [ $this, 'rest_system_logs_delete' ], |
| 87 | ) ); |
| 88 | register_rest_route( $this->namespace, '/system/logs/meta', array( |
| 89 | 'methods' => 'POST', |
| 90 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 91 | 'callback' => [ $this, 'rest_system_logs_meta_get' ], |
| 92 | ) ); |
| 93 | register_rest_route( $this->namespace, '/system/templates', array( |
| 94 | 'methods' => 'POST', |
| 95 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 96 | 'callback' => [ $this, 'rest_system_templates_save' ], |
| 97 | ) ); |
| 98 | register_rest_route( $this->namespace, '/system/templates', array( |
| 99 | 'methods' => 'GET', |
| 100 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 101 | 'callback' => [ $this, 'rest_system_templates_get' ], |
| 102 | ) ); |
| 103 | |
| 104 | // AI Endpoints |
| 105 | register_rest_route( $this->namespace, '/ai/models', array( |
| 106 | 'methods' => 'POST', |
| 107 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 108 | 'callback' => [ $this, 'rest_ai_models' ], |
| 109 | ) ); |
| 110 | register_rest_route( $this->namespace, '/ai/completions', array( |
| 111 | 'methods' => 'POST', |
| 112 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 113 | 'callback' => [ $this, 'rest_ai_completions' ], |
| 114 | ) ); |
| 115 | register_rest_route( $this->namespace, '/ai/images', array( |
| 116 | 'methods' => 'POST', |
| 117 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 118 | 'callback' => [ $this, 'rest_ai_images' ], |
| 119 | ) ); |
| 120 | register_rest_route( $this->namespace, '/ai/copilot', array( |
| 121 | 'methods' => 'POST', |
| 122 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 123 | 'callback' => [ $this, 'rest_ai_copilot' ], |
| 124 | ) ); |
| 125 | |
| 126 | register_rest_route( $this->namespace, '/ai/magic_wand', array( |
| 127 | 'methods' => 'POST', |
| 128 | 'callback' => [ $this, 'rest_ai_magic_wand' ], |
| 129 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 130 | ) ); |
| 131 | register_rest_route( $this->namespace, '/ai/moderate', array( |
| 132 | 'methods' => 'POST', |
| 133 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 134 | 'callback' => [ $this, 'rest_ai_moderate' ], |
| 135 | ) ); |
| 136 | register_rest_route( $this->namespace, '/ai/transcribe_audio', array( |
| 137 | 'methods' => 'POST', |
| 138 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 139 | 'callback' => [ $this, 'rest_ai_transcribe_audio' ], |
| 140 | ) ); |
| 141 | register_rest_route( $this->namespace, '/ai/transcribe_image', array( |
| 142 | 'methods' => 'POST', |
| 143 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 144 | 'callback' => [ $this, 'rest_ai_transcribe_image' ], |
| 145 | ) ); |
| 146 | register_rest_route( $this->namespace, '/ai/json', array( |
| 147 | 'methods' => 'POST', |
| 148 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 149 | 'callback' => [ $this, 'rest_ai_json' ], |
| 150 | ) ); |
| 151 | |
| 152 | // Helpers Endpoints |
| 153 | register_rest_route( $this->namespace, '/helpers/update_post_title', array( |
| 154 | 'methods' => 'POST', |
| 155 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 156 | 'callback' => [ $this, 'rest_helpers_update_title' ], |
| 157 | ) ); |
| 158 | register_rest_route( $this->namespace, '/helpers/update_post_excerpt', array( |
| 159 | 'methods' => 'POST', |
| 160 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 161 | 'callback' => [ $this, 'rest_helpers_update_excerpt' ], |
| 162 | ) ); |
| 163 | register_rest_route( $this->namespace, '/helpers/create_post', array( |
| 164 | 'methods' => 'POST', |
| 165 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 166 | 'callback' => [ $this, 'rest_helpers_create_post' ], |
| 167 | ) ); |
| 168 | register_rest_route( $this->namespace, '/helpers/create_image', array( |
| 169 | 'methods' => 'POST', |
| 170 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 171 | 'callback' => [ $this, 'rest_helpers_create_images' ], |
| 172 | ) ); |
| 173 | register_rest_route( $this->namespace, '/helpers/count_posts', array( |
| 174 | 'methods' => 'GET', |
| 175 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 176 | 'callback' => [ $this, 'rest_helpers_count_posts' ], |
| 177 | ) ); |
| 178 | register_rest_route( $this->namespace, '/helpers/posts_ids', array( |
| 179 | 'methods' => 'GET', |
| 180 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 181 | 'callback' => [ $this, 'rest_helpers_posts_ids' ], |
| 182 | ) ); |
| 183 | register_rest_route( $this->namespace, '/helpers/post_types', array( |
| 184 | 'methods' => 'GET', |
| 185 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 186 | 'callback' => [ $this, 'rest_helpers_post_types' ], |
| 187 | ) ); |
| 188 | register_rest_route( $this->namespace, '/helpers/post_content', array( |
| 189 | 'methods' => 'GET', |
| 190 | 'permission_callback' => [ $this->core, 'can_access_features' ], |
| 191 | 'callback' => [ $this, 'rest_helpers_post_content' ], |
| 192 | ) ); |
| 193 | |
| 194 | // OpenAI Endpoints |
| 195 | register_rest_route( $this->namespace, '/openai/files/list', array( |
| 196 | 'methods' => 'GET', |
| 197 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 198 | 'callback' => [ $this, 'rest_openai_files_get' ], |
| 199 | ) ); |
| 200 | register_rest_route( $this->namespace, '/openai/files/upload', array( |
| 201 | 'methods' => 'POST', |
| 202 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 203 | 'callback' => [ $this, 'rest_openai_files_upload' ], |
| 204 | ) ); |
| 205 | register_rest_route( $this->namespace, '/openai/files/delete', array( |
| 206 | 'methods' => 'POST', |
| 207 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 208 | 'callback' => [ $this, 'rest_openai_files_delete' ], |
| 209 | ) ); |
| 210 | register_rest_route( $this->namespace, '/openai/files/download', array( |
| 211 | 'methods' => 'POST', |
| 212 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 213 | 'callback' => [ $this, 'rest_openai_files_download' ], |
| 214 | ) ); |
| 215 | register_rest_route( $this->namespace, '/openai/files/finetune', array( |
| 216 | 'methods' => 'POST', |
| 217 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 218 | 'callback' => [ $this, 'rest_openai_files_finetune' ], |
| 219 | ) ); |
| 220 | register_rest_route( $this->namespace, '/openai/finetunes/list_deleted', array( |
| 221 | 'methods' => 'GET', |
| 222 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 223 | 'callback' => [ $this, 'rest_openai_deleted_finetunes_get' ], |
| 224 | ) ); |
| 225 | |
| 226 | // register_rest_route( $this->namespace, '/openai/models', array( |
| 227 | // 'methods' => 'GET', |
| 228 | // 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 229 | // 'callback' => [ $this, 'rest_openai_models_get' ], |
| 230 | // ) ); |
| 231 | |
| 232 | register_rest_route( $this->namespace, '/openai/finetunes/list', array( |
| 233 | 'methods' => 'GET', |
| 234 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 235 | 'callback' => [ $this, 'rest_openai_finetunes_get' ], |
| 236 | ) ); |
| 237 | register_rest_route( $this->namespace, '/openai/finetunes/delete', array( |
| 238 | 'methods' => 'POST', |
| 239 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 240 | 'callback' => [ $this, 'rest_openai_finetunes_delete' ], |
| 241 | ) ); |
| 242 | register_rest_route( $this->namespace, '/openai/finetunes/cancel', array( |
| 243 | 'methods' => 'POST', |
| 244 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 245 | 'callback' => [ $this, 'rest_openai_finetunes_cancel' ], |
| 246 | ) ); |
| 247 | register_rest_route( $this->namespace, '/openai/incidents', array( |
| 248 | 'methods' => 'GET', |
| 249 | 'permission_callback' => [ $this->core, 'can_access_settings' ], |
| 250 | 'callback' => [ $this, 'rest_openai_incidents' ], |
| 251 | ) ); |
| 252 | |
| 253 | // Logging Endpoints |
| 254 | register_rest_route( $this->namespace, '/get_logs', array( |
| 255 | 'methods' => 'GET', |
| 256 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 257 | 'callback' => array( $this, 'rest_get_logs' ) |
| 258 | ) ); |
| 259 | register_rest_route( $this->namespace, '/clear_logs', array( |
| 260 | 'methods' => 'GET', |
| 261 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 262 | 'callback' => array( $this, 'rest_clear_logs' ) |
| 263 | ) ); |
| 264 | } |
| 265 | catch ( Exception $e ) { |
| 266 | var_dump( $e ); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | function rest_start_session() { |
| 271 | try { |
| 272 | $sessionId = $this->core->get_session_id(); |
| 273 | $restNonce = $this->core->get_nonce( true ); |
| 274 | return new WP_REST_Response( [ |
| 275 | 'success' => true, |
| 276 | 'sessionId' => $sessionId, |
| 277 | 'restNonce' => $restNonce |
| 278 | ], 200 ); |
| 279 | } |
| 280 | catch ( Exception $e ) { |
| 281 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 282 | return new WP_REST_Response( [ 'success' => false, 'message' => $message ], 500 ); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | function rest_settings_list() { |
| 287 | return new WP_REST_Response( [ |
| 288 | 'success' => true, |
| 289 | 'options' => $this->core->get_all_options() |
| 290 | ], 200 ); |
| 291 | } |
| 292 | |
| 293 | function rest_settings_update( $request ) { |
| 294 | try { |
| 295 | $params = $request->get_json_params(); |
| 296 | $value = $params['options']; |
| 297 | $options = $this->core->update_options( $value ); |
| 298 | $success = !!$options; |
| 299 | $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' ); |
| 300 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 ); |
| 301 | } |
| 302 | catch ( Exception $e ) { |
| 303 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 304 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | function rest_settings_reset() { |
| 309 | try { |
| 310 | $options = $this->core->reset_options(); |
| 311 | $success = !!$options; |
| 312 | $message = __( $success ? 'OK' : "Could not reset options.", 'ai-engine' ); |
| 313 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 ); |
| 314 | } |
| 315 | catch ( Exception $e ) { |
| 316 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 317 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | function rest_ai_models( $request ) { |
| 322 | try { |
| 323 | $params = $request->get_json_params(); |
| 324 | $envId = $params['envId']; |
| 325 | $engine = Meow_MWAI_Engines_Factory::get( $this->core, $envId ); |
| 326 | $models = $engine->retrieve_models(); |
| 327 | return new WP_REST_Response([ 'success' => true, 'models' => $models ], 200 ); |
| 328 | } |
| 329 | catch ( Exception $e ) { |
| 330 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 331 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | function rest_ai_completions( $request ) { |
| 336 | try { |
| 337 | $params = $request->get_json_params(); |
| 338 | $message = $this->retrieve_message( $params ); |
| 339 | $query = new Meow_MWAI_Query_Text( $message ); |
| 340 | $query->inject_params( $params ); |
| 341 | |
| 342 | // Handle streaming |
| 343 | $stream = $params['stream'] ?? false; |
| 344 | $streamCallback = null; |
| 345 | if ( $stream ) { |
| 346 | $streamCallback = function( $reply ) { |
| 347 | //$raw = _wp_specialchars( $reply, ENT_NOQUOTES, 'UTF-8', true ); |
| 348 | $raw = $reply; |
| 349 | $this->core->stream_push( [ 'type' => 'live', 'data' => $raw ] ); |
| 350 | if ( ob_get_level() > 0 ) { |
| 351 | ob_flush(); |
| 352 | } |
| 353 | flush(); |
| 354 | }; |
| 355 | header( 'Cache-Control: no-cache' ); |
| 356 | header( 'Content-Type: text/event-stream' ); |
| 357 | header( 'X-Accel-Buffering: no' ); // This is useful to disable buffering in nginx through headers. |
| 358 | ob_implicit_flush( true ); |
| 359 | ob_end_flush(); |
| 360 | } |
| 361 | |
| 362 | // Process Reply |
| 363 | $reply = $this->core->run_query( $query, $streamCallback ); |
| 364 | $restRes = [ |
| 365 | 'success' => true, |
| 366 | 'data' => $reply->result, |
| 367 | 'usage' => $reply->usage |
| 368 | ]; |
| 369 | if ( $stream ) { |
| 370 | $this->core->stream_push( [ 'type' => 'end', 'data' => json_encode( $restRes ) ] ); |
| 371 | die(); |
| 372 | } |
| 373 | return new WP_REST_Response( $restRes, 200 ); |
| 374 | } |
| 375 | catch ( Exception $e ) { |
| 376 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 377 | if ( $stream ) { |
| 378 | $this->core->stream_push( [ 'type' => 'error', 'data' => $message ] ); |
| 379 | } |
| 380 | else { |
| 381 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | function rest_ai_images( $request ) { |
| 387 | try { |
| 388 | $params = $request->get_json_params(); |
| 389 | $message = $this->retrieve_message( $params ); |
| 390 | $query = new Meow_MWAI_Query_Image( $message ); |
| 391 | $query->inject_params( $params ); |
| 392 | $reply = $this->core->run_query( $query ); |
| 393 | return new WP_REST_Response([ 'success' => true, 'data' => $reply->results, 'usage' => $reply->usage ], 200 ); |
| 394 | } |
| 395 | catch ( Exception $e ) { |
| 396 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 397 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | function rest_ai_magic_wand( $request ) { |
| 402 | try { |
| 403 | $params = $request->get_json_params(); |
| 404 | $action = isset( $params['action'] ) ? $params['action'] : null; |
| 405 | $data = isset( $params['data'] ) ? $params['data'] : null; |
| 406 | if ( empty( $data ) || empty( $action ) ) { |
| 407 | return new WP_REST_Response([ 'success' => false, 'message' => "An action and some data are required." ], 500 ); |
| 408 | } |
| 409 | $data = apply_filters( 'mwai_magic_wand_' . $action, "", $data ); |
| 410 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 411 | } |
| 412 | catch ( Exception $e ) { |
| 413 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 414 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | function rest_ai_copilot( $request ) { |
| 419 | try { |
| 420 | $params = $request->get_json_params(); |
| 421 | $action = sanitize_text_field( $params['action'] ); |
| 422 | $message = $this->retrieve_message( $params, true ); |
| 423 | if ( empty( $action ) || empty( $message ) ) { |
| 424 | return new WP_REST_Response([ 'success' => false, 'message' => "Copilot needs an action and a prompt." ], 500 ); |
| 425 | } |
| 426 | $query = new Meow_MWAI_Query_Text( $message, 2048 ); |
| 427 | $query->set_scope( 'admin-tools' ); |
| 428 | $model = $this->core->get_option( 'ai_default_model' ); |
| 429 | $env = $this->core->get_option( 'ai_default_env' ); |
| 430 | if ( !empty( $env ) ) { |
| 431 | $query->set_env_id( $env ); |
| 432 | } |
| 433 | if ( !empty( $model ) ) { |
| 434 | $query->set_model( $model ); |
| 435 | } |
| 436 | $reply = $this->core->run_query( $query ); |
| 437 | return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 ); |
| 438 | } |
| 439 | catch ( Exception $e ) { |
| 440 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 441 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | function rest_helpers_update_title( $request ) { |
| 446 | try { |
| 447 | $params = $request->get_json_params(); |
| 448 | $title = sanitize_text_field( $params['title'] ); |
| 449 | $postId = intval( $params['postId'] ); |
| 450 | $post = get_post( $postId ); |
| 451 | if ( !$post ) { |
| 452 | throw new Exception( 'There is no post with this ID.' ); |
| 453 | } |
| 454 | $post->post_title = $title; |
| 455 | //$post->post_name = sanitize_title( $title ); |
| 456 | wp_update_post( $post ); |
| 457 | return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 ); |
| 458 | } |
| 459 | catch ( Exception $e ) { |
| 460 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 461 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | function rest_helpers_update_excerpt( $request ) { |
| 466 | try { |
| 467 | $params = $request->get_json_params(); |
| 468 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 469 | $postId = intval( $params['postId'] ); |
| 470 | $post = get_post( $postId ); |
| 471 | if ( !$post ) { |
| 472 | throw new Exception( 'There is no post with this ID.' ); |
| 473 | } |
| 474 | $post->post_excerpt = $excerpt; |
| 475 | wp_update_post( $post ); |
| 476 | return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 ); |
| 477 | } |
| 478 | catch ( Exception $e ) { |
| 479 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 480 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | function rest_helpers_create_post( $request ) { |
| 485 | try { |
| 486 | $params = $request->get_json_params(); |
| 487 | $title = sanitize_text_field( $params['title'] ); |
| 488 | $content = sanitize_textarea_field( $params['content'] ); |
| 489 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 490 | $postType = sanitize_text_field( $params['postType'] ); |
| 491 | $post = new stdClass(); |
| 492 | $post->post_title = $title; |
| 493 | $post->post_excerpt = $excerpt; |
| 494 | $post->post_content = $content; |
| 495 | $post->post_status = 'draft'; |
| 496 | $post->post_type = isset( $postType ) ? $postType : 'post'; |
| 497 | // TODO: Let's try to avoid using Markdown to create the Post |
| 498 | // Instead, we should create Gutenberg Blocks, or simple HTML. |
| 499 | // Then, we can get rid of the library for Markdown. |
| 500 | $post->post_content = $this->core->markdown_to_html( $post->post_content ); |
| 501 | $postId = wp_insert_post( $post ); |
| 502 | return new WP_REST_Response([ 'success' => true, 'postId' => $postId ], 200 ); |
| 503 | } |
| 504 | catch ( Exception $e ) { |
| 505 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 506 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | function rest_helpers_create_images( $request ) { |
| 511 | try { |
| 512 | $params = $request->get_json_params(); |
| 513 | $title = sanitize_text_field( $params['title'] ); |
| 514 | $caption = sanitize_text_field( $params['caption'] ); |
| 515 | $alt = sanitize_text_field( $params['alt'] ); |
| 516 | $description = sanitize_text_field( $params['description'] ); |
| 517 | $url = $params['url']; |
| 518 | $filename = sanitize_text_field( $params['filename'] ); |
| 519 | $attachmentId = $this->core->add_image_from_url( $url, $filename, $title, $description, $caption, $alt ); |
| 520 | return new WP_REST_Response([ 'success' => true, 'attachmentId' => $attachmentId ], 200 ); |
| 521 | } |
| 522 | catch ( Exception $e ) { |
| 523 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 524 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | function rest_openai_files_get() { |
| 529 | try { |
| 530 | $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null; |
| 531 | $purposeFilter = isset( $_GET['purpose'] ) ? $_GET['purpose'] : null; |
| 532 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 533 | $files = $openai->list_files( $purposeFilter ); |
| 534 | return new WP_REST_Response([ 'success' => true, 'files' => $files ], 200 ); |
| 535 | } |
| 536 | catch ( Exception $e ) { |
| 537 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 538 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | function rest_openai_deleted_finetunes_get() { |
| 543 | try { |
| 544 | $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null; |
| 545 | $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false; |
| 546 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 547 | $finetunes = $openai->list_deleted_finetunes( $legacy ); |
| 548 | return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 ); |
| 549 | } |
| 550 | catch ( Exception $e ) { |
| 551 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 552 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | function rest_openai_finetunes_get() { |
| 557 | try { |
| 558 | $envId = isset( $_GET['envId'] ) ? $_GET['envId'] : null; |
| 559 | $legacy = isset( $_GET['legacy'] ) ? $_GET['legacy'] === 'true' : false; |
| 560 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 561 | $finetunes = $openai->list_finetunes( $legacy ); |
| 562 | return new WP_REST_Response([ 'success' => true, 'finetunes' => $finetunes ], 200 ); |
| 563 | } |
| 564 | catch ( Exception $e ) { |
| 565 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 566 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | function rest_openai_files_upload( $request ) { |
| 571 | try { |
| 572 | $params = $request->get_json_params(); |
| 573 | $envId = $params['envId'];; |
| 574 | $filename = sanitize_text_field( $params['filename'] ); |
| 575 | $data = $params['data']; |
| 576 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 577 | $file = $openai->upload_file( $filename, $data ); |
| 578 | return new WP_REST_Response([ 'success' => true, 'file' => $file ], 200 ); |
| 579 | } |
| 580 | catch ( Exception $e ) { |
| 581 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 582 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | function rest_openai_files_delete( $request ) { |
| 587 | try { |
| 588 | $params = $request->get_json_params(); |
| 589 | $envId = $params['envId'];; |
| 590 | $fileId = $params['fileId']; |
| 591 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 592 | $openai->delete_file( $fileId ); |
| 593 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 594 | } |
| 595 | catch ( Exception $e ) { |
| 596 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 597 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | function rest_openai_finetunes_cancel( $request ) { |
| 602 | try { |
| 603 | $params = $request->get_json_params(); |
| 604 | $envId = $params['envId'];; |
| 605 | $finetuneId = $params['finetuneId']; |
| 606 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 607 | $openai->cancel_finetune( $finetuneId ); |
| 608 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 609 | } |
| 610 | catch ( Exception $e ) { |
| 611 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 612 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | function rest_openai_finetunes_delete( $request ) { |
| 617 | try { |
| 618 | $params = $request->get_json_params(); |
| 619 | $envId = $params['envId'];; |
| 620 | $modelId = $params['modelId']; |
| 621 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 622 | $openai->delete_finetune( $modelId ); |
| 623 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 624 | } |
| 625 | catch ( Exception $e ) { |
| 626 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 627 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | function rest_openai_files_download( $request ) { |
| 632 | try { |
| 633 | $params = $request->get_json_params(); |
| 634 | $envId = $params['envId'];; |
| 635 | $fileId = $params['fileId']; |
| 636 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 637 | $filename = $openai->download_file( $fileId ); |
| 638 | $data = file_get_contents( $filename ); |
| 639 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 640 | } |
| 641 | catch ( Exception $e ) { |
| 642 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 643 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | function rest_openai_files_finetune( $request ) { |
| 648 | try { |
| 649 | $params = $request->get_json_params(); |
| 650 | $envId = $params['envId'];; |
| 651 | $fileId = $params['fileId']; |
| 652 | $model = $params['model']; |
| 653 | $suffix = $params['suffix']; |
| 654 | $hyperparams = [ |
| 655 | "nEpochs" => isset( $params['nEpochs'] ) ? $params['nEpochs'] : null, |
| 656 | "batchSize" => isset( $params['batchSize'] ) ? $params['batchSize'] : null, |
| 657 | ]; |
| 658 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 659 | $finetune = $openai->run_finetune( $fileId, $model, $suffix, $hyperparams ); |
| 660 | return new WP_REST_Response([ 'success' => true, 'finetune' => $finetune ], 200 ); |
| 661 | } |
| 662 | catch ( Exception $e ) { |
| 663 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 664 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | function rest_openai_incidents() { |
| 669 | try { |
| 670 | $transient = get_transient( 'mwai_openai_incidents' ); |
| 671 | if ( $transient ) { |
| 672 | return new WP_REST_Response([ 'success' => true, 'incidents' => $transient ], 200 ); |
| 673 | } |
| 674 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core ); |
| 675 | $incidents = $openai->get_incidents(); |
| 676 | set_transient( 'mwai_openai_incidents', $incidents, 60 * 10 ); |
| 677 | return new WP_REST_Response([ 'success' => true, 'incidents' => $incidents ], 200 ); |
| 678 | } |
| 679 | catch ( Exception $e ) { |
| 680 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 681 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | function rest_helpers_count_posts( $request ) { |
| 686 | try { |
| 687 | $params = $request->get_query_params(); |
| 688 | $postType = $params['postType']; |
| 689 | $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ]; |
| 690 | $count = wp_count_posts( $postType ); |
| 691 | $count = array_sum( array_intersect_key( (array)$count, array_flip( $postStatus ) ) ); |
| 692 | return new WP_REST_Response([ 'success' => true, 'count' => $count ], 200 ); |
| 693 | } |
| 694 | catch ( Exception $e ) { |
| 695 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 696 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | function rest_helpers_posts_ids( $request ) { |
| 701 | try { |
| 702 | $params = $request->get_query_params(); |
| 703 | $postType = $params['postType']; |
| 704 | $postStatus = !empty( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ]; |
| 705 | $posts = get_posts( [ |
| 706 | 'posts_per_page' => -1, |
| 707 | 'post_type' => $postType, |
| 708 | 'post_status' => $postStatus, |
| 709 | 'fields' => 'ids' |
| 710 | ] ); |
| 711 | return new WP_REST_Response([ 'success' => true, 'postIds' => $posts ], 200 ); |
| 712 | } |
| 713 | catch ( Exception $e ) { |
| 714 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 715 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | function rest_helpers_post_content( $request ) { |
| 720 | try { |
| 721 | $params = $request->get_query_params(); |
| 722 | $offset = (int)$params['offset']; |
| 723 | $postType = $params['postType']; |
| 724 | $postStatus = isset( $params['postStatus'] ) ? explode( ',', $params['postStatus'] ) : [ 'publish' ]; |
| 725 | $postId = (int)$params['postId']; |
| 726 | |
| 727 | $post = null; |
| 728 | if ( !empty( $postId ) ) { |
| 729 | $post = get_post( $postId ); |
| 730 | if ( $post->post_status !== 'publish' && $post->post_status !== 'future' |
| 731 | && $post->post_status !== 'draft' && $post->post_status !== 'private' ) { |
| 732 | $post = null; |
| 733 | } |
| 734 | } |
| 735 | else { |
| 736 | $posts = get_posts( [ |
| 737 | 'posts_per_page' => 1, |
| 738 | 'post_type' => $postType, |
| 739 | 'offset' => $offset, |
| 740 | 'post_status' => $postStatus, |
| 741 | ] ); |
| 742 | $post = count( $posts ) === 0 ? null : $posts[0]; |
| 743 | } |
| 744 | if ( !$post ) { |
| 745 | return new WP_REST_Response([ 'success' => false, 'message' => 'Post not found' ], 404 ); |
| 746 | } |
| 747 | $cleanPost = $this->core->get_post( $post ); |
| 748 | return new WP_REST_Response([ 'success' => true, 'content' => $cleanPost['content'], |
| 749 | 'checksum' => $cleanPost['checksum'], 'language' => $cleanPost['language'], 'excerpt' => $cleanPost['excerpt'], |
| 750 | 'postId' => $cleanPost['postId'], 'title' => $cleanPost['title'], 'url' => $cleanPost['url'] ], 200 ); |
| 751 | } |
| 752 | catch ( Exception $e ) { |
| 753 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 754 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | function rest_system_templates_get( $request ) { |
| 759 | try { |
| 760 | $params = $request->get_query_params(); |
| 761 | $category = $params['category']; |
| 762 | $templates = []; |
| 763 | $templates_option = get_option( 'mwai_templates', [] ); |
| 764 | if ( !is_array( $templates_option ) ) { |
| 765 | update_option( 'mwai_templates', [] ); |
| 766 | } |
| 767 | $categories = array_column( $templates_option, 'category' ); |
| 768 | $index = array_search( $category, $categories ); |
| 769 | $templates = []; |
| 770 | if ( $index !== false ) { |
| 771 | $templates = $templates_option[$index]['templates']; |
| 772 | } |
| 773 | return new WP_REST_Response([ 'success' => true, 'templates' => $templates ], 200 ); |
| 774 | } |
| 775 | catch ( Exception $e ) { |
| 776 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 777 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | function rest_system_templates_save( $request ) { |
| 782 | try { |
| 783 | $params = $request->get_json_params(); |
| 784 | $category = $params['category']; |
| 785 | $templates = $params['templates']; |
| 786 | $templates_option = get_option( 'mwai_templates', [] ); |
| 787 | $categories = array_column( $templates_option, 'category' ); |
| 788 | $index = array_search( $category, $categories ); |
| 789 | if ( $index !== false && $index >= 0 ) { |
| 790 | $templates_option[$index]['templates'] = $templates; |
| 791 | } |
| 792 | else { |
| 793 | $group = [ 'category' => $category, 'templates' => $templates ]; |
| 794 | $templates_option[] = $group; |
| 795 | } |
| 796 | |
| 797 | update_option( 'mwai_templates', $templates_option ); |
| 798 | return new WP_REST_Response([ 'success' => true ], 200 ); |
| 799 | } |
| 800 | catch ( Exception $e ) { |
| 801 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 802 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | function rest_system_logs_list( $request ) { |
| 807 | try { |
| 808 | $params = $request->get_json_params(); |
| 809 | $offset = $params['offset']; |
| 810 | $limit = $params['limit']; |
| 811 | $filters = $params['filters']; |
| 812 | $sort = $params['sort']; |
| 813 | $logs = apply_filters( 'mwai_stats_logs_list', [], $offset, $limit, $filters, $sort ); |
| 814 | return new WP_REST_Response([ 'success' => true, 'total' => $logs['total'], 'logs' => $logs['rows'] ], 200 ); |
| 815 | } |
| 816 | catch ( Exception $e ) { |
| 817 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 818 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | function rest_system_logs_delete( $request ) { |
| 823 | try { |
| 824 | $params = $request->get_json_params(); |
| 825 | $logIds = $params['logIds']; |
| 826 | $success = apply_filters( 'mwai_stats_logs_delete', true, $logIds ); |
| 827 | return new WP_REST_Response([ 'success' => $success ], 200 ); |
| 828 | } |
| 829 | catch ( Exception $e ) { |
| 830 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 831 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | function rest_system_logs_meta_get( $request ) { |
| 836 | try { |
| 837 | $params = $request->get_json_params(); |
| 838 | $logId = $params['logId']; |
| 839 | $metaKeys = $params['metaKeys']; |
| 840 | $data = apply_filters( 'mwai_stats_logs_meta', [], $logId, $metaKeys ); |
| 841 | return new WP_REST_Response([ 'success' => true, 'data' => $data ], 200 ); |
| 842 | } |
| 843 | catch ( Exception $e ) { |
| 844 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 845 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | function rest_ai_moderate( $request ) { |
| 850 | try { |
| 851 | $params = $request->get_json_params(); |
| 852 | $envId = $params['envId']; |
| 853 | $text = $params['text']; |
| 854 | if ( !$text ) { |
| 855 | return new WP_REST_Response([ 'success' => false, 'message' => 'Text not found.' ], 404 ); |
| 856 | } |
| 857 | $openai = Meow_MWAI_Engines_Factory::get_openai( $this->core, $envId ); |
| 858 | $results = $openai->moderate( $text ); |
| 859 | return new WP_REST_Response([ 'success' => true, 'results' => $results ], 200 ); |
| 860 | } |
| 861 | catch ( Exception $e ) { |
| 862 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 863 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | function rest_ai_transcribe_audio( $request ) { |
| 868 | try { |
| 869 | $params = $request->get_json_params(); |
| 870 | $query = new Meow_MWAI_Query_Transcribe(); |
| 871 | $query->inject_params( $params ); |
| 872 | $query->set_scope('admin-tools'); |
| 873 | $reply = $this->core->run_query( $query ); |
| 874 | return new WP_REST_Response([ 'success' => true, 'data' => $reply->result ], 200 ); |
| 875 | } |
| 876 | catch ( Exception $e ) { |
| 877 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 878 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | function rest_ai_transcribe_image( $request ) { |
| 883 | try { |
| 884 | global $mwai; |
| 885 | $params = $request->get_json_params(); |
| 886 | $message = $this->retrieve_message( $params ); |
| 887 | $url = !empty( $params['url'] ) ? $params['url'] : null; |
| 888 | // This could lead to a security issue, so let's avoid using path directly. |
| 889 | //$path = !empty( $params['path'] ) ? $params['path'] : null; |
| 890 | $result = $mwai->simpleVisionQuery( $message, $url ); |
| 891 | return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 ); |
| 892 | } |
| 893 | catch ( Exception $e ) { |
| 894 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 895 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | function rest_ai_json( $request ) { |
| 900 | try { |
| 901 | global $mwai; |
| 902 | $params = $request->get_json_params(); |
| 903 | $message = $this->retrieve_message( $params ); |
| 904 | $result = $mwai->simpleJsonQuery( $message ); |
| 905 | return new WP_REST_Response([ 'success' => true, 'data' => $result ], 200 ); |
| 906 | } |
| 907 | catch ( Exception $e ) { |
| 908 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 909 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | function rest_helpers_post_types() { |
| 914 | try { |
| 915 | $postTypes = $this->core->get_post_types(); |
| 916 | return new WP_REST_Response([ 'success' => true, 'postTypes' => $postTypes ], 200 ); |
| 917 | } |
| 918 | catch ( Exception $e ) { |
| 919 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 920 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | function rest_settings_themes( $request ) { |
| 925 | try { |
| 926 | $method = $request->get_method(); |
| 927 | if ( $method === 'GET' ) { |
| 928 | $themes = $this->core->get_themes(); |
| 929 | return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 ); |
| 930 | } |
| 931 | else if ( $method === 'POST' ) { |
| 932 | $params = $request->get_json_params(); |
| 933 | $themes = $params['themes']; |
| 934 | $themes = $this->core->update_themes( $themes ); |
| 935 | return new WP_REST_Response([ 'success' => true, 'themes' => $themes ], 200 ); |
| 936 | } |
| 937 | } |
| 938 | catch ( Exception $e ) { |
| 939 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 940 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | function rest_settings_chatbots( $request ) { |
| 945 | try { |
| 946 | $method = $request->get_method(); |
| 947 | if ( $method === 'GET' ) { |
| 948 | $chatbots = $this->core->get_chatbots(); |
| 949 | return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 ); |
| 950 | } |
| 951 | else if ( $method === 'POST' ) { |
| 952 | $params = $request->get_json_params(); |
| 953 | $chatbots = $params['chatbots']; |
| 954 | $chatbots = $this->core->update_chatbots( $chatbots ); |
| 955 | return new WP_REST_Response([ 'success' => true, 'chatbots' => $chatbots ], 200 ); |
| 956 | } |
| 957 | return new WP_REST_Response([ 'success' => false, 'message' => 'Method not allowed' ], 405 ); |
| 958 | } |
| 959 | catch ( Exception $e ) { |
| 960 | $message = apply_filters( 'mwai_ai_exception', $e->getMessage() ); |
| 961 | return new WP_REST_Response([ 'success' => false, 'message' => $message ], 500 ); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | #region Logs |
| 966 | |
| 967 | function rest_get_logs() { |
| 968 | $logs = $this->core->get_logs(); |
| 969 | return new WP_REST_Response( [ 'success' => true, 'data' => $logs ], 200 ); |
| 970 | } |
| 971 | |
| 972 | function rest_clear_logs() { |
| 973 | $this->core->clear_logs(); |
| 974 | return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 975 | } |
| 976 | |
| 977 | |
| 978 | #endregion |
| 979 | } |
| 980 |