source on its own array key, without adding the
* both `width` and `height` attributes, since these are added dnamically,
* 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.
*
* @since 1.0.0
*/
class Blog_Rider_SVG_Icons {
/**
* Gets the SVG code for a given icon.
*/
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( '\s*', '><', $svg ); // Remove white space between SVG tags.
return $svg;
}
return null;
}
/**
* Detects the social network from a URL and returns the SVG code for its icon.
*/
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
*/
static $ui_icons = array(
'link' => /* material-design – link */ '
',
'watch' => /* material-design – watch-later */ '
',
'archive' => /* material-design – folder */ '
',
'tag' => /* material-design – local_offer */ '
',
'comment' => /* material-design – comment */ '
',
'person' => /* material-design – person */ '
',
'edit' => /* material-design – edit */ '
',
'chevron_left' => /* material-design – chevron_left */ '
',
'chevron_right' => /* material-design – chevron_right */ '
',
'check' => /* material-design – check */ '
',
'arrow_drop_down_circle' => /* material-design – arrow_drop_down_circle */ '
',
'keyboard_arrow_down' => /* material-design – keyboard_arrow_down */ '
',
'keyboard_arrow_right' => /* material-design – keyboard_arrow_right */ '
',
'keyboard_arrow_left' => /* material-design – keyboard_arrow_left */ '
',
'arrow_drop_down_ellipsis' => /* custom – arrow_drop_down_ellipsis */ '
',
/** Custom added icons here: **/
'search' =>
'',
'close' =>
'',
'menu_icon_up' =>
'',
'menu_icon_down' =>
'',
'read_more' =>
'',
'play' =>
'',
);
/**
* Social Icons – domain mappings.
*
* By default, each Icon ID is matched against a .com TLD. To override this behavior,
* specify all the domains it covers (including the .com TLD too, if applicable).
*
* @var array
*/
static $social_icons_map = array(
'amazon' => array(
'amazon.com',
'amazon.cn',
'amazon.in',
'amazon.fr',
'amazon.de',
'amazon.it',
'amazon.nl',
'amazon.es',
'amazon.co',
'amazon.ca',
),
'apple' => array(
'apple.com',
'itunes.com',
),
'behance' => array(
'behance.net',
),
'codepen' => array(
'codepen.io',
),
'facebook' => array(
'facebook.com',
'fb.me',
),
'feed' => array(
'feed',
),
'google-plus' => array(
'plus.google.com',
),
'lastfm' => array(
'last.fm',
),
'mail' => array(
'mailto:',
),
'slideshare' => array(
'slideshare.net',
),
'pocket' => array(
'getpocket.com',
),
'twitch' => array(
'twitch.tv',
),
'wordpress' => array(
'wordpress.com',
'wordpress.org',
),
);
/**
* Social Icons – svg sources.
*
* @var array
*/
static $social_icons = array(
'500px' => '
',
'amazon' => '
',
'apple' => '
',
'bandcamp' => '
',
'behance' => '
',
'chain' => '
',
'codepen' => '
',
'deviantart' => '
',
'digg' => '
',
'dribbble' => '
',
'dropbox' => '
',
'etsy' => '
',
'facebook' => '
',
'feed' => '
',
'flickr' => '
',
'foursquare' => '
',
'goodreads' => '
',
'google-plus' => '
',
'google' => '
',
'github' => '
',
'instagram' => '
',
'lastfm' => '
',
'linkedin' => '
',
'mail' => '
',
'meetup' => '
',
'medium' => '
',
'pinterest' => '
',
'pocket' => '
',
'reddit' => '
',
'skype' => '
',
'slideshare' => '
',
'snapchat' => '
',
'soundcloud' => '
',
'spotify' => '
',
'stumbleupon' => '
',
'tumblr' => '
',
'twitch' => '
',
'twitter' => '
',
'vimeo' => '
',
'vk' => '
',
'wordpress' => '
',
'yelp' => '
',
'youtube' => '
',
);
}
/**
* SVG icons related functions
*
* @package WordPress
* @subpackage Twenty_Nineteen
* @since 1.0.0
*/
/**
* Gets the SVG code for a given icon.
*/
function blog_rider_get_icon_svg( $icon, $size = 24 ) {
return Blog_Rider_SVG_Icons::get_svg( 'ui', $icon, $size );
}
/**
* Gets the SVG code for a given social icon.
*/
function blog_rider_get_social_icon_svg( $icon, $size = 24 ) {
return Blog_Rider_SVG_Icons::get_svg( 'social', $icon, $size );
}
/**
* Detects the social network from a URL and returns the SVG code for its icon.
*/
function blog_rider_get_social_link_svg( $uri, $size = 24 ) {
return Blog_Rider_SVG_Icons::get_social_link_svg( $uri, $size );
}
/**
* Display SVG icons in social links menu.
*
* @param string $item_output The menu item output.
* @param WP_Post $item Menu item object.
* @param int $depth Depth of the menu.
* @param array $args wp_nav_menu() arguments.
* @return string $item_output The menu item output with social icon.
*/
function blog_rider_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
// Change SVG icon inside social links menu if there is supported URL.
if ( 'social' === $args->theme_location ) {
$svg = blog_rider_get_social_link_svg( $item->url, 26 );
if ( empty( $svg ) ) {
$svg = blog_rider_get_icon_svg( 'link' );
}
$item_output = str_replace( $args->link_after, '' . $svg, $item_output );
}
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'blog_rider_nav_menu_social_icons', 10, 4 );
/**
* Add dropdown icon if menu item has children.
*
* @param string $title The menu item's title.
* @param WP_Post $item The current menu item.
* @param array $args An array of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
* @return string $title The menu item's title with dropdown icon.
*/
function blog_rider_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
if ( 'primary' === $args->theme_location || 'top' === $args->theme_location ) {
foreach ( $item->classes as $value ) {
if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
$title = $title . blog_rider_get_icon_svg( 'menu_icon_down' );
}
}
}
return $title;
}
add_filter( 'nav_menu_item_title', 'blog_rider_dropdown_icon_to_menu_link', 10, 4 );