From ebf1b0d41c5fa005a4add71b09b0d8dd3fdbce76 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Tue, 25 Feb 2025 17:40:04 +0100 Subject: [PATCH] moving artisans filters into a dedicated file --- includes/artisans-columns-filters.php | 392 ++++++++++++++++++++++++++ includes/artisans.php | 176 ------------ 2 files changed, 392 insertions(+), 176 deletions(-) create mode 100644 includes/artisans-columns-filters.php diff --git a/includes/artisans-columns-filters.php b/includes/artisans-columns-filters.php new file mode 100644 index 0000000..c8a64ab --- /dev/null +++ b/includes/artisans-columns-filters.php @@ -0,0 +1,392 @@ + 'artisans', +// 'numberposts' => -1 +// )); +// foreach ($posts as $post) { +// save_acf_post_mdp_status($post->ID); +// } +// } +// add_action('init', 'apply_acf_post_mdp_status_for_all_posts'); + + +/* ---------------------------------------------------------------------- + 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', + 'mdpstatus' => '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 (!isset($conseiller)) return; + + $conseillerDatas = get_userdata($conseiller); + if (!isset($conseillerDatas->display_name)) return; + + echo $conseillerDatas->display_name; + } + if ($column == 'mdpstatus') { + // $status = get_field('mdp_status', $post_id); + // if (!$status || !isset($status['label'])) return; + $status_meta = get_post_meta($post_id, 'mdp_status_meta', true); + if (!$status_meta || !isset($status_meta['label'])) return; + echo '
' . $status_meta['label'] . '
'; + } + + + 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 '

Aucun métier parent

'; + // return; + // } + echo '
'; + foreach ($terms as $term) { + echo ''; + echo esc_html($term->name); + echo ''; + } + echo '
'; + } else { + echo '

×

'; + } + } + if ($column == 'elements') { + $terms = get_the_terms($post_id, 'elementsbatiments'); + if ($terms) { + $parent_terms = array_filter($terms, function ($term) { + return $term->parent == 0; + }); + + echo '
'; + foreach ($terms as $term) { + echo ''; + echo esc_html($term->name); + echo ''; + } + echo '
'; + } else { + echo '

×

