223 lines
7.1 KiB
PHP
223 lines
7.1 KiB
PHP
<?php
|
|
|
|
/* -----------------------------------------
|
|
HANDLE TITLE CHANGES ON ACF SAVE POST
|
|
-----------------------------------------*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
/* -----------------------------------------
|
|
HANDLE POST UPDATED MESSAGES
|
|
-----------------------------------------*/
|
|
|
|
function chantiers_post_updated_messages($messages)
|
|
{
|
|
global $post;
|
|
$post_type = get_post_type($post);
|
|
$artisan = get_field('artisan', $post->ID);
|
|
|
|
if ($post_type === 'chantiers' && $artisan->ID) {
|
|
$artisan_link = get_the_permalink($artisan->ID);
|
|
|
|
$messages[$post_type][1] = 'Chantier mis à jour. <a target="_blank" href="' . $artisan_link . '">Voir sur la fiche de l\'artisan</a>';
|
|
$messages[$post_type][6] = 'Chantier publié. <a target="_blank" href="' . $artisan_link . '">Voir sur la fiche de l\'artisan</a>';
|
|
}
|
|
|
|
return $messages;
|
|
}
|
|
add_filter('post_updated_messages', 'chantiers_post_updated_messages');
|
|
|
|
|
|
|
|
|
|
/* -----------------------------------------
|
|
EDIT ARTISAN LINK ON CHANTIER EDIT PAGE
|
|
-----------------------------------------*/
|
|
|
|
// add_action('acf/render_field', function ($field) {
|
|
// // Vérifiez si le champ est celui que vous voulez personnaliser
|
|
// if ($field['key'] === 'field_6719065f58122') {
|
|
// if (did_action('acf/render_field/key=field_6719065f58122') % 2 === 0) {
|
|
// return;
|
|
// }
|
|
// $field['rendered'] = true;
|
|
|
|
// $linked_post_id = get_field($field['key']);
|
|
|
|
// if ($linked_post_id) {
|
|
// $edit_link = get_edit_post_link($linked_post_id);
|
|
// echo '<a href="' . esc_url($edit_link) . '" class="edit-fiche-artisan-link"><img src=' . get_stylesheet_directory_uri() . '/resources/img/icons/tools.svg' . ' /> Voir la fiche de l\'artisan </a>';
|
|
// }
|
|
// }
|
|
// }, 10, 1);
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
GESTION DE LA COLONNE ARTISAN DANS LA LISTE DES POSTS CHANTIERS
|
|
------------------------------------------------------------------------*/
|
|
|
|
// AJOUT D'UNE COLONNE CUSTOM
|
|
function metiers_patrimoine_add_acf_posts_columns($columns)
|
|
{
|
|
global $current_screen;
|
|
|
|
// SUPPRIMER LA COLONNE 'date'
|
|
if (isset($columns['date'])) {
|
|
unset($columns['date']);
|
|
}
|
|
$new_admin_col_arrays = array_slice($columns, 0, 2, true) + array('artisan' => 'Artisan') + array('date_chantier' => 'Date chantier') + array_slice($columns, 2, count($columns) - 2, true);
|
|
return array_merge($new_admin_col_arrays);
|
|
}
|
|
add_filter('manage_chantiers_posts_columns', 'metiers_patrimoine_add_acf_posts_columns');
|
|
|
|
|
|
/* ----------------------------------------------------------------------
|
|
GESTION DE LA VALEUR DE CHAQUE COLONNE
|
|
------------------------------------------------------------------------*/
|
|
|
|
function metiers_patrimoine_handle_posts_custom_columns($column)
|
|
{
|
|
$post_id = get_the_ID();
|
|
|
|
if ($column == 'artisan') {
|
|
$artisan = get_field('artisan', $post_id);
|
|
if ($artisan) {
|
|
$edit_link = esc_url(get_edit_post_link($artisan->ID));
|
|
echo '<a href="' . $edit_link . '">';
|
|
echo esc_html($artisan->post_title);
|
|
echo '</a>';
|
|
}
|
|
}
|
|
if ($column == 'date_chantier') {
|
|
$date_chantier = get_field('date', $post_id);
|
|
echo '<p>' . $date_chantier . '</p>';
|
|
}
|
|
}
|
|
add_action('manage_chantiers_posts_custom_column', 'metiers_patrimoine_handle_posts_custom_columns', 10, 2);
|
|
|
|
|
|
|
|
|
|
function metiers_patrimoine_sortable_columns($columns)
|
|
{
|
|
$columns['date_chantier'] = 'date_chantier';
|
|
return $columns;
|
|
}
|
|
add_filter('manage_edit-chantiers_sortable_columns', 'metiers_patrimoine_sortable_columns');
|
|
|
|
|
|
function metiers_patrimoine_orderby($query)
|
|
{
|
|
if (!is_admin()) {
|
|
return;
|
|
}
|
|
|
|
$orderby = $query->get('orderby');
|
|
|
|
if ('date_chantier' == $orderby) {
|
|
$query->set('meta_key', 'date');
|
|
$query->set('orderby', 'meta_value');
|
|
}
|
|
}
|
|
add_action('pre_get_posts', 'metiers_patrimoine_orderby');
|
|
|
|
|
|
|
|
/* ---------------------------------------
|
|
CUSTOM CONTENT AFTER POST TYPE TITLE
|
|
------------------------------------------*/
|
|
add_action('edit_form_after_title', function () {
|
|
$screen = get_current_screen();
|
|
|
|
if ($screen && $screen->post_type === 'chantiers') {
|
|
$linked_artisan_id = get_field('artisan');
|
|
$return_to = isset($_GET['return_to']) ? $_GET['return_to'] : '';
|
|
$edit_artisan_link = get_edit_post_link($linked_artisan_id);
|
|
?>
|
|
<div id="post-custom-superior-toolbar">
|
|
<?php if ($return_to) : ?>
|
|
<a href="<?php echo esc_url($return_to) ?>" class="edit-fiche-artisan-link"><img src='<?php echo get_stylesheet_directory_uri() . '/resources/img/icons/tools.svg' ?>' /> Retour vers la fiche de l'artisan </a>
|
|
<?php elseif ($linked_artisan_id) : ?>
|
|
<a href="<?php echo esc_url($edit_artisan_link) ?>" class="edit-fiche-artisan-link"><img src='<?php echo get_stylesheet_directory_uri() . '/resources/img/icons/tools.svg' ?>' /> Retour vers la fiche de l'artisan </a>
|
|
<?php endif; ?>
|
|
<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
|
|
}
|
|
});
|
|
|
|
|
|
|
|
/* ---------------------------------------
|
|
CUSTOM TOP CONTENT AFTER POST TYPE TITLE
|
|
------------------------------------------*/
|