| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `gutenify/section-title` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `gutenify/section-title` block on the server. |
| 10 |
* |
| 11 |
* @param array $attributes Block attributes. |
| 12 |
* |
| 13 |
* @return string Returns the filtered post title for the current post wrapped inside "h1" tags. |
| 14 |
*/ |
| 15 |
function gutenify_render_block_section_title( $attributes ) { |
| 16 |
$align_class_name = empty( $attributes['textAlignment'] ) ? '' : "has-text-align-{$attributes['textAlignment']}"; |
| 17 |
|
| 18 |
$prefix_level = ! empty( $attributes['prefixTag'] ) ? $attributes['prefixTag'] : 'h4'; |
| 19 |
|
| 20 |
$prefix_css_classes = ! empty( $attributes['blockAdvanceOptions']['prefixTitleClasses'] ) ? $attributes['blockAdvanceOptions']['prefixTitleClasses'] : ''; |
| 21 |
$prefix_css_classes = $prefix_css_classes . ' gutenify-section-title-prefix'; |
| 22 |
$wrapper_attributes = ''; |
| 23 |
if ( ! empty( $attributes['prefixColor'] ) ) { |
| 24 |
$wrapper_attributes = 'style="color:' . $attributes['prefixColor'] . '"'; |
| 25 |
} |
| 26 |
$wrapper_attributes .= 'class="' . $prefix_css_classes . '"'; |
| 27 |
$prefix_content = ! empty( $attributes['prefixContent'] ) ? $attributes['prefixContent'] : ''; |
| 28 |
|
| 29 |
$prefix = sprintf( |
| 30 |
'<%1$s %2$s>%3$s</%1$s>', |
| 31 |
$prefix_level, |
| 32 |
$wrapper_attributes, |
| 33 |
$prefix_content |
| 34 |
); |
| 35 |
|
| 36 |
// Heading. |
| 37 |
$title_level = ! empty( $attributes['titleTag'] ) ? $attributes['titleTag'] : 'h2'; |
| 38 |
$main_css_classes = ! empty( $attributes['blockAdvanceOptions']['mainTitleClasses'] ) ? $attributes['blockAdvanceOptions']['mainTitleClasses'] : ''; |
| 39 |
$main_css_classes = $main_css_classes . ' gutenify-section-title-main'; |
| 40 |
|
| 41 |
$wrapper_attributes = ''; |
| 42 |
if ( ! empty( $attributes['titleColor'] ) ) { |
| 43 |
$wrapper_attributes = 'style="color:' . $attributes['titleColor'] . '"'; |
| 44 |
} |
| 45 |
$wrapper_attributes .= 'class="' . $main_css_classes . '"'; |
| 46 |
$title_content = ! empty( $attributes['titleContent'] ) ? $attributes['titleContent'] : ''; |
| 47 |
|
| 48 |
$title = sprintf( |
| 49 |
'<%1$s %2$s>%3$s</%1$s>', |
| 50 |
$title_level, |
| 51 |
$wrapper_attributes, |
| 52 |
$title_content |
| 53 |
); |
| 54 |
|
| 55 |
// Suffix. |
| 56 |
$suffix_level = ! empty( $attributes['suffixTag'] ) ? $attributes['suffixTag'] : 'p'; |
| 57 |
$suffix_css_classes = ! empty( $attributes['blockAdvanceOptions']['suffixTitleClasses'] ) ? $attributes['blockAdvanceOptions']['suffixTitleClasses'] : ''; |
| 58 |
$suffix_css_classes = $suffix_css_classes . ' gutenify-section-title-suffix'; |
| 59 |
|
| 60 |
$wrapper_attributes = ''; |
| 61 |
if ( ! empty( $attributes['suffixColor'] ) ) { |
| 62 |
$wrapper_attributes = 'style="color:' . $attributes['suffixColor'] . '"'; |
| 63 |
} |
| 64 |
$wrapper_attributes .= 'class="' . $suffix_css_classes . '"'; |
| 65 |
$suffix_content = ! empty( $attributes['suffixContent'] ) ? $attributes['suffixContent'] : ''; |
| 66 |
|
| 67 |
$suffix = sprintf( |
| 68 |
'<%1$s %2$s>%3$s</%1$s>', |
| 69 |
$suffix_level, |
| 70 |
$wrapper_attributes, |
| 71 |
$suffix_content |
| 72 |
); |
| 73 |
$block_client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : 1; |
| 74 |
|
| 75 |
$class_name = array( 'wp-block-gutenify-section-title', 'gutenify-section-' . $block_client_id ); |
| 76 |
if ( isset( $attributes['className'] ) ) { |
| 77 |
array_push( $class_name, $attributes['className'] ); |
| 78 |
} |
| 79 |
if ( $align_class_name ) { |
| 80 |
array_push( $class_name, $align_class_name ); |
| 81 |
} |
| 82 |
|
| 83 |
$class_name = apply_filters( 'gutenify_block_wrapper_class', esc_attr( implode( ' ', $class_name ) ), $attributes ); |
| 84 |
|
| 85 |
$output = '<div class="' . $class_name . '">'; |
| 86 |
$output .= '<div id="gutenify-section-' . $block_client_id . '" class="gutenify-section-title-section ">'; |
| 87 |
if ( ! empty( $attributes['displayPrefix'] ) ) { |
| 88 |
$output .= $prefix; |
| 89 |
} |
| 90 |
$output .= $title; |
| 91 |
if ( ! empty( $attributes['displaySuffix'] ) ) { |
| 92 |
$output .= $suffix; |
| 93 |
} |
| 94 |
$output .= '</div>'; |
| 95 |
$output .= '</div>'; |
| 96 |
return $output; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Registers the `gutenify/section-title` block on the server. |
| 101 |
*/ |
| 102 |
function gutenify_register_block_section_title() { |
| 103 |
// Return early if this function does not exist. |
| 104 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Load attributes from block.json. |
| 109 |
ob_start(); |
| 110 |
include GUTENIFY_PLUGIN_DIR . 'src/blocks/section-title/block.json'; |
| 111 |
$metadata = json_decode( ob_get_clean(), true ); |
| 112 |
register_block_type( |
| 113 |
'gutenify/section-title', |
| 114 |
array( |
| 115 |
'render_callback' => 'gutenify_render_block_section_title', |
| 116 |
'editor_script' => 'gutenify-editor', |
| 117 |
'editor_style' => 'gutenify-editor', |
| 118 |
'style' => 'gutenify-frontend', |
| 119 |
'attributes' => $metadata['attributes'], |
| 120 |
) |
| 121 |
); |
| 122 |
} |
| 123 |
add_action( 'init', 'gutenify_register_block_section_title' ); |
| 124 |
|