| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class MeowCommon_Rest { | |
| 3 | +class MeowKit_MWCODE_Rest { | |
| 4 | 4 | private $namespace = 'meow-common/v1'; |
| 5 | 5 | public static $instance = null; |
| 6 | 6 | |
| 7 | 7 | public static function init_once() { |
| 8 | - if ( !MeowCommon_Rest::$instance ) { | |
| 9 | - MeowCommon_Rest::$instance = new self(); | |
| 8 | + if ( !MeowKit_MWCODE_Rest::$instance ) { | |
| 9 | + MeowKit_MWCODE_Rest::$instance = new self(); | |
| 10 | 10 | } |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | private function __construct() { |
| @@ -13,57 +13,68 @@ | ||
| 13 | 13 | private function __construct() { |
| 14 | 14 | add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 15 | 15 | } |
| 16 | 16 | |
| 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 | + } | |
| 34 | + | |
| 17 | 35 | public function rest_api_init() { |
| 18 | - if ( !current_user_can( 'manage_options' ) ) { | |
| 36 | + if ( !$this->can_setup() ) { | |
| 19 | 37 | return; |
| 20 | 38 | } |
| 39 | + $permission = function () { | |
| 40 | + return $this->can_setup(); | |
| 41 | + }; | |
| 21 | 42 | register_rest_route( $this->namespace, '/empty_request/', [ |
| 22 | 43 | 'methods' => 'POST', |
| 23 | - 'permission_callback' => function () { | |
| 24 | - return current_user_can( 'manage_options' ); | |
| 25 | - }, | |
| 44 | + 'permission_callback' => $permission, | |
| 26 | 45 | 'callback' => [ $this, 'empty_request' ] |
| 27 | 46 | ] ); |
| 28 | 47 | register_rest_route( $this->namespace, '/file_operation/', [ |
| 29 | 48 | 'methods' => 'POST', |
| 30 | - 'permission_callback' => function () { | |
| 31 | - return current_user_can( 'manage_options' ); | |
| 32 | - }, | |
| 49 | + 'permission_callback' => $permission, | |
| 33 | 50 | 'callback' => [ $this, 'file_operation' ] |
| 34 | 51 | ] ); |
| 35 | 52 | register_rest_route( $this->namespace, '/sql_request/', [ |
| 36 | 53 | 'methods' => 'POST', |
| 37 | - 'permission_callback' => function () { | |
| 38 | - return current_user_can( 'manage_options' ); | |
| 39 | - }, | |
| 54 | + 'permission_callback' => $permission, | |
| 40 | 55 | 'callback' => [ $this, 'sql_request' ] |
| 41 | 56 | ] ); |
| 42 | 57 | register_rest_route( $this->namespace, '/error_logs/', [ |
| 43 | 58 | 'methods' => 'POST', |
| 44 | - 'permission_callback' => function () { | |
| 45 | - $ok = current_user_can( 'manage_options' ); | |
| 46 | - return $ok; | |
| 47 | - }, | |
| 59 | + 'permission_callback' => $permission, | |
| 48 | 60 | 'callback' => [ $this, 'rest_error_logs' ] |
| 49 | 61 | ] ); |
| 50 | 62 | register_rest_route( $this->namespace, '/all_settings/', [ |
| 51 | 63 | 'methods' => 'POST', |
| 52 | - 'permission_callback' => function () { | |
| 53 | - $ok = current_user_can( 'manage_options' ); | |
| 54 | - return $ok; | |
| 55 | - }, | |
| 64 | + 'permission_callback' => $permission, | |
| 56 | 65 | 'callback' => [ $this, 'rest_all_settings' ] |
| 57 | 66 | ] ); |
| 58 | 67 | register_rest_route( $this->namespace, '/update_option/', [ |
| 59 | 68 | 'methods' => 'POST', |
| 60 | - 'permission_callback' => function () { | |
| 61 | - $ok = current_user_can( 'manage_options' ); | |
| 62 | - return $ok; | |
| 63 | - }, | |
| 69 | + 'permission_callback' => $permission, | |
| 64 | 70 | 'callback' => [ $this, 'rest_update_option' ] |
| 65 | 71 | ] ); |
| 72 | + register_rest_route( $this->namespace, '/installed_plugins/', [ | |
| 73 | + 'methods' => 'POST', | |
| 74 | + 'permission_callback' => $permission, | |
| 75 | + 'callback' => [ $this, 'rest_installed_plugins' ] | |
| 76 | + ] ); | |
| 66 | 77 | } |
| 67 | 78 | |
| 68 | 79 | public function file_rand( $filesize ) { |
| 69 | 80 | $tmp_file = tmpfile(); |
| @@ -107,8 +118,28 @@ | ||
| 107 | 118 | public function rest_all_settings() { |
| 108 | 119 | return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 ); |
| 109 | 120 | } |
| 110 | 121 | |
| 122 | + public function rest_installed_plugins() { | |
| 123 | + if ( !function_exists( 'get_plugins' ) ) { | |
| 124 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 125 | + } | |
| 126 | + $all_plugins = get_plugins(); | |
| 127 | + $result = []; | |
| 128 | + foreach ( $all_plugins as $plugin_file => $plugin_data ) { | |
| 129 | + // Plugin file looks like "ai-engine/ai-engine.php" — the slug is the | |
| 130 | + // first path segment. Some plugins live at the root (single file), | |
| 131 | + // those we just skip; we only care about Meow Apps directories anyway. | |
| 132 | + $parts = explode( '/', $plugin_file ); | |
| 133 | + if ( count( $parts ) < 2 ) { | |
| 134 | + continue; | |
| 135 | + } | |
| 136 | + $slug = $parts[0]; | |
| 137 | + $result[ $slug ] = is_plugin_active( $plugin_file ) ? 'active' : 'inactive'; | |
| 138 | + } | |
| 139 | + return new WP_REST_Response( [ 'success' => true, 'data' => $result ], 200 ); | |
| 140 | + } | |
| 141 | + | |
| 111 | 142 | public function rest_update_option( $request ) { |
| 112 | 143 | $params = $request->get_json_params(); |
| 113 | 144 | try { |
| 114 | 145 | $name = $params['name']; |
| @@ -128,8 +159,8 @@ | ||
| 128 | 159 | } |
| 129 | 160 | } |
| 130 | 161 | |
| 131 | 162 | public function rest_error_logs( $request ) { |
| 132 | - return new WP_REST_Response( [ 'success' => true, 'data' => MeowCommon_Helpers::php_error_logs() ], 200 ); | |
| 163 | + return new WP_REST_Response( [ 'success' => true, 'data' => MeowKit_MWCODE_Helpers::php_error_logs() ], 200 ); | |
| 133 | 164 | } |
| 134 | 165 | |
| 135 | 166 | } |