in_array($placement, self::PLACEMENTS, true) ? $placement : 'top',
'animation' => in_array($animation, self::ANIMATIONS, true) ? $animation : 'fade',
'trigger' => $trigger,
'arrow' => 'off' !== ($advanced['arrow']['desktop']['value'] ?? 'on'),
'interactive' => 'off' !== ($advanced['interactive']['desktop']['value'] ?? 'on'),
// Gives the pointer somewhere to cross on the way to the tooltip;
// without it an interactive tooltip closes before it is reached.
'interactiveBorder' => 8,
'delay' => [
self::to_ms($advanced['delayIn']['desktop']['value'] ?? '100ms', 100),
self::to_ms($advanced['delayOut']['desktop']['value'] ?? '0ms', 0),
],
'offset' => [0, self::to_ms($advanced['offsetDistance']['desktop']['value'] ?? '10px', 10)],
'maxWidth' => self::to_ms($advanced['maxWidth']['desktop']['value'] ?? '300px', 300),
];
}
/**
* Build the wrapper class list.
*
* PHP twin of src/divi5/modules/tooltip/wrapper-class.js.
*
* @param array $advanced The `module.advanced` attrs array.
*
* @return string
*/
public static function wrapper_class($advanced)
{
$trigger_type = $advanced['triggerType']['desktop']['value'] ?? 'text';
$alignment = $advanced['alignment']['desktop']['value'] ?? 'center';
if (!in_array($trigger_type, self::TRIGGER_TYPES, true)) {
$trigger_type = 'text';
}
if (!in_array($alignment, self::ALIGNMENTS, true)) {
$alignment = 'center';
}
return implode(' ', [
'dtq-module',
'dtq-tipbox',
// Marks this as Divi 5 markup so the site-wide Divi 4 frontend.js
// leaves it alone.
'dtq-d5',
'dtq-tipbox--trigger-' . $trigger_type,
'dtq-tipbox--align-' . $alignment,
]);
}
/**
* Server-side render for the Tooltip module.
*
* @param array $attrs Block attributes.
* @param string $content Block content (unused; no children).
* @param WP_Block $block Parsed block.
* @param ModuleElements $elements Module elements helper.
*
* @return string
*/
public static function render_callback($attrs, $content, $block, $elements)
{
$advanced = $attrs['module']['advanced'] ?? [];
$trigger_type = $advanced['triggerType']['desktop']['value'] ?? 'text';
if (!in_array($trigger_type, self::TRIGGER_TYPES, true)) {
$trigger_type = 'text';
}
// The libraries load only on pages that actually use a tooltip. Handles
// are registered in includes/assets.php; popper must come before tippy,
// whose UMD build reads window.Popper at parse time.
wp_enqueue_script('divi-torque-lite-popper');
wp_enqueue_script('divi-torque-lite-tippy');
wp_enqueue_style('divi-torque-lite-tippy');
// Trigger contents.
$trigger_inner = '';
if ('icon' === $trigger_type) {
// Divi 5 stores an icon as an object; Divi 4 stored it as
// "unicode||type||weight". Normalise to the string form, because
// that is what dtq_inject_fa_icons and Divi's own
// et_pb_maybe_fa_font_icon expect — handing them the array is an
// uncaught TypeError inside Divi's icon manager, which takes the
// whole page down.
$icon_raw = $advanced['triggerIcon']['desktop']['value'] ?? '';
if (is_array($icon_raw)) {
$unicode = $icon_raw['unicode'] ?? '';
$type = $icon_raw['type'] ?? 'divi';
$weight = $icon_raw['weight'] ?? '400';
} else {
$parts = explode('||', (string) $icon_raw);
$unicode = $parts[0] ?? '';
$type = $parts[1] ?? 'divi';
$weight = $parts[2] ?? '400';
}
$glyph = dtq_resolve_icon_unicode($unicode);
if ('' !== $glyph) {
if (function_exists('dtq_inject_fa_icons')) {
dtq_inject_fa_icons($unicode . '||' . $type . '||' . $weight);
}
$trigger_inner = sprintf(
'%3$s',
'fa' === $type ? 'FontAwesome' : 'ETmodules',
esc_attr($weight),
$glyph
);
}
} elseif ('image' === $trigger_type) {
$image = $advanced['triggerImage']['desktop']['value'] ?? '';
$src = is_array($image) ? ($image['src'] ?? '') : $image;
if ('' !== $src) {
$trigger_inner = sprintf(
'',
esc_url($src),
esc_attr($advanced['triggerImageAlt']['desktop']['value'] ?? '')
);
}
} else {
$trigger_inner = $elements->render(['attrName' => 'triggerText']);
}
$config = wp_json_encode(self::tippy_config($advanced));
$children = sprintf(
'