| @@ -1,162 +1,136 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class MeowKit_MWCODE_Rest { | |
| 4 | - private $namespace = 'meow-common/v1'; | |
| 5 | - public static $instance = null; | |
| 3 | +class MeowCommon_Rest | |
| 4 | +{ | |
| 5 | + private $namespace = "meow-common/v1"; | |
| 6 | + static public $instance = null; | |
| 6 | 7 | |
| 7 | - public static function init_once() { | |
| 8 | - if ( !MeowKit_MWCODE_Rest::$instance ) { | |
| 9 | - MeowKit_MWCODE_Rest::$instance = new self(); | |
| 10 | - } | |
| 11 | - } | |
| 8 | + static public function init_once() { | |
| 9 | + if ( !MeowCommon_Rest::$instance ) { | |
| 10 | + MeowCommon_Rest::$instance = new self(); | |
| 11 | + } | |
| 12 | + } | |
| 12 | 13 | |
| 13 | - private function __construct() { | |
| 14 | - add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); | |
| 15 | - } | |
| 14 | + private function __construct() { | |
| 15 | + add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); | |
| 16 | + } | |
| 16 | 17 | |
| 17 | - public function rest_api_init() { | |
| 18 | - if ( !current_user_can( 'manage_options' ) ) { | |
| 19 | - return; | |
| 20 | - } | |
| 21 | - register_rest_route( $this->namespace, '/empty_request/', [ | |
| 22 | - 'methods' => 'POST', | |
| 23 | - 'permission_callback' => function () { | |
| 24 | - return current_user_can( 'manage_options' ); | |
| 25 | - }, | |
| 26 | - 'callback' => [ $this, 'empty_request' ] | |
| 27 | - ] ); | |
| 28 | - register_rest_route( $this->namespace, '/file_operation/', [ | |
| 29 | - 'methods' => 'POST', | |
| 30 | - 'permission_callback' => function () { | |
| 31 | - return current_user_can( 'manage_options' ); | |
| 32 | - }, | |
| 33 | - 'callback' => [ $this, 'file_operation' ] | |
| 34 | - ] ); | |
| 35 | - register_rest_route( $this->namespace, '/sql_request/', [ | |
| 36 | - 'methods' => 'POST', | |
| 37 | - 'permission_callback' => function () { | |
| 38 | - return current_user_can( 'manage_options' ); | |
| 39 | - }, | |
| 40 | - 'callback' => [ $this, 'sql_request' ] | |
| 41 | - ] ); | |
| 42 | - register_rest_route( $this->namespace, '/error_logs/', [ | |
| 43 | - 'methods' => 'POST', | |
| 44 | - 'permission_callback' => function () { | |
| 45 | - $ok = current_user_can( 'manage_options' ); | |
| 46 | - return $ok; | |
| 47 | - }, | |
| 48 | - 'callback' => [ $this, 'rest_error_logs' ] | |
| 49 | - ] ); | |
| 50 | - register_rest_route( $this->namespace, '/all_settings/', [ | |
| 51 | - 'methods' => 'POST', | |
| 52 | - 'permission_callback' => function () { | |
| 53 | - $ok = current_user_can( 'manage_options' ); | |
| 54 | - return $ok; | |
| 55 | - }, | |
| 56 | - 'callback' => [ $this, 'rest_all_settings' ] | |
| 57 | - ] ); | |
| 58 | - register_rest_route( $this->namespace, '/update_option/', [ | |
| 59 | - 'methods' => 'POST', | |
| 60 | - 'permission_callback' => function () { | |
| 61 | - $ok = current_user_can( 'manage_options' ); | |
| 62 | - return $ok; | |
| 63 | - }, | |
| 64 | - 'callback' => [ $this, 'rest_update_option' ] | |
| 65 | - ] ); | |
| 66 | - register_rest_route( $this->namespace, '/installed_plugins/', [ | |
| 67 | - 'methods' => 'POST', | |
| 68 | - 'permission_callback' => function () { | |
| 69 | - return current_user_can( 'manage_options' ); | |
| 70 | - }, | |
| 71 | - 'callback' => [ $this, 'rest_installed_plugins' ] | |
| 72 | - ] ); | |
| 73 | - } | |
| 18 | + function rest_api_init() { | |
| 19 | + if ( !current_user_can( 'manage_options' ) ) { | |
| 20 | + return; | |
| 21 | + } | |
| 22 | + register_rest_route( $this->namespace, '/empty_request/', [ | |
| 23 | + 'methods' => 'POST', | |
| 24 | + 'permission_callback' => function () { | |
| 25 | + return current_user_can( 'manage_options' ); | |
| 26 | + }, | |
| 27 | + 'callback' => [ $this, 'empty_request' ] | |
| 28 | + ]); | |
| 29 | + register_rest_route( $this->namespace, '/file_operation/', [ | |
| 30 | + 'methods' => 'POST', | |
| 31 | + 'permission_callback' => function () { | |
| 32 | + return current_user_can( 'manage_options' ); | |
| 33 | + }, | |
| 34 | + 'callback' => [ $this, 'file_operation' ] | |
| 35 | + ]); | |
| 36 | + register_rest_route( $this->namespace, '/sql_request/', [ | |
| 37 | + 'methods' => 'POST', | |
| 38 | + 'permission_callback' => function () { | |
| 39 | + return current_user_can( 'manage_options' ); | |
| 40 | + }, | |
| 41 | + 'callback' => [ $this, 'sql_request' ] | |
| 42 | + ]); | |
| 43 | + register_rest_route( $this->namespace, '/error_logs/', [ | |
| 44 | + 'methods' => 'POST', | |
| 45 | + 'permission_callback' => function () { | |
| 46 | + $ok = current_user_can( 'manage_options' ); | |
| 47 | + return $ok; | |
| 48 | + }, | |
| 49 | + 'callback' => [ $this, 'rest_error_logs' ] | |
| 50 | + ]); | |
| 51 | + register_rest_route( $this->namespace, '/all_settings/', [ | |
| 52 | + 'methods' => 'POST', | |
| 53 | + 'permission_callback' => function () { | |
| 54 | + $ok = current_user_can( 'manage_options' ); | |
| 55 | + return $ok; | |
| 56 | + }, | |
| 57 | + 'callback' => [ $this, 'rest_all_settings' ] | |
| 58 | + ]); | |
| 59 | + register_rest_route( $this->namespace, '/update_option/', [ | |
| 60 | + 'methods' => 'POST', | |
| 61 | + 'permission_callback' => function () { | |
| 62 | + $ok = current_user_can( 'manage_options' ); | |
| 63 | + return $ok; | |
| 64 | + }, | |
| 65 | + 'callback' => [ $this, 'rest_update_option' ] | |
| 66 | + ]); | |
| 67 | + } | |
| 74 | 68 | |
| 75 | - public function file_rand( $filesize ) { | |
| 76 | - $tmp_file = tmpfile(); | |
| 77 | - fseek( $tmp_file, $filesize - 1, SEEK_CUR ); | |
| 78 | - fwrite( $tmp_file, 'a' ); | |
| 79 | - fclose( $tmp_file ); | |
| 80 | - } | |
| 69 | + function file_rand( $filesize ) { | |
| 70 | + $tmp_file = tmpfile(); | |
| 71 | + fseek( $tmp_file, $filesize - 1, SEEK_CUR ); | |
| 72 | + fwrite( $tmp_file, 'a'); | |
| 73 | + fclose( $tmp_file ); | |
| 74 | + } | |
| 81 | 75 | |
| 82 | - public function empty_request() { | |
| 76 | + function empty_request() { | |
| 83 | 77 | return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 84 | - } | |
| 85 | - | |
| 86 | - public function file_operation() { | |
| 87 | - $this->file_rand( 1024 * 10 ); | |
| 78 | + } | |
| 79 | + | |
| 80 | + function file_operation() { | |
| 81 | + $this->file_rand( 1024 * 10 ); | |
| 88 | 82 | return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 89 | - } | |
| 90 | - | |
| 91 | - public function sql_request() { | |
| 92 | - global $wpdb; | |
| 93 | - $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + function sql_request() { | |
| 86 | + global $wpdb; | |
| 87 | + $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" ); | |
| 94 | 88 | return new WP_REST_Response( [ 'success' => true, 'data' => $count ], 200 ); |
| 95 | - } | |
| 89 | + } | |
| 96 | 90 | |
| 97 | - // List all the options with their default values. | |
| 98 | - public function list_options() { | |
| 99 | - return [ | |
| 100 | - 'meowapps_hide_meowapps' => false, | |
| 101 | - 'force_sslverify' => false | |
| 102 | - ]; | |
| 103 | - } | |
| 91 | + // List all the options with their default values. | |
| 92 | + function list_options() { | |
| 93 | + return array( | |
| 94 | + 'meowapps_hide_meowapps' => false, | |
| 95 | + 'force_sslverify' => false | |
| 96 | + ); | |
| 97 | + } | |
| 104 | 98 | |
| 105 | - public function get_all_options() { | |
| 106 | - $options = $this->list_options(); | |
| 107 | - $current_options = []; | |
| 108 | - foreach ( $options as $option => $default ) { | |
| 109 | - $current_options[$option] = get_option( $option, $default ); | |
| 110 | - } | |
| 111 | - return $current_options; | |
| 112 | - } | |
| 99 | + function get_all_options() { | |
| 100 | + $options = $this->list_options(); | |
| 101 | + $current_options = array(); | |
| 102 | + foreach ( $options as $option => $default ) { | |
| 103 | + $current_options[$option] = get_option( $option, $default ); | |
| 104 | + } | |
| 105 | + return $current_options; | |
| 106 | + } | |
| 113 | 107 | |
| 114 | - public function rest_all_settings() { | |
| 115 | - return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 ); | |
| 116 | - } | |
| 108 | + function rest_all_settings() { | |
| 109 | + return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 ); | |
| 110 | + } | |
| 117 | 111 | |
| 118 | - public function rest_installed_plugins() { | |
| 119 | - if ( !function_exists( 'get_plugins' ) ) { | |
| 120 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 121 | - } | |
| 122 | - $all_plugins = get_plugins(); | |
| 123 | - $result = []; | |
| 124 | - foreach ( $all_plugins as $plugin_file => $plugin_data ) { | |
| 125 | - // Plugin file looks like "ai-engine/ai-engine.php" — the slug is the | |
| 126 | - // first path segment. Some plugins live at the root (single file), | |
| 127 | - // those we just skip; we only care about Meow Apps directories anyway. | |
| 128 | - $parts = explode( '/', $plugin_file ); | |
| 129 | - if ( count( $parts ) < 2 ) { | |
| 130 | - continue; | |
| 131 | - } | |
| 132 | - $slug = $parts[0]; | |
| 133 | - $result[ $slug ] = is_plugin_active( $plugin_file ) ? 'active' : 'inactive'; | |
| 134 | - } | |
| 135 | - return new WP_REST_Response( [ 'success' => true, 'data' => $result ], 200 ); | |
| 136 | - } | |
| 112 | + function rest_update_option( $request ) { | |
| 113 | + $params = $request->get_json_params(); | |
| 114 | + try { | |
| 115 | + $name = $params['name']; | |
| 116 | + $options = $this->list_options(); | |
| 117 | + if ( !array_key_exists( $name, $options ) ) { | |
| 118 | + return new WP_REST_Response([ 'success' => false, 'message' => 'This option does not exist.' ], 200 ); | |
| 119 | + } | |
| 120 | + $value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value']; | |
| 121 | + $success = update_option( $name, $value ); | |
| 122 | + if ( !$success ) { | |
| 123 | + return new WP_REST_Response( [ 'success' => false, 'message' => 'Could not update option.' ], 200 ); | |
| 124 | + } | |
| 125 | + return new WP_REST_Response( [ 'success' => true, 'data' => $value ], 200 ); | |
| 126 | + } | |
| 127 | + catch (Exception $e) { | |
| 128 | + return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); | |
| 129 | + } | |
| 130 | + } | |
| 137 | 131 | |
| 138 | - public function rest_update_option( $request ) { | |
| 139 | - $params = $request->get_json_params(); | |
| 140 | - try { | |
| 141 | - $name = $params['name']; | |
| 142 | - $options = $this->list_options(); | |
| 143 | - if ( !array_key_exists( $name, $options ) ) { | |
| 144 | - return new WP_REST_Response( [ 'success' => false, 'message' => 'This option does not exist.' ], 200 ); | |
| 145 | - } | |
| 146 | - $value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value']; | |
| 147 | - $success = update_option( $name, $value ); | |
| 148 | - if ( !$success ) { | |
| 149 | - return new WP_REST_Response( [ 'success' => false, 'message' => 'Could not update option.' ], 200 ); | |
| 150 | - } | |
| 151 | - return new WP_REST_Response( [ 'success' => true, 'data' => $value ], 200 ); | |
| 152 | - } | |
| 153 | - catch ( Exception $e ) { | |
| 154 | - return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); | |
| 155 | - } | |
| 156 | - } | |
| 157 | - | |
| 158 | - public function rest_error_logs( $request ) { | |
| 159 | - return new WP_REST_Response( [ 'success' => true, 'data' => MeowKit_MWCODE_Helpers::php_error_logs() ], 200 ); | |
| 160 | - } | |
| 132 | + function rest_error_logs( $request ) { | |
| 133 | + return new WP_REST_Response( [ 'success' => true, 'data' => MeowCommon_Helpers::php_error_logs() ], 200 ); | |
| 134 | + } | |
| 161 | 135 | |
| 162 | 136 | } |