| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering for the Woody Snippets block. |
| 4 |
* |
| 5 |
* @var array<string, mixed> $attributes Block attributes. |
| 6 |
* @var string $content Block default content. |
| 7 |
* @var WP_Block $block Block instance. |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
// Get the snippet ID from attributes |
| 15 |
$snippet_id = isset( $attributes['id'] ) ? $attributes['id'] : null; |
| 16 |
|
| 17 |
if ( ! is_numeric( $snippet_id ) ) { |
| 18 |
return ''; |
| 19 |
} |
| 20 |
|
| 21 |
// Get snippets data |
| 22 |
$snippets_data = WINP_Gutenberg_Snippet::get_prepared_snippets_data(); |
| 23 |
$current_snippet = isset( $snippets_data[ $snippet_id ] ) ? $snippets_data[ $snippet_id ] : null; |
| 24 |
|
| 25 |
if ( empty( $current_snippet ) ) { |
| 26 |
return ''; |
| 27 |
} |
| 28 |
|
| 29 |
$snippet_attrs = $current_snippet['tags']; |
| 30 |
$type = $current_snippet['type']; |
| 31 |
|
| 32 |
// Build shortcode attributes |
| 33 |
$shortcode_attributes = apply_filters( 'wbcr/inp/gutenberg/shortcode_attributes', ' id="' . $snippet_id . '" ', $snippet_id ); |
| 34 |
|
| 35 |
$attr_values = isset( $attributes['attrValues'] ) ? $attributes['attrValues'] : null; |
| 36 |
if ( ! empty( $attr_values ) ) { |
| 37 |
if ( empty( $snippet_attrs ) ) { |
| 38 |
return ''; |
| 39 |
} |
| 40 |
|
| 41 |
if ( count( $snippet_attrs ) !== count( $attr_values ) ) { |
| 42 |
return ''; |
| 43 |
} |
| 44 |
|
| 45 |
foreach ( $attr_values as $key => $value ) { |
| 46 |
$snippet_attr = $snippet_attrs[ $key ]; |
| 47 |
$value = esc_attr( $value ); |
| 48 |
|
| 49 |
if ( empty( $value ) ) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
$shortcode_attributes .= " {$snippet_attr}=\"{$value}\""; |
| 53 |
} |
| 54 |
|
| 55 |
$shortcode_attributes = trim( $shortcode_attributes ); |
| 56 |
} |
| 57 |
|
| 58 |
// Build shortcode name |
| 59 |
$shortcode_name = apply_filters( |
| 60 |
'wbcr/inp/gutenberg/shortcode_name', |
| 61 |
sprintf( 'wbcr%s_snippet', ( $type === WINP_SNIPPET_TYPE_UNIVERSAL ? '' : '_' . $type ) ), |
| 62 |
$snippet_id |
| 63 |
); |
| 64 |
|
| 65 |
// Build the shortcode |
| 66 |
$shortcode = "[{$shortcode_name} {$shortcode_attributes}]"; |
| 67 |
|
| 68 |
if ( ! empty( $content ) ) { |
| 69 |
$shortcode .= "{$content}[/{$shortcode_name}]"; |
| 70 |
} |
| 71 |
|
| 72 |
// Render the shortcode |
| 73 |
echo do_shortcode( $shortcode ); |
| 74 |
|