(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 '
ML

';
echo '

';
echo '

Sondages My Legacy

';
echo '

' . $total . ' réponse' . ( $total > 1 ? 's' : '' ) . ' collectée' . ( $total > 1 ? 's' : '' ) . '

';
echo '

';
echo '

';

// KPIs
echo '

';

// Total
echo '

';
echo '
' . $total . '

';
echo '

Réponses totales

';
echo '

';

// Note moyenne
echo '

';
echo '
' . $avg_note . '/5

';
echo '

Note moyenne utilité

';
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 '
' . $sat_pct . '%

';
echo '

Satisfaction positive

';
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 '

';
echo '

Satisfaction

';
$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 '' . esc_html( $label ) . '' . $count . ' (' . $pct . '%)';
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
}
echo '

';

echo '

';
echo '

Situation visiteurs

';
arsort( $sits );
foreach ( $sits as $label => $count ) {
$pct = $total ? round( $count / $total * 100 ) : 0;
echo '

';
echo '
';
echo '' . esc_html( $label ) . '' . $count . ' (' . $pct . '%)';
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
}
echo '

';

echo '

';

// Tableau des dernières réponses
echo '

';
echo '
Dernières réponses

';
echo '

';
echo ' ';
echo ' ';
echo ' ';
foreach ( array( 'Date', 'Satisfaction', 'Note', 'Situation', 'Source', 'Page' ) as $h ) {
echo '

';
}
echo '

';

$shown = 0;
foreach ( $posts as $p ) {
if ( $shown >= 50 ) break;
$shown++;
$sat = get_post_meta( $p->ID, 'mlsv_satisfaction', true );
$note = get_post_meta( $p->ID, 'mlsv_note', true );
$sit = get_post_meta( $p->ID, 'mlsv_situation', true );
$src = get_post_meta( $p->ID, 'mlsv_source', true );
$pg = get_post_meta( $p->ID, 'mlsv_page', true );
$pg_short = $pg ? preg_replace( '/https?://[^/]+/', '', $pg ) : '–';

$bg = $shown % 2 === 0 ? '#F8F9FC' : '#fff';
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
}

echo '

' . $h . '
' . get_the_date( 'd/m H:i', $p->ID ) . ' ' . esc_html( $sat ?: '–' ) . ' ' . esc_html( $note ?: '–' ) . ' ' . esc_html( $sit ?: '–' ) . ' ' . esc_html( $src ?: '–' ) . ' ' . esc_html( $pg_short ?: '–' ) . '

';

echo '

';
}

Mis à jour le 5 avril 2026
Avertissement : Les informations présentées sur ce site sont fournies à titre informatif uniquement et ne constituent pas des conseils financiers, fiscaux, juridiques ou d'investissement. Les auteurs et l'équipe du site ne sont pas des conseillers financiers et ne prétendent pas l'être. Nous nous efforçons de présenter des informations précises et à jour, mais nous ne pouvons garantir l'exactitude ou l'exhaustivité de toutes les informations disponibles sur notre site.
menu-circlecross-circle