| 1 |
<?php |
| 2 |
/** |
| 3 |
* The blocks base file. |
| 4 |
* |
| 5 |
* @link https://sureforms.com |
| 6 |
* @since 0.0.1 |
| 7 |
* @package SureForms |
| 8 |
* @author SureForms <https://sureforms.com/> |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace SRFM\Inc\Blocks; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Block base class. |
| 19 |
*/ |
| 20 |
abstract class Base { |
| 21 |
/** |
| 22 |
* Optional directory to .json block data files. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
* @since 0.0.1 |
| 26 |
*/ |
| 27 |
protected $directory = ''; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the block. |
| 31 |
* |
| 32 |
* @var object |
| 33 |
* @since 0.0.1 |
| 34 |
*/ |
| 35 |
protected $block; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the block for dynamic output |
| 39 |
* |
| 40 |
* @return void |
| 41 |
* @since 0.0.1 |
| 42 |
*/ |
| 43 |
public function register() { |
| 44 |
register_block_type_from_metadata( |
| 45 |
$this->get_dir(), |
| 46 |
apply_filters( |
| 47 |
'srfm_block_registration_args', |
| 48 |
[ 'render_callback' => [ $this, 'pre_render' ] ] |
| 49 |
) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get the called class directory path |
| 55 |
* |
| 56 |
* @return string |
| 57 |
* @since 0.0.1 |
| 58 |
*/ |
| 59 |
public function get_dir() { |
| 60 |
if ( $this->directory ) { |
| 61 |
return $this->directory; |
| 62 |
} |
| 63 |
|
| 64 |
$reflector = new \ReflectionClass( $this ); |
| 65 |
$fn = (string) $reflector->getFileName(); |
| 66 |
return dirname( $fn ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Optionally run a function to modify attributes before rendering. |
| 72 |
* |
| 73 |
* @param array<mixed> $attributes Block attributes. |
| 74 |
* @param string $content Post content. |
| 75 |
* @param array<mixed> $block Block attributes. |
| 76 |
* |
| 77 |
* @return boolean|string |
| 78 |
* @since 0.0.1 |
| 79 |
*/ |
| 80 |
public function pre_render( $attributes, $content, $block ) { |
| 81 |
$this->block = (object) $block; |
| 82 |
|
| 83 |
// run middlware. |
| 84 |
$render = $this->middleware( $attributes, $content ); |
| 85 |
|
| 86 |
if ( is_wp_error( $render ) ) { |
| 87 |
return $render->get_error_message(); |
| 88 |
} |
| 89 |
|
| 90 |
if ( true !== $render ) { |
| 91 |
return $render; |
| 92 |
} |
| 93 |
|
| 94 |
/** $attributes = $this->getAttributes( $attributes ); */ |
| 95 |
|
| 96 |
// render. |
| 97 |
return $this->render( $attributes, $content ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Run any block middleware before rendering. |
| 102 |
* |
| 103 |
* @param array<mixed> $attributes Block attributes. |
| 104 |
* @param string $content Post content. |
| 105 |
* @return boolean|\WP_Error; |
| 106 |
* @since 0.0.1 |
| 107 |
*/ |
| 108 |
protected function middleware( $attributes, $content ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Allows filtering of attributes before rendering. |
| 114 |
* |
| 115 |
* @param array<mixed> $attributes Block attributes. |
| 116 |
* @return array<mixed> $attributes |
| 117 |
* @since 0.0.1 |
| 118 |
*/ |
| 119 |
public function get_attributes( $attributes ) { |
| 120 |
return $attributes; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Render the block |
| 125 |
* |
| 126 |
* @param array<mixed> $attributes Block attributes. |
| 127 |
* @param string $content Post content. |
| 128 |
* |
| 129 |
* @return string |
| 130 |
* @since 0.0.1 |
| 131 |
*/ |
| 132 |
public function render( $attributes, $content ) { |
| 133 |
return ''; |
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
|