I have a few custom endpoints to fetch and edit orders, but this one is super slow despite it targets an order per id. It takes up to 1500ms. Any idea why, i cant find any issue. Searched online, nothing. Any idea how to fix it?
// Register the custom endpoint
add_action('init', 'end_point_0007');
function end_point_0007() {
add_rewrite_rule('^edit-order-om/?', 'index.php?edit-order-om=1', 'top');
}
// Add the custom query variable
add_filter('query_vars', 'end_point_0007_query_vars');
function end_point_0007_query_vars($vars) {
$vars[] = 'edit-order-om';
return $vars;
}
// Custom handler for editing orders
add_action('template_redirect', 'end_point_0007_handler');
function end_point_0007_handler() {
global $wp_query;
if (isset($wp_query->query_vars['edit-order-om'])) {
// Nonce verification
$nonce_action = 'edit_orders_om_nonce';
if (!isset($_GET['nonce']) || !wp_verify_nonce($_GET['nonce'], $nonce_action)) {
wp_send_json_error(array('status' => 'error', 'message' => 'Invalid nonce.'));
}
// Set content type
header('Content-Type: application/json');
// Get order ID and new status from the request
$order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
$new_status = isset($_GET['new_status']) ? sanitize_text_field($_GET['new_status']) : '';
if ($order_id <= 0 || empty($new_status)) {
wp_send_json_error(array('status' => 'error', 'message' => 'Invalid order ID or status.'));
}
// Load the order
$order = wc_get_order($order_id);
if (!$order) {
wp_send_json_error(array('status' => 'error', 'message' => 'Order not found.'));
}
// Update the order status
$order->update_status($new_status, 'Order status updated by custom endpoint.');
wp_send_json_success(array('status' => 'success', 'message' => 'Order status updated.'));
}
}