<?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 homegrade_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', 'homegrade_remove_rank_math_seo_column_for_artisans', 20);
