Allow for Dashicons and base64-encoded data:image/svg+xml URIs when specifying menu icons.

Both of these icons can be colored to match the color scheme, including hover states.
Both are accepted for register_post_type()'s menu_icon argument, and also add_menu_page()'s $icon_url argument.

To use a Dashicon, pass the name of the helper class, e.g. 'dashicons-piechart'.
To use an SVG, pass a valid data URI string starting with 'data:image/svg+xml;base64,'.

props helen.
fixes #25147.

Built from https://develop.svn.wordpress.org/trunk@26664


git-svn-id: http://core.svn.wordpress.org/trunk@26554 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2013-12-05 06:38:09 +00:00
parent 053898f8bc
commit 9b29ad0529
11 changed files with 58 additions and 16 deletions

View File

@ -1874,6 +1874,8 @@ form.upgrade .hint {
width: 34px;
height: 30px;
margin: 0;
background-repeat: no-repeat;
background-position: center;
text-align: center;
}

File diff suppressed because one or more lines are too long

View File

@ -1874,6 +1874,8 @@ form.upgrade .hint {
width: 34px;
height: 30px;
margin: 0;
background-repeat: no-repeat;
background-position: center;
text-align: center;
}

File diff suppressed because one or more lines are too long

View File

@ -618,9 +618,21 @@ function wp_color_scheme_settings() {
$color_scheme = get_user_option( 'admin_color' );
if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
echo '<script type="text/javascript">var _wpColorScheme = ' . json_encode( array( 'icons' => $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) . ";</script>\n";
// It's possible to have a color scheme set that is no longer registered.
if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
$color_scheme = 'fresh';
}
if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
$icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
} elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
$icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
} else {
// Fall back to the default set of icon colors if the default scheme is missing.
$icon_colors = array( 'base' => '#999', 'focus' => '#2ea2cc', 'current' => '#fff' );
}
echo '<script type="text/javascript">var _wpColorScheme = ' . json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
}
add_action( 'admin_head', 'wp_color_scheme_settings' );

View File

@ -966,8 +966,11 @@ function uninstall_plugin($plugin) {
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu)
* @param callback $function The function to be called to output the content for this page.
* @param string $icon_url The url to the icon to be used for this menu. Using 'none' would leave div.wp-menu-image empty
* so an icon can be added as background with CSS.
* @param string $icon_url The url to the icon to be used for this menu.
* * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
* This should begin with 'data:image/svg+xml;base64,'.
* * Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-piechart'.
* * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
* @param int $position The position in the menu order this one should appear
*
* @return string The resulting page's hook_suffix

View File

