/** * Load an admin menu structure from an associative array. * * @static * * @param array $arr * @param bool $assume_correct_format * @param bool $always_normalize * @throws InvalidMenuException * @return array */ public static function load_array($arr, $assume_correct_format = false, $always_normalize = false) { $is_normalized = false; if (!$assume_correct_format) { if (isset($arr['format']) && $arr['format']['name'] == self::format_name) { $compared = version_compare($arr['format']['version'], self::format_version); if ($compared > 0) { throw new InvalidMenuException("Can't load a menu created by a newer version of the plugin."); } //We can skip normalization if the version number matches exactly and the menu is already normalized. if ($compared === 0 && isset($arr['format']['is_normalized'])) { $is_normalized = $arr['format']['is_normalized']; } } else { return self::load_menu_40($arr); } } if (!(isset($arr['tree']) && is_array($arr['tree']))) { throw new InvalidMenuException("Failed to load a menu - the menu tree is missing."); } $menu = array('tree' => array()); $menu = self::add_format_header($menu); if ($is_normalized && !$always_normalize) { $menu['tree'] = $arr['tree']; } else { foreach ($arr['tree'] as $file => $item) { $menu['tree'][$file] = ameMenuItem::normalize($item); } $menu['format']['is_normalized'] = true; } return $menu; }
/** * Load an admin menu structure from an associative array. * * @static * @throws InvalidMenuException when the supplied input is not a valid menu. * * @param array $arr * @param bool $assume_correct_format * @return array */ public static function load_array($arr, $assume_correct_format = false) { if (!$assume_correct_format) { if (isset($arr['format']) && $arr['format']['name'] == self::format_name) { if (!version_compare($arr['format']['version'], self::format_version, '<=')) { throw new InvalidMenuException("Can't load a menu created by a newer version of the plugin."); } } else { return self::load_menu_40($arr); } } if (!(isset($arr['tree']) && is_array($arr['tree']))) { throw new InvalidMenuException("Failed to load a menu - the menu tree is missing."); } $menu = array('tree' => array()); $menu = self::add_format_header($menu); foreach ($arr['tree'] as $file => $item) { $menu['tree'][$file] = ameMenuItem::normalize($item); } return $menu; }
/** * Load an admin menu structure from an associative array. * * @static * * @param array $arr * @param bool $assume_correct_format * @param bool $always_normalize * @throws InvalidMenuException * @return array */ public static function load_array($arr, $assume_correct_format = false, $always_normalize = false) { $is_normalized = false; if (!$assume_correct_format) { if (isset($arr['format']) && $arr['format']['name'] == self::format_name) { $compared = version_compare($arr['format']['version'], self::format_version); if ($compared > 0) { throw new InvalidMenuException(sprintf("Can't load a menu created by a newer version of the plugin. Menu format: '%s', newest supported format: '%s'.", $arr['format']['version'], self::format_version)); } //We can skip normalization if the version number matches exactly and the menu is already normalized. if ($compared === 0 && isset($arr['format']['is_normalized'])) { $is_normalized = $arr['format']['is_normalized']; } } else { return self::load_menu_40($arr); } } if (!(isset($arr['tree']) && is_array($arr['tree']))) { throw new InvalidMenuException("Failed to load a menu - the menu tree is missing."); } if (isset($arr['format']) && !empty($arr['format']['compressed'])) { $arr = self::decompress($arr); } $menu = array('tree' => array()); $menu = self::add_format_header($menu); if ($is_normalized && !$always_normalize) { $menu['tree'] = $arr['tree']; } else { foreach ($arr['tree'] as $file => $item) { $menu['tree'][$file] = ameMenuItem::normalize($item); } $menu['format']['is_normalized'] = true; } if (isset($arr['color_css']) && is_string($arr['color_css'])) { $menu['color_css'] = $arr['color_css']; $menu['color_css_modified'] = isset($arr['color_css_modified']) ? intval($arr['color_css_modified']) : 0; } //Sanitize color presets. if (isset($arr['color_presets']) && is_array($arr['color_presets'])) { $color_presets = array(); foreach ($arr['color_presets'] as $name => $preset) { $name = substr(trim(strip_tags(strval($name))), 0, 250); if (empty($name) || !is_array($preset)) { continue; } //Each color must be a hexadecimal HTML color code. For example: "#12456" $is_valid_preset = true; foreach ($preset as $property => $color) { //Note: It would good to check $property against a list of known color names. if (!is_string($property) || !is_string($color) || !preg_match('/^\\#[0-9a-f]{6}$/i', $color)) { $is_valid_preset = false; break; } } if ($is_valid_preset) { $color_presets[$name] = $preset; } } $menu['color_presets'] = $color_presets; } //Copy directly granted capabilities. if (isset($arr['granted_capabilities']) && is_array($arr['granted_capabilities'])) { $granted_capabilities = array(); foreach ($arr['granted_capabilities'] as $actor => $capabilities) { //Skip empty lists to avoid problems with {} => [] and to save space. if (!empty($capabilities)) { $granted_capabilities[strval($actor)] = $capabilities; } } if (!empty($granted_capabilities)) { $menu['granted_capabilities'] = $granted_capabilities; } } return $menu; }