/** * Generate CSS rules for menu items that have user-defined colors. * * This method stores the CSS at the "color_css" key in the menu structure and returns a modified menu. * By storing the color scheme CSS in the menu itself we avoid having to regenerate it on every page load. * We also don't have to worry about cache lifetime - when the menu is modified the old CSS will be * overwritten automatically. * * @param array $custom_menu Admin menu in the internal format. * @return array Modified menu. */ public function add_menu_color_css($custom_menu) { if (empty($custom_menu) || !is_array($custom_menu) || !isset($custom_menu['tree'])) { return $custom_menu; } if (!class_exists('ameMenuColorGenerator')) { require_once dirname(__FILE__) . '/extras/menu-color-generator.php'; } $generator = new ameMenuColorGenerator(); $css = array(); $used_ids = array(); $colorized_menu_count = 0; foreach ($custom_menu['tree'] as &$item) { if (!isset($item['colors']) || empty($item['colors'])) { continue; } $colorized_menu_count++; //Each item needs to have a unique ID so we can target it in CSS. Using a class would be cleaner, //but the selectors wouldn't have enough specificity to override WP defaults. $id = ameMenuItem::get($item, 'hookname'); if (empty($id) || isset($used_ids[$id])) { $id = (empty($id) ? 'ame-colorized-item' : $id) . '-'; $id .= $colorized_menu_count . '-t' . time(); $item['hookname'] = $id; } $used_ids[$id] = true; $item_css = $generator->getCss($id, $item['colors']); if (!empty($item_css)) { $css[] = sprintf('/* %1$s (%2$s) */', str_replace('*/', ' ', ameMenuItem::get($item, 'menu_title', 'Untitled menu')), str_replace('*/', ' ', ameMenuItem::get($item, 'file', '(no URL)'))); $css[] = $item_css; } } if (!empty($css)) { $css = implode("\n", $css); $custom_menu['color_css'] = $css; $custom_menu['color_css_modified'] = time(); } else { $custom_menu['color_css'] = ''; $custom_menu['color_css_modified'] = 0; } return $custom_menu; }
/** * Create a unique name for a specific field of a specific menu item. * Intended for use with the icl_register_string() function. * * @param array $item Admin menu item in the internal format. * @param string $field Field name. * @return string */ private function get_wpml_name_for($item, $field = '') { $name = ameMenuItem::get($item, 'template_id'); if (empty($name)) { $name = 'custom: ' . ameMenuItem::get($item, 'file'); } if (!empty($field)) { $name = $name . '[' . $field . ']'; } return $name; }
private function get_virtual_caps_for($item) { $caps = array(); if ($item['template_id'] !== '') { $required_cap = ameMenuItem::get($item, 'access_level'); foreach ($item['grant_access'] as $grant => $has_access) { if ($has_access) { if (!isset($caps[$grant])) { $caps[$grant] = array(); } $caps[$grant][$required_cap] = true; } } } foreach ($item['items'] as $sub_item) { $caps = array_merge_recursive($caps, $this->get_virtual_caps_for($sub_item)); } return $caps; }
/** * Check if a menu contains any items with the "hidden" flag set to true. * * @param array $menu * @return bool */ public static function has_hidden_items($menu) { if (!is_array($menu) || empty($menu) || empty($menu['tree'])) { return false; } foreach ($menu['tree'] as $item) { if (ameMenuItem::get($item, 'hidden')) { return true; } if (!empty($item['items'])) { foreach ($item['items'] as $child) { if (ameMenuItem::get($child, 'hidden')) { return true; } } } } return false; }
/** * Flag menus (and menu items) that are set to open in a new window * so that they can be identified later. * * Adds a <span class="ws-new-window-please"></span> element to the title * of each detected menu. * * @param array $item * @return array */ function flag_new_window_menus($item) { $open_in = ameMenuItem::get($item, 'open_in', 'same_window'); if ($open_in == 'new_window') { $old_title = ameMenuItem::get($item, 'menu_title', ''); $item['menu_title'] = $old_title . '<span class="ws-new-window-please" style="display:none;"></span>'; //For compatibility with Ozh's Admin Drop Down menu, record the link ID that will be //assigned to this item. This lets us modify it later. if (function_exists('wp_ozh_adminmenu_sanitize_id')) { $subid = 'oamsub_' . wp_ozh_adminmenu_sanitize_id(ameMenuItem::get($item, 'file', '')); $this->ozhs_new_window_menus[] = '#' . str_replace(array(':', '&'), array('\\\\:', '\\\\&'), $subid); } } return $item; }