'; + } + } + + 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 FILTRAGE + ------------------------------------------------------------------------*/ +// **** CREATION DES DROPDOWN SELECT DE FILTRAGE +function metiers_patrimoine_filter_posts_declare_dropdowns() +{ + global $typenow; + global $wp_meta_boxes; + // global $pagenow; + + // write_log($wp_meta_boxes); + + $post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post'; + if ($post_type !== 'artisans') return; + + $metiers_by_parent = getAllMetiersTermsByParents(); + $batiments_by_parent = getAllBatimentsTermsByParents(); + $conseillers = get_users(); + + // $mdp_status_field = get_field_object('mdp_status'); + // $mdp_status_choices = isset($mdp_status_field['choices']) ? $mdp_status_field['choices'] : array(); + $mdp_status_datar = acf_get_field("mdp_status",); + $mdp_status_datas = get_field_object("mdp_status"); + // write_log($typenow); + // write_log($mdp_status_datar); + // write_log($mdp_status_datas); + + $mdp_status_choices = isset($mdp_status_datas['choices']) ? $mdp_status_datas['choices'] : null; + + + + // write_log($mdp_status_choices); + + $meta_key = 'mdp_status'; + $values = get_posts([ + 'post_type' => 'artisans', + 'posts_per_page' => -1, + 'meta_key' => $meta_key, + 'fields' => 'ids', + ]); + // $test = []; + // foreach ($values as $post_id) { + // $value = get_field('mdp_status', $post_id); + // if (!$value) continue; + // $test[] = $value['value']; + // } + // $unique_values = array_unique($test); + // write_log($unique_values); + + $unique_values = array_reduce(array_filter(array_map( + fn($post_id) => get_field('mdp_status', $post_id) ?? null, + $values + )), function ($acc, $item) { + if (isset($item['value'], $item['label'])) { + $acc[$item['value']] = $item['label']; + } + return $acc; + }, []); + // write_log("unique_values"); + // write_log($unique_values); + + + // $values = array_map(fn($post_id) => get_field('mdp_status', $post_id), $values); + // write_log($values); + // $values = array_unique(array: array_map(callback: fn($post_id) => get_field($meta_key, $post_id), $values)); +?> + + + + + + +set('meta_query', [ + [ + 'key' => 'mdp_status', + 'value' => $_GET['mdpstatus'], + 'compare' => '=' + ] + ]); + } + + // FILTRE CONSEILLER + if (!empty($_GET['conseiller'])) { + $query->set('meta_query', [ + [ + 'key' => 'conseiller', + 'value' => $_GET['conseiller'], + 'compare' => '=' + ] + ]); + } +} + +add_action('pre_get_posts', 'filter_posts_by_acf_conseiller'); + + + + + + +// ########################################################## +// ########################################################## +// ########################################################## +// ########################################################## + +function mytheme_filter_posts_declare_dropdowns() +{ + // [.. Vericications de base] + + // Les données sont correctement retournées au chargement initial de la page (lorsqu’aucun filtre n’est appliqué), + // mais elles sont vides lorsque le filtre est activé. + $mdp_status_datas_object = get_field_object("mdp_status"); + $mdp_status_datas = acf_get_field("mdp_status"); + + // [.. Boucle foreach et construction d'un dropdown] +} + +add_action('restrict_manage_posts', 'mytheme_filter_posts_declare_dropdowns'); + +function filter_posts_by_custom_datas($query) +{ + // [.. Vericications de base] + + if (!empty($_GET['mdpstatus'])) { + $query->set('meta_query', [ + [ + 'key' => 'mdp_status', + 'value' => $_GET['mdpstatus'], + 'compare' => '=' + ] + ]); + } +} + +add_action('pre_get_posts', 'filter_posts_by_custom_datas'); + + +// **** FILTRAGE DES POSTS QUAND LA QUERY DE FILTER EST ENVOYEE +function metiers_patrimoine_filter_post_by_metiers_query($query) +{ + write_log("filter_post_by_metiers_query"); +} + +// add_filter('parse_query', 'metiers_patrimoine_filter_post_by_metiers_query'); + + +/* ---------------------------------------------------------------------- + RENDRE LES COLONNES "MISE EN LIGNE" et "DERNIÈRE MODIFICATION" TRIABLE + ------------------------------------------------------------------------*/ + +function metiers_patrimoine_artisans_sortable_columns($columns) +{ + $columns['onlinedate'] = 'onlinedate'; + $columns['lastmodified'] = 'lastmodified'; + + return $columns; +} +add_filter('manage_edit-artisans_sortable_columns', 'metiers_patrimoine_artisans_sortable_columns'); diff --git a/includes/artisans.php b/includes/artisans.php index 1608892..a2a0349 100644 --- a/includes/artisans.php +++ b/includes/artisans.php @@ -245,180 +245,4 @@ function 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 '
' . $status['label'] . '
'; - } - - - 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 '

Aucun métier parent

'; - // return; - // } - echo '
'; - foreach ($terms as $term) { - echo ''; - echo esc_html($term->name); - echo ''; - } - echo '
'; - } else { - echo '

×

'; - } - } - if ($column == 'elements') { - $terms = get_the_terms($post_id, 'elementsbatiments'); - if ($terms) { - $parent_terms = array_filter($terms, function ($term) { - return $term->parent == 0; - }); - - echo '
'; - foreach ($terms as $term) { - echo ''; - echo esc_html($term->name); - echo ''; - } - echo '
'; - } else { - echo '

×

'; - } - } - - - 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(); - - ?> - -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');