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 7.0.5 7.0.6 7.0.7
media-cleaner / common / rest.php
media-cleaner / common Last commit date
admin.php 1 month ago helpers.php 5 months ago issues.php 7 months ago news.php 7 months ago ratings.php 7 months ago releases.txt 2 years ago rest.php 1 month ago
rest.php
167 lines
1 <?php
2
3 class MeowKit_WPMC_Rest {
4 private $namespace = 'meow-common/v1';
5 public static $instance = null;
6
7 public static function init_once() {
8 if ( !MeowKit_WPMC_Rest::$instance ) {
9 MeowKit_WPMC_Rest::$instance = new self();
10 }
11 }
12
13 private function __construct() {
14 add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
15 }
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
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 }
78
79 public function file_rand( $filesize ) {
80 $tmp_file = tmpfile();
81 fseek( $tmp_file, $filesize - 1, SEEK_CUR );
82 fwrite( $tmp_file, 'a' );
83 fclose( $tmp_file );
84 }
85
86 public function empty_request() {
87 return new WP_REST_Response( [ 'success' => true ], 200 );
88 }
89
90 public function file_operation() {
91 $this->file_rand( 1024 * 10 );
92 return new WP_REST_Response( [ 'success' => true ], 200 );
93 }
94
95 public function sql_request() {
96 global $wpdb;
97 $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts}" );
98 return new WP_REST_Response( [ 'success' => true, 'data' => $count ], 200 );
99 }
100
101 // List all the options with their default values.
102 public function list_options() {
103 return [
104 'meowapps_hide_meowapps' => false,
105 'force_sslverify' => false
106 ];
107 }
108
109 public function get_all_options() {
110 $options = $this->list_options();
111 $current_options = [];
112 foreach ( $options as $option => $default ) {
113 $current_options[$option] = get_option( $option, $default );
114 }
115 return $current_options;
116 }
117
118 public function rest_all_settings() {
119 return new WP_REST_Response( [ 'success' => true, 'data' => $this->get_all_options() ], 200 );
120 }
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
142 public function rest_update_option( $request ) {
143 $params = $request->get_json_params();
144 try {
145 $name = $params['name'];
146 $options = $this->list_options();
147 if ( !array_key_exists( $name, $options ) ) {
148 return new WP_REST_Response( [ 'success' => false, 'message' => 'This option does not exist.' ], 200 );
149 }
150 $value = is_bool( $params['value'] ) ? ( $params['value'] ? '1' : '' ) : $params['value'];
151 $success = update_option( $name, $value );
152 if ( !$success ) {
153 return new WP_REST_Response( [ 'success' => false, 'message' => 'Could not update option.' ], 200 );
154 }
155 return new WP_REST_Response( [ 'success' => true, 'data' => $value ], 200 );
156 }
157 catch ( Exception $e ) {
158 return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 );
159 }
160 }
161
162 public function rest_error_logs( $request ) {
163 return new WP_REST_Response( [ 'success' => true, 'data' => MeowKit_WPMC_Helpers::php_error_logs() ], 200 );
164 }
165
166 }
167