417 lines
13 KiB
PHP
417 lines
13 KiB
PHP
<?php
|
|
/* ---------------------------------------
|
|
ARTISANS POST TITLE & ADRESS STATE UPDATER
|
|
------------------------------------------*/
|
|
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);
|
|
|
|
// ###### Update post title if name is set ######
|
|
if (get_post_type() == 'artisans' && $name) {
|
|
$my_post['post_title'] = "";
|
|
$my_post['post_title'] = $name;
|
|
}
|
|
|
|
// ###### Update custom "state" post Meta (used in search) ######
|
|
if ($address && isset($address['country'])) {
|
|
$stateGenericName = getGenericStateNameFromAcfStateName($address['state']) ?? "";
|
|
update_post_meta($post_id, 'state', $stateGenericName);
|
|
}
|
|
|
|
wp_update_post($my_post);
|
|
}
|
|
add_action('acf/save_post', 'metiers_patrimoine_artisans_post_updater', 20);
|
|
|
|
|
|
/* ------------------------------------------------------
|
|
AUTO CHECK PARENT TAXONOMIES WHEN CHILDREN ARE CHOSEN
|
|
---------------------------------------------------------*/
|
|
function metiers_patrimoine_artisans_post_taxonomies_updater($post_id)
|
|
{
|
|
|
|
if (!$post_id || get_post_type() !== 'artisans') return;
|
|
|
|
$metiersTerms = get_the_terms($post_id, 'metiers');
|
|
$elementsBatimentsTerms = get_the_terms($post_id, 'elementsbatiments');
|
|
|
|
// ###### METIERS — ADD PARENT TAXONOMIES IF NOT ALREADY ADDED ######
|
|
if ($metiersTerms) {
|
|
$existingMetiersTermIds = array_column($metiersTerms, 'term_id');
|
|
|
|
// Parcours chaque terme et ajoute son parent si absent
|
|
foreach ($metiersTerms as $term) {
|
|
if ($term->parent && !in_array($term->parent, $existingMetiersTermIds)) {
|
|
$parentTerm = get_term($term->parent, 'metiers');
|
|
if ($parentTerm && !is_wp_error($parentTerm)) {
|
|
wp_add_object_terms($post_id, $parentTerm->term_id, 'metiers'); // Update les posts terms directement
|
|
$existingMetiersTermIds[] = $parentTerm->term_id; // Ajoute l'ID du parent à une liste pour éviter de pusher plusieurs fois le parent
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// ###### ELEMENTS BATIMENTS — ADD PARENT TAXONOMIES IF NOT ALREADY ADDED ######
|
|
if ($elementsBatimentsTerms) {
|
|
$existingElementsBatimentsTermIds = array_column($elementsBatimentsTerms, 'term_id');
|
|
|
|
foreach ($elementsBatimentsTerms as $term) {
|
|
if ($term->parent && !in_array($term->parent, $existingElementsBatimentsTermIds)) {
|
|
$parentTerm = get_term($term->parent, 'elementsbatiments');
|
|
if ($parentTerm && !is_wp_error($parentTerm)) {
|
|
wp_add_object_terms($post_id, $parentTerm->term_id, 'elementsbatiments'); // Update les posts terms directement
|
|
$existingElementsBatimentsTermIds[] = $parentTerm->term_id; // Ajoute l'ID du parent à une liste pour éviter de pusher plusieurs fois le parent
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
add_action('acf/save_post', 'metiers_patrimoine_artisans_post_taxonomies_updater', 20);
|
|
|
|
|
|
|
|
|
|
/* -----------------------------------------------
|
|
AUTOMATIZE POST STATUS DEPENDING ON MDP STATUS
|
|
--------------------------------------------------*/
|
|
function update_post_status_based_on_mdp_status($post_id)
|
|
{
|
|
if (get_post_type($post_id) !== 'artisans') return;
|
|
|
|
$mdp_status = get_field('mdp_status', $post_id);
|
|
$post_update = array(
|
|
'ID' => $post_id
|
|
);
|
|
// Définir le statut en fonction de la valeur du statut de travail mdp_status
|
|
if ($mdp_status && isset($mdp_status['value'])) {
|
|
switch ($mdp_status['value']) {
|
|
case 'ok':
|
|
case 'to_actualize':
|
|
$post_update['post_status'] = 'publish';
|
|
break;
|
|
case 'to_contact':
|
|
case 'deleted':
|
|
case 'rejected':
|
|
case 'none':
|
|
$post_update['post_status'] = 'offline';
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
|
|
wp_update_post($post_update);
|
|
}
|
|
}
|
|
add_action('acf/save_post', 'update_post_status_based_on_mdp_status', 20);
|
|
|
|
/* ---------------------------------------
|
|
CUSTOM TOP 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);
|
|
$post_link = get_preview_post_link();
|
|
|
|
if ($screen && $screen->post_type === 'artisans') {
|
|
?>
|
|
<h1 class="admin-artisan-title"><?php echo get_the_title() ?></h1>
|
|
<?php if (!empty($post_link) && $screen->action !== 'add') : ?>
|
|
<div class="preview-artisan-link">
|
|
<a href="<?php echo $post_link; ?>" target="_blank" class="button button-primary">
|
|
<span class="dashicons dashicons-admin-users" style="margin-right: 5px;"></span>
|
|
Voir la fiche
|
|
</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
<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 de travail :</p>
|
|
<?php if (isset($status_mdp) && isset($status_mdp['value']) && isset($status_mdp['label'])): ?>
|
|
<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 id="save-post-custom">
|
|
<button class="">Sauvegarder</button>
|
|
<span class="spinner"></span>
|
|
</div>
|
|
|
|
</div>
|
|
<script>
|
|
jQuery(function($) {
|
|
$('#save-post-custom').on('click', function(e) {
|
|
e.preventDefault();
|
|
const $button = $(this.querySelector('button'));
|
|
const $spinner = $(this.querySelector('.spinner'));
|
|
|
|
// Désactiver le bouton et changer le texte
|
|
$button.prop('disabled', true).text('Sauvegarde en cours...');
|
|
$spinner.show();
|
|
$spinner.addClass('is-active');
|
|
|
|
// Déclencher la sauvegarde
|
|
$('#publish').trigger('click');
|
|
|
|
// Vérifier les erreurs ACF après un court délai
|
|
setTimeout(function() {
|
|
// Vérifier s'il y a des messages d'erreur ACF
|
|
if ($('.acf-notice.-error').length > 0) {
|
|
// Réinitialiser le bouton immédiatement si une erreur est détectée
|
|
$button.prop('disabled', false).text('Sauvegarder');
|
|
$spinner.hide();
|
|
$spinner.removeClass('is-active');
|
|
}
|
|
}, 2000);
|
|
|
|
// Réactiver le bouton et restaurer le texte après un délai plus long
|
|
// (ceci ne s'exécutera que si la page n'est pas rechargée)
|
|
setTimeout(function() {
|
|
$button.prop('disabled', false).text('Sauvegarder');
|
|
$spinner.hide();
|
|
$spinner.removeClass('is-active');
|
|
}, 20000);
|
|
});
|
|
});
|
|
</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és',
|
|
'render_custom_chantier_box_content',
|
|
'artisans',
|
|
'normal',
|
|
'default'
|
|
);
|
|
});
|
|
|
|
/** RENDER CHANTIER METABOX **/
|
|
function render_custom_chantier_box_content($post)
|
|
{
|
|
|
|
|
|
$artisans_id_fr = apply_filters('wpml_object_id', $post->ID, 'artisans', TRUE, 'fr');
|
|
|
|
$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()) {
|
|
|
|
$artisan_id = get_the_ID();
|
|
$return_to = get_edit_post_link($artisan_id);
|
|
|
|
|
|
echo '<h3> Chantiers réalisés </h3>';
|
|
echo '<ul>';
|
|
while ($chantiers->have_posts()) {
|
|
$chantiers->the_post();
|
|
$date = get_field('date');
|
|
$city = get_field('city')['city'] ?? null;
|
|
$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 . ' | ' . $city . ' | ' . $date . '</a></li>';
|
|
}
|
|
echo '</ul>';
|
|
echo '<a href="' . admin_url('post-new.php?post_type=chantiers&return_to=' . urlencode($return_to)) . '" class="new-chantier">Ajouter un chantier</a>';
|
|
} 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)
|
|
{
|
|
$user_id = get_current_user_id();
|
|
// $metaboxes_order = get_user_meta($user_id, 'meta-box-order_artisans', true);
|
|
// $user_info = get_user_meta($user_id);
|
|
$metaboxes_hidden = get_user_meta($user_id, 'metaboxhidden_artisans', true);
|
|
|
|
$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);
|
|
|
|
if ($metaboxes_hidden === false || !is_array($metaboxes_hidden)) return;
|
|
|
|
$newArrray = array_merge($metaboxes_hidden, ['postimagediv', 'icldiv']);
|
|
update_user_meta($user_id, 'metaboxhidden_artisans', $newArrray);
|
|
}
|
|
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 (isset($current_user->roles) && is_array($current_user->roles) && isset($current_user->roles[0]) && $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'));
|
|
|
|
|
|
/* -------------------------------------------------
|
|
CHANGE ACF GALLERY ADD BUTTON TEXT TO "Ajouter des photos 📸"
|
|
-------------------------------------------------*/
|
|
|
|
add_action('admin_enqueue_scripts', function () {
|
|
wp_add_inline_script('acf-input', "
|
|
jQuery(function($) {
|
|
$('.acf-gallery-add').text('🏞️ Ajouter des photos');
|
|
});
|
|
");
|
|
});
|
|
|
|
|
|
|
|
|
|
add_action('save_post', 'update_artisan_nl_status_on_post_save');
|
|
|
|
function update_artisan_nl_status_on_post_save($post_id)
|
|
{
|
|
if (get_post_type($post_id) !== 'artisans') return;
|
|
|
|
// Éviter les sauvegardes en boucle
|
|
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
|
|
if (defined('DOING_AJAX') && DOING_AJAX) return;
|
|
if (wp_is_post_revision($post_id)) return;
|
|
if (get_post_status($post_id) === 'auto-draft') return;
|
|
|
|
|
|
$current_language = apply_filters('wpml_post_language_details', null, $post_id);
|
|
if (isset($current_language['language_code']) && $current_language['language_code'] === 'fr') {
|
|
|
|
// Récupérer l'ID du post correspondant en néerlandais
|
|
$nl_post_id = apply_filters('wpml_object_id', $post_id, 'artisans', false, 'nl');
|
|
if (!isset($nl_post_id)) return null;
|
|
|
|
wp_update_post(array(
|
|
'ID' => $nl_post_id,
|
|
'post_status' => get_post_status($post_id)
|
|
));
|
|
}
|
|
}
|