(filtré par kses via REST API)
*/
// ── 1. Register CPT ml_survey ──────────────────────────
add_action( 'init', 'ml_survey_register_cpt' );
function ml_survey_register_cpt() {
$cfg = json_decode( '{"label":"Sondages","labels":{"name":"Sondages","singular_name":"Sondage","menu_name":"Sondages","all_items":"Toutes les ru00e9ponses","add_new":"Ajouter","add_new_item":"Nouvelle ru00e9ponse","edit_item":"u00c9diter","view_item":"Voir","search_items":"Rechercher","not_found":"Aucune ru00e9ponse"},"public":false,"show_ui":true,"show_in_menu":false,"supports":["title","custom-fields"],"capability_type":"post","map_meta_cap":true}', true );
register_post_type( 'ml_survey', $cfg );
}
// ── 2. Menu admin "Sondages" avec icône ronde ──────────
add_action( 'admin_menu', 'ml_survey_admin_menu' );
function ml_survey_admin_menu() {
add_menu_page(
'Sondages My Legacy',
'Sondages',
'manage_options',
'ml-sondages',
'ml_survey_admin_page',
'dashicons-chart-pie',
26
);
}
// ── 3. REST API endpoint public ────────────────────────
add_action( 'rest_api_init', 'ml_survey_register_route' );
function ml_survey_register_route() {
$cfg = json_decode( '{"methods":"POST","permission_callback":"__return_true"}', true );
$cfg['callback'] = 'ml_survey_save';
register_rest_route( 'ml/v1', '/survey', $cfg );
}
function ml_survey_save( $request ) {
$raw = file_get_contents( 'php://input' );
$data = json_decode( $raw, true );
if ( ! is_array( $data ) ) {
$data = array();
}
$insert = json_decode( '{"post_type":"ml_survey","post_status":"publish"}', true );
$insert['post_title'] = 'Sondage — ' . date( 'd/m/Y H:i:s' );
$pid = wp_insert_post( $insert );
if ( $pid && ! is_wp_error( $pid ) ) {
$allowed = array( 'satisfaction', 'note', 'situation', 'source', 'page', 'user_agent' );
foreach ( $allowed as $field ) {
if ( isset( $data[ $field ] ) ) {
update_post_meta( $pid, 'mlsv_' . $field, sanitize_text_field( $data[ $field ] ) );
}
}
update_post_meta( $pid, 'mlsv_ip', sanitize_text_field( $_SERVER['REMOTE_ADDR'] ) );
$result = json_decode( '{"success":true}', true );
$result['id'] = $pid;
return $result;
}
return json_decode( '{"success":false}', true );
}
// ── 4. Page admin dashboard ────────────────────────────
function ml_survey_admin_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$q_args = json_decode( '{"post_type":"ml_survey","post_status":"publish","posts_per_page":200,"orderby":"date","order":"DESC"}', true );
$posts = get_posts( $q_args );
$total = count( $posts );
// Agrégation par satisfaction
$sats = array();
foreach ( $posts as $p ) {
$s = get_post_meta( $p->ID, 'mlsv_satisfaction', true );
if ( $s ) {
$sats[ $s ] = isset( $sats[ $s ] ) ? $sats[ $s ] + 1 : 1;
}
}
// Agrégation situation
$sits = array();
foreach ( $posts as $p ) {
$s = get_post_meta( $p->ID, 'mlsv_situation', true );
if ( $s ) {
$sits[ $s ] = isset( $sits[ $s ] ) ? $sits[ $s ] + 1 : 1;
}
}
// Note moyenne
$notes = array();
foreach ( $posts as $p ) {
$n = get_post_meta( $p->ID, 'mlsv_note', true );
if ( $n ) {
$notes[] = intval( $n );
}
}
$avg_note = count( $notes ) ? round( array_sum( $notes ) / count( $notes ), 1 ) : '–';
echo '
// Header
echo '
';
echo '
';
echo '
' . $total . ' réponse' . ( $total > 1 ? 's' : '' ) . ' collectée' . ( $total > 1 ? 's' : '' ) . '
';
echo '
';
echo '
';
// KPIs
echo '
// Total
echo '
';
echo '
';
echo '
';
// Note moyenne
echo '
';
echo '
';
echo '
';
// Taux satisfaction positive
$positives = 0;
foreach ( $sats as $k => $v ) {
if ( $k === 'Très satisfait' || $k === 'Satisfait' ) {
$positives += $v;
}
}
$sat_pct = $total ? round( $positives / $total * 100 ) : 0;
echo '
';
echo '
';
echo '
';
echo '
';
if ( $total === 0 ) {
echo '
Aucune réponse pour l'instant. Le sondage est actif sur les articles.
';
echo '
';
return;
}
// Répartition satisfaction
echo '
echo '
';
$sat_order = array( 'Très satisfait', 'Satisfait', 'Neutre', 'Insatisfait', 'Très insatisfait' );
$sat_colors = array( '#0F7B56', '#2ECC8A', '#6C757D', '#E8A212', '#C0392B' );
foreach ( $sat_order as $idx => $label ) {
$count = isset( $sats[ $label ] ) ? $sats[ $label ] : 0;
$pct = $total ? round( $count / $total * 100 ) : 0;
$color = $sat_colors[ $idx ];
echo '
';
echo '
';
echo '
';
echo '
';
}
echo '
';
echo '
';
arsort( $sits );
foreach ( $sits as $label => $count ) {
$pct = $total ? round( $count / $total * 100 ) : 0;
echo '
';
echo '
';
echo '
';
echo '
';
}
echo '
';
echo '
';
// Tableau des dernières réponses
echo '
';
echo '
';
}