* [trigger button]
*
*
*
* @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'] ?? [];
// Mirrors src/divi5/modules/modal-popup/wrapper-class.js (builder=false).
$size = $advanced['size']['desktop']['value'] ?? 'medium';
$position = $advanced['position']['desktop']['value'] ?? 'center';
$animation = $advanced['animation']['desktop']['value'] ?? 'fade';
$trigger_align = $advanced['triggerAlign']['desktop']['value'] ?? 'left';
$close_shape = $advanced['closeShape']['desktop']['value'] ?? 'plain';
$close_position = $advanced['closePosition']['desktop']['value'] ?? 'inside';
$wrapper_class = implode(
' ',
[
'dtq-module',
'dtq-modalpopup',
'dtq-modalpopup--size-' . $size,
'dtq-modalpopup--pos-' . $position,
'dtq-modalpopup--anim-' . $animation,
'dtq-modalpopup--trig-' . $trigger_align,
'dtq-modalpopup--close-' . $close_shape,
'dtq-modalpopup--close-' . $close_position,
]
);
$show_close = ($advanced['showClose']['desktop']['value'] ?? 'on') === 'on';
$close_on_overlay = ($advanced['closeOnOverlay']['desktop']['value'] ?? 'on') === 'on' ? 'on' : 'off';
$close_on_esc = ($advanced['closeOnEsc']['desktop']['value'] ?? 'on') === 'on' ? 'on' : 'off';
$open_on = $advanced['openOn']['desktop']['value'] ?? 'click';
$load_delay = $advanced['loadDelay']['desktop']['value'] ?? '';
$content_type = $advanced['contentType']['desktop']['value'] ?? 'text';
// Plain, non-navigating trigger button (no link — opening is handled by
// initModals' delegated click). Avoids the Divi button element's .
$trigger_label = $attrs['triggerText']['innerContent']['desktop']['value'] ?? 'Open Popup';
$trigger_html = sprintf(
'',
esc_attr(self::trigger_class($attrs)),
esc_html($trigger_label)
);
$title_html = $elements->render(['attrName' => 'title', 'tagName' => 'h3']);
// A role="dialog" with no accessible name is announced as just "dialog".
// The title goes through $elements->render(), so it carries no id for
// aria-labelledby to point at -- name the dialog with the title TEXT
// instead, which is what Pro's Popup Maker does. Fall back to the
// trigger label so the dialog is named even with no title set.
$title_text = DynamicValue::resolve($attrs['title']['innerContent']['desktop']['value'] ?? '');
$title_text = is_string($title_text) ? trim(wp_strip_all_tags($title_text)) : '';
$dialog_label = '' !== $title_text ? $title_text : $trigger_label;
if ('layout' === $content_type) {
$layout_id = (int) ($advanced['layoutId']['desktop']['value'] ?? 0);
$content_html = sprintf(
'',
self::render_saved_layout($layout_id)
);
} else {
$content_html = $elements->render(['attrName' => 'content']);
}
$close_html = $show_close
? ''
: '';
$box_html = sprintf(
'',
$close_html,
$title_html,
$content_html,
esc_attr($dialog_label)
);
$overlay_html = sprintf(
'',
esc_attr($close_on_overlay),
esc_attr($close_on_esc),
$box_html
);
// "Links Only": the popup opens from links to the module's CSS ID (a
// menu item, say), so no button is printed. frontend.js handles those
// links for every Open On choice.
$trigger_wrap = 'link' === $open_on
? ''
: sprintf('', $trigger_html);
$modal_html = sprintf(
'%4$s%5$s
',
esc_attr($wrapper_class),
esc_attr($open_on),
esc_attr($load_delay),
$trigger_wrap,
$overlay_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']),
$modal_html,
],
]
);
}
/**
* Class list for the trigger button.
*
* PHP twin of src/divi5/modules/modal-popup/trigger-class.js; the two must
* agree or the builder and the published page hover differently.
*
* The stylesheet's hover flourish (a darker indigo, an indigo glow and a
* 1px lift) belongs to the default indigo look. Once the author gives the
* trigger a background of their own, `--custom-bg` switches it off and
* hover follows the Design tab only.
*
* @param array $attrs Block attributes.
*
* @return string
*/
public static function trigger_class($attrs)
{
$classes = 'dtq-modalpopup__trigger';
if (self::has_custom_trigger_background($attrs)) {
$classes .= ' dtq-modalpopup__trigger--custom-bg';
}
return $classes;
}
/**
* Whether the author set a trigger background, at any breakpoint or state.
*
* Only what paints from CSS counts: a color, an enabled gradient or an
* image.
*
* @param array $attrs Block attributes.
*
* @return bool
*/
public static function has_custom_trigger_background($attrs)
{
$background = $attrs['triggerText']['decoration']['background'] ?? [];
if (!is_array($background)) {
return false;
}
foreach ($background as $states) {
if (!is_array($states)) {
continue;
}
foreach ($states as $value) {
if (!is_array($value)) {
continue;
}
$color = is_string($value['color'] ?? null) ? trim($value['color']) : '';
$gradient = 'on' === ($value['gradient']['enabled'] ?? null);
$image = is_string($value['image']['url'] ?? null) && '' !== trim($value['image']['url']);
if ('' !== $color || $gradient || $image) {
return true;
}
}
}
return false;
}
/**
* IDs currently being rendered, used as a re-entrancy guard.
*
* @var int[]
*/
private static $rendering_layouts = [];
/**
* Render a saved Divi Library layout (or a page/post) by ID for the popup.
*
* Restricted to published `et_pb_layout`, `page` or `post` content to avoid
* exposing private/arbitrary posts. Runs the content through both the D4
* shortcode and D5 block parsers so a layout built in either renders.
*
* Re-entrancy is guarded by an ID stack: a layout that contains a Modal Popup
* pointing back at itself (or a pair of layouts referencing each other) would
* otherwise recurse until the memory limit and take the whole page down — a
* fatal reachable purely by a content-editing mistake.
*
* @param int $id The layout/post ID.
*
* @return string
*/
private static function render_saved_layout($id)
{
$id = (int) $id;
if ($id <= 0) {
return '';
}
if (in_array($id, self::$rendering_layouts, true)) {
return '';
}
$post = get_post($id);
if (!$post || 'publish' !== $post->post_status) {
return '';
}
if (!in_array($post->post_type, ['et_pb_layout', 'page', 'post'], true)) {
return '';
}
self::$rendering_layouts[] = $id;
try {
return do_blocks(do_shortcode($post->post_content));
} finally {
array_pop(self::$rendering_layouts);
}
}
}