<?php
// Remove admin Dahsboard boxs
add_action( 'admin_init', 'Ds1_remove_dashboard_meta' );
remove_action( 'welcome_panel', 'wp_welcome_panel' ); 
function Ds1_remove_dashboard_meta() {
    remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
    remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
    remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
    remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
    remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
    remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
    remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
    remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
    remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
    remove_meta_box( 'e-dashboard-overview', 'dashboard', 'normal' );
    
    if(! current_user_can('administrator')) {
        remove_meta_box( 'dashboard_site_health', 'dashboard', 'normal' );
    }

    if( ds1_checkSettings("remove_dashboard_meta_items") ) {
        foreach( ds1_checkSettings("remove_dashboard_meta_items") as $item){
            remove_meta_box( sanitize_text_field($item[0]), sanitize_text_field($item[1]), sanitize_text_field($item[2]) );
        }
    }
}



//remove meta boxs from postnew and edit
add_action( 'do_meta_boxes', 'Ds1_my_remove_meta_boxes_b', 998 );
function Ds1_my_remove_meta_boxes_b() {
    remove_meta_box('postcustom', 'post', 'normal');
    remove_meta_box('trackbacksdiv', 'post', 'normal');
    remove_meta_box('expirationdatediv', 'post', 'advanced');

    if( ds1_checkSettings("remove_meta_boxes_items") ) {
        foreach(checkSettings("remove_meta_boxes_items") as $item){
            remove_meta_box( sanitize_text_field($item[0]), sanitize_text_field($item[1]), sanitize_text_field($item[2]) );
        }
    }

    if( ds1_checkSettings("remove_meta_boxes_editor") ) {
        if( isset( $_GET['post'] ) && in_array(intval($_GET['post']) , ds1_checkSettings("remove_meta_boxes_editor")) )   {
            remove_post_type_support( 'page', 'editor' );
        }
    }
}



if( ds1_checkSettings("admin_menu_elements_list") ){
    add_action('admin_menu', 'Ds1_my_change_to_admin_menu', 102);

    function Ds1_my_change_to_admin_menu() {

        global $submenu;
    
        // الحصول على البيانات الجديدة من إعدادات القوائم
        $menu_settings = ds1_checkSettings("admin_menu_elements_list");
        // معالجة عناصر القوائم
        
        foreach ($menu_settings as $type => $items_by_for) {
            foreach ($items_by_for as $for => $items) {
    
                // التحقق من الشروط بناءً على 'for'
                if (
                    $for == 'all' ||
                    ($for == 'not_admin' && (!current_user_can('editor') && !current_user_can('administrator'))) ||
                    ($for == 'admin_only' && (current_user_can('editor') || current_user_can('administrator'))) ||
                    ($for == 'not_user_1' && (get_current_user_id() != 1)) ||
                    ($for == 'not_support' && strpos(wp_get_current_user()->user_login, 'support') !== 0)
                ) {
            

                    // التعامل مع كل نوع
                    switch ($type) {
                        case 'add_menu':
                            foreach ($items as $item) {
                                add_menu_page(
                                    __(sanitize_text_field($item['title'])),
                                    __(sanitize_text_field($item['menu'])),
                                    sanitize_text_field($item['cap']),
                                    sanitize_text_field($item['slug']),
                                    sanitize_text_field($item['callback']),
                                    sanitize_text_field($item['icon']),
                                    sanitize_text_field($item['pos'])
                                );
                            }
                            break;
                        case 'add_submenu':
                            foreach ($items as $item) {
                                add_submenu_page(
                                    sanitize_text_field($item['parent']),
                                    __(sanitize_text_field($item['title'])),
                                    __(sanitize_text_field($item['menu'])),
                                    sanitize_text_field($item['cap']),
                                    sanitize_text_field($item['slug']),
                                    sanitize_text_field($item['callback']),
                                    $item['pos']
                                );
                            }
                            break;
                        case 'remove_menu':
                            foreach ($items as $item) {
                                remove_menu_page(sanitize_text_field($item));
                            }
                            break;
                        case 'remove_submenu':
                            foreach ($items as $item) {
                                remove_submenu_page(sanitize_text_field($item['parent']), sanitize_text_field($item['slug']));
                            }
                            break;
                    }
                }
            }
        }
    }
}
    
if( ds1_checkSettings("admin_bar_elements") ){
    function Ds1_my_change_to_admin_bar($admin_bar) {
    
        foreach(checkSettings("admin_bar_elements") as $item) {
			$for = (!empty($item['for'])) ? sanitize_text_field($item['for']) : false;
            if(
                $for == 'all' ||
                ( $for == 'not_admin' && ( !current_user_can('editor') && ! current_user_can('administrator') ) ) ||
                ( $for == 'admin_only' && ( current_user_can('editor') || current_user_can('administrator') ) ) ||
                ( $for == 'not_user_1' && ( get_current_user_id(  )  != 1 ) ) 
            ){
                switch ( sanitize_text_field($item['type'])) {
                    case 'add':
                        $admin_bar->add_node( $item );
                        break;
                    case 'remove':
                        $admin_bar->remove_node( sanitize_text_field( $item["id"] ) );
                        break;
                }
            }
        }
        
    }
    add_action('admin_bar_menu', 'Ds1_my_change_to_admin_bar', 999);
}

// Change admin menu oders from settings file
if( ds1_checkSettings("admin_menu_order") ){
    
    function Ds1_my_change_to_menu_order( $menu_ord ) {
        if ( !$menu_ord ) return true;
    
        return ds1_checkSettings("admin_menu_order");
    }
    add_filter( 'custom_menu_order', 'Ds1_my_change_to_menu_order', 10, 1 );
    add_filter( 'menu_order', 'Ds1_my_change_to_menu_order', 10, 1 );
    

}