homegrade_theme_production/includes/admin.php

346 lines
10 KiB
PHP

<?php
/* ---------------------------
PAGE D'OPTIONS DU THEME
---------------------------*/
function register_theme_settings_option_page()
{
// Check function exists.
if (function_exists('acf_add_options_page')) {
// Register options page.
$homegrade_option_page = acf_add_options_page(array(
'page_title' => __('Paramètres & Outils', 'homegrade-theme__texte-backoffice'),
'menu_title' => __('Paramètres & Outils', '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');
/* ----------------------------------------------------------------------
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');
/* ----------------------------------------------------------------------
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;
};