FEATURE Refactoring and optimizing admin columns for thematiques

This commit is contained in:
Nonimart 2025-10-28 10:39:26 +01:00
parent ac60bde9a9
commit 6f277c7c83
3 changed files with 117 additions and 101 deletions

View File

@ -16,3 +16,4 @@ require_once(__DIR__ . '/includes/wysiwyg.php');
require_once(__DIR__ . '/includes/search.php'); require_once(__DIR__ . '/includes/search.php');
require_once(__DIR__ . '/includes/rooting.php'); require_once(__DIR__ . '/includes/rooting.php');
require_once(__DIR__ . '/includes/forms.php'); require_once(__DIR__ . '/includes/forms.php');
require_once(__DIR__ . '/includes/admin-columns.php');

116
includes/admin-columns.php Normal file
View File

@ -0,0 +1,116 @@
<?php
/* ----------------------------------------------------------------------
GESTION DE LA COLONNE THEMATIQUE DANS LA LISTE DES POSTS CONSEILS
------------------------------------------------------------------------*/
// AJOUT D'UNE COLONNE CUSTOM
function add_acf_posts_columns($columns)
{
global $current_screen;
$new_admin_col_arrays = array_slice($columns, 0, 2, true) + array('thematiques_taxo' => 'Thématique') + array_slice($columns, 2, count($columns) - 2, true);
return array_merge($new_admin_col_arrays);
}
add_filter('manage_conseils_posts_columns', 'add_acf_posts_columns');
add_filter('manage_questions_posts_columns', 'add_acf_posts_columns');
add_filter('manage_brochures_posts_columns', 'add_acf_posts_columns');
/* ----------------------------------------------------------------------
CUSTOMISATION DE LA COLONNE POUR AFFICHER : THEMATIQUE PARENT > THEMATIQUE
------------------------------------------------------------------------*/
function handle_posts_custom_columns($column, $post_id)
{
if ($column != 'thematiques_taxo') {
return;
}
$tax = get_the_terms($post_id, ('thematiques'));
$edit_link = esc_url(get_edit_term_link($tax[0]->term_id, 'thematiques', 'conseils'));
echo '<a href="' . $edit_link . '">';
if (isset($tax[0]->parent) && $tax[0]->parent != 0) {
$tax_parent = get_term($tax[0]->parent, 'thematiques');
echo $tax_parent->name . "";
}
echo $tax[0]->name;
echo '</a>';
}
add_action('manage_conseils_posts_custom_column', 'handle_posts_custom_columns', 10, 2);
add_action('manage_questions_posts_custom_column', 'handle_posts_custom_columns', 10, 2);
add_action('manage_brochures_posts_custom_column', 'handle_posts_custom_columns', 10, 2);
/* ----------------------------------------------------------------------
GESTION DU FILTRAGE
------------------------------------------------------------------------*/
// **** CREATION DES DROPDOWN SELECT DE FILTRAGE
function homegrade_brochures_filter_posts_declare_dropdowns()
{
global $typenow;
global $wp_meta_boxes;
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post';
if ($post_type !== 'brochures' && $post_type !== 'fiches-infos' && $post_type !== 'videos-webinaires' && $post_type !== 'conseils' && $post_type !== 'questions') return;
$thematiques_by_parent = getAllThematiquesTermsByParents();
?>
<select name="thematiques">
<option value="" class="please-select"><?php _e('Thématique', 'homegrade-theme'); ?></option>
<?php
$is_current = isset($_GET['thematiques']) ? $_GET['thematiques'] : '';
foreach ($thematiques_by_parent as $index => $parent_term_data) {
$parent_term = $parent_term_data['term'];
// Afficher le parent
printf(
'<option value="%s"%s>%s</option>',
$parent_term->slug,
$parent_term->slug == $is_current ? ' selected="selected"' : '',
$parent_term->name
);
// Afficher les enfants avec indentation
if (!empty($parent_term_data['children'])) {
foreach ($parent_term_data['children'] as $child_term) {
printf(
'<option value="%s"%s>— %s</option>',
$child_term->slug,
$child_term->slug == $is_current ? ' selected="selected"' : '',
$child_term->name
);
}
}
}
?>
</select>
<?php
}
add_action('restrict_manage_posts', 'homegrade_brochures_filter_posts_declare_dropdowns');
/* ----------------------------------------------------------------------
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_brochures_posts_columns', 'remove_rank_math_seo_column_for_artisans', 20);

View File

@ -62,7 +62,6 @@ function my_custom_login_url($url)
/* ---------------------------------- /* ----------------------------------
ENQUEUE DU COLOR SCHEME ADMIN ENQUEUE DU COLOR SCHEME ADMIN
-----------------------------------*/ -----------------------------------*/
@ -163,106 +162,6 @@ function change_login_logo_url($url)
add_filter('login_headerurl', 'change_login_logo_url'); add_filter('login_headerurl', 'change_login_logo_url');
/* ----------------------------------------------------------------------
GESTION DE LA COLONNE THEMATIQUE DANS LA LISTE DES POSTS CONSEILS
------------------------------------------------------------------------*/
// AJOUT D'UNE COLONNE CUSTOM
function add_acf_posts_columns($columns)
{
global $current_screen;
$new_admin_col_arrays = array_slice($columns, 0, 2, true) + array('thematiques_taxo' => 'Thématique') + array_slice($columns, 2, count($columns) - 2, true);
return array_merge($new_admin_col_arrays);
}
add_filter('manage_conseils_posts_columns', 'add_acf_posts_columns');
add_filter('manage_questions_posts_columns', 'add_acf_posts_columns');
add_filter('manage_brochures_posts_columns', 'add_acf_posts_columns');
/* ----------------------------------------------------------------------
CUSTOMISATION DE LA COLONNE POUR AFFICHER : THEMATIQUE PARENT > THEMATIQUE
------------------------------------------------------------------------*/
function handle_posts_custom_columns($column, $post_id)
{
if ($column != 'thematiques_taxo') {
return;
}
$tax = get_the_terms($post_id, ('thematiques'));
$edit_link = esc_url(get_edit_term_link($tax[0]->term_id, 'thematiques', 'conseils'));
echo '<a href="' . $edit_link . '">';
if (isset($tax[0]->parent) && $tax[0]->parent != 0) {
$tax_parent = get_term($tax[0]->parent, 'thematiques');
echo $tax_parent->name . "";
}
echo $tax[0]->name;
echo '</a>';
}
add_action('manage_conseils_posts_custom_column', 'handle_posts_custom_columns', 10, 2);
add_action('manage_questions_posts_custom_column', 'handle_posts_custom_columns', 10, 2);
add_action('manage_brochures_posts_custom_column', 'handle_posts_custom_columns', 10, 2);
/* -------------------------------------------
CREATION DU DROPDOWN SELECT AVEC LES OPTIONS
---------------------------------------------*/
function filter_posts_per_thematique_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 == 'questions') {
$all_thematiques = get_terms([
'taxonomy' => 'thematiques',
'hide_empty' => true,
]);
?>
<select name="thematiques">
<option value=""><?php _e('Filtrer par Thématique', 'homegrade-theme__texte-backoffice'); ?></option>
<?php
$current_v = isset($_GET['thematiques']) ? $_GET['thematiques'] : '';
foreach ($all_thematiques as $index => $thematique) {
printf(
'<option value="%s"%s>%s</option>',
$thematique->slug,
$thematique->slug == $current_v ? ' selected="selected"' : '',
$thematique->name
);
}
?>
</select>
<?php
}
}
add_action('restrict_manage_posts', 'filter_posts_per_thematique_declare_dropdown');
/* ------------------------------------------------------
FILTRAGE DES POSTS QUAND LA QUERY DE FILTER EST ENVOYEE
--------------------------------------------------------*/
function filter_post_by_thematiques_query($query)
{
global $pagenow;
$type = 'questions'; // change to custom post name.
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if (($type === 'questions') && is_admin() && $pagenow == 'edit.php' && isset($_GET['thematiques']) && $_GET['thematiques'] != '') {
$query->query_vars['meta_key'] = 'thematiques_taxo'; // change to meta key created by acf.
$query->query_vars['meta_value'] = $_GET['thematiques'];
}
}
// add_filter('parse_query', 'filter_post_by_thematiques_query');
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------