Metiers_du_patrimoine_theme/includes/admin.php
Antoine M dab69b5ab9
All checks were successful
continuous-integration/drone/push Build is passing
refactoring and cleaning
2025-02-18 15:16:25 +01:00

438 lines
12 KiB
PHP

<?php
/* ---------------------------
REMOVING POSTS FROM ADMIN MENU
---------------------------*/
function post_remove()
{
remove_menu_page('edit.php');
}
add_action('admin_menu', 'post_remove');
/* ---------------------------
ADMIN STYLES
---------------------------*/
// Amin Stylesheet
function metiers_du_patrimoine_custom_admin_stylesheet()
{
wp_enqueue_style('admin_styles', get_stylesheet_directory_uri() . '/css/admin-style.css');
}
add_action('admin_head', 'metiers_du_patrimoine_custom_admin_stylesheet');
/* ---------------------------
COMMENTS REMOVAL
---------------------------*/
function metiers_remove_meta_boxes()
{
# Removes meta from Posts #
// remove_meta_box('postexcerpt', 'post', 'normal');
// remove_meta_box('postcustom', 'post', 'normal');
// remove_meta_box('trackbacksdiv', 'post', 'normal');
remove_meta_box('commentstatusdiv', 'page', 'normal');
remove_meta_box('commentsdiv', 'page', 'normal');
remove_post_type_support('artisans', 'comments');
remove_post_type_support('chantiers', 'comments');
}
add_action('admin_init', 'metiers_remove_meta_boxes');
/* ---------------------------
OFFLINE POST STATUS
---------------------------*/
function custom_post_status()
{
register_post_status('offline', array(
'label' => __('Hors ligne', 'metiers-patrimoine-theme'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Unread (%s)', 'Unread (%s)'),
));
}
add_action('init', 'custom_post_status');
function add_to_post_status_dropdown()
{
global $post;
// Ajoute une option 'offline' au menu déroulant des statuts
echo "<script>
jQuery(document).ready(function() {
jQuery('select[name=\"post_status\"]').append('<option value=\"offline\">offline</option>');
";
// Vérifie si le statut du post actuel est 'offline' et le pré-sélectionne
if ($post->post_status === 'offline') {
echo "jQuery('#post-status-display').text('offline');
jQuery('select[name=\"post_status\"]').val('offline');";
}
echo "
});
</script>";
}
// add_action('post_submitbox_misc_actions', 'add_to_post_status_dropdown');
add_action('admin_footer-post.php', function () {
?><script>
jQuery(function($) {
$('#post_status').append('<option value="offline">Hors ligne</option>')
$('#post_status option[value="publish"]').text('En ligne');
$('#post_status option[value="pending"], #post_status option[value="draft"], #post_status option[value="private"]').remove();
<?php if ('offline' === get_post_status()) : ?>
$('#post-status-display').text('Hors ligne')
$('#post_status').val('offline')
<?php endif; ?>
})
</script>
<?php
});
add_action('admin_footer-edit.php', function () {
?>
<script>
jQuery(function($) {
$('#bulk-edit select[name="_status"]').append('<option value="offline">Hors ligne</option>');
$('#bulk-edit select[name="_status"] option[value="publish"]').text('En ligne');
$('#bulk-edit select[name="_status"] option[value="offline"]').text('Hors ligne');
$('#bulk-edit select[name="_status"] option[value="pending"], #bulk-edit select[name="_status"] option[value="draft"], #bulk-edit select[name="_status"] option[value="private"]').remove();
});
</script>
<?php
});
// Ajouter un filtre pour afficher les posts avec le statut 'offline'
add_filter('views_edit-artisans', function ($views) {
global $wpdb;
$post_type = 'artisans';
$status = 'offline';
// Compte le nombre de posts avec le statut 'offline'
$count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = %s",
$post_type,
$status
));
if ($count > 0) {
$class = (isset($_GET['post_status']) && $_GET['post_status'] === $status) ? 'current' : '';
$views[$status] = sprintf(
'<a href="edit.php?post_type=%s&post_status=%s" class="%s">%s <span class="count">(%d)</span></a>',
$post_type,
$status,
$class,
__('Offline', 'metiers-patrimoine-theme'),
$count
);
}
return $views;
});
// Ajouter le statut personnalisé aux actions de groupe
function add_custom_bulk_actions($actions)
{
$actions['mark_offline'] = __('Mettre hors ligne', 'metiers-patrimoine-theme');
$actions['mark_online'] = __('Mettre en ligne', 'metiers-patrimoine-theme');
return $actions;
}
add_filter('bulk_actions-edit-artisans', 'add_custom_bulk_actions');
// Gérer l'action de groupe pour appliquer le statut 'offline'
function handle_custom_bulk_actions($redirect_to, $doaction, $post_ids)
{
if ($doaction === 'mark_offline') {
foreach ($post_ids as $post_id) {
wp_update_post(array(
'ID' => $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 "<div class=\"updated\"><p>{$message}</p></div>";
}
if (!empty($_REQUEST['bulk_offline_updated'])) {
$message = sprintf(__('%d artisans mis hors ligne', 'metiers-patrimoine-theme'), intval($_REQUEST['bulk_offline_updated']));
echo "<div class=\"updated\"><p>{$message}</p></div>";
}
}
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)
{
// Vérifier si on est dans l'admin et si le post appartient au CPT 'artisans'
if ('artisans' === $post->post_type) {
global $pagenow;
// Vérifier si on est sur la page d'index des articles (Tous les articles)
if ('edit.php' === $pagenow && !isset($_GET['post_status'])) {
// Ajouter le statut "Publié" si l'article est publié
if ('publish' === $post->post_status) {
$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[] = __('Hors ligne', 'metiers-patrimoine-theme'); // Affiche "En attente"
}
}
}
return $post_states;
}
add_filter('display_post_states', 'add_custom_post_status', 10, 2);
/* ----------------------------------------------------------------------
REORDER ADMIN MENU & POST TYPE ORDER ON MENU BAR
------------------------------------------------------------------------*/
/**
* Filters WordPress' default menu order
*/
function metiers_new_admin_menu_order($menu_order)
{
// define your new desired menu positions here
// for example, move 'upload.php' to position #9 and built-in pages to position #1
$new_positions = array(
'edit.php?post_type=artisans' => 3,
'edit.php?post_type=chantiers' => 4,
'edit.php?post_type=page' => 7,
'upload.php' => 8,
'export_datas' => 9,
'theme-general-settings' => 11,
'rank-math' => 20,
);
// helper function to move an element inside an array
function metiers_move_element(&$array, $a, $b)
{
$out = array_splice($array, $a, 1);
array_splice($array, $b, 0, $out);
}
// traverse through the new positions and move
// the items if found in the original menu_positions
foreach ($new_positions as $value => $new_index) {
// write_log($value);
if ($current_index = array_search($value, $menu_order)) {
metiers_move_element($menu_order, $current_index, $new_index);
}
}
return $menu_order;
};
add_action('admin_menu', function () {
add_filter('menu_order', 'metiers_new_admin_menu_order');
add_filter('custom_menu_order', '__return_true');
});
function hide_meta_boxes_by_default()
{
// List of meta boxes to hide by default
$hidden_meta_boxes = array(
'post' => array(
'postexcerpt', // Excerpt
'trackbacksdiv', // Trackbacks
'postcustom', // Custom Fields
'commentstatusdiv', // Discussion
'commentsdiv', // Comments
'authordiv', // Author
'slugdiv' // Slug
),
'page' => array(
'postexcerpt', // Excerpt
'trackbacksdiv', // Trackbacks
'postcustom', // Custom Fields
'commentstatusdiv', // Discussion
'commentsdiv', // Comments
'authordiv', // Author
'slugdiv' // Slug
)
);
// Get the current user ID
$user_id = get_current_user_id();
// Get the hidden meta boxes for the current user
$hidden = get_user_meta($user_id, 'metaboxhidden_post', true);
// Merge the existing hidden meta boxes with the new ones
$hidden = array_unique(array_merge((array) $hidden, $hidden_meta_boxes['post']));
// Update the user meta with the new hidden meta boxes
update_user_meta($user_id, 'metaboxhidden_post', $hidden);
// Repeat for pages
$hidden = get_user_meta($user_id, 'metaboxhidden_page', true);
$hidden = array_unique(array_merge((array) $hidden, $hidden_meta_boxes['page']));
update_user_meta($user_id, 'metaboxhidden_page', $hidden);
}
// Hook the function to the admin_init action
// add_action('admin_init', 'hide_meta_boxes_by_default');
function hide_wpml_language_metabox()
{
// Supprime la meta box "Langue" de WPML dans l'éditeur de post
remove_meta_box('icl_div', 'post', 'side');
remove_meta_box('icl_div', 'page', 'side');
// Ajoutez d'autres types de post si nécessaire
}
// 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;
}
}
return $classes;
}
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',
'Métiers',
'edit_others_posts',
'edit-tags.php?taxonomy=metiers&post_type=artisans',
'',
'dashicons-tag',
50
);
add_menu_page(
'Éléments du batiment',
'Éléments du batiment',
'edit_others_posts',
'edit-tags.php?taxonomy=elementsbatiments&post_type=artisans',
'',
'dashicons-tag',
58
);
}
add_action('admin_menu', 'add_custom_taxonomy_menu_item');
// function add_custom_update_button()
// {
// global $post;
// if ($post->post_status === 'publish') {
// $update_url = admin_url('post.php?post=' . $post->ID . '&action=edit');
// echo '<a href="' . esc_url($update_url) . '" class="button button-secondary" style="margin-left:10px;">Mettre à jour l\'article</a>';
// }
// }
// add_action('post_submitbox_misc_actions', 'add_custom_update_button');