| 241 | |
| 242 | add_action( 'admin_head', '_admin_menu_icons_css' ); |
| 243 | function _admin_menu_icons_css() { |
| 244 | $css = ''; |
| 245 | |
| 246 | // Loop through all available post types |
| 247 | foreach ( (array) get_post_types( array( 'show_ui' => true, '_builtin' => false, 'show_in_menu' => true ) ) as $ptype ) { |
| 248 | $ptype_obj = get_post_type_object( $ptype ); |
| 249 | |
| 250 | // Check if it should be a submenu. |
| 251 | if ( $ptype_obj->show_in_menu !== true ) |
| 252 | continue; |
| 253 | |
| 254 | // If no menu icon is set don't do anything. |
| 255 | if ( ! $ptype_obj->menu_icon || empty( $ptype_obj->menu_icon ) ) |
| 256 | continue; |
| 257 | |
| 258 | // Will use this as a class name later. |
| 259 | $ptype_slug = sanitize_html_class( $ptype ); |
| 260 | $menu_icon = $ptype_obj->menu_icon; |
| 261 | |
| 262 | // Back-compat, transforms to new-style menu icon. |
| 263 | if ( ! is_array( $menu_icon ) ) { |
| 264 | $menu_icon = array( |
| 265 | 'image_url' => $menu_icon, |
| 266 | 'background_size' => '16px 16px', |
| 267 | 'background_position' => '7px 7px', |
| 268 | ); |
| 269 | } |
| 270 | |
| 271 | $background_image = ''; |
| 272 | $background_size = ''; |
| 273 | $background_position = ''; |
| 274 | $background_position_hover = ''; |
| 275 | |
| 276 | if ( ! isset( $menu_icon['regular'] ) ) |
| 277 | $menu_icon = array( 'regular' => $menu_icon ); |
| 278 | |
| 279 | $regular = $menu_icon['regular']; |
| 280 | |
| 281 | if ( isset( $regular['image_url'] ) ) |
| 282 | $background_image = sprintf( "background-image: url('%s');", esc_url( $regular['image_url'] ) ); |
| 283 | |
| 284 | if ( isset( $regular['background_size'] ) ) { |
| 285 | $xy = array_map( 'intval', preg_split( '/[\s,]+/', $regular['background_size'] ) ); |
| 286 | if ( count( $xy ) == 2 ) |
| 287 | $background_size = sprintf( "background-size: %dpx %dpx;", $xy[0], $xy[1] ); |
| 288 | } |
| 289 | |
| 290 | if ( isset( $regular['background_position'] ) ) { |
| 291 | $xy = array_map( 'intval', preg_split( '/[\s,]+/', $regular['background_position'] ) ); |
| 292 | if ( count( $xy ) == 2 ) |
| 293 | $background_position = sprintf( "background-position: %dpx %dpx;", $xy[0], $xy[1] ); |
| 294 | } |
| 295 | |
| 296 | if ( isset( $regular['background_position_hover'] ) ) { |
| 297 | $xy = array_map( 'intval', preg_split( '/[\s,]+/', $regular['background_position_hover'] ) ); |
| 298 | if ( count( $xy ) == 2 ) |
| 299 | $background_position_hover = sprintf( "background-position: %dpx %dpx;", $xy[0], $xy[1] ); |
| 300 | } |
| 301 | |
| 302 | $css .= " |
| 303 | #adminmenu li.menu-icon-{$ptype_slug} .wp-menu-image { |
| 304 | $background_image |
| 305 | $background_size |
| 306 | $background_position |
| 307 | } |
| 308 | #adminmenu li.menu-icon-{$ptype_slug}:hover .wp-menu-image { |
| 309 | $background_position_hover |
| 310 | }"; |
| 311 | |
| 312 | // Hidpi |
| 313 | $hidpi = isset( $menu_icon['2x'] ) ? $menu_icon['2x'] : array(); |
| 314 | |
| 315 | $hidpi_background_image = ''; |
| 316 | $hidpi_background_size = ''; |
| 317 | $hidpi_background_position = ''; |
| 318 | $hidpi_background_position_hover = ''; |
| 319 | |
| 320 | if ( isset( $hidpi['image_url'] ) ) |
| 321 | $hidpi_background_image = sprintf( "background-image: url('%s');", esc_url( $hidpi['image_url'] ) ); |
| 322 | |
| 323 | if ( isset( $hidpi['background_size'] ) ) { |
| 324 | $xy = array_map( 'intval', preg_split( '/[\s,]+/', $hidpi['background_size'] ) ); |
| 325 | if ( count( $xy ) == 2 ) |
| 326 | $hidpi_background_size = sprintf( "background-size: %dpx %dpx;", $xy[0], $xy[1] ); |
| 327 | } |
| 328 | |
| 329 | if ( isset( $hidpi['background_position'] ) ) { |
| 330 | $xy = array_map( 'intval', preg_split( '/[\s,]+/', $hidpi['background_position'] ) ); |
| 331 | if ( count( $xy ) == 2 ) |
| 332 | $hidpi_background_position = sprintf( "background-position: %dpx %dpx;", $xy[0], $xy[1] ); |
| 333 | } |
| 334 | |
| 335 | if ( isset( $hidpi['background_position_hover'] ) ) { |
| 336 | $xy = array_map( 'intval', preg_split( '/[\s,]+/', $hidpi['background_position_hover'] ) ); |
| 337 | if ( count( $xy ) == 2 ) |
| 338 | $hidpi_background_position_hover = sprintf( "background-position: %dpx %dpx;", $xy[0], $xy[1] ); |
| 339 | } |
| 340 | |
| 341 | if ( $hidpi ) { |
| 342 | $css .= "@media only screen and (-webkit-min-device-pixel-ratio: 1.5) { |
| 343 | #adminmenu li.menu-icon-{$ptype_slug} .wp-menu-image { |
| 344 | $hidpi_background_image |
| 345 | $hidpi_background_position |
| 346 | $hidpi_background_size |
| 347 | } |
| 348 | #adminmenu li.menu-icon-{$ptype_slug}:hover .wp-menu-image { |
| 349 | $hidpi_background_position_hover |
| 350 | } |
| 351 | }"; |
| 352 | } |
| 353 | } // foreach |
| 354 | |
| 355 | if ( strlen( $css ) > 0 ) |
| 356 | echo "<style>$css</style>"; |
| 357 | } |
| 358 | No newline at end of file |