settings.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Rest_Settings extends Meow_MWAI_Rest_Base { |
| 4 | public function register_routes() { |
| 5 | register_rest_route( $this->namespace, '/settings/update', [ |
| 6 | 'methods' => 'POST', |
| 7 | 'callback' => [ $this, 'rest_settings_update' ], |
| 8 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 9 | ] ); |
| 10 | register_rest_route( $this->namespace, '/settings/options', [ |
| 11 | 'methods' => 'GET', |
| 12 | 'callback' => [ $this, 'rest_settings_options' ], |
| 13 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 14 | ] ); |
| 15 | register_rest_route( $this->namespace, '/settings/reset', [ |
| 16 | 'methods' => 'POST', |
| 17 | 'callback' => [ $this, 'rest_settings_reset' ], |
| 18 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 19 | ] ); |
| 20 | register_rest_route( $this->namespace, '/settings/chatbots', [ |
| 21 | 'methods' => 'GET', |
| 22 | 'callback' => [ $this, 'rest_settings_get_chatbots' ], |
| 23 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 24 | ] ); |
| 25 | register_rest_route( $this->namespace, '/settings/chatbots', [ |
| 26 | 'methods' => 'POST', |
| 27 | 'callback' => [ $this, 'rest_settings_update_chatbots' ], |
| 28 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 29 | ] ); |
| 30 | register_rest_route( $this->namespace, '/settings/themes', [ |
| 31 | 'methods' => 'GET', |
| 32 | 'callback' => [ $this, 'rest_settings_get_themes' ], |
| 33 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 34 | ] ); |
| 35 | register_rest_route( $this->namespace, '/settings/themes', [ |
| 36 | 'methods' => 'POST', |
| 37 | 'callback' => [ $this, 'rest_settings_update_themes' ], |
| 38 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 39 | ] ); |
| 40 | register_rest_route( $this->namespace, '/settings/reset-usage', [ |
| 41 | 'methods' => 'POST', |
| 42 | 'callback' => [ $this, 'rest_settings_reset_usage' ], |
| 43 | 'permission_callback' => [ $this->core, 'check_rest_nonce' ] |
| 44 | ] ); |
| 45 | } |
| 46 | |
| 47 | public function rest_settings_update( $request ) { |
| 48 | try { |
| 49 | $params = $request->get_json_params(); |
| 50 | $filters_options = $params['options']; |
| 51 | $this->core->update_options( $filters_options ); |
| 52 | $this->core->update_options( [ |
| 53 | 'module_suggestions' => isset( $params['options']['module_suggestions'] ), |
| 54 | 'module_chatbots' => isset( $params['options']['module_chatbots'] ), |
| 55 | 'module_search' => isset( $params['options']['module_search'] ), |
| 56 | 'module_tasks' => isset( $params['options']['module_tasks'] ), |
| 57 | 'module_advisor' => isset( $params['options']['module_advisor'] ), |
| 58 | ] ); |
| 59 | return $this->create_rest_response( [ 'success' => true ], 200 ); |
| 60 | } |
| 61 | catch ( Exception $e ) { |
| 62 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function rest_settings_options( $request ) { |
| 67 | try { |
| 68 | $options = $this->core->get_all_options(); |
| 69 | return $this->create_rest_response( [ 'success' => true, 'options' => $options ], 200 ); |
| 70 | } |
| 71 | catch ( Exception $e ) { |
| 72 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public function rest_settings_reset( $request ) { |
| 77 | try { |
| 78 | $options = $this->core->get_all_options( true ); |
| 79 | $this->core->update_options( $options ); |
| 80 | return $this->create_rest_response( [ 'success' => true, 'options' => $options ], 200 ); |
| 81 | } |
| 82 | catch ( Exception $e ) { |
| 83 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public function rest_settings_get_chatbots( $request ) { |
| 88 | try { |
| 89 | $chatbots = $this->core->get_chatbots(); |
| 90 | return $this->create_rest_response( [ 'success' => true, 'chatbots' => $chatbots ], 200 ); |
| 91 | } |
| 92 | catch ( Exception $e ) { |
| 93 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public function rest_settings_update_chatbots( $request ) { |
| 98 | try { |
| 99 | $params = $request->get_json_params(); |
| 100 | $chatbots = $params['chatbots']; |
| 101 | $this->core->update_chatbots( $chatbots ); |
| 102 | return $this->create_rest_response( [ 'success' => true ], 200 ); |
| 103 | } |
| 104 | catch ( Exception $e ) { |
| 105 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public function rest_settings_get_themes( $request ) { |
| 110 | try { |
| 111 | $themes = $this->core->get_themes(); |
| 112 | return $this->create_rest_response( [ 'success' => true, 'themes' => $themes ], 200 ); |
| 113 | } |
| 114 | catch ( Exception $e ) { |
| 115 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function rest_settings_update_themes( $request ) { |
| 120 | try { |
| 121 | $params = $request->get_json_params(); |
| 122 | $themes = $params['themes']; |
| 123 | $this->core->update_themes( $themes ); |
| 124 | return $this->create_rest_response( [ 'success' => true ], 200 ); |
| 125 | } |
| 126 | catch ( Exception $e ) { |
| 127 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | public function rest_settings_reset_usage( $request ) { |
| 132 | try { |
| 133 | // Reset the actual backend options that store usage data |
| 134 | $this->core->update_option( 'ai_usage', [] ); |
| 135 | $this->core->update_option( 'ai_usage_daily', [] ); |
| 136 | |
| 137 | // Force refresh to get updated options to return to frontend |
| 138 | $options = $this->core->get_all_options( true ); |
| 139 | |
| 140 | return $this->create_rest_response( [ |
| 141 | 'success' => true, |
| 142 | 'options' => $options |
| 143 | ], 200 ); |
| 144 | } |
| 145 | catch ( Exception $e ) { |
| 146 | return $this->create_rest_response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 |