/**
 * Check secondary menu is enabled for the logged in user
 * @param string $primary_menu
 * @param string $secondary_menu
 * @return boolean
 */
function mgm_is_mgm_menu_enabled($type = 'primary', $primary_menu, $secondary_menu = null)
{
    $user_id = get_current_user_id();
    $obj_roles = new mgm_roles();
    // for the firsttime load
    if (!get_option('rolebasedmenu_init')) {
        // update administrator role with capabilities
        $custom_caps = $obj_roles->get_custom_capabilities();
        foreach ($custom_caps as $cap) {
            $obj_roles->update_capability_role('administrator', $cap, true);
        }
        // set init value
        update_option('rolebasedmenu_init', 1);
    }
    $capabilities = $obj_roles->get_loggedinuser_custom_capabilities($user_id);
    $system_obj = mgm_get_class('system');
    // common setting
    $dml = $system_obj->setting['enable_role_based_menu_loading'];
    // if dynamic menu loading is disabled/priamry and secondary menus are enabled
    return is_super_admin() && !bool_from_yn($dml) || (bool_from_yn($dml) && ($type == 'primary' && in_array($primary_menu, $capabilities)) || $type == 'secondary' && in_array($primary_menu, $capabilities) && in_array($secondary_menu, $capabilities));
    // check secondary menu capability is loaded
}
<?php

/** 
 * Objects merge/update
 * Add New System Setting: enable_role_based_menu_loading
 * Populate Custom MGM capabilities for 'administrator' role
 */
// read
$system_obj = mgm_get_class('system');
if (!isset($system_obj->setting['enable_role_based_menu_loading'])) {
    // default to 'NO'
    $system_obj->setting['enable_role_based_menu_loading'] = 'N';
    // save
    $system_obj->save();
}
//default MGM custom capabilities to true for administrator role:
$obj_role = new mgm_roles();
$custom_caps = $obj_role->get_custom_capabilities();
foreach ($custom_caps as $cap) {
    $obj_role->update_capability_role('administrator', $cap, true);
}
// end file