422 lines
13 KiB
PHP
422 lines
13 KiB
PHP
<?php
|
||
function metiers_patrimoine_artisans_post_updater($post_id)
|
||
{
|
||
|
||
|
||
if (!$post_id) return;
|
||
|
||
$my_post = array();
|
||
$my_post['ID'] = $post_id;
|
||
$name = get_field("name", $post_id);
|
||
$address = get_field("adresse", $post_id);
|
||
|
||
if (get_post_type() == 'artisans' && $name) {
|
||
$my_post['post_title'] = "";
|
||
$my_post['post_title'] = $name;
|
||
}
|
||
|
||
if ($address && isset($address['country'])) {
|
||
$stateGenericName = getGenericStateNameFromAcfStateName($address['state']) ?? "";
|
||
// Met à jour le champ personnalisé 'country' avec la valeur du pays de l'adresse
|
||
update_post_meta($post_id, 'state', $stateGenericName);
|
||
}
|
||
|
||
wp_update_post($my_post);
|
||
}
|
||
add_action('acf/save_post', 'metiers_patrimoine_artisans_post_updater', 20);
|
||
|
||
|
||
/* ---------------------------------------
|
||
CUSTOM CONTENT AFTER POST TYPE TITLE
|
||
------------------------------------------*/
|
||
add_action('edit_form_after_title', function () {
|
||
$screen = get_current_screen();
|
||
|
||
$status_mdp = get_field('mdp_status', get_the_ID());
|
||
$post_status = get_post_status();
|
||
$translatedStatus = translate_wordpress_online_statuses($post_status);
|
||
|
||
if ($screen && $screen->post_type === 'artisans') {
|
||
?>
|
||
<h1 class="admin-artisan-title"><?php echo get_the_title() ?></h1>
|
||
<div id="top-status" class="top-status">
|
||
<div class="state-container">
|
||
<p>État :</p>
|
||
<span class="post-state post-state--<?php echo $post_status; ?>"><?php echo $translatedStatus; ?></span>
|
||
</div>
|
||
|
||
<div class="state-container">
|
||
<p>Statut :</p>
|
||
<?php if (isset($status_mdp)): ?>
|
||
<span class="status-state status-state--<?php echo $status_mdp['value']; ?> "><?php echo $status_mdp['label']; ?></span>
|
||
<?php else : ?>
|
||
<span class="status-state status-state--none">Aucun !!</span>
|
||
|
||
<?php endif; ?>
|
||
</div>
|
||
|
||
<button id="set-offline-status" class="button button-secondary">Mettre Hors Ligne</button>
|
||
<button id="set-online-status" class="button button-secondary">Mettre En Ligne</button>
|
||
|
||
</div>
|
||
<script>
|
||
jQuery(function($) {
|
||
$('#set-offline-status').on('click', function(e) {
|
||
e.preventDefault();
|
||
|
||
// Mettre à jour le champ caché du statut du post
|
||
$('#post_status').val('offline');
|
||
$('#post-status-display').text('Hors ligne');
|
||
|
||
// Sauvegarder immédiatement sans recharger
|
||
$('#publish').trigger('click');
|
||
});
|
||
});
|
||
</script>
|
||
<?php
|
||
}
|
||
});
|
||
|
||
|
||
/* ---------------------------------------
|
||
METABOXES
|
||
------------------------------------------*/
|
||
/** ENQUEUE CUSTOM CHANTIER META BOX AT THE END OF THE PAGE **/
|
||
add_action('add_meta_boxes', function () {
|
||
add_meta_box(
|
||
'admin_artisan_chantier_list',
|
||
'Chantiers réalisé par l\'entreprise',
|
||
'render_custom_chantier_box_content',
|
||
'artisans',
|
||
'normal',
|
||
'default'
|
||
);
|
||
});
|
||
|
||
/** RENDER CHANTIER METABOX **/
|
||
function render_custom_chantier_box_content($post)
|
||
{
|
||
|
||
$args = array(
|
||
'post_type' => 'chantiers',
|
||
'posts_per_page' => -1,
|
||
'post_status' => 'publish',
|
||
'meta_query' => array(
|
||
array(
|
||
'key' => 'artisan',
|
||
'value' => $post->ID,
|
||
'compare' => '=',
|
||
),
|
||
),
|
||
);
|
||
|
||
$chantiers = new WP_Query($args);
|
||
|
||
|
||
// Afficher les chantiers
|
||
|
||
if ($chantiers->have_posts()) {
|
||
|
||
echo '<h3> Chantiers réalisé par l\'entreprise </h3>';
|
||
echo '<ul>';
|
||
while ($chantiers->have_posts()) {
|
||
$chantiers->the_post();
|
||
$chantier_name = get_field('chantier_name');
|
||
echo '<li><a href="' . get_edit_post_link() . '"> <img src=' . get_stylesheet_directory_uri() . '/resources/img/icons/document-inspect.svg' . ' /> ' . $chantier_name . '</a></li>';
|
||
}
|
||
echo '</ul>';
|
||
} else {
|
||
echo '<h3> Chantiers réalisés</h3>';
|
||
echo '<p class="no-results">Aucun chantier pour cette entreprise</p>';
|
||
echo '<a href="' . admin_url('post-new.php?post_type=chantiers') . '" class="new-chantier">Ajouter un chantier</a>';
|
||
}
|
||
}
|
||
|
||
/** REMOVE SUBMITDIV METABOX **/
|
||
add_action('admin_menu', 'remove_submit_div');
|
||
function remove_submit_div()
|
||
{
|
||
remove_meta_box('submitdiv', 'artisans', 'side');
|
||
remove_meta_box('postimagediv', 'artisans', 'normal');
|
||
remove_meta_box('postimagediv', 'artisans', 'side');
|
||
remove_meta_box('postimagediv', 'artisans', 'advanced');
|
||
|
||
$user = wp_get_current_user();
|
||
$metaboxes = get_user_meta($user->ID, 'metaboxhidden_artisans', true);
|
||
$metabox_order = get_user_meta($user->ID, 'meta-box-order_artisans', true);
|
||
}
|
||
|
||
/** UPDATE USER METABOX PREFERENCES WITH MANUAL ARRAY **/
|
||
function set_user_metabox_order($user_id)
|
||
{
|
||
$new_order = [
|
||
'acf_after_title' => '',
|
||
'side' => 'postimagediv,icl_div',
|
||
'normal' => 'acf-group_670d265c6ec0e,admin_artisan_chantier_list,acf-group_67581cdaf3262,acf-group_672358433051b,submitdiv,slugdiv,revisionsdiv',
|
||
'advanced' => 'rank_math_metabox',
|
||
];
|
||
update_user_meta($user_id, 'meta-box-order_artisans', $new_order);
|
||
|
||
$metaboxes_hidden = get_user_meta($user_id, 'metaboxhidden_artisans', true);
|
||
if ($metaboxes_hidden === false) return;
|
||
|
||
$newArrray = array_merge($metaboxes_hidden, ['postimagediv', 'icldiv']);
|
||
update_user_meta($user_id, 'metaboxhidden_artisans', $newArrray);
|
||
}
|
||
// set_user_metabox_order(get_current_user_id());
|
||
add_action('admin_init', 'set_user_metabox_order');
|
||
|
||
/** RE-ENQUEUE SUBMITDIV METABOX **/
|
||
add_action('do_meta_boxes', 'reinsert_submitdiv_meta_box');
|
||
function reinsert_submitdiv_meta_box()
|
||
{
|
||
add_meta_box(
|
||
'submitdiv',
|
||
__('Sauver'),
|
||
'post_submit_meta_box',
|
||
'artisans',
|
||
'normal',
|
||
'low'
|
||
);
|
||
}
|
||
|
||
|
||
/** DE ACTIVATE SCREEN OPTIONS **/
|
||
function de_activite_screen_options()
|
||
{
|
||
$current_user = wp_get_current_user();
|
||
if ($current_user->roles[0] !== 'administrator') {
|
||
add_filter('screen_options_show_screen', '__return_false');
|
||
}
|
||
}
|
||
add_action('admin_init', 'de_activite_screen_options');
|
||
|
||
|
||
/** FORBID SAVING USER REORGANISATION ON DRAG **/
|
||
add_action('check_ajax_referer', 'prevent_meta_box_order');
|
||
function prevent_meta_box_order($action)
|
||
{
|
||
if ('meta-box-order' == $action /* && $wp_user == 'santa claus' */) {
|
||
die('-1');
|
||
}
|
||
}
|
||
|
||
/* ---------------------------------------
|
||
DELETE USER PREFERENCES
|
||
------------------------------------------*/
|
||
function reset_user_metabox_order()
|
||
{
|
||
$user_id = get_current_user_id();
|
||
if ($user_id && !current_user_can('administrator')) {
|
||
delete_user_meta($user_id, 'meta-box-order_artisans');
|
||
}
|
||
}
|
||
add_action('load-post.php', 'reset_user_metabox_order');
|
||
add_action('load-post-new.php', 'reset_user_metabox_order');
|
||
|
||
|
||
/* -------------------------------------------------
|
||
ADD LAST MODIFIED DATE TO SUBMIT DIV PUBLISH BOX
|
||
-------------------------------------------------*/
|
||
|
||
function add_last_modified_date_to_publish_box()
|
||
{
|
||
global $post;
|
||
|
||
// Vérifiez que vous êtes dans un post et que l'ID du post est valide
|
||
if ($post && $post->ID) {
|
||
// Récupérer la date de dernière modification
|
||
$dashicon = '<span class="dashicons dashicons-update"></span>';
|
||
$last_modified_date = get_the_modified_date('j F Y \à H:i', $post->ID);
|
||
|
||
// Afficher la date de dernière modification dans le panneau
|
||
echo '<div class="misc-pub-section">';
|
||
echo $dashicon . ' Dernière update : ';
|
||
echo $last_modified_date;
|
||
echo '</div>';
|
||
}
|
||
}
|
||
// add_action('post_submitbox_start', 'add_last_modified_date_to_publish_box');
|
||
|
||
|
||
// post_submit_meta_box($post, array('side' => 'core'));
|
||
|
||
|
||
/* ----------------------------------------------------------------------
|
||
SUPPRIMER LA COLONNE RANK MATH SEO SI ELLE EST PRÉSENTE
|
||
------------------------------------------------------------------------*/
|
||
|
||
function remove_rank_math_seo_column_for_artisans($columns)
|
||
{
|
||
if (isset($columns['rank_math_seo_details'])) {
|
||
unset($columns['rank_math_seo_details']);
|
||
}
|
||
return $columns;
|
||
}
|
||
|
||
// Appliquer le filtre uniquement pour le post type "artisans"
|
||
add_filter('manage_artisans_posts_columns', 'remove_rank_math_seo_column_for_artisans', 20);
|
||
|
||
|
||
|
||
/* ----------------------------------------------------------------------
|
||
DÉCLARATION DES COLONNES CUSTOM DANS LA LISTE DES POSTS ARTISANS
|
||
------------------------------------------------------------------------*/
|
||
|
||
// **** AJOUT DES COLONNES
|
||
function metiers_patrimoine_artisans_add_acf_posts_columns($columns)
|
||
{
|
||
global $current_screen;
|
||
|
||
// SUPPRIMER LA COLONNE 'date'
|
||
if (isset($columns['date'])) {
|
||
unset($columns['date']);
|
||
}
|
||
$customColumns = array(
|
||
'metiers' => 'Métiers',
|
||
'elements' => 'Éléments du bâtiment',
|
||
'conseiller' => 'Conseiller',
|
||
'status' => 'Statut',
|
||
'lastmodified' => 'Dernière modification',
|
||
'onlinedate' => 'Mise en ligne'
|
||
);
|
||
$new_admin_col_arrays = array_slice($columns, 0, 2, true) + $customColumns + array_slice($columns, 2, count($columns) - 2, true);
|
||
return array_merge($new_admin_col_arrays);
|
||
}
|
||
add_filter('manage_artisans_posts_columns', 'metiers_patrimoine_artisans_add_acf_posts_columns');
|
||
|
||
|
||
|
||
/* ----------------------------------------------------------------------
|
||
GESTION DE LA VALEUR DE CHAQUE COLONNE
|
||
------------------------------------------------------------------------*/
|
||
function metiers_patrimoine_artisans_handle_posts_custom_columns($column)
|
||
{
|
||
|
||
$post_id = get_the_ID();
|
||
|
||
if ($column == 'conseiller') {
|
||
$conseiller = get_field('conseiller', $post_id);
|
||
if (!$conseiller) return;
|
||
echo $conseiller['user_firstname'] . ' ' . $conseiller['user_lastname'];
|
||
}
|
||
if ($column == 'status') {
|
||
$status = get_field('mdp_status', $post_id);
|
||
if (!$status || !isset($status['label'])) return;
|
||
echo '<div class="status-state status-state--' . $status['value'] . '">' . $status['label'] . '</div>';
|
||
}
|
||
|
||
|
||
if ($column == 'metiers') {
|
||
$terms = get_the_terms($post_id, 'metiers');
|
||
if ($terms) {
|
||
$parent_terms = array_filter($terms, function ($term) {
|
||
return $term->parent == 0;
|
||
});
|
||
// if (empty($parent_terms)) {
|
||
// echo '<p class="no-results">Aucun métier parent</p>';
|
||
// return;
|
||
// }
|
||
echo '<div class="admin-column-metiers-container">';
|
||
foreach ($terms as $term) {
|
||
echo '<a href="' . get_edit_term_link($term->term_id, 'metiers', 'artisans') . '" class="admin-column-taxonomy-term">';
|
||
echo esc_html($term->name);
|
||
echo '</a>';
|
||
}
|
||
echo '</div >';
|
||
} else {
|
||
echo '<p class="no-results"> × </p>';
|
||
}
|
||
}
|
||
if ($column == 'elements') {
|
||
$terms = get_the_terms($post_id, 'elementsbatiments');
|
||
if ($terms) {
|
||
$parent_terms = array_filter($terms, function ($term) {
|
||
return $term->parent == 0;
|
||
});
|
||
|
||
echo '<div class="admin-column-metiers-container">';
|
||
foreach ($terms as $term) {
|
||
echo '<a href="' . get_edit_term_link($term->term_id, 'metiers', 'artisans') . '" class="admin-column-taxonomy-term">';
|
||
echo esc_html($term->name);
|
||
echo '</a>';
|
||
}
|
||
echo '</div >';
|
||
} else {
|
||
echo '<p class="no-results"> × </p>';
|
||
}
|
||
}
|
||
|
||
|
||
if ($column == 'lastmodified') {
|
||
// $last_modified_date = get_the_modified_date('j F Y \à H:i', $post_id);
|
||
$last_modified_date = get_the_modified_date('d/m/Y', $post_id);
|
||
if (!$last_modified_date) return;
|
||
echo $last_modified_date;
|
||
}
|
||
if ($column == 'onlinedate') {
|
||
$published_date = get_the_date('d/m/Y', $post_id);
|
||
if (!$published_date) return;
|
||
echo $published_date;
|
||
}
|
||
}
|
||
add_action('manage_artisans_posts_custom_column', 'metiers_patrimoine_artisans_handle_posts_custom_columns', 10, 2);
|
||
|
||
|
||
/* ----------------------------------------------------------------------
|
||
GESTION DU FILTRAGES PAR METIERS
|
||
------------------------------------------------------------------------*/
|
||
// **** CREATION DU DROPDOWN SELECT AVEC LES OPTIONS
|
||
function metiers_patrimoine_filter_posts_per_metiers_declare_dropdown()
|
||
{
|
||
global $pagenow;
|
||
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post';
|
||
|
||
//only add filter to post type you want
|
||
if ($post_type == 'artisans') {
|
||
$metiers_by_parent = getAllMetiersTermsByParents();
|
||
|
||
?>
|
||
<select name="metiers">
|
||
<option value=""><?php _e('Filtrer par Métier', 'metiers-patrimoine-theme'); ?></option>
|
||
<?php
|
||
$current_v = isset($_GET['metiers']) ? $_GET['metiers'] : '';
|
||
foreach ($metiers_by_parent as $index => $parent_term_data) {
|
||
$parent_term = $parent_term_data['term'];
|
||
|
||
printf(
|
||
'<option value="%s"%s>%s</option>',
|
||
$parent_term->slug,
|
||
$parent_term->slug == $current_v ? ' selected="selected"' : '',
|
||
$parent_term->name
|
||
);
|
||
}
|
||
?>
|
||
</select>
|
||
<?php
|
||
}
|
||
}
|
||
add_action('restrict_manage_posts', 'metiers_patrimoine_filter_posts_per_metiers_declare_dropdown');
|
||
|
||
// **** FILTRAGE DES POSTS QUAND LA QUERY DE FILTER EST ENVOYEE
|
||
function metiers_patrimoine_filter_post_by_metiers_query($query)
|
||
{
|
||
global $pagenow;
|
||
$type = 'artisans'; // change to custom post name.
|
||
if (isset($_GET['post_type'])) {
|
||
$type = $_GET['post_type'];
|
||
}
|
||
if (($type === 'artisans') && is_admin() && $pagenow == 'edit.php' && isset($_GET['metiers']) && $_GET['metiers'] != '') {
|
||
$query->query_vars['tax_query'] = array(
|
||
array(
|
||
'taxonomy' => 'metiers',
|
||
'field' => 'slug',
|
||
'terms' => $_GET['metiers'],
|
||
'include_children' => true,
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
add_filter('parse_query', 'metiers_patrimoine_filter_post_by_metiers_query');
|