From dab69b5ab927027627ff343b0c8beff1828afbf6 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Tue, 18 Feb 2025 15:16:25 +0100 Subject: [PATCH] refactoring and cleaning --- includes/admin.php | 206 ++++++++++++++++++++++++++++++++++------- includes/artisans.php | 120 +++++------------------- includes/chantiers.php | 28 ++++++ 3 files changed, 223 insertions(+), 131 deletions(-) diff --git a/includes/admin.php b/includes/admin.php index c68bb09..418932f 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -43,13 +43,13 @@ add_action('admin_init', 'metiers_remove_meta_boxes'); /* --------------------------- -CUSTOM POST STATUS +OFFLINE POST STATUS ---------------------------*/ function custom_post_status() { register_post_status('offline', array( - 'label' => __('offline', 'metiers-patrimoine-theme'), + 'label' => __('Hors ligne', 'metiers-patrimoine-theme'), 'public' => true, 'exclude_from_search' => false, 'show_in_admin_all_list' => true, @@ -79,7 +79,55 @@ jQuery(document).ready(function() { }); "; } -add_action('post_submitbox_misc_actions', 'add_to_post_status_dropdown'); +// add_action('post_submitbox_misc_actions', 'add_to_post_status_dropdown'); + + + +add_action('admin_footer-post.php', function () { + +?> + + + $post_id, + 'post_status' => 'offline' + )); + } + $redirect_to = add_query_arg('bulk_online_updated', count($post_ids), $redirect_to); + } + if ($doaction === 'mark_online') { + foreach ($post_ids as $post_id) { + wp_update_post(array( + 'ID' => $post_id, + 'post_status' => 'published' + )); + } + $redirect_to = add_query_arg('bulk_offline_updated', count($post_ids), $redirect_to); + } + return $redirect_to; +} +add_filter('handle_bulk_actions-edit-artisans', 'handle_custom_bulk_actions', 10, 3); + +// Ajouter un message d'administration pour confirmer l'action de groupe +function custom_bulk_admin_notices() +{ + if (!empty($_REQUEST['bulk_online_updated'])) { + $message = sprintf(__('%d artisan(s) mis en ligne', 'metiers-patrimoine-theme'), intval($_REQUEST['bulk_online_updated'])); + echo "

{$message}

"; + } + if (!empty($_REQUEST['bulk_offline_updated'])) { + $message = sprintf(__('%d artisans mis hors ligne', 'metiers-patrimoine-theme'), intval($_REQUEST['bulk_offline_updated'])); + echo "

{$message}

"; + } +} +add_action('admin_notices', 'custom_bulk_admin_notices'); + + + + + + + + + + + + + +/* ----------------------------------------------------- + POST STATUS AFTER TITLE ON EDIT PAGE WITH ALL POSTS +-----------------------------------------------------*/ + // Ajouter un statut personnalisé dans la colonne des statuts, uniquement dans la vue "Tous" function add_custom_post_status($post_states, $post) @@ -126,12 +239,12 @@ function add_custom_post_status($post_states, $post) // Ajouter le statut "Publié" si l'article est publié if ('publish' === $post->post_status) { - $post_states[] = __('Publié', 'metiers-patrimoine-theme'); // Affiche "Publié" + $post_states[] = __('En ligne', 'metiers-patrimoine-theme'); // Affiche "Publié" } // Ajouter votre statut personnalisé (ex : "offline") si c'est le cas if ('offline' === get_post_status($post->ID)) { - $post_states[] = __('Offline', 'metiers-patrimoine-theme'); // Affiche "En attente" + $post_states[] = __('Hors ligne', 'metiers-patrimoine-theme'); // Affiche "En attente" } } } @@ -145,10 +258,6 @@ add_filter('display_post_states', 'add_custom_post_status', 10, 2); /* ---------------------------------------------------------------------- REORDER ADMIN MENU & POST TYPE ORDER ON MENU BAR ------------------------------------------------------------------------*/ -/** - * Activates the 'menu_order' filter and then hooks into 'menu_order' - */ - /** * Filters WordPress' default menu order */ @@ -247,57 +356,82 @@ function hide_wpml_language_metabox() // Applique la fonction pour masquer la meta box "Langue" // add_action('admin_menu', 'hide_wpml_language_metabox'); + +/* ---------------------------------------------------------------------- + BODY CLASS + ------------------------------------------------------------------------*/ + +//** BODY CLASS FOR USER ROLE *// function add_user_role_to_admin_body_class($classes) { $current_user = wp_get_current_user(); if (!empty($current_user->roles)) { foreach ($current_user->roles as $role) { $classes .= ' role-' . $role; - $classes .= ' salut fred'; } } + return $classes; } -// Applique le filtre pour ajouter la classe au corps de l'interface admin add_filter('admin_body_class', 'add_user_role_to_admin_body_class'); +//** BODY CLASS FOR POST STATUS *// +function add_post_status_to_body_class($classes) +{ + global $post; + + if ($post && isset($post->post_status)) { + $post_status = $post->post_status; + $classes .= ' post-status-' . $post_status; + // $classes[] = 'post-status-' . $post_status; // Ajoute une classe pour le statut du post + write_log($post_status); + } + + return $classes; +} +add_filter('admin_body_class', 'add_post_status_to_body_class'); + + + + +/* --------------------------------------------------------------------------------- + RÉ-AJOUT DES ITEMS DE TAXONOMY DANS LE MENU, MAIS PAS SOUS ARTISANS MAIS À PART + -----------------------------------------------------------------------------------*/ function add_custom_taxonomy_menu_item() { add_menu_page( - 'Métiers', // Titre de la page dans le navigateur - 'Métiers', // Texte à afficher dans le menu - 'edit_others_posts', // Capacité requise pour voir le menu - 'edit-tags.php?taxonomy=metiers&post_type=artisans', // URL de la taxonomie - '', // Pas de fonction de rappel, car nous redirigeons - 'dashicons-tag', // Icône (facultatif) - 50 // Position dans le menu (facultatif) + 'Métiers', + 'Métiers', + 'edit_others_posts', + 'edit-tags.php?taxonomy=metiers&post_type=artisans', + '', + 'dashicons-tag', + 50 ); add_menu_page( - 'Éléments du batiment', // Titre de la page dans le navigateur - 'Éléments du batiment', // Texte à afficher dans le menu - 'edit_others_posts', // Capacité requise pour voir le menu - 'edit-tags.php?taxonomy=elementsbatiments&post_type=artisans', // URL de la taxonomie - '', // Pas de fonction de rappel, car nous redirigeons - 'dashicons-tag', // Icône (facultatif) - 58 // Position dans le menu (facultatif) + 'Éléments du batiment', + 'Éléments du batiment', + 'edit_others_posts', + 'edit-tags.php?taxonomy=elementsbatiments&post_type=artisans', + '', + 'dashicons-tag', + 58 ); } - -// Ajoute l'élément de menu lors de l'initialisation de l'admin add_action('admin_menu', 'add_custom_taxonomy_menu_item'); -function add_custom_update_button() -{ - global $post; +// function add_custom_update_button() +// { +// global $post; - if ($post->post_status === 'publish') { - $update_url = admin_url('post.php?post=' . $post->ID . '&action=edit'); - echo 'Mettre à jour l\'article'; - } -} -add_action('post_submitbox_misc_actions', 'add_custom_update_button'); +// if ($post->post_status === 'publish') { +// $update_url = admin_url('post.php?post=' . $post->ID . '&action=edit'); +// echo 'Mettre à jour l\'article'; +// } +// } +// add_action('post_submitbox_misc_actions', 'add_custom_update_button'); diff --git a/includes/artisans.php b/includes/artisans.php index dccb63d..2259130 100644 --- a/includes/artisans.php +++ b/includes/artisans.php @@ -26,33 +26,6 @@ function metiers_patrimoine_artisans_post_updater($post_id) add_action('acf/save_post', 'metiers_patrimoine_artisans_post_updater', 20); -function metiers_patrimoine_chantiers_post_updater($post_id) -{ - if (!$post_id || get_post_type() !== 'chantiers') return; - - $my_post = array(); - $my_post['ID'] = $post_id; - - $artisan = get_field("artisan", $post_id) ?? ""; - $chantier_name = get_field("chantier_name", $post_id) ?? ""; - $date = get_field("date", $post_id) ?? ""; - $localisation = get_field("localisation", $post_id) ?? ""; - - if (!$artisan || !$chantier_name) return; - - $title = $artisan->post_title . " | " . $chantier_name; - - if ($localisation && isset($localisation['city'])) { - $title .= " | " . $localisation['city']; - } - if ($date) { - $title .= " | " . $date; - } - $my_post['post_title'] = $title; - wp_update_post($my_post); -} -add_action('acf/save_post', 'metiers_patrimoine_chantiers_post_updater', 20); - /* --------------------------------------- CUSTOM CONTENT AFTER POST TYPE TITLE ------------------------------------------*/ @@ -77,9 +50,25 @@ add_action('edit_form_after_title', function () {

Statut :

+ + + + + -
- - -
- -
-

Page d’options personnalisée

- -
- 'core')); - /* ---------------------------------------------------------------------- SUPPRIMER LA COLONNE RANK MATH SEO SI ELLE EST PRÉSENTE ------------------------------------------------------------------------*/ diff --git a/includes/chantiers.php b/includes/chantiers.php index 9f47a0d..29f2b5d 100644 --- a/includes/chantiers.php +++ b/includes/chantiers.php @@ -1,5 +1,33 @@ post_title . " | " . $chantier_name; + + if ($localisation && isset($localisation['city'])) { + $title .= " | " . $localisation['city']; + } + if ($date) { + $title .= " | " . $date; + } + $my_post['post_title'] = $title; + wp_update_post($my_post); +} +add_action('acf/save_post', 'metiers_patrimoine_chantiers_post_updater', 20); + + /* ----------------------------------------- EDIT ARTISAN LINK ON CHANTIER EDIT PAGE -----------------------------------------*/