context['openInNewTab'] ) ? $block->context['openInNewTab'] : false;
$text = ! empty( $attributes['label'] ) ? trim( $attributes['label'] ) : '';
$service = isset( $attributes['service'] ) ? $attributes['service'] : 'Icon';
$url = isset( $attributes['url'] ) ? $attributes['url'] : false;
$text = $text ? $text : block_core_social_link_get_name( $service );
$rel = isset( $attributes['rel'] ) ? $attributes['rel'] : '';
$show_labels = array_key_exists( 'showLabels', $block->context ) ? $block->context['showLabels'] : false;
// Don't render a link if there is no URL set.
if ( ! $url ) {
return '';
}
/**
* Prepend emails with `mailto:` if not set.
* The `is_email` returns false for emails with schema.
*/
if ( is_email( $url ) ) {
$url = 'mailto:' . antispambot( $url );
}
/**
* Prepend URL with https:// if it doesn't appear to contain a scheme
* and it's not a relative link or a fragment.
*/
if ( ! parse_url( $url, PHP_URL_SCHEME ) && ! str_starts_with( $url, '//' ) && ! str_starts_with( $url, '#' ) ) {
$url = 'https://' . $url;
}
$icon = block_core_social_link_get_icon( $service );
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => 'wp-social-link wp-social-link-' . $service . block_core_social_link_get_color_classes( $block->context ),
'style' => block_core_social_link_get_color_styles( $block->context ),
)
);
$link = '
';
$link .= '';
$link .= $icon;
$link .= '' . esc_html( $text ) . '';
$link .= '';
$processor = new WP_HTML_Tag_Processor( $link );
$processor->next_tag( 'a' );
if ( $open_in_new_tab ) {
$processor->set_attribute( 'rel', trim( $rel . ' noopener nofollow' ) );
$processor->set_attribute( 'target', '_blank' );
} elseif ( '' !== $rel ) {
$processor->set_attribute( 'rel', trim( $rel ) );
}
return $processor->get_updated_html();
}
/**
* Registers the `core/social-link` blocks.
*
* @since 5.4.0
*/
function register_block_core_social_link() {
register_block_type_from_metadata(
__DIR__ . '/social-link',
array(
'render_callback' => 'render_block_core_social_link',
)
);
}
add_action( 'init', 'register_block_core_social_link' );
/**
* Returns the SVG for social link.
*
* @since 5.4.0
*
* @param string $service The service icon.
*
* @return string SVG Element for service icon.
*/
function block_core_social_link_get_icon( $service ) {
$services = block_core_social_link_services();
if ( isset( $services[ $service ] ) && isset( $services[ $service ]['icon'] ) ) {
return $services[ $service ]['icon'];
}
return $services['share']['icon'];
}
/**
* Returns the brand name for social link.
*
* @since 5.4.0
*
* @param string $service The service icon.
*
* @return string Brand label.
*/
function block_core_social_link_get_name( $service ) {
$services = block_core_social_link_services();
if ( isset( $services[ $service ] ) && isset( $services[ $service ]['name'] ) ) {
return $services[ $service ]['name'];
}
return $services['share']['name'];
}
/**
* Returns the SVG for social link.
*
* @since 5.4.0
*
* @param string $service The service slug to extract data from.
* @param string $field The field ('name', 'icon', etc) to extract for a service.
*
* @return array|string
*/
function block_core_social_link_services( $service = '', $field = '' ) {
$services_data = array(
'fivehundredpx' => array(
'name' => _x( '500px', 'social link block variation name' ),
'icon' => '',
),
'amazon' => array(
'name' => _x( 'Amazon', 'social link block variation name' ),
'icon' => '',
),
'bandcamp' => array(
'name' => _x( 'Bandcamp', 'social link block variation name' ),
'icon' => '',
),
'behance' => array(
'name' => _x( 'Behance', 'social link block variation name' ),
'icon' => '',
),
'bluesky' => array(
'name' => _x( 'Bluesky', 'social link block variation name' ),
'icon' => '',
),
'chain' => array(
'name' => _x( 'Link', 'social link block variation name' ),
'icon' => '',
),
'codepen' => array(
'name' => _x( 'CodePen', 'social link block variation name' ),
'icon' => '',
),
'deviantart' => array(
'name' => _x( 'DeviantArt', 'social link block variation name' ),
'icon' => '',
),
'discord' => array(
'name' => _x( 'Discord', 'social link block variation name' ),
'icon' => '',
),
'dribbble' => array(
'name' => _x( 'Dribbble', 'social link block variation name' ),
'icon' => '',
),
'dropbox' => array(
'name' => _x( 'Dropbox', 'social link block variation name' ),
'icon' => '',
),
'etsy' => array(
'name' => _x( 'Etsy', 'social link block variation name' ),
'icon' => '',
),
'facebook' => array(
'name' => _x( 'Facebook', 'social link block variation name' ),
'icon' => '',
),
'feed' => array(
'name' => _x( 'RSS Feed', 'social link block variation name' ),
'icon' => '',
),
'flickr' => array(
'name' => _x( 'Flickr', 'social link block variation name' ),
'icon' => '',
),
'foursquare' => array(
'name' => _x( 'Foursquare', 'social link block variation name' ),
'icon' => '',
),
'goodreads' => array(
'name' => _x( 'Goodreads', 'social link block variation name' ),
'icon' => '',
),
'google' => array(
'name' => _x( 'Google', 'social link block variation name' ),
'icon' => '',
),
'github' => array(
'name' => _x( 'GitHub', 'social link block variation name' ),
'icon' => '',
),
'gravatar' => array(
'name' => _x( 'Gravatar', 'social link block variation name' ),
'icon' => '',
),
'instagram' => array(
'name' => _x( 'Instagram', 'social link block variation name' ),
'icon' => '',
),
'lastfm' => array(
'name' => _x( 'Last.fm', 'social link block variation name' ),
'icon' => '',
),
'linkedin' => array(
'name' => _x( 'LinkedIn', 'social link block variation name' ),
'icon' => '',
),
'mail' => array(
'name' => _x( 'Mail', 'social link block variation name' ),
'icon' => '',
),
'mastodon' => array(
'name' => _x( 'Mastodon', 'social link block variation name' ),
'icon' => '',
),
'meetup' => array(
'name' => _x( 'Meetup', 'social link block variation name' ),
'icon' => '',
),
'medium' => array(
'name' => _x( 'Medium', 'social link block variation name' ),
'icon' => '',
),
'patreon' => array(
'name' => _x( 'Patreon', 'social link block variation name' ),
'icon' => '',
),
'pinterest' => array(
'name' => _x( 'Pinterest', 'social link block variation name' ),
'icon' => '',
),
'pocket' => array(
'name' => _x( 'Pocket', 'social link block variation name' ),
'icon' => '',
),
'reddit' => array(
'name' => _x( 'Reddit', 'social link block variation name' ),
'icon' => '',
),
'share' => array(
'name' => _x( 'Share Icon', 'social link block variation name' ),
'icon' => '',
),
'skype' => array(
'name' => _x( 'Skype', 'social link block variation name' ),
'icon' => '',
),
'snapchat' => array(
'name' => _x( 'Snapchat', 'social link block variation name' ),
'icon' => '',
),
'soundcloud' => array(
'name' => _x( 'SoundCloud', 'social link block variation name' ),
'icon' => '',
),
'spotify' => array(
'name' => _x( 'Spotify', 'social link block variation name' ),
'icon' => '',
),
'telegram' => array(
'name' => _x( 'Telegram', 'social link block variation name' ),
'icon' => '',
),
'threads' => array(
'name' => _x( 'Threads', 'social link block variation name' ),
'icon' => '',
),
'tiktok' => array(
'name' => _x( 'TikTok', 'social link block variation name' ),
'icon' => '',
),
'tumblr' => array(
'name' => _x( 'Tumblr', 'social link block variation name' ),
'icon' => '',
),
'twitch' => array(
'name' => _x( 'Twitch', 'social link block variation name' ),
'icon' => '',
),
'twitter' => array(
'name' => _x( 'Twitter', 'social link block variation name' ),
'icon' => '',
),
'vimeo' => array(
'name' => _x( 'Vimeo', 'social link block variation name' ),
'icon' => '',
),
'vk' => array(
'name' => _x( 'VK', 'social link block variation name' ),
'icon' => '',
),
'wordpress' => array(
'name' => _x( 'WordPress', 'social link block variation name' ),
'icon' => '',
),
'whatsapp' => array(
'name' => _x( 'WhatsApp', 'social link block variation name' ),
'icon' => '',
),
'x' => array(
'name' => _x( 'X', 'social link block variation name' ),
'icon' => '',
),
'yelp' => array(
'name' => _x( 'Yelp', 'social link block variation name' ),
'icon' => '',
),
'youtube' => array(
'name' => _x( 'YouTube', 'social link block variation name' ),
'icon' => '',
),
);
/**
* Filter the list of available social service.
*
* This can be used to change icons or add custom icons (additionally to variations in the editor).
* Icons should be directly renderable - therefore SVGs work best.
*
* @since 6.9.0
*
* @param array $services_data The list of services. Each item is an array containing a 'name' and 'icon' key.
* @return array The list of social services.
*/
$services_data = apply_filters( 'block_core_social_link_get_services', $services_data );
if ( ! empty( $service )
&& ! empty( $field )
&& isset( $services_data[ $service ] )
&& ( 'icon' === $field || 'name' === $field )
) {
return $services_data[ $service ][ $field ];
} elseif ( ! empty( $service ) && isset( $services_data[ $service ] ) ) {
return $services_data[ $service ];
}
return $services_data;
}
/**
* Returns CSS styles for icon and icon background colors.
*
* @since 5.7.0
*
* @param array $context Block context passed to Social Link.
*
* @return string Inline CSS styles for link's icon and background colors.
*/
function block_core_social_link_get_color_styles( $context ) {
$styles = array();
if ( array_key_exists( 'iconColorValue', $context ) ) {
$styles[] = 'color:' . $context['iconColorValue'] . ';';
}
if ( array_key_exists( 'iconBackgroundColorValue', $context ) ) {
$styles[] = 'background-color:' . $context['iconBackgroundColorValue'] . ';';
}
return implode( '', $styles );
}
/**
* Returns CSS classes for icon and icon background colors.
*
* @since 6.3.0
*
* @param array $context Block context passed to Social Sharing Link.
*
* @return string CSS classes for link's icon and background colors.
*/
function block_core_social_link_get_color_classes( $context ) {
$classes = array();
if ( array_key_exists( 'iconColor', $context ) ) {
$classes[] = 'has-' . $context['iconColor'] . '-color';
}
if ( array_key_exists( 'iconBackgroundColor', $context ) ) {
$classes[] = 'has-' . $context['iconBackgroundColor'] . '-background-color';
}
return ' ' . implode( ' ', $classes );
}