<?php
/* ---------------------------
	REMOVING POSTS FROM ADMIN MENU
 ---------------------------*/
function post_remove()
{
	remove_menu_page('edit.php');
}

add_action('admin_menu', 'post_remove');


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

// function register_theme_settings_option_page()
// {

// 	// Check function exists.
// 	if (function_exists('acf_add_options_page')) {

// 		// Register options page.
// 		$lhoist_option_page = acf_add_options_page(array(
// 			'page_title'    => __('Texte interface', 'homegrade-theme__texte-backoffice'),
// 			'menu_title'    => __('Texte interface', 'homegrade-theme__texte-backoffice'),
// 			'menu_slug'     => 'theme-general-settings',
// 			'capability'    => 'activate_plugins',
// 			'redirect'      => false,
// 			'icon_url' => 'dashicons-admin-site-alt',
// 			'position' => 2,
// 		));
// 		// Add sub page.
// 		$press_kit_admin_page = acf_add_options_sub_page(array(
// 			'page_title'  => __('Presse'),
// 			'menu_title'  => __('Presse'),
// 			'parent_slug' => $lhoist_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 STYLE
function my_custom_admin_styles()
{
	wp_enqueue_style('admin_styles', get_template_directory_uri() . '/css/admin-style.css');
}

add_action('admin_head', 'my_custom_admin_styles');


function enqueue_admin_script($hook)
{
	wp_enqueue_script('stats-dashboard-js', get_template_directory_uri() . '/js/stats-dashboard.js', array(), '1.0');
}
add_action('admin_enqueue_scripts', 'enqueue_admin_script');



// ##### 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');


// ##### Removes 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');

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


/* ----------------------------------------------------------------------
	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(
		'session-datas-options' => 2,
		'edit.php?post_type=search-and-find' => 3,
		'edit.php' => 6,
		'edit.php?post_type=page' => 9,
		'upload.php' => 11,
	);
	// 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;
};
