' ', '2' => '', '3' => '', '4' => '', '5' => '', ); } /** * Server-side render for the Testimonial module. * * Mirrors the D4 render() markup (and the VB edit component) exactly: * same class names, same SVG quote icons, same conditional layout matrix * (img_position x reviewer_position x ratings_position x review_design). * Keep byte-for-byte identical with src/divi5/modules/testimonial/edit.jsx. * * @param array $attrs Block attributes. * @param string $content Block content (unused). * @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'] ?? []; // D4 parity (dt_if_not_migrated()): legacy installs (the `ba_version` // option exists) shipped different D4 defaults for these fields. // module.json carries the NEW-USER default; when the attr is absent on // a legacy install we fall back to the legacy default so migrated // layouts render unchanged. $is_legacy = (bool) get_option('ba_version'); $alignment = $advanced['alignment']['desktop']['value'] ?? 'center'; $img_position = $advanced['imgPosition']['desktop']['value'] ?? 'relative'; $reviewer_position = $advanced['reviewerPosition']['desktop']['value'] ?? ($is_legacy ? 'bottom' : 'top'); $ratings_position = $advanced['ratingsPosition']['desktop']['value'] ?? ($is_legacy ? '_default' : 'bottom'); $use_rating = $advanced['useRating']['desktop']['value'] ?? ($is_legacy ? 'off' : 'on'); $hide_quote = $advanced['hideQuote']['desktop']['value'] ?? ($is_legacy ? 'off' : 'on'); $rating = (int) ($advanced['rating']['desktop']['value'] ?? '5'); $icon_placement = $advanced['iconPlacement']['desktop']['value'] ?? 'background'; $use_custom_icon = $advanced['useCustomIcon']['desktop']['value'] ?? 'off'; $selected_icon = $advanced['selectedIcon']['desktop']['value'] ?? '5'; $website_url = DynamicValue::resolve($advanced['websiteUrl']['desktop']['value'] ?? ''); $company_url = DynamicValue::resolve($advanced['companyUrl']['desktop']['value'] ?? ''); $link_target = ('on' === ($advanced['linkNewWindow']['desktop']['value'] ?? 'off')) ? '_blank' : '_self'; // The D5 upload field stores the image as an object (`{ src, ... }`); // accept both that and a plain URL string (the migrated D4 value). $image_val = DynamicValue::resolve($attrs['image']['innerContent']['desktop']['value'] ?? ''); $image_src = is_array($image_val) ? ($image_val['src'] ?? '') : $image_val; $image_alt = $attrs['image']['advanced']['alt']['desktop']['value'] ?? ''; $name_val = $attrs['name']['innerContent']['desktop']['value'] ?? ''; $title_val = $attrs['title']['innerContent']['desktop']['value'] ?? ''; $testimonial_val = $attrs['testimonial']['innerContent']['desktop']['value'] ?? ''; // Quote icon (mirrors D4 render_quote_icon()): rendered only when the // quote is not hidden AND the chosen placement matches. With a custom // icon image the span stays empty (the image is a CSS background). $icons = self::get_quote_icons(); $render_quote_icon = function ($cls, $placement) use ($hide_quote, $icon_placement, $use_custom_icon, $selected_icon, $icons) { if ('off' !== $hide_quote || $icon_placement !== $placement) { return ''; } $svg = ('off' === $use_custom_icon) ? ($icons[$selected_icon] ?? '') : ''; return sprintf( '
%2$s
', esc_attr($cls), $svg ); }; // Star rating (mirrors D4 render_rating()). $render_rating = function ($pos) use ($use_rating, $ratings_position, $rating) { if ('on' !== $use_rating || $ratings_position !== $pos) { return ''; } $stars = ''; for ($i = 0; $i < $rating; $i++) { $stars .= '★'; } return sprintf('
%1$s
', $stars); }; // Reviewer image (mirrors D4 _render_image()). $render_image = function ($positions) use ($image_src, $image_alt, $img_position) { if (empty($image_src) || !in_array($img_position, $positions, true)) { return ''; } return sprintf( '
%2$s
', esc_url($image_src), esc_attr($image_alt) ); }; // Reviewer name/title — rendered through elements->render (dynamic // content) and wrapped in the optional D4 link. A value that is only // whitespace or empty markup counts as empty, or the wrapper prints // with its bottom padding and nothing in it (#112). $name_html = ''; if (TextValue::visible($name_val)) { $name_inner = $elements->render(['attrName' => 'name']); $name_html = !empty($website_url) ? sprintf('%3$s', esc_url($website_url), esc_attr($link_target), $name_inner) : $name_inner; } $title_html = ''; if (TextValue::visible($title_val)) { $title_inner = $elements->render(['attrName' => 'title']); $title_html = !empty($company_url) ? sprintf('%3$s', esc_url($company_url), esc_attr($link_target), $title_inner) : $title_inner; } // Reviewer block (mirrors D4 render_reviewer()). $render_reviewer = function ($pos) use ($reviewer_position, $render_image, $name_html, $title_html, $render_rating) { if ($reviewer_position !== $pos) { return ''; } return sprintf( '
%1$s
%2$s%3$s%4$s
', $render_image(array('relative')), $name_html, $title_html, $render_rating('reviewer') ); }; // Review block (mirrors D4 render_review()): the default-placement // quote icon lives INSIDE the review box, before the testimonial text. $review_html = sprintf( '
%1$s%2$s
', $render_quote_icon('dtq-icon-default', '_default'), // Gate on the raw value, print the resolved one (#86). ('' !== $testimonial_val) ? sprintf('

%1$s

', DynamicValue::resolve($testimonial_val)) : '' ); $children = sprintf( '
%2$s%3$s
%5$s%6$s
%7$s%8$s%9$s%10$s%11$s
%12$s
', esc_attr($alignment), $render_quote_icon('dtq-icon-absolute', 'absolute'), $render_image(array('absolute')), esc_attr($img_position), $render_quote_icon('dtq-icon-bg', 'background'), $render_image(array('top', 'left')), $render_reviewer('top'), $render_rating('_default'), $review_html, $render_reviewer('bottom'), $render_rating('bottom'), $render_image(array('right')) ); return Module::render( [ 'attrs' => $attrs, 'elements' => $elements, 'id' => $block->parsed_block['id'], 'name' => $block->block_type->name, 'moduleCategory' => $block->block_type->category, 'classnamesFunction' => [self::class, 'module_classnames'], 'stylesComponent' => [self::class, 'module_styles'], 'scriptDataComponent' => [self::class, 'module_script_data'], 'orderIndex' => $block->parsed_block['orderIndex'] ?? 0, 'storeInstance' => $block->parsed_block['storeInstance'] ?? null, 'children' => [ // Background pattern/mask and the box-shadow overlay are DOM, // not CSS. Without this call Divi renders them in the builder // and nowhere else, so the options silently do nothing on the // front end. $elements->style_components(['attrName' => 'module']), $children, ], ] ); } }