PluginProbe
Performance Lab / 3.6.1
Performance Lab v3.6.1
trunk 1.0.0 1.0.0-beta.1 1.0.0-beta.2 1.0.0-beta.3 1.0.0-rc.1 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.7.0 2.8.0 All 44 releases
performance-lab / includes / admin / rest-api.php

rest-api.php in Performance Lab 3.6.1, at includes/admin/rest-api.php

184 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API integration for the plugin.
4 *
5 * @package performance-lab
6 * @since 3.6.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 /**
14 * Namespace for performance-lab REST API.
15 *
16 * @since 3.6.0
17 * @var string
18 */
19 const PERFLAB_REST_API_NAMESPACE = 'performance-lab/v1';
20
21 /**
22 * Route for activating plugin/feature.
23 *
24 * Note the `:activate` art of the endpoint follows Google's guidance in AIP-136 for the use of the POST method in a way
25 * that does not strictly follow the standard usage.
26 *
27 * @since 3.6.0
28 * @link https://google.aip.dev/136
29 * @var string
30 */
31 const PERFLAB_FEATURES_ACTIVATE_ROUTE = '/features/(?P<slug>[a-z0-9_-]+):activate';
32
33 /**
34 * Route for fetching plugin/feature information.
35 *
36 * @since 3.6.0
37 * @var string
38 */
39 const PERFLAB_FEATURES_INFORMATION_ROUTE = '/features/(?P<slug>[a-z0-9_-]+)';
40
41 /**
42 * Registers endpoint for performance-lab REST API.
43 *
44 * @since 3.6.0
45 * @access private
46 */
47 function perflab_register_endpoint(): void {
48 register_rest_route(
49 PERFLAB_REST_API_NAMESPACE,
50 PERFLAB_FEATURES_ACTIVATE_ROUTE,
51 array(
52 'methods' => 'POST',
53 'args' => array(
54 'slug' => array(
55 'type' => 'string',
56 'description' => __( 'Plugin slug of the Performance Lab feature to be activated.', 'performance-lab' ),
57 'required' => true,
58 'validate_callback' => 'perflab_validate_slug_endpoint_arg',
59 ),
60 ),
61 'callback' => 'perflab_handle_feature_activation',
62 'permission_callback' => static function () {
63 // Important: The endpoint calls perflab_install_and_activate_plugin() which does more granular capability checks.
64 if ( current_user_can( 'activate_plugins' ) ) {
65 return true;
66 }
67
68 return new WP_Error( 'cannot_activate', __( 'Sorry, you are not allowed to activate this feature.', 'performance-lab' ) );
69 },
70 )
71 );
72
73 register_rest_route(
74 PERFLAB_REST_API_NAMESPACE,
75 PERFLAB_FEATURES_INFORMATION_ROUTE,
76 array(
77 'methods' => 'GET',
78 'args' => array(
79 'slug' => array(
80 'type' => 'string',
81 'description' => __( 'Plugin slug of plugin/feature whose information is needed.', 'performance-lab' ),
82 'required' => true,
83 'validate_callback' => 'perflab_validate_slug_endpoint_arg',
84 ),
85 ),
86 'callback' => 'perflab_handle_get_feature_information',
87 'permission_callback' => static function () {
88 if ( current_user_can( 'manage_options' ) ) {
89 return true;
90 }
91
92 return new WP_Error( 'cannot_access_plugin_settings_url', __( 'Sorry, you are not allowed to access plugin/feature information on this site.', 'performance-lab' ) );
93 },
94 )
95 );
96 }
97 add_action( 'rest_api_init', 'perflab_register_endpoint' );
98
99 /**
100 * Validates whether the provided plugin slug is a valid Performance Lab plugin.
101 *
102 * Note that an enum is not being used because additional PHP files have to be required to access the necessary functions,
103 * and this would not be ideal to do at rest_api_init.
104 *
105 * @since 3.6.0
106 * @access private
107 *
108 * @param string $slug Plugin slug.
109 * @return bool Whether valid.
110 */
111 function perflab_validate_slug_endpoint_arg( string $slug ): bool {
112 require_once ABSPATH . 'wp-admin/includes/plugin.php';
113 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/load.php';
114 require_once PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/plugins.php';
115 return in_array( $slug, perflab_get_standalone_plugins(), true );
116 }
117
118 /**
119 * Handles REST API request to activate plugin/feature.
120 *
121 * @since 3.6.0
122 * @access private
123 *
124 * @phpstan-param WP_REST_Request<array<string, mixed>> $request
125 *
126 * @param WP_REST_Request $request Request.
127 * @return WP_REST_Response|WP_Error Response.
128 */
129 function perflab_handle_feature_activation( WP_REST_Request $request ) {
130 require_once ABSPATH . 'wp-admin/includes/file.php';
131 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
132 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
133 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
134
135 // Install and activate the plugin/feature and its dependencies.
136 $result = perflab_install_and_activate_plugin( $request['slug'] );
137 if ( is_wp_error( $result ) ) {
138 switch ( $result->get_error_code() ) {
139 case 'cannot_install_plugin':
140 case 'cannot_activate_plugin':
141 $response_code = rest_authorization_required_code();
142 break;
143 case 'plugin_not_found':
144 $response_code = 404;
145 break;
146 default:
147 $response_code = 500;
148 }
149 return new WP_Error(
150 $result->get_error_code(),
151 $result->get_error_message(),
152 array( 'status' => $response_code )
153 );
154 }
155
156 return new WP_REST_Response(
157 array(
158 'success' => true,
159 )
160 );
161 }
162
163 /**
164 * Handles REST API request to get plugin/feature information.
165 *
166 * @since 3.6.0
167 * @access private
168 *
169 * @phpstan-param WP_REST_Request<array<string, mixed>> $request
170 *
171 * @param WP_REST_Request $request Request.
172 * @return WP_REST_Response Response.
173 */
174 function perflab_handle_get_feature_information( WP_REST_Request $request ): WP_REST_Response {
175 $plugin_settings_url = perflab_get_plugin_settings_url( $request['slug'] );
176
177 return new WP_REST_Response(
178 array(
179 'slug' => $request['slug'],
180 'settingsUrl' => $plugin_settings_url,
181 )
182 );
183 }
184