1212 */
1313class REST_API {
1414
15+ /**
16+ * Base path for the REST API endpoints.
17+ *
18+ * @var string
19+ */
1520 const BASE = 'cloudinary/v1 ' ;
1621
1722 /**
@@ -21,6 +26,13 @@ class REST_API {
2126 */
2227 public $ endpoints ;
2328
29+ /**
30+ * The nonce key used for WordPress REST API authentication.
31+ *
32+ * @var string
33+ */
34+ const NONCE_KEY = 'wp_rest ' ;
35+
2436 /**
2537 * REST_API constructor.
2638 *
@@ -39,13 +51,14 @@ public function rest_api_init() {
3951 'method ' => \WP_REST_Server::READABLE ,
4052 'callback ' => __return_empty_array (),
4153 'args ' => array (),
42- 'permission_callback ' => ' __return_true ' ,
54+ 'permission_callback ' => array ( __CLASS__ , ' validate_request ' ) ,
4355 );
4456
4557 $ this ->endpoints = apply_filters ( 'cloudinary_api_rest_endpoints ' , array () );
4658
4759 foreach ( $ this ->endpoints as $ route => $ endpoint ) {
4860 $ endpoint = wp_parse_args ( $ endpoint , $ defaults );
61+
4962 register_rest_route (
5063 static ::BASE ,
5164 $ route ,
@@ -81,7 +94,7 @@ public function background_request( $endpoint, $params = array(), $method = 'POS
8194
8295 $ url = Utils::rest_url ( static ::BASE . '/ ' . $ endpoint );
8396 // Setup a call for a background sync.
84- $ params ['nonce ' ] = wp_create_nonce ( ' wp_rest ' );
97+ $ params ['nonce ' ] = wp_create_nonce ( static :: NONCE_KEY );
8598 $ args = array (
8699 'timeout ' => 0.1 ,
87100 'blocking ' => false ,
@@ -115,4 +128,27 @@ public function background_request( $endpoint, $params = array(), $method = 'POS
115128 // Send request.
116129 wp_remote_request ( $ url , $ args );
117130 }
131+
132+ /**
133+ * Validation for request.
134+ *
135+ * @param \WP_REST_Request $request The original request.
136+ *
137+ * @return bool
138+ */
139+ public static function validate_request ( $ request ) {
140+ return wp_verify_nonce ( $ request ->get_header ( 'x_wp_nonce ' ), self ::NONCE_KEY );
141+ }
142+
143+ /**
144+ * Permission callback for public health check endpoints.
145+ *
146+ * Intentionally allows unauthenticated access for REST API connectivity testing.
147+ * This endpoint is read-only and returns no sensitive data.
148+ *
149+ * @return bool Always returns true to allow public access.
150+ */
151+ public static function allow_public_health_check () {
152+ return true ;
153+ }
118154}
0 commit comments