| 1 |
<?php |
| 2 |
|
| 3 |
class MeowCommon_Rest |
| 4 |
{ |
| 5 |
private $namespace = "meow-common/v1"; |
| 6 |
static public $instance = null; |
| 7 |
|
| 8 |
static public function init_once() { |
| 9 |
if ( !MeowCommon_Rest::$instance ) { |
| 10 |
MeowCommon_Rest::$instance = new self(); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
private function __construct() { |
| 15 |
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 16 |
} |
| 17 |
|
| 18 |
function rest_api_init() { |
| 19 |
if ( !current_user_can( 'administrator' ) ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
register_rest_route( $this->namespace, '/empty_request/', [ |
| 23 |
'methods' => 'POST', |
| 24 |
'permission_callback' => function () { |
| 25 |
return current_user_can( 'administrator' ); |
| 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( 'administrator' ); |
| 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( 'administrator' ); |
| 40 |
}, |
| 41 |
'callback' => [ $this, 'sql_request' ] |
| 42 |
]); |
| 43 |
register_rest_route( $this->namespace, '/error_logs/', [ |
| 44 |
'methods' => 'POST', |
| 45 |
'permission_callback' => function () { |
| 46 |
return current_user_can( 'administrator' ); |
| 47 |
}, |
| 48 |
'callback' => [ $this, 'rest_error_logs' ] |
| 49 |
]); |
| 50 |
register_rest_route( $this->namespace, '/all_settings/', [ |
| 51 |
'methods' => 'GET', |
| 52 |
'permission_callback' => function () { |
| 53 |
return current_user_can( 'administrator' ); |
| 54 |
}, |
| 55 |
'callback' => [ $this, 'rest_all_settings' ] |
| 56 |
]); |
| 57 |
register_rest_route( $this->namespace, '/update_option/', [ |
| 58 |
'methods' => 'POST', |
| 59 |
'permission_callback' => function () { |
| 60 |
return current_user_can( 'administrator' ); |
| 61 |
}, |
| 62 |
'callback' => [ $this, 'rest_update_option' ] |
| 63 |
]); |
| 64 |
} |
| 65 |
|
| 66 |
function file_rand( $filesize ) { |
| 67 |
$tmp_file = tmpfile(); |
| 68 |
fseek( $tmp_file, $filesize - 1, SEEK_CUR ); |
| 69 |
fwrite( $tmp_file, 'a'); |
| 70 |
fclose( $tmp_file ); |
| 71 |
} |
| 72 |
|
| 73 |
function empty_request() { |
| 74 |
return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 75 |
} |
| 76 |
|
| 77 |
function file_operation() { |
| 78 |
$this->file_rand( 1024 * 10 ); |
| 79 |
return new WP_REST_Response( [ 'success' => true ], 200 ); |
| 80 |
} |
| 81 |
|
| 82 |
function sql_request() { |
| 83 |
global $wpdb; |
| 84 |
$count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" ); |
| 85 |
return new WP_REST_Response( [ 'success' => true, 'data' => $count ], 200 ); |
| 86 |
} |
| 87 |
|
| 88 |
// List all the options with their default values. |
| 89 |
function list_options() { |
| 90 |
return array( |
| 91 |
'meowapps_hide_meowapps' => false, |
| 92 |
'force_sslverify' => false |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
function get_all_options() { |
| 97 |
$options = $this->list_options(); |
| 98 |
$current_options = array(); |
| 99 |
foreach ( $options as $option => $default ) { |
| 100 |
$current_options[$option] = get_option( $option, $default ); |
| 101 |
} |
| 102 |
return $current_options; |
| 103 |
} |
| 104 |
|
| 105 |
function rest_all_settings() { |
| 106 |
return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 ); |
| 107 |
} |
| 108 |
|
| 109 |
function rest_update_option( $request ) { |
| 110 |
$params = $request->get_json_params(); |
| 111 |
try { |
| 112 |
$name = $params['name']; |
| 113 |
$options = $this->list_options(); |
| 114 |
if ( !array_key_exists( $name, $options ) ) { |
| 115 |
return new WP_REST_Response([ 'success' => false, 'message' => 'This option does not exist.' ], 200 ); |
| 116 |
} |
| 117 |
$value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value']; |
| 118 |
$success = update_option( $name, $value ); |
| 119 |
if ( !$success ) { |
| 120 |
return new WP_REST_Response( [ 'success' => false, 'message' => 'Could not update option.' ], 200 ); |
| 121 |
} |
| 122 |
return new WP_REST_Response( [ 'success' => true, 'data' => $value ], 200 ); |
| 123 |
} |
| 124 |
catch (Exception $e) { |
| 125 |
return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
function rest_error_logs( $request ) { |
| 130 |
return new WP_REST_Response( [ 'success' => true, 'data' => MeowCommon_Helpers::php_error_logs() ], 200 ); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|