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.
*/
class Icons {
/**
* Gets the SVG code for a given icon.
*
* @param string $group Icon group.
* @param string $icon Icon key.
* @param int $size Icon size.
* @return string
*/
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.
*
* @param string $uri Social link uri.
* @param int $size Icon size.
* @return string
*/
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' => '',
'menu' => '',
'close' => '',
'chevron_down' => '',
'search' => '',
'calendar' => '',
'user' => '',
'category' => '',
'tag' => '',
'bookmark' => '',
'comment' => '',
);
/**
* 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
*/
public 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',
),
'behance' => array(
'behance.net',
),
'codepen' => array(
'codepen.io',
),
'discord' => array(
'discord.com',
'discord.gg',
),
'facebook' => array(
'facebook.com',
'fb.me',
),
'feed' => array(
'feed',
),
'google' => array(
'g.page',
),
'kbin' => array(
'kbin.social',
),
'lastfm' => array(
'last.fm',
),
'lemmy' => array(
'lemmy.world',
'join-lemmy.org',
),
'mail' => array(
'mailto:',
),
'mastodon' => array(
'mastodon.world',
'mastodon.social',
),
'pocket' => array(
'getpocket.com',
),
'threads' => array(
'threads.net',
),
'tiktok' => array(
'tiktok.com',
),
'twitch' => array(
'twitch.tv',
),
'twitter' => array(
'twitter.com',
'x.com',
),
'wordpress' => array(
'wordpress.com',
'wordpress.org',
),
);
/**
* Social Icons – svg sources.
*
* @var array
*/
public static $social_icons = array(
'500px' => '',
'amazon' => '',
'bandcamp' => '',
'behance' => '',
'codepen' => '',
'deviantart' => '',
'discord' => '',
'dribbble' => '',
'dropbox' => '',
'etsy' => '',
'facebook' => '',
'feed' => '',
'flickr' => '',
'foursquare' => '',
'goodreads' => '',
'google' => '',
'github' => '',
'instagram' => '',
'kbin' => '',
'lastfm' => '',
'lemmy' => '',
'linkedin' => '',
'mail' => '',
'mastodon' => '',
'medium' => '',
'meetup' => '',
'pinterest' => '',
'pocket' => '',
'reddit' => '',
'skype' => '',
'snapchat' => '',
'soundcloud' => '',
'spotify' => '',
'threads' => '',
'tiktok' => '',
'tumblr' => '',
'twitch' => '',
'twitter' => '',
'vimeo' => '',
'vk' => '',
'whatsapp' => '',
// phpcs:disable WordPress.WP.CapitalPDangit.Misspelled
'wordpress' => '',
'yelp' => '',
'youtube' => '',
);
}
}