Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks

Social_Links::generate_social_link_contentprivateWC 1.0

Generates the social link content.

Метод класса: Social_Links{}

Хуков нет.

Возвращает

Строку. The generated content.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->generate_social_link_content( $block, $parent_block_attrs );
$block(массив) (обязательный)
The block data.
$parent_block_attrs(массив) (обязательный)
The parent block attributes.

Код Social_Links::generate_social_link_content() WC 10.3.5

private function generate_social_link_content( $block, $parent_block_attrs ) {
	$service_name = $block['attrs']['service'] ?? '';
	$service_url  = $block['attrs']['url'] ?? '';
	$label        = $block['attrs']['label'] ?? '';

	if ( empty( $service_name ) || empty( $service_url ) ) {
		return '';
	}

	/**
	 * Prepend emails with `mailto:` if not set.
	 * The `is_email` returns false for emails with schema.
	 */
	if ( is_email( $service_url ) ) {
		$service_url = 'mailto:' . antispambot( $service_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 ( ! wp_parse_url( $service_url, PHP_URL_SCHEME ) && ! str_starts_with( $service_url, '//' ) && ! str_starts_with( $service_url, '#' ) ) {
		$service_url = 'https://' . $service_url;
	}

	$open_in_new_tab = $parent_block_attrs['openInNewTab'] ?? false;
	$show_labels     = $parent_block_attrs['showLabels'] ?? false;
	$size            = $parent_block_attrs['size'] ?? Social_Links_Helper::get_default_social_link_size();

	$service_brand_color = Social_Links_Helper::get_service_brand_color( $service_name );

	$icon_color_value            = $parent_block_attrs['iconColorValue'] ?? '#ffffff'; // use white as default icon color.
	$icon_background_color_value = $parent_block_attrs['iconBackgroundColorValue'] ?? '';

	$is_logos_only = strpos( $parent_block_attrs['className'] ?? '', 'is-style-logos-only' ) !== false;
	$is_pill_shape = strpos( $parent_block_attrs['className'] ?? '', 'is-style-pill-shape' ) !== false;

	if ( ! $is_logos_only && Social_Links_Helper::detect_whiteish_color( $icon_color_value ) && ( Social_Links_Helper::detect_whiteish_color( $icon_background_color_value ) || empty( $icon_background_color_value ) ) ) {
		// If the icon color is white and the background color is white or empty, use the service brand color for the icon background color.
		$icon_background_color_value = ! empty( $service_brand_color ) ? $service_brand_color : '#000';
	}

	if ( $is_logos_only ) {
		// logos only mode does not need background color. We also don't really need the icon color (we can't change png image color anyways).
		// We set it so that the label text color will reflect the service brand color.
		$icon_color_value = ! empty( $service_brand_color ) ? $service_brand_color : '#000';
	}

	$icon_size = Social_Links_Helper::get_social_link_size_option_value( $size );

	$service_icon_url = $this->get_service_icon_url( $service_name, $is_logos_only ? 'brand' : 'white' );

	$service_label = '';
	if ( $show_labels ) {
		$text          = ! empty( $label ) ? trim( $label ) : '';
		$service_label = $text ? $text : block_core_social_link_get_name( $service_name );
	}

	$main_table_styles = $this->compile_css(
		array(
			'background-color' => $icon_background_color_value,
			'border-radius'    => '9999px',
			'display'          => 'inline-table',
			'float'            => 'none',
		)
	);

	// divide the icon value by 2 to get the font size.
	$font_size_value = (int) rtrim( $icon_size, 'px' );
	$font_size       = ( $font_size_value / 2 ) + 1; // inline with core styles.
	$text_font_size  = "{$font_size}px";
	$anchor_styles   = $this->compile_css(
		array(
			'color'           => $icon_color_value,
			'text-decoration' => 'none',
			'text-transform'  => 'none',
			'font-size'       => $text_font_size,
		)
	);

	$anchor_html = sprintf( ' style="%s" ', esc_attr( $anchor_styles ) );
	if ( $open_in_new_tab ) {
		$anchor_html .= ' rel="noopener nofollow" target="_blank" ';
	}

	$row_container_styles = array(
		'display' => 'block',
		'padding' => '0.25em',
	);

	if ( $is_pill_shape ) {
		$row_container_styles['padding-left']  = '17px';
		$row_container_styles['padding-right'] = '17px';
	}
	$row_container_styles = $this->compile_css( $row_container_styles );

	// Generate the icon content.
	$icon_content = sprintf(
		'<a href="%1$s" %2$s class="wp-block-social-link-anchor">
			<img height="%3$s" src="%4$s" style="display:block;margin-right:0;" width="%3$s" alt="%5$s">
		</a>',
		esc_url( $service_url ),
		$anchor_html,
		esc_attr( $icon_size ),
		esc_url( $service_icon_url ),
		// translators: %s is the social service name.
		sprintf( __( '%s icon', 'woocommerce' ), $service_name )
	);
	$icon_content = Table_Wrapper_Helper::render_table_wrapper( $icon_content, array(), array( 'style' => 'vertical-align:middle;' ) );
	$icon_content = Table_Wrapper_Helper::render_table_cell( $icon_content, array( 'style' => sprintf( 'vertical-align:middle;font-size:%s;', $text_font_size ) ) );

	// Generate the label content if needed.
	$label_content = '';
	if ( $service_label ) {
		$label_content    = sprintf(
			'<a href="%1$s" %2$s class="wp-block-social-link-anchor">
				<span style="margin-left:.5em;margin-right:.5em"> %3$s </span>
			</a>',
			esc_url( $service_url ),
			$anchor_html,
			esc_html( $service_label )
		);
		$label_cell_style = sprintf(
			'vertical-align:middle;padding-left:6px;padding-right:6px;font-size:%s;',
			$text_font_size
		);
		$label_content    = Table_Wrapper_Helper::render_table_cell( $label_content, array( 'style' => $label_cell_style ) );
	}

	// Combine icon and label tables.
	$social_link_content = $icon_content . $label_content;

	// Create the main social link table.
	$main_table_attrs = array(
		'align' => 'center',
		'style' => $main_table_styles,
	);

	$main_row_attrs = array(
		'style' => $row_container_styles,
	);

	$main_table = Table_Wrapper_Helper::render_table_wrapper( $social_link_content, $main_table_attrs, array(), $main_row_attrs, false );

	return Table_Wrapper_Helper::render_outlook_table_cell( $main_table );
}