Sanro CRM – API REST Endpoints

‘Clientes’,
‘sanro_training_plan’ => ‘Planes de Entrenamiento’,
‘sanro_workout’ => ‘Sesiones de Entrenamiento’,
‘sanro_progress’ => ‘Progreso’,
‘sanro_note’ => ‘Notas’,
‘sanro_form’ => ‘Formularios’,
);

foreach ( $post_types as $slug => $label ) {
register_post_type( $slug, array(
‘label’ => $label,
‘public’ => true,
‘show_in_rest’ => true,
‘supports’ => array( ‘title’, ‘editor’ ),
‘menu_icon’ => ‘dashicons-groups’,
));
}
});

// Autenticación
function sanro_crm_verify_api_key( $request ) {
$api_key = $request->get_header( ‘X-Claude-API-Key’ );
$stored_key = get_option( ‘sanro_crm_api_key’, ‘claude_sanro_2026_secure_key_v1’ );

if ( ! $api_key || $api_key !== $stored_key ) {
return new WP_Error( ‘invalid_api_key’, ‘Invalid API key’, array( ‘status’ => 401 ) );
}
return true;
}

// Endpoints REST
add_action( ‘rest_api_init’, function() {
// Clientes GET
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $request ) {
$client_id = $request[‘id’];
$client = get_post( $client_id );

if ( ! $client || $client->post_type !== ‘sanro_client’ ) {
return new WP_Error( ‘not_found’, ‘Client not found’, array( ‘status’ => 404 ) );
}

$meta = get_post_meta( $client_id );
$data = array(
‘id’ => $client->ID,
‘title’ => $client->post_title,
‘email’ => $meta[‘client_email’][0] ?? »,
‘age’ => $meta[‘client_age’][0] ?? »,
‘height’ => $meta[‘client_height’][0] ?? »,
‘weight’ => $meta[‘client_weight’][0] ?? »,
‘experience’ => $meta[‘client_experience’][0] ?? »,
‘injuries’ => $meta[‘client_injuries’][0] ?? »,
‘goals’ => $meta[‘client_goals’][0] ?? »,
‘training_frequency’ => $meta[‘client_training_frequency’][0] ?? »,
‘equipment’ => $meta[‘client_equipment’][0] ?? »,
);

return rest_ensure_response( $data );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Clientes POST
register_rest_route( ‘sanro-crm/v1’, ‘/clients’, array(
‘methods’ => ‘POST’,
‘callback’ => function( $request ) {
$params = $request->get_json_params();

$post_id = wp_insert_post( array(
‘post_type’ => ‘sanro_client’,
‘post_title’ => $params[‘name’] ?? ‘New Client’,
‘post_content’ => $params[‘description’] ?? »,
‘post_status’ => ‘publish’,
));

if ( is_wp_error( $post_id ) ) return $post_id;

$meta_fields = array(
‘client_email’, ‘client_age’, ‘client_height’, ‘client_weight’,
‘client_experience’, ‘client_injuries’, ‘client_goals’,
‘client_training_frequency’, ‘client_equipment’
);

foreach ( $meta_fields as $field ) {
if ( isset( $params[ str_replace( ‘client_’, », $field ) ] ) ) {
update_post_meta( $post_id, $field, $params[ str_replace( ‘client_’, », $field ) ] );
}
}

return rest_ensure_response( array( ‘id’ => $post_id, ‘message’ => ‘Client created’ ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Planes de Entrenamiento GET
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/training-plans’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $request ) {
$client_id = $request[‘client_id’];
$plans = get_posts( array(
‘post_type’ => ‘sanro_training_plan’,
‘meta_key’ => ‘training_plan_client_id’,
‘meta_value’ => $client_id,
));

$data = array_map( function( $p ) {
return array(
‘id’ => $p->ID,
‘title’ => $p->post_title,
‘duration’ => get_post_meta( $p->ID, ‘training_plan_duration’, true ),
‘focus’ => get_post_meta( $p->ID, ‘training_plan_focus’, true ),
);
}, $plans );

return rest_ensure_response( $data );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Planes POST
register_rest_route( ‘sanro-crm/v1’, ‘/training-plans’, array(
‘methods’ => ‘POST’,
‘callback’ => function( $request ) {
$params = $request->get_json_params();
$post_id = wp_insert_post( array(
‘post_type’ => ‘sanro_training_plan’,
‘post_title’ => $params[‘title’] ?? ‘Plan’,
‘post_content’ => $params[‘description’] ?? »,
‘post_status’ => ‘publish’,
));

if ( is_wp_error( $post_id ) ) return $post_id;

update_post_meta( $post_id, ‘training_plan_client_id’, $params[‘client_id’] );
update_post_meta( $post_id, ‘training_plan_duration’, $params[‘duration_weeks’] ?? 12 );
update_post_meta( $post_id, ‘training_plan_focus’, $params[‘focus’] ?? ‘General’ );

return rest_ensure_response( array( ‘id’ => $post_id, ‘message’ => ‘Plan created’ ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Sesiones GET
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/workouts’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $request ) {
$workouts = get_posts( array(
‘post_type’ => ‘sanro_workout’,
‘meta_key’ => ‘workout_client_id’,
‘meta_value’ => $request[‘client_id’],
‘posts_per_page’ => 20,
));

return rest_ensure_response( array_map( function( $w ) {
return array(
‘id’ => $w->ID,
‘title’ => $w->post_title,
‘date’ => $w->post_date,
‘exercises’ => get_post_meta( $w->ID, ‘workout_exercises’, true ),
);
}, $workouts ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Sesiones POST
register_rest_route( ‘sanro-crm/v1’, ‘/workouts’, array(
‘methods’ => ‘POST’,
‘callback’ => function( $request ) {
$params = $request->get_json_params();
$post_id = wp_insert_post( array(
‘post_type’ => ‘sanro_workout’,
‘post_title’ => $params[‘title’] ?? ‘Workout’,
‘post_content’ => $params[‘description’] ?? »,
‘post_status’ => ‘publish’,
));

if ( is_wp_error( $post_id ) ) return $post_id;

update_post_meta( $post_id, ‘workout_client_id’, $params[‘client_id’] );
update_post_meta( $post_id, ‘workout_exercises’, wp_json_encode( $params[‘exercises’] ?? array() ) );
update_post_meta( $post_id, ‘workout_duration_minutes’, $params[‘duration’] ?? 60 );

return rest_ensure_response( array( ‘id’ => $post_id, ‘message’ => ‘Workout created’ ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Progreso GET
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/progress’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $request ) {
$progress = get_posts( array(
‘post_type’ => ‘sanro_progress’,
‘meta_key’ => ‘progress_client_id’,
‘meta_value’ => $request[‘client_id’],
‘posts_per_page’ => 10,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
));

return rest_ensure_response( array_map( function( $p ) {
return array(
‘id’ => $p->ID,
‘date’ => $p->post_date,
‘weight’ => get_post_meta( $p->ID, ‘progress_weight’, true ),
‘notes’ => $p->post_content,
);
}, $progress ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Progreso POST
register_rest_route( ‘sanro-crm/v1’, ‘/progress’, array(
‘methods’ => ‘POST’,
‘callback’ => function( $request ) {
$params = $request->get_json_params();
$post_id = wp_insert_post( array(
‘post_type’ => ‘sanro_progress’,
‘post_title’ => ‘Progress – ‘ . current_time( ‘Y-m-d’ ),
‘post_content’ => $params[‘notes’] ?? »,
‘post_status’ => ‘publish’,
));

if ( is_wp_error( $post_id ) ) return $post_id;

update_post_meta( $post_id, ‘progress_client_id’, $params[‘client_id’] );
update_post_meta( $post_id, ‘progress_weight’, $params[‘weight’] );
update_post_meta( $post_id, ‘progress_date’, current_time( ‘mysql’ ) );

return rest_ensure_response( array( ‘id’ => $post_id, ‘message’ => ‘Progress logged’ ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Notas GET
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/notes’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $request ) {
$notes = get_posts( array(
‘post_type’ => ‘sanro_note’,
‘meta_key’ => ‘note_client_id’,
‘meta_value’ => $request[‘client_id’],
‘posts_per_page’ => -1,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
));

return rest_ensure_response( array_map( function( $n ) {
return array(
‘id’ => $n->ID,
‘date’ => $n->post_date,
‘content’ => $n->post_content,
‘category’ => get_post_meta( $n->ID, ‘note_category’, true ),
);
}, $notes ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));

// Notas POST
register_rest_route( ‘sanro-crm/v1’, ‘/notes’, array(
‘methods’ => ‘POST’,
‘callback’ => function( $request ) {
$params = $request->get_json_params();
$post_id = wp_insert_post( array(
‘post_type’ => ‘sanro_note’,
‘post_title’ => $params[‘title’] ?? ‘Note’,
‘post_content’ => $params[‘content’] ?? »,
‘post_status’ => ‘publish’,
));

if ( is_wp_error( $post_id ) ) return $post_id;

update_post_meta( $post_id, ‘note_client_id’, $params[‘client_id’] );
update_post_meta( $post_id, ‘note_category’, $params[‘category’] ?? ‘general’ );
update_post_meta( $post_id, ‘note_date’, current_time( ‘mysql’ ) );

return rest_ensure_response( array( ‘id’ => $post_id, ‘message’ => ‘Note created’ ) );
},
‘permission_callback’ => ‘sanro_crm_verify_api_key’,
));
});
?>

Scroll al inicio
Esta web utiliza cookies propias y de terceros para su correcto funcionamiento y para fines analíticos. Contiene enlaces a sitios web de terceros con políticas de privacidad ajenas que podrás aceptar o no cuando accedas a ellos. Al hacer clic en el botón Aceptar, acepta el uso de estas tecnologías y el procesamiento de tus datos para estos propósitos. Más información
Privacidad