Metiers_du_patrimoine_theme/includes/artisans.php

246 lines
7.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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']) ?? "";
// write_log($stateGenericName);
// Met à jour le champ personnalisé 'country' avec la valeur du pays de l'adresse
update_post_meta($post_id, 'state', $stateGenericName);
}
// write_log($address);
wp_update_post($my_post);
}
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 CODE FOR TITLE
---------------------------*/
add_action('edit_form_after_title', function () {
// Vérifie si on est sur le bon post type
$screen = get_current_screen();
if ($screen && $screen->post_type === 'artisans') {
echo '<h1 class="admin-artisan-title">' . get_the_title() . '</h1>';
}
});
/* ---------------------------
CUSTOM CHANTIER META BOX AT THE END OF THE PAGE
---------------------------*/
add_action('add_meta_boxes', function () {
// Ajout de la metabox uniquement pour le post type 'artisans'
add_meta_box(
'admin_artisan_chantier_list', // ID de la metabox
'Chantiers réalisé par l\'entreprise', // Titre de la metabox
'render_custom_chantier_box_content', // Fonction de rendu
'artisans', // Post type
'normal', // Contexte
'low' // Priorité
);
});
/**
* Fonction de rendu du contenu de la 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>';
}
}
/* ----------------------------------------------------------------------
GESTION DE LA COLONNE METIERS DANS LA LISTE DES POSTS ARTISANS
------------------------------------------------------------------------*/
// AJOUT D'UNE COLONNE CUSTOM
function metiers_patrimoine_artisans_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('metiers' => 'Métiers') + 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 == 'metiers') {
$artisan = get_field('artisan', $post_id);
$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) {
// write_log($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>';
}
}
}
add_action('manage_artisans_posts_custom_column', 'metiers_patrimoine_artisans_handle_posts_custom_columns', 10, 2);
/* -------------------------------------------
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');