[
'name' => 'Formations Upskillia',
'singular_name' => 'Formation Upskillia',
],
'public' => false,
'show_ui' => true,
'menu_icon' => 'dashicons-welcome-learn-more',
'supports' => ['title'],
'capability_type' => 'post',
'map_meta_cap' => true,
]);
// 2) Meta
$meta_keys = [
'upsk_tag',
'upsk_mode',
'upsk_date_short',
'upsk_date_long',
'upsk_duration',
'upsk_hours',
'upsk_place',
'upsk_description',
'upsk_badge',
'upsk_is_featured',
'upsk_image',
];
foreach ($meta_keys as $k) {
register_post_meta('upsk_formation', $k, [
'type' => 'string',
'single' => true,
'show_in_rest' => false,
'auth_callback' => function () { return current_user_can('manage_options'); },
]);
}
}, 0);
// 3) Auto-création de la page /upskillia-formations
add_action('init', function () {
$flag = get_option('upsk_dashboard_page_created', '0');
if ($flag === '1') return;
$slug = 'upskillia-formations';
$existing = get_page_by_path($slug);
if (!$existing) {
$page_id = wp_insert_post([
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Gestion des formations',
'post_name' => $slug,
'post_content' => '[upsk_formations_dashboard]',
]);
// si Elementor est utilisé, tu pourras ensuite éditer la page et garder le shortcode
if (!is_wp_error($page_id) && $page_id) {
update_option('upsk_dashboard_page_created', '1');
}
} else {
update_option('upsk_dashboard_page_created', '1');
}
}, 20);
// 4) Shortcode Dashboard
add_shortcode('upsk_formations_dashboard', function () {
if (!is_user_logged_in() || !current_user_can('manage_options')) {
return '
Accès refusé.
';
}
$ajax_url = admin_url('admin-ajax.php');
$nonce = wp_create_nonce('upsk_formations_nonce_action');
ob_start(); ?>
Upskillia
Tableau de bord des formations
Ajoute, modifie ou supprime des formations. C’est enregistré en base WordPress.
'upsk_formation',
'post_status' => 'any',
'posts_per_page' => 200,
'orderby' => 'date',
'order' => 'DESC',
]);
$items = [];
foreach ($q->posts as $p) {
$items[] = [
'id' => $p->ID,
'title' => get_the_title($p->ID),
'meta' => [
'upsk_tag' => (string) get_post_meta($p->ID, 'upsk_tag', true),
'upsk_mode' => (string) get_post_meta($p->ID, 'upsk_mode', true),
'upsk_date_short' => (string) get_post_meta($p->ID, 'upsk_date_short', true),
'upsk_date_long' => (string) get_post_meta($p->ID, 'upsk_date_long', true),
'upsk_duration' => (string) get_post_meta($p->ID, 'upsk_duration', true),
'upsk_hours' => (string) get_post_meta($p->ID, 'upsk_hours', true),
'upsk_place' => (string) get_post_meta($p->ID, 'upsk_place', true),
'upsk_description' => (string) get_post_meta($p->ID, 'upsk_description', true),
'upsk_badge' => (string) get_post_meta($p->ID, 'upsk_badge', true),
'upsk_is_featured' => (string) get_post_meta($p->ID, 'upsk_is_featured', true),
'upsk_image' => (string) get_post_meta($p->ID, 'upsk_image', true),
]
];
}
wp_send_json_success($items);
});
add_action('wp_ajax_upsk_save_formation', function () {
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
if (!wp_verify_nonce($nonce, 'upsk_formations_nonce_action')) wp_send_json_error('Nonce invalide.');
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_send_json_error('Accès refusé.');
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : '';
if (!$title) wp_send_json_error('Titre obligatoire.');
$payload = [
'post_type' => 'upsk_formation',
'post_title' => $title,
'post_status' => 'publish',
];
$new_id = 0;
if ($id > 0) {
$payload['ID'] = $id;
$new_id = wp_update_post($payload, true);
} else {
$new_id = wp_insert_post($payload, true);
}
if (is_wp_error($new_id)) wp_send_json_error($new_id->get_error_message());
$fields = [
'upsk_tag','upsk_mode','upsk_date_short','upsk_date_long','upsk_duration',
'upsk_hours','upsk_place','upsk_description','upsk_badge','upsk_is_featured','upsk_image'
];
foreach ($fields as $k) {
$val = isset($_POST[$k]) ? wp_kses_post($_POST[$k]) : '';
update_post_meta($new_id, $k, $val);
}
// Unicité simple : si une formation est mise en avant, on désactive les autres
$is_featured = get_post_meta($new_id, 'upsk_is_featured', true) === '1';
if ($is_featured) {
$others = get_posts([
'post_type' => 'upsk_formation',
'post_status' => 'any',
'numberposts' => -1,
'exclude' => [$new_id],
'fields' => 'ids',
'meta_key' => 'upsk_is_featured',
'meta_value' => '1',
]);
foreach ($others as $oid) update_post_meta($oid, 'upsk_is_featured', '0');
}
wp_send_json_success(['id' => $new_id]);
});
add_action('wp_ajax_upsk_delete_formation', function () {
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
if (!wp_verify_nonce($nonce, 'upsk_formations_nonce_action')) wp_send_json_error('Nonce invalide.');
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_send_json_error('Accès refusé.');
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
if ($id <= 0) wp_send_json_error('ID invalide.');
$p = get_post($id);
if (!$p || $p->post_type !== 'upsk_formation') wp_send_json_error('Introuvable.');
$ok = wp_delete_post($id, true);
if (!$ok) wp_send_json_error('Suppression impossible.');
wp_send_json_success(true);
});