PluginProbe
Meow Gallery / 4.2.2
Meow Gallery v4.2.2
5.5.5 5.5.4 5.5.3 5.5.2 5.5.1 5.5.0 5.4.9 5.4.8 5.4.7 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 All 157 releases
← All changes | common/rest.php +116 -159 5.5.2 → 4.2.2 View file →
@@ -1,176 +1,133 @@
1 1 <?php
2 2
3 -class MeowKit_MGL_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_MGL_Rest::$instance ) {
9 - MeowKit_MGL_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 - /**
18 - * Capability gate for plugin admin endpoints. Defaults to manage_options but
19 - * honours a per-plugin filter so a site can grant access to other roles
20 - * without re-implementing every permission_callback. The filter name is
21 - * derived from the class name (`MeowKit_<PREFIX>_Rest` => `<prefix>_allow_setup`),
22 - * so each plugin gets its own filter automatically after Nekofy substitution.
23 - */
24 - private function can_setup() {
25 - static $filter = null;
26 - if ( $filter === null ) {
27 - $parts = explode( '_', __CLASS__ );
28 - $prefix = isset( $parts[1] ) ? strtolower( $parts[1] ) : '';
29 - $filter = $prefix !== '' ? $prefix . '_allow_setup' : '';
30 - }
31 - $default = current_user_can( 'manage_options' );
32 - return $filter !== '' ? apply_filters( $filter, $default ) : $default;
33 - }
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 + }
34 65
35 - public function rest_api_init() {
36 - if ( !$this->can_setup() ) {
37 - return;
38 - }
39 - $permission = function () {
40 - return $this->can_setup();
41 - };
42 - register_rest_route( $this->namespace, '/empty_request/', [
43 - 'methods' => 'POST',
44 - 'permission_callback' => $permission,
45 - 'callback' => [ $this, 'empty_request' ]
46 - ] );
47 - register_rest_route( $this->namespace, '/file_operation/', [
48 - 'methods' => 'POST',
49 - 'permission_callback' => $permission,
50 - 'callback' => [ $this, 'file_operation' ]
51 - ] );
52 - register_rest_route( $this->namespace, '/sql_request/', [
53 - 'methods' => 'POST',
54 - 'permission_callback' => $permission,
55 - 'callback' => [ $this, 'sql_request' ]
56 - ] );
57 - register_rest_route( $this->namespace, '/error_logs/', [
58 - 'methods' => 'POST',
59 - 'permission_callback' => $permission,
60 - 'callback' => [ $this, 'rest_error_logs' ]
61 - ] );
62 - register_rest_route( $this->namespace, '/all_settings/', [
63 - 'methods' => 'POST',
64 - 'permission_callback' => $permission,
65 - 'callback' => [ $this, 'rest_all_settings' ]
66 - ] );
67 - register_rest_route( $this->namespace, '/update_option/', [
68 - 'methods' => 'POST',
69 - 'permission_callback' => $permission,
70 - 'callback' => [ $this, 'rest_update_option' ]
71 - ] );
72 - register_rest_route( $this->namespace, '/installed_plugins/', [
73 - 'methods' => 'POST',
74 - 'permission_callback' => $permission,
75 - 'callback' => [ $this, 'rest_installed_plugins' ]
76 - ] );
77 - }
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 + }
78 72
79 - public function file_rand( $filesize ) {
80 - // Write the benchmark file inside a dedicated subfolder of the uploads
81 - // directory (created on demand), then remove it. wp.org forbids writing to
82 - // the plugin folder or the uploads root — only a sanctioned subfolder.
83 - $upload = wp_upload_dir();
84 - if ( !empty( $upload['error'] ) || empty( $upload['basedir'] ) ) { return; }
85 - $dir = trailingslashit( $upload['basedir'] ) . 'meowapps';
86 - if ( !wp_mkdir_p( $dir ) ) { return; }
87 - $path = trailingslashit( $dir ) . 'speedtest-' . wp_generate_password( 12, false ) . '.tmp';
88 - $fh = @fopen( $path, 'wb' );
89 - if ( $fh === false ) { return; }
90 - fseek( $fh, $filesize - 1, SEEK_CUR );
91 - fwrite( $fh, 'a' );
92 - fclose( $fh );
93 - @unlink( $path );
94 - }
95 -
96 - public function empty_request() {
73 + function empty_request() {
97 74 return new WP_REST_Response( [ 'success' => true ], 200 );
98 - }
99 -
100 - public function file_operation() {
101 - $this->file_rand( 1024 * 10 );
75 + }
76 +
77 + function file_operation() {
78 + $this->file_rand( 1024 * 10 );
102 79 return new WP_REST_Response( [ 'success' => true ], 200 );
103 - }
104 -
105 - public function sql_request() {
106 - global $wpdb;
107 - $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" );
80 + }
81 +
82 + function sql_request() {
83 + global $wpdb;
84 + $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" );
108 85 return new WP_REST_Response( [ 'success' => true, 'data' => $count ], 200 );
109 - }
86 + }
110 87
111 - // List all the options with their default values.
112 - public function list_options() {
113 - return [
114 - 'meowapps_hide_meowapps' => false,
115 - 'force_sslverify' => false
116 - ];
117 - }
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 + }
118 95
119 - public function get_all_options() {
120 - $options = $this->list_options();
121 - $current_options = [];
122 - foreach ( $options as $option => $default ) {
123 - $current_options[$option] = get_option( $option, $default );
124 - }
125 - return $current_options;
126 - }
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 + }
127 104
128 - public function rest_all_settings() {
129 - return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 );
130 - }
105 + function rest_all_settings() {
106 + return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 );
107 + }
131 108
132 - public function rest_installed_plugins() {
133 - if ( !function_exists( 'get_plugins' ) ) {
134 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
135 - }
136 - $all_plugins = get_plugins();
137 - $result = [];
138 - foreach ( $all_plugins as $plugin_file => $plugin_data ) {
139 - // Plugin file looks like "ai-engine/ai-engine.php" — the slug is the
140 - // first path segment. Some plugins live at the root (single file),
141 - // those we just skip; we only care about Meow Apps directories anyway.
142 - $parts = explode( '/', $plugin_file );
143 - if ( count( $parts ) < 2 ) {
144 - continue;
145 - }
146 - $slug = $parts[0];
147 - $result[ $slug ] = is_plugin_active( $plugin_file ) ? 'active' : 'inactive';
148 - }
149 - return new WP_REST_Response( [ 'success' => true, 'data' => $result ], 200 );
150 - }
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 + }
151 128
152 - public function rest_update_option( $request ) {
153 - $params = $request->get_json_params();
154 - try {
155 - $name = $params['name'];
156 - $options = $this->list_options();
157 - if ( !array_key_exists( $name, $options ) ) {
158 - return new WP_REST_Response( [ 'success' => false, 'message' => 'This option does not exist.' ], 200 );
159 - }
160 - $value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value'];
161 - $success = update_option( $name, $value );
162 - if ( !$success ) {
163 - return new WP_REST_Response( [ 'success' => false, 'message' => 'Could not update option.' ], 200 );
164 - }
165 - return new WP_REST_Response( [ 'success' => true, 'data' => $value ], 200 );
166 - }
167 - catch ( Exception $e ) {
168 - return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 );
169 - }
170 - }
171 -
172 - public function rest_error_logs( $request ) {
173 - return new WP_REST_Response( [ 'success' => true, 'data' => MeowKit_MGL_Helpers::php_error_logs() ], 200 );
174 - }
129 + function rest_error_logs( $request ) {
130 + return new WP_REST_Response( [ 'success' => true, 'data' => MeowCommon_Helpers::php_error_logs() ], 200 );
131 + }
175 132
176 133 }