TwentyNineteen_SVG_Icons
- Since
- Twenty Nineteen 1.0
- Source
wp-content/themes/twentynineteen/classes/class-twentynineteen-svg-icons.php:22
This class is in charge of displaying SVG icons across the site.
Description
Place each source on its own array key, without adding the both width and height attributes, since these are added dynamically, before rendering the SVG code.
All icons are assumed to have equal width and height, hence the option to only specify a $size parameter in the svg methods.
Compatibility
- WordPress
- since Twenty Nineteen 1.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Properties · 3
$ui_iconsarraypublic static- User Interface icons – svg sources.
$social_icons_maparraypublic static- Social Icons – domain mappings.
$social_iconsarraypublic static- Social Icons – svg sources.
Methods · 2
- get_svg()Gets the SVG code for a given icon.
- get_social_link_svg()Detects the social network from a URL and returns the SVG code for its icon.
Source code
<?phpclass TwentyNineteen_SVG_Icons { /** * Gets the SVG code for a given icon. * * @param string $group The group of icons ('ui' or 'social'). * @param string $icon The specific icon to retrieve. * @param int $size The desired width and height for the SVG icon. * @return string|null SVG code for the icon, or null if not found. */ public static function get_svg( $group, $icon, $size ) { if ( 'ui' === $group ) { $arr = self::$ui_icons; } elseif ( 'social' === $group ) { $arr = self::$social_icons; } else { $arr = array(); } if ( array_key_exists( $icon, $arr ) ) { $repl = sprintf( '<svg class="svg-icon" width="%d" height="%d" aria-hidden="true" role="img" focusable="false" ', $size, $size ); $svg = preg_replace( '/^<svg /', $repl, trim( $arr[ $icon ] ) ); // Add extra attributes to SVG code. $svg = preg_replace( "/([\n\t]+)/", ' ', $svg ); // Remove newlines & tabs. $svg = preg_replace( '/>\s*</', '><', $svg ); // Remove whitespace between SVG tags. return $svg; } return null; } /** * Detects the social network from a URL and returns the SVG code for its icon. * * @param string $uri The URL of the social network link. * @param int $size The desired width and height for the SVG icon. * @return string|null SVG code for the social link icon, or null if not found. */ public static function get_social_link_svg( $uri, $size ) { static $regex_map; // Only compute regex map once, for performance. if ( ! isset( $regex_map ) ) { $regex_map = array(); $map = &self::$social_icons_map; // Use reference instead of copy, to save memory. foreach ( array_keys( self::$social_icons ) as $icon ) { $domains = array_key_exists( $icon, $map ) ? $map[ $icon ] : array( sprintf( '%s.com', $icon ) ); $domains = array_map( 'trim', $domains ); // Remove leading/trailing spaces, to prevent regex from failing to match. $domains = array_map( 'preg_quote', $domains ); $regex_map[ $icon ] = sprintf( '/(%s)/i', implode( '|', $domains ) ); } } foreach ( $regex_map as $icon => $regex ) { if ( preg_match( $regex, $uri ) ) { return self::get_svg( 'social', $icon, $size ); } } return null; } /** * User Interface icons – svg sources. * * @var array */ public static $ui_icons = array( 'link' => /* material-design – link */ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> <path d="M0 0h24v24H0z" fill="none"></path> <path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"></path></svg>', 'watch' => /* material-design – watch-later */ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> <defs> <path id="a" d="M0 0h24v24H0V0z"></path> </defs> <clipPath id="b"> <use xlink:href="#a" overflow="visible"></use> </clipPath> <path clip-path="url(#b)" d="M12 2C6.5 2 2 6.5 2 12s4.5 10 10 10 10-4.5 10-10S17.5 2 12 2zm4.2 14.2L11 13V7h1.5v5.2l4.5 2.7-.8 1.3z"></path></svg>', 'archive' => /* material-design – folder */ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">Changelog
Introduced in Twenty Nineteen 1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-content/themes/twentynineteen/classes/class-twentynineteen-svg-icons.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.