* before
* fancy
* after
*
*
* @param array $attrs Block attributes.
* @param string $content Block content (unused).
* @param object $block Parsed block.
* @param object $elements Module elements helper.
*
* @return string
*/
public static function render_callback($attrs, $content, $block, $elements)
{
$advanced = $attrs['module']['advanced'] ?? [];
// An empty value falls back, so a cleared Fancy Text prints "Fancy".
// Mirrored by fancyValue() in wrapper-class.js for the builder.
$val = function ($key, $fallback = '') use ($advanced) {
$v = $advanced[$key]['desktop']['value'] ?? '';
return '' !== $v ? $v : $fallback;
};
$effect = $val('effect', 'highlight');
$before = $val('beforeText', '');
$after = $val('afterText', '');
// Rotating words.
//
// Pipes split as well as newlines. The builder control is a textarea, so
// a person typing into it produces newlines — but layout packs, child
// themes and imports write the attribute directly and reach for a pipe.
// Splitting on newlines alone turned "a|b|c" into one long "rotating"
// word with no error anywhere, which reads as a broken font rather than
// a data problem. Mirrored in fancyStrings() in wrapper-class.js.
$strings = array_values(array_filter(array_map(
'trim',
preg_split('/\r\n|\r|\n|\|/', (string) $val('rotateStrings', ''))
), function ($s) {
return '' !== $s;
}));
$fancy = 'rotate' === $effect ? ($strings[0] ?? 'Fancy') : $val('fancyText', 'Fancy');
// Tag (whitelisted).
$tag = $val('tag', 'h2');
if (!in_array($tag, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span'], true)) {
$tag = 'h2';
}
// Classes (mirrors wrapper-class.js).
$classes = ['dtq-fancy', 'dtq-fancy--effect-' . $effect];
if ('highlight' === $effect) {
$classes[] = 'dtq-fancy--hl-' . $val('highlightStyle', 'marker');
}
if ('rotate' === $effect) {
$classes[] = 'dtq-fancy--anim-' . $val('rotateAnimation', 'fade-up');
}
$before_html = '' !== $before ? sprintf('%1$s ', esc_html($before)) : '';
$after_html = '' !== $after ? sprintf(' %1$s', esc_html($after)) : '';
$fancy_html = sprintf(
'<%1$s class="%2$s" data-dtq-effect="%3$s" data-dtq-anim="%4$s" data-dtq-speed="%5$s" data-dtq-strings="%6$s">%7$s%8$s%9$s%1$s>',
$tag,
esc_attr(implode(' ', $classes)),
esc_attr($effect),
esc_attr($val('rotateAnimation', 'fade-up')),
esc_attr($val('rotateSpeed', '2000')),
esc_attr(wp_json_encode($strings)),
$before_html,
esc_html($fancy),
$after_html
);
return Module::render(
[
'orderIndex' => $block->parsed_block['orderIndex'] ?? 0,
'storeInstance' => $block->parsed_block['storeInstance'] ?? null,
'id' => $block->parsed_block['id'],
'name' => $block->block_type->name,
'moduleCategory' => $block->block_type->category,
'attrs' => $attrs,
'elements' => $elements,
'classnamesFunction' => [self::class, 'module_classnames'],
'stylesComponent' => [self::class, 'module_styles'],
'scriptDataComponent' => [self::class, 'module_script_data'],
'children' => [
$elements->style_components(['attrName' => 'module']),
$fancy_html,
],
]
);
}
}