2019-11-15 23:26:29 +01:00
< ? php
/**
* SVG icons related functions and filters
*
2021-04-27 08:32:47 +02:00
* @ package automattic / jetpack
2019-11-15 23:26:29 +01:00
*/
2025-02-28 08:42:11 +01:00
if ( ! function_exists ( 'jetpack_social_menu_include_svg_icons' ) ) {
2019-11-15 23:26:29 +01:00
/**
* Add SVG definitions to the footer .
*/
function jetpack_social_menu_include_svg_icons () {
2025-02-28 08:42:11 +01:00
_deprecated_function ( __FUNCTION__ , 'jetpack-13.7' );
2022-12-15 17:41:53 +01:00
// Return early if Social Menu doesn't exist.
if ( ! has_nav_menu ( 'jetpack-social-menu' ) ) {
return ;
}
2019-11-15 23:26:29 +01:00
// Define SVG sprite file.
2020-12-10 14:04:11 +01:00
$svg_icons = __DIR__ . '/social-menu.svg' ;
2022-12-15 17:41:53 +01:00
// If it exists and we use the SVG menu type, include it.
if ( file_exists ( $svg_icons ) && 'svg' === jetpack_social_menu_get_type () ) {
2025-02-28 08:42:11 +01:00
$svg_contents = file_get_contents ( $svg_icons ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Only reading a local file.
}
if ( ! empty ( $svg_contents ) ) {
$allowed_tags = array (
'svg' => array (
'style' => true ,
'version' => true ,
'xmlns' => true ,
'xmlns:xlink' => true ,
),
'defs' => array (),
'symbol' => array (
'id' => true ,
'viewbox' => true ,
),
'path' => array (
'd' => true ,
'style' => true ,
),
);
echo wp_kses ( $svg_contents , $allowed_tags );
2019-11-15 23:26:29 +01:00
}
}
add_action ( 'wp_footer' , 'jetpack_social_menu_include_svg_icons' , 9999 );
2025-02-28 08:42:11 +01:00
}
2019-11-15 23:26:29 +01:00
2025-02-28 08:42:11 +01:00
if ( ! function_exists ( 'jetpack_social_menu_get_svg' ) ) {
2019-11-15 23:26:29 +01:00
/**
* Return SVG markup .
*
* @ param array $args {
* Parameters needed to display an SVG .
*
* @ type string $icon Required SVG icon filename .
* }
* @ return string SVG markup .
*/
function jetpack_social_menu_get_svg ( $args = array () ) {
2025-02-28 08:42:11 +01:00
_deprecated_function ( __FUNCTION__ , 'jetpack-13.7' );
2019-11-15 23:26:29 +01:00
// Make sure $args are an array.
if ( empty ( $args ) ) {
return esc_html__ ( 'Please define default parameters in the form of an array.' , 'jetpack' );
}
// Define an icon.
if ( false === array_key_exists ( 'icon' , $args ) ) {
return esc_html__ ( 'Please define an SVG icon filename.' , 'jetpack' );
}
// Set defaults.
$defaults = array (
'icon' => '' ,
'fallback' => false ,
);
// Parse args.
$args = wp_parse_args ( $args , $defaults );
// Set aria hidden.
$aria_hidden = ' aria-hidden="true"' ;
// Begin SVG markup.
$svg = '<svg class="icon icon-' . esc_attr ( $args [ 'icon' ] ) . '"' . $aria_hidden . ' role="img">' ;
/*
* Display the icon .
*
* The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
*
* See https :// core . trac . wordpress . org / ticket / 38387.
*/
$svg .= ' <use href="#icon-' . esc_html ( $args [ 'icon' ] ) . '" xlink:href="#icon-' . esc_html ( $args [ 'icon' ] ) . '"></use> ' ;
// Add some markup to use as a fallback for browsers that do not support SVGs.
if ( $args [ 'fallback' ] ) {
$svg .= '<span class="svg-fallback icon-' . esc_attr ( $args [ 'icon' ] ) . '"></span>' ;
}
$svg .= '</svg>' ;
return $svg ;
}
2025-02-28 08:42:11 +01:00
}
2019-11-15 23:26:29 +01:00
2025-02-28 08:42:11 +01:00
if ( ! function_exists ( 'jetpack_social_menu_nav_menu_social_icons' ) ) {
2019-11-15 23:26:29 +01:00
/**
* 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 jetpack_social_menu_nav_menu_social_icons ( $item_output , $item , $depth , $args ) {
2025-02-28 08:42:11 +01:00
_deprecated_function ( __FUNCTION__ , 'jetpack-13.7' );
2019-11-15 23:26:29 +01:00
// Get supported social icons.
$social_icons = jetpack_social_menu_social_links_icons ();
// Change SVG icon inside social links menu if there is supported URL.
if ( 'jetpack-social-menu' === $args -> theme_location ) {
foreach ( $social_icons as $attr => $value ) {
2023-04-26 17:39:43 +02:00
/*
* attr can be a URL host , or a regex , starting with #.
* Let ' s check for both scenarios .
*/
if (
// First Regex.
(
2023-12-07 09:44:11 +01:00
str_starts_with ( $attr , '#' ) && str_ends_with ( $attr , '#' )
2023-04-26 17:39:43 +02:00
&& preg_match ( $attr , $item_output )
)
// Then, regular host name.
2023-12-07 09:44:11 +01:00
|| str_contains ( $item_output , $attr )
2023-04-26 17:39:43 +02:00
) {
$item_output = str_replace (
$args -> link_after ,
'</span>' . jetpack_social_menu_get_svg ( array ( 'icon' => esc_attr ( $value ) ) ),
$item_output
);
2019-11-15 23:26:29 +01:00
}
}
}
return $item_output ;
}
add_filter ( 'walker_nav_menu_start_el' , 'jetpack_social_menu_nav_menu_social_icons' , 10 , 4 );
2025-02-28 08:42:11 +01:00
}
2019-11-15 23:26:29 +01:00
2025-02-28 08:42:11 +01:00
if ( ! function_exists ( 'jetpack_social_menu_social_links_icons' ) ) {
2019-11-15 23:26:29 +01:00
/**
2023-04-26 17:39:43 +02:00
* Returns an array of supported social links ( URL / regex and icon name ) .
* For regex , use the # delimiter.
2019-11-15 23:26:29 +01:00
*
* @ return array $social_links_icons
*/
function jetpack_social_menu_social_links_icons () {
2025-02-28 08:42:11 +01:00
_deprecated_function ( __FUNCTION__ , 'jetpack-13.7' );
2019-11-15 23:26:29 +01:00
// Supported social links icons.
$social_links_icons = array (
2023-04-26 17:39:43 +02:00
'#https?:\/\/(www\.)?amazon\.(com|cn|in|fr|de|it|nl|es|co|ca)\/#' => 'amazon' ,
2019-11-15 23:26:29 +01:00
'500px.com' => '500px' ,
'apple.com' => 'apple' ,
'itunes.com' => 'apple' ,
'bandcamp.com' => 'bandcamp' ,
'behance.net' => 'behance' ,
2021-04-27 08:32:47 +02:00
'blogger.com' => 'blogger' ,
'blogspot.com' => 'blogger' ,
2024-02-16 11:03:01 +01:00
'bsky.app' => 'bluesky' ,
2019-11-15 23:26:29 +01:00
'codepen.io' => 'codepen' ,
'deviantart.com' => 'deviantart' ,
'discord.gg' => 'discord' ,
'discordapp.com' => 'discord' ,
'digg.com' => 'digg' ,
'dribbble.com' => 'dribbble' ,
'dropbox.com' => 'dropbox' ,
'etsy.com' => 'etsy' ,
2021-04-27 08:32:47 +02:00
'eventbrite.com' => 'eventbrite' ,
2019-11-15 23:26:29 +01:00
'facebook.com' => 'facebook' ,
'/feed/' => 'feed' ,
'flickr.com' => 'flickr' ,
'foursquare.com' => 'foursquare' ,
2021-04-27 08:32:47 +02:00
'ghost.org' => 'ghost' ,
2019-11-15 23:26:29 +01:00
'goodreads.com' => 'goodreads' ,
'google.com' => 'google' ,
'github.com' => 'github' ,
'instagram.com' => 'instagram' ,
'linkedin.com' => 'linkedin' ,
'mailto:' => 'mail' ,
'meetup.com' => 'meetup' ,
'medium.com' => 'medium' ,
2023-09-26 10:24:36 +02:00
'nextdoor.com' => 'nextdoor' ,
2021-04-27 08:32:47 +02:00
'patreon.com' => 'patreon' ,
2019-11-15 23:26:29 +01:00
'pinterest.' => 'pinterest' ,
'getpocket.com' => 'pocket' ,
2020-05-06 17:20:49 +02:00
'ravelry.com' => 'ravelry' ,
2019-11-15 23:26:29 +01:00
'reddit.com' => 'reddit' ,
'slideshare.net' => 'slideshare' ,
2024-04-17 11:32:24 +02:00
'sms:' => 'sms' ,
2019-11-15 23:26:29 +01:00
'snapchat.com' => 'snapchat' ,
'soundcloud.com' => 'soundcloud' ,
'spotify.com' => 'spotify' ,
'stackoverflow.com' => 'stackoverflow' ,
2022-04-02 10:26:41 +02:00
'strava.com' => 'strava' ,
2019-11-15 23:26:29 +01:00
'stumbleupon.com' => 'stumbleupon' ,
2020-11-18 09:10:44 +01:00
'telegram.me' => 'telegram' ,
2023-09-26 10:24:36 +02:00
'threads.net' => 'threads' ,
2021-04-27 08:32:47 +02:00
'tiktok.com' => 'tiktok' ,
2019-11-15 23:26:29 +01:00
'tumblr.com' => 'tumblr' ,
'twitch.tv' => 'twitch' ,
'twitter.com' => 'twitter' ,
'vimeo.com' => 'vimeo' ,
'vk.com' => 'vk' ,
2021-04-27 08:32:47 +02:00
'whatsapp.com' => 'whatsapp' ,
'woocommerce.com' => 'woocommerce' ,
2019-11-15 23:26:29 +01:00
'wordpress.org' => 'wordpress' ,
'wordpress.com' => 'wordpress' ,
'yelp.com' => 'yelp' ,
2023-12-07 09:44:11 +01:00
'x.com' => 'x' ,
2021-04-27 08:32:47 +02:00
'xanga.com' => 'xanga' ,
2019-11-15 23:26:29 +01:00
'youtube.com' => 'youtube' ,
);
2023-04-26 17:39:43 +02:00
/*
* Add Mastodon instances to this array .
*/
$mastodon_instance_list = jetpack_mastodon_get_instance_list ();
foreach ( $mastodon_instance_list as $instance ) {
$social_links_icons [ $instance ] = 'mastodon' ;
}
2019-11-15 23:26:29 +01:00
return $social_links_icons ;
}
2025-02-28 08:42:11 +01:00
}