| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP render for Heading Block. |
| 4 |
* |
| 5 |
* A display-only block that renders a section heading within the donation form. |
| 6 |
* It carries no submission value and does not participate in field validation. |
| 7 |
* |
| 8 |
* @package SureDonation |
| 9 |
* @since 1.1.1 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SureDonation\Inc\Blocks\Heading; |
| 13 |
|
| 14 |
use SureDonation\Inc\Blocks\Base; |
| 15 |
use SureDonation\Inc\Helper; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Heading Block. |
| 23 |
* |
| 24 |
* @since 1.1.1 |
| 25 |
*/ |
| 26 |
class Block extends Base { |
| 27 |
/** |
| 28 |
* Render the block. |
| 29 |
* |
| 30 |
* @param array<string, mixed> $attributes Block attributes. |
| 31 |
* @param string $content Block content. |
| 32 |
* @return string |
| 33 |
* @since 1.1.1 |
| 34 |
*/ |
| 35 |
public function render( $attributes, $content = '' ) { |
| 36 |
unset( $content ); // Unused parameter. |
| 37 |
|
| 38 |
if ( empty( $attributes ) ) { |
| 39 |
return ''; |
| 40 |
} |
| 41 |
|
| 42 |
$text = isset( $attributes['headingText'] ) ? Helper::get_string_value( $attributes['headingText'] ) : ''; |
| 43 |
if ( '' === trim( $text ) ) { |
| 44 |
return ''; |
| 45 |
} |
| 46 |
|
| 47 |
// Restrict the tag to the supported set (H1–H6, P, Div) so it can be used |
| 48 |
// safely as an element name. |
| 49 |
$tag = isset( $attributes['headingTag'] ) ? strtolower( Helper::get_string_value( $attributes['headingTag'] ) ) : 'h2'; |
| 50 |
if ( ! in_array( $tag, [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div' ], true ) ) { |
| 51 |
$tag = 'h2'; |
| 52 |
} |
| 53 |
|
| 54 |
// Restrict alignment to a known set so it can be used safely in a class. |
| 55 |
$align = isset( $attributes['textAlign'] ) ? Helper::get_string_value( $attributes['textAlign'] ) : 'left'; |
| 56 |
if ( ! in_array( $align, [ 'left', 'center', 'right' ], true ) ) { |
| 57 |
$align = 'left'; |
| 58 |
} |
| 59 |
|
| 60 |
$wrapper_classes = self::get_display_block_classes( $attributes, 'sd-heading-block' ); |
| 61 |
|
| 62 |
$heading_markup = sprintf( |
| 63 |
'<%1$s class="sd-heading sd-heading-align-%2$s">%3$s</%1$s>', |
| 64 |
esc_attr( $tag ), |
| 65 |
esc_attr( $align ), |
| 66 |
esc_html( $text ) |
| 67 |
); |
| 68 |
|
| 69 |
// Optional subheading, placed above or below the heading. |
| 70 |
$sub_markup = ''; |
| 71 |
$sub_text = isset( $attributes['subHeadingText'] ) ? Helper::get_string_value( $attributes['subHeadingText'] ) : ''; |
| 72 |
if ( ! empty( $attributes['enableSubHeading'] ) && '' !== trim( $sub_text ) ) { |
| 73 |
$sub_markup = sprintf( |
| 74 |
'<p class="sd-subheading sd-subheading-align-%1$s">%2$s</p>', |
| 75 |
esc_attr( $align ), |
| 76 |
esc_html( $sub_text ) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
$sub_position = isset( $attributes['subHeadingPosition'] ) ? Helper::get_string_value( $attributes['subHeadingPosition'] ) : 'below'; |
| 81 |
if ( ! in_array( $sub_position, [ 'above', 'below' ], true ) ) { |
| 82 |
$sub_position = 'below'; |
| 83 |
} |
| 84 |
|
| 85 |
// Optional separator (rule), placed relative to the heading/subheading. |
| 86 |
$sep_markup = ''; |
| 87 |
$sep_style = isset( $attributes['separatorStyle'] ) ? Helper::get_string_value( $attributes['separatorStyle'] ) : 'none'; |
| 88 |
if ( in_array( $sep_style, [ 'solid', 'double', 'dashed', 'dotted' ], true ) ) { |
| 89 |
$sep_markup = sprintf( |
| 90 |
'<hr class="sd-heading-separator sd-heading-separator--%1$s sd-heading-separator-align-%2$s" />', |
| 91 |
esc_attr( $sep_style ), |
| 92 |
esc_attr( $align ) |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
$sep_position = isset( $attributes['separatorPosition'] ) ? Helper::get_string_value( $attributes['separatorPosition'] ) : 'below-heading'; |
| 97 |
if ( ! in_array( $sep_position, [ 'above-heading', 'below-heading', 'below-subheading' ], true ) ) { |
| 98 |
$sep_position = 'below-heading'; |
| 99 |
} |
| 100 |
|
| 101 |
// Order the heading and subheading, then weave in the separator at its anchor. |
| 102 |
$order = ( 'above' === $sub_position && '' !== $sub_markup ) |
| 103 |
? [ 'sub', 'heading' ] |
| 104 |
: [ 'heading', 'sub' ]; |
| 105 |
|
| 106 |
$inner = ''; |
| 107 |
$sep_placed = false; |
| 108 |
foreach ( $order as $part ) { |
| 109 |
if ( 'heading' === $part ) { |
| 110 |
if ( '' !== $sep_markup && 'above-heading' === $sep_position ) { |
| 111 |
$inner .= $sep_markup; |
| 112 |
$sep_placed = true; |
| 113 |
} |
| 114 |
$inner .= $heading_markup; |
| 115 |
if ( '' !== $sep_markup && 'below-heading' === $sep_position ) { |
| 116 |
$inner .= $sep_markup; |
| 117 |
$sep_placed = true; |
| 118 |
} |
| 119 |
} elseif ( '' !== $sub_markup ) { |
| 120 |
$inner .= $sub_markup; |
| 121 |
if ( '' !== $sep_markup && 'below-subheading' === $sep_position ) { |
| 122 |
$inner .= $sep_markup; |
| 123 |
$sep_placed = true; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// Fallback: separator anchored to a missing subheading still renders. |
| 129 |
if ( '' !== $sep_markup && ! $sep_placed ) { |
| 130 |
$inner .= $sep_markup; |
| 131 |
} |
| 132 |
|
| 133 |
$markup = sprintf( |
| 134 |
'<div class="%1$s">%2$s</div>', |
| 135 |
esc_attr( $wrapper_classes ), |
| 136 |
$inner |
| 137 |
); |
| 138 |
|
| 139 |
ob_start(); |
| 140 |
echo wp_kses( $markup, self::get_allowed_form_html() ); |
| 141 |
$output = ob_get_clean(); |
| 142 |
return false !== $output ? $output : ''; |
| 143 |
} |
| 144 |
} |
| 145 |
|