'GET', 'callback' => array(__CLASS__, 'answer'), 'permission_callback' => '__return_true', 'args' => array( 'nonce' => array( 'required' => true, 'type' => 'string', 'validate_callback' => array(__CLASS__, 'is_valid_nonce'), 'sanitize_callback' => 'sanitize_text_field', ), ), )); } public static function is_valid_nonce($value) { return is_string($value) && (bool) preg_match('/^[a-f0-9]{16,128}$/', $value); } /** * @param WP_REST_Request $request * @return WP_REST_Response|WP_Error */ public static function answer($request) { if (!class_exists('AllAccessible_ApiClient')) { return new WP_Error('aacb_not_ready', 'Plugin not fully loaded', array('status' => 503)); } $token = AllAccessible_ApiClient::get_instance()->get_install_token(false); if (empty($token)) { // No token yet means this install has never asked for a credential; // there is nothing to prove ownership of. return new WP_Error('aacb_no_install_token', 'No credential request pending for this site', array('status' => 404)); } $nonce = (string) $request->get_param('nonce'); $response = rest_ensure_response(array( 'answer' => hash_hmac('sha256', $nonce, $token), 'version' => defined('AACB_VERSION') ? AACB_VERSION : 'unknown', )); $response->header('Cache-Control', 'no-store, max-age=0'); $response->header('X-Robots-Tag', 'noindex'); return $response; } }