<?php

// ONLY WORDPRESS DEFAULT PAGES
add_filter('manage_page_posts_columns', 'custom_admin_columns_head', 10);
add_action('manage_page_posts_custom_column', 'custom_admin_columns_content', 10, 2);

// ADD NEW COLUMN
function custom_admin_columns_head($defaults)
{
    $defaults['page_template_file'] = 'Page Template File';
    return $defaults;
}

// SHOW THE PAGE TEMPLATE FILE NAME
function custom_admin_columns_content($column_name, $post_ID)
{
    if ($column_name == 'page_template_file') {
        $page_template_file = get_post_meta($post_ID, '_wp_page_template', true);
        echo ($page_template_file ? $page_template_file : '-');
    }
}

function lp_change_post_object_label()
{
    global $wp_post_types;
    $labels = &$wp_post_types['post']->labels;
    $labels->name = 'Magazine';
    $labels->singular_name = 'Magazine';
    $labels->add_new = 'Ajouter';
    $labels->add_new_item = 'Ajouter une nouvelle article';
    $labels->edit_item = 'Editer l\'article';
    $labels->new_item = 'Nouvel article';
    $labels->view_item = 'Voir l\'article';
    $labels->search_items = 'Rechercher un article';
    $labels->not_found = 'Aucun article trouvée';
    $labels->not_found_in_trash = 'Aucun article trouvé dans la corbeille';
    $labels->name_admin_bar = 'Magazine';
}

add_action('admin_menu', 'lp_change_post_object_label');


// REMOVE ELEMENT FROM ADMIN MENU
function lp_admin_page_removing()
{
    remove_menu_page('index.php');                  //Dashboard
    //  remove_menu_page( 'edit.php?post_type=page' );    //Pages
    //  remove_menu_page( 'edit.php?post_type=page' );    //Pages
    // remove_menu_page('edit.php');                   //Posts
    remove_menu_page('edit-comments.php');          //Comments
    remove_menu_page('options-discussion.php');          //setup Comments
}

add_action('admin_menu', 'lp_admin_page_removing');




// remove fields from admin bar
function semiobase_remove_admin_bar_links()
{
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo');          // Remove the WordPress Logo and the submenu
    $wp_admin_bar->remove_menu('about');            // Remove the about WordPress link
    $wp_admin_bar->remove_menu('wporg');            // Remove the WordPress.org link
    $wp_admin_bar->remove_menu('documentation');    // Remove the WordPress documentation link
    $wp_admin_bar->remove_menu('support-forums');   // Remove the support forums link
    $wp_admin_bar->remove_menu('feedback');         // Remove the feedback link
    $wp_admin_bar->remove_menu('comments');         // Remove the comments link
    // $wp_admin_bar->remove_menu('w3tc');             // If you use w3 total cache remove the performance link
    $wp_admin_bar->remove_menu('new-media');        // Remove the content link

    // Replace the "Howdy" by "Hello"
    $my_account = $wp_admin_bar->get_node('my-account');
    $newtitle = str_replace('Howdy,', 'Hello,', $my_account->title);
    $wp_admin_bar->add_node(array(
        'id' => 'my-account',
        'title' => $newtitle,
    ));
}
add_action('wp_before_admin_bar_render', 'semiobase_remove_admin_bar_links');

// disable category and tags for posts
function semiobase_unregister_taxonomy_from_object_type($taxonomy, $object_type)
{

    global $wp_taxonomies;

    if (!isset($wp_taxonomies[$taxonomy]))
        return false;

    if (!get_post_type_object($object_type))
        return false;

    foreach (array_keys($wp_taxonomies[$taxonomy]->object_type) as $array_key) {
        if ($wp_taxonomies[$taxonomy]->object_type[$array_key] == $object_type) {
            unset($wp_taxonomies[$taxonomy]->object_type[$array_key]);
            return true;
        }
    }
    return false;
}


remove_action('wp_head', 'wp_generator');



# Prevent user Enumeration Query Parameter

function redirect_to_home_if_author_parameter()
{

    $is_author_set = get_query_var('author', '');
    if ($is_author_set != '' && !is_admin()) {
        wp_redirect(home_url(), 301);
        exit;
    }
}
add_action('template_redirect', 'redirect_to_home_if_author_parameter');

# Prevent user Enumeration JSON REST Endpoints

function disable_rest_endpoints($endpoints)
{
    if (isset($endpoints['/wp/v2/users'])) {
        unset($endpoints['/wp/v2/users']);
    }
    if (isset($endpoints['/wp/v2/users/(?P<id>[\d]+)'])) {
        unset($endpoints['/wp/v2/users/(?P<id>[\d]+)']);
    }
    return $endpoints;
}
add_filter('rest_endpoints', 'disable_rest_endpoints');
