// AJAX handler for pattern generation
add_action('wp_ajax_pixcraft_generate', 'pixcraft_generate_handler');
add_action('wp_ajax_nopriv_pixcraft_generate', 'pixcraft_generate_handler');
function pixcraft_generate_handler() {
$api_key = get_option('pixcraft_api_key');
if (!$api_key) {
wp_send_json_error('API key not configured');
}
$image_url = sanitize_url($_POST['image_url'] ?? '');
$niche = sanitize_text_field($_POST['niche'] ?? 'mosaics');
$width = intval($_POST['width_cm'] ?? 100);
$height = intval($_POST['height_cm'] ?? 100);
$response = wp_remote_post('https://www.pixcraft.es/api/v1/generate', [
'headers' => [
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode([
'image_url' => $image_url,
'niche' => $niche,
'width_cm' => $width,
'height_cm' => $height,
]),
'timeout' => 30,
]);
if (is_wp_error($response)) {
wp_send_json_error($response->get_error_message());
}
$body = json_decode(wp_remote_retrieve_body($response), true);
wp_send_json_success($body);
}