‘Clientes’, ‘sanro_training_plan’ => ‘Planes de Entrenamiento’, ‘sanro_workout’ => ‘Sesiones’, ‘sanro_progress’ => ‘Progreso’, ‘sanro_note’ => ‘Notas’, ‘sanro_form’ => ‘Formularios’);
foreach ( $types as $s => $l ) {
register_post_type( $s, array(‘label’ => $l, ‘public’ => true, ‘show_in_rest’ => true, ‘supports’ => array( ‘title’, ‘editor’ ), ‘menu_icon’ => ‘dashicons-groups’));
}
});
function sanro_verify_key( $r ) {
$key = $r->get_header( ‘X-Claude-API-Key’ );
if ( $key !== ‘claude_sanro_2026_secure_key_v1’ ) return new WP_Error( ‘auth’, ‘Invalid’, array( ‘status’ => 401 ) );
return true;
}
add_action( ‘rest_api_init’, function() {
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P
$c = get_post( $r[‘id’] );
if ( !$c || $c->post_type !== ‘sanro_client’ ) return new WP_Error( ‘not_found’, ‘Not found’, array( ‘status’ => 404 ) );
$m = get_post_meta( $c->ID );
return rest_ensure_response( array(‘id’ => $c->ID, ‘title’ => $c->post_title, ‘email’ => $m[‘client_email’][0] ?? », ‘age’ => $m[‘client_age’][0] ?? », ‘height’ => $m[‘client_height’][0] ?? », ‘weight’ => $m[‘client_weight’][0] ?? », ‘experience’ => $m[‘client_experience’][0] ?? », ‘injuries’ => $m[‘client_injuries’][0] ?? », ‘goals’ => $m[‘client_goals’][0] ?? », ‘training_frequency’ => $m[‘client_training_frequency’][0] ?? », ‘equipment’ => $m[‘client_equipment’][0] ?? »));
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/clients’, array(‘methods’ => ‘POST’, ‘callback’ => function( $r ) {
$p = $r->get_json_params();
$id = wp_insert_post( array(‘post_type’ => ‘sanro_client’, ‘post_title’ => $p[‘name’] ?? ‘Client’, ‘post_status’ => ‘publish’));
if ( is_wp_error( $id ) ) return $id;
foreach ( array( ‘email’, ‘age’, ‘height’, ‘weight’, ‘experience’, ‘injuries’, ‘goals’, ‘training_frequency’, ‘equipment’ ) as $f ) {
if ( isset( $p[$f] ) ) update_post_meta( $id, ‘client_’ . $f, $p[$f] );
}
return rest_ensure_response( array( ‘id’ => $id, ‘message’ => ‘Client created’ ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P
$plans = get_posts( array( ‘post_type’ => ‘sanro_training_plan’, ‘meta_key’ => ‘training_plan_client_id’, ‘meta_value’ => $r[‘cid’] ) );
return rest_ensure_response( 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 ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/training-plans’, array(‘methods’ => ‘POST’, ‘callback’ => function( $r ) {
$p = $r->get_json_params();
$id = wp_insert_post( array( ‘post_type’ => ‘sanro_training_plan’, ‘post_title’ => $p[‘title’] ?? ‘Plan’, ‘post_status’ => ‘publish’ ) );
if ( is_wp_error( $id ) ) return $id;
update_post_meta( $id, ‘training_plan_client_id’, $p[‘client_id’] );
update_post_meta( $id, ‘training_plan_duration’, $p[‘duration_weeks’] ?? 12 );
update_post_meta( $id, ‘training_plan_focus’, $p[‘focus’] ?? ‘General’ );
return rest_ensure_response( array( ‘id’ => $id, ‘message’ => ‘Plan created’ ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P
$w = get_posts( array( ‘post_type’ => ‘sanro_workout’, ‘meta_key’ => ‘workout_client_id’, ‘meta_value’ => $r[‘cid’], ‘posts_per_page’ => 20 ) );
return rest_ensure_response( array_map( function( $x ) { return array( ‘id’ => $x->ID, ‘title’ => $x->post_title, ‘date’ => $x->post_date, ‘exercises’ => get_post_meta( $x->ID, ‘workout_exercises’, true ) ); }, $w ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/workouts’, array(‘methods’ => ‘POST’, ‘callback’ => function( $r ) {
$p = $r->get_json_params();
$id = wp_insert_post( array( ‘post_type’ => ‘sanro_workout’, ‘post_title’ => $p[‘title’] ?? ‘Workout’, ‘post_status’ => ‘publish’ ) );
if ( is_wp_error( $id ) ) return $id;
update_post_meta( $id, ‘workout_client_id’, $p[‘client_id’] );
update_post_meta( $id, ‘workout_exercises’, wp_json_encode( $p[‘exercises’] ?? array() ) );
return rest_ensure_response( array( ‘id’ => $id, ‘message’ => ‘Workout created’ ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P
$pg = get_posts( array( ‘post_type’ => ‘sanro_progress’, ‘meta_key’ => ‘progress_client_id’, ‘meta_value’ => $r[‘cid’], ‘posts_per_page’ => 10, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’ ) );
return rest_ensure_response( array_map( function( $x ) { return array( ‘id’ => $x->ID, ‘date’ => $x->post_date, ‘weight’ => get_post_meta( $x->ID, ‘progress_weight’, true ), ‘notes’ => $x->post_content ); }, $pg ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/progress’, array(‘methods’ => ‘POST’, ‘callback’ => function( $r ) {
$p = $r->get_json_params();
$id = wp_insert_post( array( ‘post_type’ => ‘sanro_progress’, ‘post_title’ => ‘Progress’, ‘post_status’ => ‘publish’ ) );
if ( is_wp_error( $id ) ) return $id;
update_post_meta( $id, ‘progress_client_id’, $p[‘client_id’] );
update_post_meta( $id, ‘progress_weight’, $p[‘weight’] );
return rest_ensure_response( array( ‘id’ => $id, ‘message’ => ‘Progress logged’ ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P
$n = get_posts( array( ‘post_type’ => ‘sanro_note’, ‘meta_key’ => ‘note_client_id’, ‘meta_value’ => $r[‘cid’] ) );
return rest_ensure_response( array_map( function( $x ) { return array( ‘id’ => $x->ID, ‘date’ => $x->post_date, ‘content’ => $x->post_content ); }, $n ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
register_rest_route( ‘sanro-crm/v1’, ‘/notes’, array(‘methods’ => ‘POST’, ‘callback’ => function( $r ) {
$p = $r->get_json_params();
$id = wp_insert_post( array( ‘post_type’ => ‘sanro_note’, ‘post_title’ => $p[‘title’] ?? ‘Note’, ‘post_status’ => ‘publish’ ) );
if ( is_wp_error( $id ) ) return $id;
update_post_meta( $id, ‘note_client_id’, $p[‘client_id’] );
return rest_ensure_response( array( ‘id’ => $id, ‘message’ => ‘Note created’ ) );
}, ‘permission_callback’ => ‘sanro_verify_key’));
});