‘POST’,
‘callback’ => ‘da_classify_handler’,
‘permission_callback’ => ‘da_permission_check’,
]);
});
/**
* Permission check — by default, only logged-in users can call this so random
* visitors can’t burn your API credits. Change to ‘__return_true’ to allow
* anyone, or add your own logic (nonce, rate limiting, etc).
*/
function da_permission_check(WP_REST_Request $req) {
// Verify the nonce that wp_localize_script injected into the page.
$nonce = $req->get_header(‘x-wp-nonce’);
if (!wp_verify_nonce($nonce, ‘wp_rest’)) {
return new WP_Error(‘rest_forbidden’, ‘Invalid nonce.’, [‘status’ => 403]);
}
return is_user_logged_in();
}
/**
* Main handler — forwards the request body to Anthropic and returns the response.
*/
function da_classify_handler(WP_REST_Request $req) {
$api_key = defined(‘ANTHROPIC_API_KEY’) ? ANTHROPIC_API_KEY : get_option(‘da_anthropic_api_key’);
if (empty($api_key)) {
return new WP_Error(‘no_key’, ‘Anthropic API key not configured.’, [‘status’ => 500]);
}
$body = $req->get_body();
if (empty($body)) {
return new WP_Error(‘no_body’, ‘Empty request body.’, [‘status’ => 400]);
}
// Optional: cap request body size to prevent abuse (5 MB).
if (strlen($body) > 5 * 1024 * 1024) {
return new WP_Error(‘too_large’, ‘Request too large.’, [‘status’ => 413]);
}
$response = wp_remote_post(‘https://api.anthropic.com/v1/messages’, [
‘timeout’ => 60,
‘headers’ => [
‘Content-Type’ => ‘application/json’,
‘x-api-key’ => $api_key,
‘anthropic-version’ => ‘2023-06-01’,
],
‘body’ => $body,
]);
if (is_wp_error($response)) {
return new WP_Error(‘upstream_error’, $response->get_error_message(), [‘status’ => 502]);
}
$code = wp_remote_retrieve_response_code($response);
$resp_body = wp_remote_retrieve_body($response);
// Pass through the upstream status code and JSON body.
return new WP_REST_Response(json_decode($resp_body, true), $code);
}
/**
* Settings page — Settings → Document Arranger.
*/
add_action(‘admin_menu’, function () {
add_options_page(‘Document Arranger’, ‘Document Arranger’, ‘manage_options’, ‘document-arranger’, ‘da_settings_page’);
});
add_action(‘admin_init’, function () {
register_setting(‘da_settings’, ‘da_anthropic_api_key’);
});
function da_settings_page() {
?>
Document Arranger Settings
Enter your Anthropic API key, or define ANTHROPIC_API_KEY in wp-config.php (preferred — keeps it out of the database).
Usage
Add the shortcode [document_arranger] to any page or post.
Only logged-in users can use the tool by default — adjust da_permission_check() in the plugin file to change this.
rest_url(‘arranger/v1/classify’),
‘nonce’ => wp_create_nonce(‘wp_rest’),
];
ob_start();
?>
l
External Link Notice
Please be aware that by clicking onto the above link you are leaving the Strive Mortgages Ltd website. Please note that neither Strive Mortgages Ltd nor PRIMIS Mortgage Network are responsible for the accuracy of the information contained within the linked site accessible from this page.