<?php


/* -----------------------
	HIDE DEFAULT TAXONOMIES
 -------------------------*/
function homegrade_remove_default_taxonomies()
{
  global $pagenow;

  register_taxonomy('post_tag', array());
  register_taxonomy('category', array());

  // $tax = array('post_tag', 'category');
  $tax = array('category');

  if ($pagenow == 'edit-tags.php' && in_array($_GET['taxonomy'], $tax)) {
    wp_die('Invalid taxonomy');
  }
}
add_action('init', 'homegrade_remove_default_taxonomies');




/* ---------------------------------
	OTHER THAN ADMIN CANNOT CREATE TAGS
 -----------------------------------*/
add_action('create_term', 'undo_create_term', 10, 3);

function undo_create_term($term_id, $tt_id, $taxonomy)
{
  if (!current_user_can('administrator')) {
    if ($taxonomy == 'post_tag') {
      wp_delete_term($term_id, $taxonomy);
    }
  }
}


/* -------------------
	REGISTER TAXONOMIES
 --------------------*/

function homegrade_add_homegrade_custom_taxonomies()
{

$current_blog_id = get_current_blog_id();
// write_log("Current blog ID: " . $current_blog_id);
// if ($current_blog_id !== 1) {
//     return; // Skip if not on the main site
//   }

  // ————— Thématiques —————
  register_taxonomy('thematiques', ['questions', 'conseils', 'brochures', 'fiches-infos', 'videos-webinaires'], array(
    // 'hierarchical' => true,
    'labels' => array(
      'name' => __('Thématiques', 'homegrade-theme__texte-backoffice'),
      'singular_name' => __('Thématique', 'homegrade-theme__texte-backoffice'),
      'search_items' =>  __('Chercher une Thématique', 'homegrade-theme__texte-backoffice'),
      'all_items' => __('Toutes les Thématiques', 'homegrade-theme__texte-backoffice'),
      'parent_item' => __('Thématique Parent', 'homegrade-theme__texte-backoffice'),
      'parent_item_colon' => __('Thématique Parent:', 'homegrade-theme__texte-backoffice'),
      'edit_item' => __('Editer la Thématique', 'homegrade-theme__texte-backoffice'),
      'update_item' => __('Mettre à jour la Thématique', 'homegrade-theme__texte-backoffice'),
      'add_new_item' => __('Ajouter une Thématique', 'homegrade-theme__texte-backoffice'),
      'new_item_name' => __('Nom de la nouvelle Thématique', 'homegrade-theme__texte-backoffice'),
      'menu_name' => __('Thématiques', 'homegrade-theme__texte-backoffice'),
    ),
    'public' => true,
    'show_in_rest' => true, // Needed for tax to appear in Gutenberg editor
    'show_ui'           => true,
    'show_admin_column' => false,
    'show_in_quick_edit'         => false,
    'meta_box_cb'                => false,
    'hierarchical' => true, // This will allow URL's like "/locations/boston/cambridge/"
    'rewrite' => array(
      'slug' => 'questions-thematiques',
      // 'with_front' => false,
      'hierarchical' => true,
      'has_archive' => true
    ),

  ));


  // ————— Type de news  —————
  register_taxonomy('news_type', 'post', array(
    'labels' => array(
      'name' => __('Types', 'homegrade-theme__texte-backoffice'),
      'singular_name' => __('Type', 'homegrade-theme__texte-backoffice'),
      'search_items' =>  __('Chercher un Type', 'homegrade-theme__texte-backoffice'),
      'all_items' => __('Tous les Types', 'homegrade-theme__texte-backoffice'),
      'parent_item' => __('Type Parent', 'homegrade-theme__texte-backoffice'),
      'parent_item_colon' => __('Type Parent:', 'homegrade-theme__texte-backoffice'),
      'edit_item' => __('Editer le Type', 'homegrade-theme__texte-backoffice'),
      'update_item' => __('Mettre à jour le Type', 'homegrade-theme__texte-backoffice'),
      'add_new_item' => __('Ajouter un Type', 'homegrade-theme__texte-backoffice'),
      'new_item_name' => __('Nom du nouveau Type', 'homegrade-theme__texte-backoffice'),
      'menu_name' => __('Type', 'homegrade-theme__texte-backoffice'),
    ),
    'hierarchical' => false,
    'show_in_rest' => true, // Needed for tax to appear in Gutenberg editor
    'show_ui'           => true,
    'show_admin_column' => false,
    'show_in_quick_edit'         => false,
    'meta_box_cb'                => false,
    'default_term' => [
      'name' => '	Général',
      'slug' => 'general',
    ],
  ));

  // ————— mots-clés  —————
  register_taxonomy('mots-cles', ['questions', 'conseils'], array(

    // This array of options controls the labels displayed in the WordPress Admin UI
    'labels' => array(
      'name' => __('Mots-Clés', 'homegrade-theme__texte-backoffice'),
      'singular_name' => __('Mots-Clés', 'homegrade-theme__texte-backoffice'),
      'search_items' =>  __('Chercher un Mots-Clé', 'homegrade-theme__texte-backoffice'),
      'all_items' => __('Tous les Mots-Clés', 'homegrade-theme__texte-backoffice'),
      'parent_item' => __('Mots-Clé Parent', 'homegrade-theme__texte-backoffice'),
      'parent_item_colon' => __('Mots-Clé Parent:', 'homegrade-theme__texte-backoffice'),
      'edit_item' => __('Editer le Mots-Clé', 'homegrade-theme__texte-backoffice'),
      'update_item' => __('Mettre à jour le Mots-Clé', 'homegrade-theme__texte-backoffice'),
      'add_new_item' => __('Ajouter un Mots-Clé', 'homegrade-theme__texte-backoffice'),
      'new_item_name' => __('Nom du nouveau Mots-Clés', 'homegrade-theme__texte-backoffice'),
      'menu_name' => __('Mots-Clés', 'homegrade-theme__texte-backoffice'),
    ),


    'hierarchical' => false,
    'public' => false,

  ));
}
add_action('init', 'homegrade_add_homegrade_custom_taxonomies', 0);

/* -----------------------
	HIDE THEMATIQUE METABOX
 -------------------------*/
// on passe le metabox_cb à false pour ne pas afficher la metabox
function tna_edit_taxonomy_args($args, $tax_slug, $cptui_tax_args)
{
  // Alternatively, you can check for specific taxonomies.
  if ('thematiques' === $tax_slug) {
    $args['meta_box_cb'] = false;
  }
  return $args;
}
add_filter('cptui_pre_register_taxonomy', 'tna_edit_taxonomy_args', 10, 3);

// on récupère les élément avec un metabox_cb false et on les masque dans la sidebar
add_filter('rest_prepare_taxonomy', function ($response, $taxonomy, $request) {
  $context = !empty($request['context']) ? $request['context'] : 'view';
  // Context is edit in the editor
  if ($context === 'edit' && $taxonomy->meta_box_cb === false) {
    $data_response = $response->get_data();
    $data_response['visibility']['show_ui'] = false;
    $response->set_data($data_response);
  }
  return $response;
}, 10, 3);
