File "social-link-20241222183726.php"

Full Path: /home/ycoalition/public_html/blog/wp-includes/blocks/group/social-link-20241222183726.php
File size: 4 KB
MIME-type: text/x-php
Charset: utf-8

<?php
/**
 * Server-side rendering of the `core/social-link` blocks.
 *
 * @package WordPress
 */

/**
 * Renders the `core/social-link` block on server.
 *
 * @since 5.4.0
 *
 * @param Array    $attributes The block attributes.
 * @param String   $content    InnerBlocks content of the Block.
 * @param WP_Block $block      Block object.
 *
 * @return string Rendered HTML of the referenced block.
 */
function render_block_core_social_link( $attributes, $content, $block ) {
	$open_in_new_tab = isset( $block->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 starting with //.
	 */
	if ( ! parse_url( $url, PHP_URL_SCHEME ) && ! 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  = '<li ' . $wrapper_attributes . '>';
	$link .= '<a href="' . esc_url( $url ) . '" class="wp-block-social-link-anchor">';
	$link .= $icon;
	$link .= '<span class="wp-block-social-link-label' . ( $show_labels ? '' : ' screen-reader-text' ) . '">' . esc_html( $text ) . '</span>';
	$link .= '</a></li>';

	$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(