<?php


/* ---------------------------
	PAGE D'OPTIONS DU THEME
 ---------------------------*/

function register_theme_settings_option_page()
{
	$current_blog_id = get_current_blog_id();

	if (function_exists('acf_add_options_page') && $current_blog_id === 1) {

		// Register options page.
		$homegrade_option_page = acf_add_options_page(array(
			'page_title'    => __('Paramètres & Outils', 'homegrade-theme__texte-backoffice'),
			'menu_title'    => __('Paramètres', 'homegrade-theme__texte-backoffice'),
			'menu_slug'     => 'theme-general-settings',
			'capability'    => 'activate_plugins',
			'redirect'      => false,
			'position' => 20,
		));
		// Add sub page.
		$press_kit_admin_page = acf_add_options_sub_page(array(
			'page_title'  => __('Presse'),
			'menu_title'  => __('Presse'),
			'parent_slug' => $homegrade_option_page['menu_slug'],
		));
	}
}
// add_options_page( 'MyPlugin Options', 'MyPlugin Options', 'activate_plugins', 'myplugin-options', 'myplugin_options' );


add_action('acf/init', 'register_theme_settings_option_page');


/* ---------------------------
	ADMIN STYLES
 ---------------------------*/

// Amin Stylesheet
function my_custom_admin()
{
	wp_enqueue_style('admin_styles', get_template_directory_uri() . '/css/admin-style.css');
}

add_action('admin_head', 'my_custom_admin');

// ENQUEUE DU CSS BLADE LOGIN ADMIN
function enqueue_custom_login_stylesheet()
{
	wp_enqueue_style('custom-login', get_stylesheet_directory_uri() . '/style-login.css');
}
add_action('login_enqueue_scripts', 'enqueue_custom_login_stylesheet');

add_filter('login_headerurl', 'my_custom_login_url');
function my_custom_login_url($url)
{
	return site_url();
}




/* ----------------------------------
	ENQUEUE DU COLOR SCHEME ADMIN
 -----------------------------------*/

function homegrade_admin_color_scheme()
{
	//Get the theme directory
	$theme_dir = get_stylesheet_directory_uri();

	//Homegrade
	wp_admin_css_color(
		'homegrade_light',
		'Homegrade Light',
		$theme_dir . '/homegrade-light-color-scheme.css',
		array('#cf4944', '#fff', '#d54e21', '#2f0154')
	);
	wp_admin_css_color(
		'homegrade_dark',
		'Homegrade Dark',
		$theme_dir . '/homegrade-dark-color-scheme.css',
		array('#2f2a33', '#fff', '#e14d43', '#e14d43')
	);
}
add_action('admin_init', 'homegrade_admin_color_scheme');


/* ----------------------------------
	 REMOVE FROM  ADMIN MENU
 -----------------------------------*/
function custom_menu_page_removing()
{
	// remove_menu_page('edit.php'); // Hide Articles
	remove_menu_page('edit-comments.php'); // Hide Commentaires
}
add_action('admin_menu', 'custom_menu_page_removing');

/* ----------------------------------
	 HIDE EDITOR ON CERTAIN PAGES
 -----------------------------------*/

function hide_editor()
{
	if (!is_admin()) return null;
	$post_id = isset($_GET['post']) ? $_GET['post'] : (isset($_POST['post_ID']) ? $_POST['post_ID'] : null);

	if (!isset($post_id)) return;

	$template_file = get_post_meta($post_id, '_wp_page_template', true);

	if ($template_file == 'template-publications.php') { // edit the template name
		remove_post_type_support('page', 'editor', 'comments');
	}
}
add_action('admin_init', 'hide_editor');

function remove_meta_boxes()
{
	# Removes meta from Posts #
	// remove_meta_box('postexcerpt', 'post', 'normal');
	// remove_meta_box('postcustom', 'post', 'normal');
	// remove_meta_box('trackbacksdiv', 'post', 'normal');
	remove_meta_box('commentstatusdiv', 'page', 'normal');
	remove_meta_box('commentsdiv', 'page', 'normal');
}
add_action('admin_init', 'remove_meta_boxes');

/* ----------------------------------
	 HIDE THUMBNAIL SUPPORT ON PAGES
 -----------------------------------*/

add_action('init', 'remove_thumbnail_support');
function remove_thumbnail_support()
{
	remove_post_type_support('page', 'thumbnail');
}


/* ----------------------------------
	REMOVE FROM  ADMIN BAR
 -----------------------------------*/

function mytheme_admin_bar_render()
{
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('comments');
}
add_action('wp_before_admin_bar_render', 'mytheme_admin_bar_render');



/* ----------------------------------------------------------------------
	MODIFIER LE LIEN DU LOGO SUR LA BLADE DE CONNEXION À L'INTERFACE ADMIN
 ------------------------------------------------------------------------*/
function change_login_logo_url($url)
{
	return site_url();
}
add_filter('login_headerurl', 'change_login_logo_url');




/* ----------------------------------------------------------------------
	ADDING CLASS TO ADMIN BODY
 ------------------------------------------------------------------------*/
function wpdocs_admin_classes($classes)
{
	// if (is_admin()) {
	// 	return $classes;
	// }
	global $pagenow;
	global $post;

	$thematique = get_the_terms(get_the_ID(), 'thematiques')[0] ?? null;
	if (!$thematique) return $classes;

	$thematiqueParent = getMainThematique($thematique);
	$thematiqueColorSlug = getThematiqueFamilySlug($thematiqueParent->slug);
	$postType = get_post_type($post->ID) ?? null;

	if (in_array($pagenow, array('post.php', 'post-new.php'), true) && $thematiqueColorSlug && ($postType == 'conseils' || $postType == 'questions')) {
		$classes .= ' ' . $thematiqueParent->slug;
	}

	return $classes;
}

add_filter('admin_body_class', 'wpdocs_admin_classes');





/* ----------------------------------------------------------------------
	REORDER ADMIN MENU & POST TYPE ORDER ON MENU BAR
 ------------------------------------------------------------------------*/
/**
 * Activates the 'menu_order' filter and then hooks into 'menu_order'
 */
add_filter('custom_menu_order', function () {
	return true;
});
add_filter('menu_order', 'my_new_admin_menu_order');
/**
 * Filters WordPress' default menu order
 */
function my_new_admin_menu_order($menu_order)
{
	// define your new desired menu positions here
	// for example, move 'upload.php' to position #9 and built-in pages to position #1
	$new_positions = array(
		'edit.php?post_type=conseils' => 1,
		'edit.php?post_type=questions' => 2,
		'edit.php?post_type=brochures' => 3,
		'edit.php?post_type=fiches-infos' => 4,
		'edit.php?post_type=parcours' => 5,
		'edit.php?post_type=vocabulaire' => 6,
		'edit.php' => 7,
		'edit.php?post_type=jobs' => 8,
		'edit.php?post_type=videos-webinaires' => 9,
		'edit.php?post_type=page' => 10,
		'theme-general-settings' => 11,
		'upload.php' => 12,
		'gf_edit_forms' => 13,
	);
	// helper function to move an element inside an array
	function move_element(&$array, $a, $b)
	{
		$out = array_splice($array, $a, 1);
		array_splice($array, $b, 0, $out);
	}
	// traverse through the new positions and move 
	// the items if found in the original menu_positions
	foreach ($new_positions as $value => $new_index) {
		if ($current_index = array_search($value, $menu_order)) {
			move_element($menu_order, $current_index, $new_index);
		}
	}
	return $menu_order;
};
