Sanro CRM – API REST Active

‘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 API key’, array( ‘status’ => 401 ) );
}
return true;
}

add_action( ‘rest_api_init’, function() {
// GET /clients/{id}
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $r ) {
$c = get_post( $r[‘id’] );
if ( !$c || $c->post_type !== ‘sanro_client’ ) {
return new WP_Error( ‘not_found’, ‘Client 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’,
));

// POST /clients
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’,
));

// GET /clients/{client_id}/training-plans
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/training-plans’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $r ) {
$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’,
));

// POST /training-plans
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’,
));

// GET /clients/{cid}/workouts
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/workouts’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $r ) {
$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’,
));

// POST /workouts
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’,
));

// GET /clients/{cid}/progress
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/progress’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $r ) {
$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’,
));

// POST /progress
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_content’ => $p[‘notes’] ?? », ‘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’,
));

// GET /clients/{cid}/notes
register_rest_route( ‘sanro-crm/v1’, ‘/clients/(?P\d+)/notes’, array(
‘methods’ => ‘GET’,
‘callback’ => function( $r ) {
$n = get_posts( array( ‘post_type’ => ‘sanro_note’, ‘meta_key’ => ‘note_client_id’, ‘meta_value’ => $r[‘cid’], ‘posts_per_page’ => -1 ) );
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’,
));

// POST /notes
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_content’ => $p[‘content’] ?? », ‘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’,
));
});
?>

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