@ -67,11 +67,24 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
$class = $class ? ' class="' . join( ' ', $class ) . '"' : '';
$id = ! empty( $item[5] ) ? ' id="' . preg_replace( '|[^a-zA-Z0-9_:.]|', '-', $item[5] ) . '"' : '';
$img = '';
$img = $img_style = $img_class = '';
// if the string 'none' (previously 'div') is passed instead of an URL, don't output the default menu image
// so an icon can be added to div.wp-menu-image as background with CSS.
if ( ! empty( $item[6] ) )
$img = ( 'none' === $item[6] || 'div' === $item[6] ) ? '<br />' : '<img src="' . $item[6] . '" alt="" />';
// Dashicons and base64-encoded data:image/svg_xml URIs are also handled as special cases.
if ( ! empty( $item[6] ) ) {
$img = '<img src="' . $item[6] . '" alt="" />';
if ( 'none' === $item[6] || 'div' === $item[6] ) {
$img = '<br />';
} elseif ( 0 === strpos( $item[6], 'data:image/svg+xml;base64,' ) ) {
$img = '<br />';
$img_style = ' style="background-image:url(\'' . esc_attr( $item[6] ) . '\')"';
} elseif ( 0 === strpos( $item[6], 'dashicons-' ) ) {
$img = '<br />';
$img_class = ' dashicons ' . sanitize_html_class( $item[6] );
}
}
$arrow = '<div class="wp-menu-arrow"><div></div></div>';
$title = wptexturize( $item[0] );
@ -88,9 +101,9 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
$menu_file = substr( $menu_file, 0, $pos );
if ( ! empty( $menu_hook ) || ( ( 'index.php' != $submenu_items[0][2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) {
$admin_is_parent = true;
echo "<a href='admin.php?page={$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image'>$img</div><div class='wp-menu-name'>$title</div></a>";
echo "<a href='admin.php?page={$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>$title</div></a>";
} else {
echo "\n\t<a href='{$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image'>$img</div><div class='wp-menu-name'>$title</div></a>";
echo "\n\t<a href='{$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>$title</div></a>";
}
} elseif ( ! empty( $item[2] ) && current_user_can( $item[1] ) ) {
$menu_hook = get_plugin_page_hook( $item[2], 'admin.php' );
@ -99,9 +112,9 @@ function _wp_menu_output( $menu, $submenu, $submenu_as_parent = true ) {
$menu_file = substr( $menu_file, 0, $pos );
if ( ! empty( $menu_hook ) || ( ( 'index.php' != $item[2] ) && file_exists( WP_PLUGIN_DIR . "/$menu_file" ) && ! file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) ) {
$admin_is_parent = true;
echo "\n\t<a href='admin.php?page={$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image'>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
echo "\n\t<a href='admin.php?page={$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
} else {
echo "\n\t<a href='{$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image'>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
echo "\n\t<a href='{$item[2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>{$item[0]}</div></a>";
}
}

View File

@ -107,8 +107,14 @@ foreach ( (array) get_post_types( array('show_ui' => true, '_builtin' => false,
continue;
$ptype_menu_position = is_int( $ptype_obj->menu_position ) ? $ptype_obj->menu_position : ++$_wp_last_object_menu; // If we're to use $_wp_last_object_menu, increment it first.
$ptype_for_id = sanitize_html_class( $ptype );
if ( is_string( $ptype_obj->menu_icon ) ) {
$menu_icon = esc_url( $ptype_obj->menu_icon );
// Special handling for data:image/svg+xml and Dashicons.
if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) {
$menu_icon = $ptype_obj->menu_icon;
} else {
$menu_icon = esc_url( $ptype_obj->menu_icon );
}
$ptype_class = $ptype_for_id;
} else {
$menu_icon = 'none';

View File

@ -200,7 +200,7 @@ var svgPainter = ( function( $, window, document, undefined ) {
$element.data( 'wp-ui-svg-' + color, xml );
}
$element.attr( 'style', 'background-image: url("data:image/svg+xml;base64,' + xml + '") !important;' );
$element.css( 'background-image', 'url("data:image/svg+xml;base64,' + xml + '")' );
}
};

View File

@ -1 +1 @@
var svgPainter=function(a,b,c){"use strict";var d,e,f={},g=[];return a(c).ready(function(){c.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")&&(a(c.body).removeClass("no-svg").addClass("svg"),svgPainter.init())}),e=function(){function a(a,b,c,e,f,g){var h,i,j=0,k=0,l="",m=0;for(a=String(a),i=a.length;i>k;){for(d=a.charCodeAt(k),d=256>d?c[d]:-1,j=(j<<f)+d,m+=f;m>=g;)m-=g,h=j>>m,l+=e.charAt(h),j^=h<<m;++k}return!b&&m>0&&(l+=e.charAt(j<<g-m)),l}function b(b){return b=a(b,!1,h,e,8,6),b+"====".slice(b.length%4||4)}function c(b){var c;b=b.replace(/[^A-Za-z0-9\+\/\=]/g,""),b=String(b).split("="),c=b.length;do--c,b[c]=a(b[c],!0,g,f,6,8);while(c>0);return b=b.join("")}for(var d,e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",f="",g=[256],h=[256],i=0;256>i;)d=String.fromCharCode(i),f+=d,h[i]=i,g[i]=e.indexOf(d),++i;return{atob:c,btoa:b}}(),{init:function(){d=a("#adminmenu .wp-menu-image, #wpadminbar .ab-item"),this.setColors(),this.findElements(),this.paint()},setColors:function(a){"undefined"==typeof a&&"undefined"!=typeof b._wpColorScheme&&(a=b._wpColorScheme),a&&a.icons&&a.icons.base&&a.icons.current&&a.icons.focus&&(f=a.icons)},findElements:function(){d.each(function(){var b=a(this),c=b.css("background-image");c&&-1!=c.indexOf("data:image/svg+xml;base64")&&g.push(b)})},paint:function(){a.each(g,function(a,b){var c=b.parent().parent();c.hasClass("current")||c.hasClass("wp-has-current-submenu")?svgPainter.paintElement(b,"current"):(svgPainter.paintElement(b,"base"),c.hover(function(){svgPainter.paintElement(b,"focus")},function(){svgPainter.paintElement(b,"base")}))})},paintElement:function(a,c){var d,g,h;if(c&&f.hasOwnProperty(c)&&(h=f[c],h.match(/^(#[0-9a-f]{3}|#[0-9a-f]{6})$/i))){if(d=a.data("wp-ui-svg-"+h),!d){if(g=a.css("background-image").match(/.+data:image\/svg\+xml;base64,(.+?)['"]? ?\)/),!g||!g[1])return;d="atob"in b?b.atob(g[1]):e.atob(g[1]),d=d.replace(/fill="(.+?)"/g,'fill="'+h+'"'),d=d.replace(/style="(.+?)"/g,'style="fill:'+h+'"'),d=d.replace(/fill:.*?;/g,"fill: "+h+";"),d="btoa"in b?b.btoa(d):e.btoa(d),a.data("wp-ui-svg-"+h,d)}a.attr("style",'background-image: url("data:image/svg+xml;base64,'+d+'") !important;')}}}}(jQuery,window,document);
var svgPainter=function(a,b,c){"use strict";var d,e,f={},g=[];return a(c).ready(function(){c.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")&&(a(c.body).removeClass("no-svg").addClass("svg"),svgPainter.init())}),e=function(){function a(a,b,c,e,f,g){var h,i,j=0,k=0,l="",m=0;for(a=String(a),i=a.length;i>k;){for(d=a.charCodeAt(k),d=256>d?c[d]:-1,j=(j<<f)+d,m+=f;m>=g;)m-=g,h=j>>m,l+=e.charAt(h),j^=h<<m;++k}return!b&&m>0&&(l+=e.charAt(j<<g-m)),l}function b(b){return b=a(b,!1,h,e,8,6),b+"====".slice(b.length%4||4)}function c(b){var c;b=b.replace(/[^A-Za-z0-9\+\/\=]/g,""),b=String(b).split("="),c=b.length;do--c,b[c]=a(b[c],!0,g,f,6,8);while(c>0);return b=b.join("")}for(var d,e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",f="",g=[256],h=[256],i=0;256>i;)d=String.fromCharCode(i),f+=d,h[i]=i,g[i]=e.indexOf(d),++i;return{atob:c,btoa:b}}(),{init:function(){d=a("#adminmenu .wp-menu-image, #wpadminbar .ab-item"),this.setColors(),this.findElements(),this.paint()},setColors:function(a){"undefined"==typeof a&&"undefined"!=typeof b._wpColorScheme&&(a=b._wpColorScheme),a&&a.icons&&a.icons.base&&a.icons.current&&a.icons.focus&&(f=a.icons)},findElements:function(){d.each(function(){var b=a(this),c=b.css("background-image");c&&-1!=c.indexOf("data:image/svg+xml;base64")&&g.push(b)})},paint:function(){a.each(g,function(a,b){var c=b.parent().parent();c.hasClass("current")||c.hasClass("wp-has-current-submenu")?svgPainter.paintElement(b,"current"):(svgPainter.paintElement(b,"base"),c.hover(function(){svgPainter.paintElement(b,"focus")},function(){svgPainter.paintElement(b,"base")}))})},paintElement:function(a,c){var d,g,h;if(c&&f.hasOwnProperty(c)&&(h=f[c],h.match(/^(#[0-9a-f]{3}|#[0-9a-f]{6})$/i))){if(d=a.data("wp-ui-svg-"+h),!d){if(g=a.css("background-image").match(/.+data:image\/svg\+xml;base64,(.+?)['"]? ?\)/),!g||!g[1])return;d="atob"in b?b.atob(g[1]):e.atob(g[1]),d=d.replace(/fill="(.+?)"/g,'fill="'+h+'"'),d=d.replace(/style="(.+?)"/g,'style="fill:'+h+'"'),d=d.replace(/fill:.*?;/g,"fill: "+h+";"),d="btoa"in b?b.btoa(d):e.btoa(d),a.data("wp-ui-svg-"+h,d)}a.css("background-image",'url("data:image/svg+xml;base64,'+d+'")')}}}}(jQuery,window,document);

View File

@ -1129,6 +1129,10 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
* * show_in_menu must be true
* * Defaults to null, which places it at the bottom of its area.
* - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
* * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
* This should begin with 'data:image/svg+xml;base64,'.
* * Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-piechart'.
* * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
* - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
* * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
* capabilities, e.g. array('story', 'stories').