admin.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
rest.php
3 years ago
ui.php
3 years ago
rest.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Rest |
| 4 | { |
| 5 | private $admin = null; |
| 6 | private $core = null; |
| 7 | private $namespace = 'ai-engine/v1'; |
| 8 | |
| 9 | public function __construct( $core, $admin ) { |
| 10 | if ( !current_user_can( 'administrator' ) ) { |
| 11 | return; |
| 12 | } |
| 13 | $this->core = $core; |
| 14 | $this->admin = $admin; |
| 15 | add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 16 | } |
| 17 | |
| 18 | function rest_api_init() { |
| 19 | try { |
| 20 | // SETTINGS |
| 21 | register_rest_route( $this->namespace, '/update_option', array( |
| 22 | 'methods' => 'POST', |
| 23 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 24 | 'callback' => array( $this, 'rest_update_option' ) |
| 25 | ) ); |
| 26 | register_rest_route( $this->namespace, '/all_settings', array( |
| 27 | 'methods' => 'GET', |
| 28 | 'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 29 | 'callback' => array( $this, 'rest_all_settings' ), |
| 30 | ) ); |
| 31 | register_rest_route( $this->namespace, '/completions', array( |
| 32 | 'methods' => 'POST', |
| 33 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 34 | 'callback' => array( $this, 'completions' ), |
| 35 | ) ); |
| 36 | register_rest_route( $this->namespace, '/generate_titles', array( |
| 37 | 'methods' => 'POST', |
| 38 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 39 | 'callback' => array( $this, 'generate_titles' ), |
| 40 | ) ); |
| 41 | register_rest_route( $this->namespace, '/generate_excerpts', array( |
| 42 | 'methods' => 'POST', |
| 43 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 44 | 'callback' => array( $this, 'generate_excerpts' ), |
| 45 | ) ); |
| 46 | register_rest_route( $this->namespace, '/update_post_title', array( |
| 47 | 'methods' => 'POST', |
| 48 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 49 | 'callback' => array( $this, 'update_post_title' ), |
| 50 | ) ); |
| 51 | register_rest_route( $this->namespace, '/update_post_excerpt', array( |
| 52 | 'methods' => 'POST', |
| 53 | 'permission_callback' => array( $this->core, 'can_access_features' ), |
| 54 | 'callback' => array( $this, 'update_post_excerpt' ), |
| 55 | ) ); |
| 56 | } |
| 57 | catch ( Exception $e ) { |
| 58 | var_dump( $e ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | function rest_all_settings() { |
| 63 | return new WP_REST_Response( [ |
| 64 | 'success' => true, |
| 65 | 'data' => $this->admin->get_all_options() |
| 66 | ], 200 ); |
| 67 | } |
| 68 | |
| 69 | function rest_update_option( $request ) { |
| 70 | try { |
| 71 | $params = $request->get_json_params(); |
| 72 | $value = $params['options']; |
| 73 | $options = $this->core->update_options( $value ); |
| 74 | $success = !!$options; |
| 75 | $message = __( $success ? 'OK' : "Could not update options.", 'ai-engine' ); |
| 76 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $options ], 200 ); |
| 77 | } |
| 78 | catch ( Exception $e ) { |
| 79 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | function validate_updated_option( $option_name ) { |
| 84 | $option_checkbox = get_option( 'mwai_option_checkbox', false ); |
| 85 | $option_text = get_option( 'mwai_option_text', 'Default' ); |
| 86 | if ( $option_checkbox === '' ) |
| 87 | update_option( 'mwai_option_checkbox', false ); |
| 88 | if ( $option_text === '' ) |
| 89 | update_option( 'mwai_option_text', 'Default' ); |
| 90 | return $this->createValidationResult(); |
| 91 | } |
| 92 | |
| 93 | function generate_titles( $request ) { |
| 94 | try { |
| 95 | $params = $request->get_json_params(); |
| 96 | $postId = intval( $params['postId'] ); |
| 97 | $titles = $this->core->generate_titles( $postId ); |
| 98 | $success = !!$titles; |
| 99 | $message = __( $success ? 'OK' : "Could not fetch titles.", 'ai-engine' ); |
| 100 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'titles' => $titles ], 200 ); |
| 101 | } |
| 102 | catch ( Exception $e ) { |
| 103 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | function generate_excerpts( $request ) { |
| 108 | try { |
| 109 | $params = $request->get_json_params(); |
| 110 | $postId = intval( $params['postId'] ); |
| 111 | $excerpts = $this->core->generate_excerpts( $postId ); |
| 112 | $success = !!$excerpts; |
| 113 | $message = __( $success ? 'OK' : "Could not fetch excerpts.", 'ai-engine' ); |
| 114 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'excerpts' => $excerpts ], 200 ); |
| 115 | } |
| 116 | catch ( Exception $e ) { |
| 117 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | function update_post_title( $request ) { |
| 122 | try { |
| 123 | $params = $request->get_json_params(); |
| 124 | $title = sanitize_text_field( $params['title'] ); |
| 125 | $this->core->update_post_title( $params['postId'], $title ); |
| 126 | return new WP_REST_Response([ 'success' => true, 'message' => "Title updated." ], 200 ); |
| 127 | } |
| 128 | catch ( Exception $e ) { |
| 129 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | function update_post_excerpt( $request ) { |
| 134 | try { |
| 135 | $params = $request->get_json_params(); |
| 136 | $excerpt = sanitize_text_field( $params['excerpt'] ); |
| 137 | $this->core->update_post_excerpt( $params['postId'], $excerpt ); |
| 138 | return new WP_REST_Response([ 'success' => true, 'message' => "Excerpt updated." ], 200 ); |
| 139 | } |
| 140 | catch ( Exception $e ) { |
| 141 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | function completions( $request ) { |
| 146 | try { |
| 147 | $params = $request->get_json_params(); |
| 148 | $res = $this->core->get_completions( $params['prompt'] ); |
| 149 | $success = !!$res; |
| 150 | $message = __( $success ? 'OK' : "Could not fetch completions.", 'ai-engine' ); |
| 151 | return new WP_REST_Response([ 'success' => $success, 'message' => $message, |
| 152 | 'data' => $res['data'], 'usage' => $res['usage'] ], 200 ); |
| 153 | } |
| 154 | catch ( Exception $e ) { |
| 155 | return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | function createValidationResult( $result = true, $message = null) { |
| 160 | $message = $message ? $message : __( 'OK', 'ai-engine' ); |
| 161 | return ['result' => $result, 'message' => $message]; |
| 162 | } |
| 163 | } |
| 164 |