| 1 |
<?php |
| 2 |
|
| 3 |
class MeowCommon_Classes_Rest |
| 4 |
{ |
| 5 |
private $namespace = "meow-common/v1"; |
| 6 |
static public $instance = null; |
| 7 |
|
| 8 |
static public function init_once() { |
| 9 |
if ( !function_exists( 'wp_get_current_user' ) ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
if ( !current_user_can( 'administrator' ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
if ( !MeowCommon_Classes_Rest::$instance ) { |
| 16 |
MeowCommon_Classes_Rest::$instance = new self(); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
private function __construct() { |
| 21 |
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
function rest_api_init() { |
| 25 |
register_rest_route( $this->namespace, '/empty_request/', [ |
| 26 |
'methods' => 'POST', |
| 27 |
'callback' => [ $this, 'empty_request' ] |
| 28 |
]); |
| 29 |
register_rest_route( $this->namespace, '/file_operation/', [ |
| 30 |
'methods' => 'POST', |
| 31 |
'callback' => [ $this, 'file_operation' ] |
| 32 |
]); |
| 33 |
register_rest_route( $this->namespace, '/sql_request/', [ |
| 34 |
'methods' => 'POST', |
| 35 |
'callback' => [ $this, 'sql_request' ] |
| 36 |
]); |
| 37 |
register_rest_route( $this->namespace, '/error_logs/', [ |
| 38 |
'methods' => 'POST', |
| 39 |
'callback' => [ $this, 'rest_error_logs' ] |
| 40 |
]); |
| 41 |
register_rest_route( $this->namespace, '/all_settings/', [ |
| 42 |
'methods' => 'GET', |
| 43 |
'callback' => [ $this, 'rest_all_settings' ] |
| 44 |
]); |
| 45 |
register_rest_route( $this->namespace, '/update_option/', [ |
| 46 |
'methods' => 'POST', |
| 47 |
'callback' => [ $this, 'rest_update_option' ] |
| 48 |
]); |
| 49 |
} |
| 50 |
|
| 51 |
function file_rand( $filesize ) { |
| 52 |
$tmp_file = tmpfile(); |
| 53 |
fseek( $tmp_file, $filesize - 1, SEEK_CUR ); |
| 54 |
fwrite( $tmp_file, 'a'); |
| 55 |
fclose( $tmp_file ); |
| 56 |
} |
| 57 |
|
| 58 |
function empty_request() { |
| 59 |
return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 60 |
} |
| 61 |
|
| 62 |
function file_operation() { |
| 63 |
$this->file_rand( 1024 * 10 ); |
| 64 |
return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 65 |
} |
| 66 |
|
| 67 |
function sql_request() { |
| 68 |
global $wpdb; |
| 69 |
$count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" ); |
| 70 |
return new WP_REST_Response( [ 'success' => true, 'data' => $count ], 200 ); |
| 71 |
} |
| 72 |
|
| 73 |
function rest_all_settings() { |
| 74 |
$settings = array( |
| 75 |
'meowapps_hide_meowapps' => get_option( 'meowapps_hide_meowapps', false ), |
| 76 |
'force_sslverify' => get_option( 'force_sslverify', false ) |
| 77 |
); |
| 78 |
return new WP_REST_Response([ 'success' => true, 'data' => $settings ], 200 ); |
| 79 |
} |
| 80 |
|
| 81 |
function rest_update_option( $request ) { |
| 82 |
$params = $request->get_json_params(); |
| 83 |
try { |
| 84 |
$result = update_option( $params['name'], $params['value'] ); |
| 85 |
return new WP_REST_Response([ 'success' => $result ], 200 ); |
| 86 |
} |
| 87 |
catch (Exception $e) { |
| 88 |
return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
function rest_error_logs( $request ) { |
| 93 |
return new WP_REST_Response( [ 'success' => true, 'data' => MeowCommon_Helpers::php_error_logs() ], 200 ); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
?> |