admin.php
4 months ago
helpers.php
6 months ago
issues.php
7 months ago
news.php
7 months ago
ratings.php
7 months ago
releases.txt
2 years ago
rest.php
7 months ago
rest.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | class MeowKit_MWAI_Rest { |
| 4 | private $namespace = 'meow-common/v1'; |
| 5 | public static $instance = null; |
| 6 | |
| 7 | public static function init_once() { |
| 8 | if ( !MeowKit_MWAI_Rest::$instance ) { |
| 9 | MeowKit_MWAI_Rest::$instance = new self(); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | private function __construct() { |
| 14 | add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 15 | } |
| 16 | |
| 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 | } |
| 67 | |
| 68 | public function file_rand( $filesize ) { |
| 69 | $tmp_file = tmpfile(); |
| 70 | fseek( $tmp_file, $filesize - 1, SEEK_CUR ); |
| 71 | fwrite( $tmp_file, 'a' ); |
| 72 | fclose( $tmp_file ); |
| 73 | } |
| 74 | |
| 75 | public function empty_request() { |
| 76 | return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 77 | } |
| 78 | |
| 79 | public function file_operation() { |
| 80 | $this->file_rand( 1024 * 10 ); |
| 81 | return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 82 | } |
| 83 | |
| 84 | public function sql_request() { |
| 85 | global $wpdb; |
| 86 | $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" ); |
| 87 | return new WP_REST_Response( [ 'success' => true, 'data' => $count ], 200 ); |
| 88 | } |
| 89 | |
| 90 | // List all the options with their default values. |
| 91 | public function list_options() { |
| 92 | return [ |
| 93 | 'meowapps_hide_meowapps' => false, |
| 94 | 'force_sslverify' => false |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | public function get_all_options() { |
| 99 | $options = $this->list_options(); |
| 100 | $current_options = []; |
| 101 | foreach ( $options as $option => $default ) { |
| 102 | $current_options[$option] = get_option( $option, $default ); |
| 103 | } |
| 104 | return $current_options; |
| 105 | } |
| 106 | |
| 107 | public function rest_all_settings() { |
| 108 | return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 ); |
| 109 | } |
| 110 | |
| 111 | public function rest_update_option( $request ) { |
| 112 | $params = $request->get_json_params(); |
| 113 | try { |
| 114 | $name = $params['name']; |
| 115 | $options = $this->list_options(); |
| 116 | if ( !array_key_exists( $name, $options ) ) { |
| 117 | return new WP_REST_Response( [ 'success' => false, 'message' => 'This option does not exist.' ], 200 ); |
| 118 | } |
| 119 | $value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value']; |
| 120 | $success = update_option( $name, $value ); |
| 121 | if ( !$success ) { |
| 122 | return new WP_REST_Response( [ 'success' => false, 'message' => 'Could not update option.' ], 200 ); |
| 123 | } |
| 124 | return new WP_REST_Response( [ 'success' => true, 'data' => $value ], 200 ); |
| 125 | } |
| 126 | catch ( Exception $e ) { |
| 127 | return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | public function rest_error_logs( $request ) { |
| 132 | return new WP_REST_Response( [ 'success' => true, 'data' => MeowKit_MWAI_Helpers::php_error_logs() ], 200 ); |
| 133 | } |
| 134 | |
| 135 | } |
| 136 |