PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.9
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.9
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / blocks / base.php

base.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.9, at inc/blocks/base.php

138 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $dir = $this->get_dir();
45 register_block_type_from_metadata(
46 $this->get_dir(),
47 apply_filters(
48 'srfm_block_registration_args',
49 [ 'render_callback' => [ $this, 'pre_render' ] ]
50 )
51 );
52 }
53
54 /**
55 * Get the called class directory path
56 *
57 * @return string
58 * @since 0.0.1
59 */
60 public function get_dir() {
61 if ( $this->directory ) {
62 return $this->directory;
63 }
64
65 $reflector = new \ReflectionClass( $this );
66 $fn = (string) $reflector->getFileName();
67 return dirname( $fn );
68 }
69
70
71 /**
72 * Optionally run a function to modify attributes before rendering.
73 *
74 * @param array<mixed> $attributes Block attributes.
75 * @param string $content Post content.
76 * @param array<mixed> $block Block attributes.
77 *
78 * @return boolean|string
79 * @since 0.0.1
80 */
81 public function pre_render( $attributes, $content, $block ) {
82 $this->block = (object) $block;
83
84 // run middlware.
85 $render = $this->middleware( $attributes, $content );
86
87 if ( is_wp_error( $render ) ) {
88 return $render->get_error_message();
89 }
90
91 if ( true !== $render ) {
92 return $render;
93 }
94
95 /** $attributes = $this->getAttributes( $attributes ); */
96
97 // render.
98 return $this->render( $attributes, $content );
99 }
100
101 /**
102 * Run any block middleware before rendering.
103 *
104 * @param array<mixed> $attributes Block attributes.
105 * @param string $content Post content.
106 * @return boolean|\WP_Error;
107 * @since 0.0.1
108 */
109 protected function middleware( $attributes, $content ) {
110 return true;
111 }
112
113 /**
114 * Allows filtering of attributes before rendering.
115 *
116 * @param array<mixed> $attributes Block attributes.
117 * @return array<mixed> $attributes
118 * @since 0.0.1
119 */
120 public function get_attributes( $attributes ) {
121 return $attributes;
122 }
123
124 /**
125 * Render the block
126 *
127 * @param array<mixed> $attributes Block attributes.
128 * @param string $content Post content.
129 *
130 * @return string
131 * @since 0.0.1
132 */
133 public function render( $attributes, $content ) {
134 return '';
135
136 }
137 }
138