| 1 |
<?php |
| 2 |
/** |
| 3 |
* The blocks base file. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Blocks; |
| 10 |
|
| 11 |
use SureDonation\Inc\Helper; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Block base class. |
| 19 |
* |
| 20 |
* @since 0.0.1 |
| 21 |
*/ |
| 22 |
abstract class Base { |
| 23 |
/** |
| 24 |
* Optional directory to .json block data files. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
* @since 0.0.1 |
| 28 |
*/ |
| 29 |
protected $directory = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* Register the block for dynamic output |
| 33 |
* |
| 34 |
* @return void |
| 35 |
* @since 0.0.1 |
| 36 |
*/ |
| 37 |
public function register() { |
| 38 |
register_block_type( |
| 39 |
$this->get_dir(), |
| 40 |
apply_filters( |
| 41 |
'suredonation_block_registration_args', |
| 42 |
[ 'render_callback' => [ $this, 'render' ] ] |
| 43 |
) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get the called class directory path |
| 49 |
* |
| 50 |
* @return string |
| 51 |
* @since 0.0.1 |
| 52 |
*/ |
| 53 |
public function get_dir() { |
| 54 |
if ( $this->directory ) { |
| 55 |
return $this->directory; |
| 56 |
} |
| 57 |
|
| 58 |
$reflector = new \ReflectionClass( $this ); |
| 59 |
$fn = (string) $reflector->getFileName(); |
| 60 |
return dirname( $fn ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Render the block |
| 65 |
* |
| 66 |
* @param array<string, mixed> $attributes Block attributes. |
| 67 |
* @param string $content Block content. |
| 68 |
* |
| 69 |
* @return string |
| 70 |
* @since 0.0.1 |
| 71 |
*/ |
| 72 |
public function render( $attributes, $content = '' ) { |
| 73 |
unset( $content ); // Unused parameter in base class. |
| 74 |
unset( $attributes ); // Unused parameter in base class. |
| 75 |
return ''; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get allowed HTML tags for form markup. |
| 80 |
* |
| 81 |
* The wp_kses_post() doesn't allow form elements, so we need a custom allowed tags array. |
| 82 |
* This is safe because the markup is generated internally by trusted code that already |
| 83 |
* escapes user input with esc_attr(), esc_html(), etc. |
| 84 |
* |
| 85 |
* @return array<string, array<string, bool>> Allowed HTML tags and attributes. |
| 86 |
* @since 0.0.1 |
| 87 |
*/ |
| 88 |
protected static function get_allowed_form_html() { |
| 89 |
return Helper::get_allowed_form_html(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Build the wrapper class string shared by display blocks (Heading, HTML). |
| 94 |
* |
| 95 |
* Display blocks have no field name, so they intentionally omit the |
| 96 |
* per-instance `sd-{slug}-{block_id}-block` class that field blocks carry. |
| 97 |
* This derives the common width/className/slug classes and prepends the |
| 98 |
* block-specific base class. |
| 99 |
* |
| 100 |
* @param array<string, mixed> $attributes Block attributes. |
| 101 |
* @param string $base_class Block-specific base class (e.g. 'sd-heading-block'). |
| 102 |
* @return string Joined wrapper class string. |
| 103 |
* @since 1.1.1 |
| 104 |
*/ |
| 105 |
protected static function get_display_block_classes( $attributes, $base_class ) { |
| 106 |
$field_width = isset( $attributes['fieldWidth'] ) ? Helper::get_string_value( $attributes['fieldWidth'] ) : ''; |
| 107 |
$block_width = $field_width ? ' sd-block-width-' . str_replace( '.', '-', $field_width ) : ''; |
| 108 |
$class_name = isset( $attributes['className'] ) && is_string( $attributes['className'] ) ? ' ' . $attributes['className'] : ''; |
| 109 |
$block_slug = isset( $attributes['slug'] ) && is_string( $attributes['slug'] ) ? $attributes['slug'] : ''; |
| 110 |
$slug_class = $block_slug ? ' sd-slug-' . $block_slug : ''; |
| 111 |
|
| 112 |
return Helper::join_strings( |
| 113 |
[ |
| 114 |
'sd-block-single', |
| 115 |
'sd-block', |
| 116 |
$base_class, |
| 117 |
$block_width, |
| 118 |
$class_name, |
| 119 |
$slug_class, |
| 120 |
] |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|