> */ private const TEXT_TAGS = [ 'strong' => [], 'em' => [], 's' => [], 'sub' => [], 'sup' => [], 'code' => [], 'br' => [], 'mark' => ['class' => true, 'style' => true], 'a' => ['href' => true, 'target' => true, 'rel' => true], ]; /** * Renders the block. * * @param array $attributes Block attributes. * @return string Block HTML, or '' when the block has no text. */ public function render(array $attributes): string { $hasLink = Link::has($attributes); $href = $hasLink ? Link::href($attributes) : ''; $tags = $href !== '' ? array_diff_key(self::TEXT_TAGS, ['a' => true]) : self::TEXT_TAGS; $text = trim(wp_kses(is_string($attributes['content'] ?? null) ? $attributes['content'] : '', $tags)); if ($text === '') { return ''; } $tag = Style::oneOf($attributes['tagName'] ?? '', self::TAGS) ?: 'p'; $iconHtml = Icons::svg($attributes['icon'] ?? '', 'wp-block-codexa-blocks-advanced-text__icon'); $atEnd = ($attributes['iconPosition'] ?? '') === 'end'; $meaning = is_string($attributes['iconLabel'] ?? null) ? trim(wp_strip_all_tags($attributes['iconLabel'])) : ''; $newTab = $href !== '' && ($attributes['linkOpensInNewTab'] ?? false) === true; $body = ($atEnd ? '' : $iconHtml) . '' . ($meaning !== '' && $iconHtml !== '' ? '' . esc_html($meaning) . ': ' : '') . $text // A new tab is announced, as screen readers don't say so on their own. . ($newTab ? ' ' . esc_html__('(opens in a new tab)', 'codexa-blocks') . '' : '') . '' . ($atEnd ? $iconHtml : ''); if ($href !== '') { $rel = Link::rel($attributes['linkRel'] ?? [], $newTab); $body = sprintf( '%4$s', esc_url($href, Link::PROTOCOLS), $newTab ? ' target="_blank"' : '', $rel !== '' ? ' rel="' . esc_attr($rel) . '"' : '', $body ); } else { $body = '' . $body . ''; } $styles = $this->styles($attributes); $hidden = DesignStyles::apply($styles, $attributes); $classes = array_filter([ $styles->classNames(), $iconHtml !== '' ? ($atEnd ? 'has-icon-end' : 'has-icon-start') : '', ...$hidden, ]); return sprintf( '<%1$s %2$s>%3$s', $tag, get_block_wrapper_attributes(array_filter([ 'class' => implode(' ', $classes), 'style' => $styles->style(), ]) + Anchor::wrapperAttributes($attributes)), $body ); } /** * The block's own settings as classes and variables. Mirrors * advancedTextStyles() in text-styles.js, which also adds the design * settings (DesignStyles) after these. * * @param array $attributes Block attributes. * @return ResponsiveStyles The block's styles. */ private function styles(array $attributes): ResponsiveStyles { $single = static fn(string $name): array => ['desktop' => $attributes[$name] ?? '']; $color = [Style::class, 'color']; $length = [Style::class, 'length']; $styles = (new ResponsiveStyles()) ->box('padding', $attributes['padding'] ?? [], $length) ->box('margin', $attributes['margin'] ?? [], $length) ->add('text-align', $attributes['textAlign'] ?? [], static fn($value): string => Style::oneOf($value, self::TEXT_ALIGNS)) ->add('advanced-text-hover-color', $single('textHoverColor'), $color) ->add('advanced-text-icon-color', $single('iconColor'), $color) ->add('advanced-text-icon-hover-color', $single('iconHoverColor'), $color) ->add('advanced-text-icon-size', $attributes['iconSize'] ?? [], $length) ->add('advanced-text-icon-gap', $attributes['iconGap'] ?? [], $length); TypographyStyles::apply($styles, 'advanced-text', $attributes['typography'] ?? null); return $styles; } }