PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.6.4
Kubio AI Page Builder v1.6.4
2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / Registry.php
kubio / lib / src / Core Last commit date
Background 3 years ago Blocks 3 years ago GlobalElements 4 years ago Layout 4 years ago License 4 years ago Separators 4 years ago StyleManager 3 years ago Styles 4 years ago Activation.php 3 years ago Backup.php 4 years ago CustomizerImporter.php 3 years ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 4 years ago ElementBase.php 4 years ago Importer.php 3 years ago InnerBlocks.php 4 years ago LodashBasic.php 4 years ago Registry.php 4 years ago Utils.php 3 years ago
Registry.php
273 lines
1 <?php
2
3 namespace Kubio\Core;
4
5 use Exception;
6 use IlluminateAgnostic\Arr\Support\Arr;
7 use Kubio\Core\Background\Background;
8 use Kubio\Core\Blocks\BlockElement;
9 use Kubio\Core\GlobalElements\Icon;
10 use Kubio\Core\GlobalElements\LinkWrapper;
11 use Kubio\Core\Separators\Separators;
12
13 class Registry {
14
15
16 private static $instance;
17
18 private $registered = array();
19
20 private $elementsByType = array(
21 'background' => Background::class,
22 'separators' => Separators::class,
23 'element' => BlockElement::class,
24 'wp:InnerBlocks' => InnerBlocks::class,
25 'LinkWrapper' => LinkWrapper::class,
26 'icon' => Icon::class,
27 );
28
29 private $blocksStack = array();
30 private $lastBlocksByName = array();
31 private $fonts = array();
32 // normal and bold should be here by defauly for inline text
33 private $window_font_weights = array( '400', '700', '400italic', '700italic' );
34
35 /**
36 * @param $block_dir
37 * @param $handle_class
38 * @param array $args
39 *
40 * @throws Exception
41 */
42 static function registerBlock( $block_dir, $handle_class, $args = array() ) {
43 $block_json = wp_normalize_path( $block_dir . '/' . Arr::get( $args, 'metadata', 'block.json' ) );
44 $metadata = kubio_get_block_metadata_mixin( $block_json );
45
46 if ( ! $metadata ) {
47 throw new Exception( "Kubio register block missing metadata. Path: {$block_json}" );
48 }
49
50 $metadata_mixins = Arr::get( $args, 'metadata_mixins', array() );
51
52 foreach ( $metadata_mixins as $mixin ) {
53 $mixin_path = wp_normalize_path( "{$block_dir}/$mixin" );
54 $mixin_data = kubio_get_block_metadata_mixin( $mixin_path );
55
56 if ( ! $mixin_data ) {
57 throw new Exception( "Kubio register block missing metadata mixin. Path: {$mixin_path}" );
58 }
59
60 $metadata = array_replace_recursive( $metadata, $mixin_data );
61
62 $exact_replaces = Arr::get( $args, 'mixins_exact_replace', array() );
63
64 if ( isset( $exact_replaces[ $mixin ] ) ) {
65 foreach ( (array) $exact_replaces[ $mixin ] as $exact_replace ) {
66 Arr::set( $metadata, $exact_replace, Arr::get( $mixin_data, $exact_replace ) );
67 }
68 }
69 }
70 $metadata = array_replace_recursive(
71 $metadata,
72 array(
73 'supports' => array(
74 'anchor' => true,
75 'customClassName' => true,
76 ),
77 )
78 );
79 $metadata = apply_filters( 'kubio/blocks/register_block_type', $metadata );
80
81 $block_name = Arr::get( $metadata, 'name', null );
82
83 if ( ! $block_name ) {
84 throw new Exception( "Kubio register block missing block name. Path: {$block_json}" );
85 }
86
87 self::getInstance()->registered[ $block_name ] = $handle_class;
88
89 if ( kubio_can_register_block( $block_name ) ) {
90
91 if ( did_action( 'init' ) ) {
92 kubio_register_block_type_from_metadata_array(
93 $metadata,
94 array(
95 'render_callback' => 'kubio_render_block_callback',
96 'skip_inner_blocks' => true,
97 'editor_style' => 'kubio-block-library-editor',
98 'style' => 'kubio-block-library',
99 )
100 );
101 } else {
102 add_action(
103 'init',
104 function () use ( $block_name, $metadata ) {
105 kubio_register_block_type_from_metadata_array(
106 $metadata,
107 array(
108 'render_callback' => 'kubio_render_block_callback',
109 'skip_inner_blocks' => true,
110 'editor_style' => 'kubio-block-library-editor',
111 'style' => 'kubio-block-library',
112 )
113 );
114 },
115 20
116 );
117 }
118 }
119 }
120
121 static function getInstance() {
122 if ( ! self::$instance ) {
123 self::$instance = new self();
124 }
125
126 return self::$instance;
127 }
128
129 function getRenderedFonts() {
130 $result = array();
131
132 foreach ( $this->fonts as $font => $weights ) {
133 $next_weights = LodashBasic::uniq( array_merge( $weights, $this->window_font_weights ) );
134 $result[ $font ] = $next_weights;
135 }
136
137 return $result;
138 }
139
140 function registerFonts( $familiesStr, $weight, $style = 'normal' ) {
141 $families = explode( ',', $familiesStr );
142 foreach ( $families as $family ) {
143 $family = trim( $family );
144 if ( $family ) {
145 if ( ! isset( $this->fonts[ $family ] ) ) {
146 $this->fonts[ $family ] = array();
147 }
148
149 if ( empty( $weight ) ) {
150 $weight = '400';
151 }
152
153 $next_weights = array( strval( $weight ) );
154
155 if ( $style === 'italic' ) {
156 $next_weights[] = $weight . 'italic';
157 }
158
159 $this->fonts[ $family ] = LodashBasic::uniq(
160 LodashBasic::concat(
161 $this->fonts[ $family ],
162 $next_weights
163 )
164 );
165 } else {
166 if ( empty( $weight ) ) {
167 $weight = '400';
168 }
169
170 $weight = strval( $weight );
171
172 if ( $style === 'italic' ) {
173 $weight . 'italic';
174 }
175
176 if ( ! in_array( $weight, $this->window_font_weights ) ) {
177 $this->window_font_weights[] = $weight;
178 }
179 }
180 }
181 }
182
183
184 function getBlock( $block, $context ) {
185 $blockName = $block['blockName'];
186 if ( isset( $this->registered[ $blockName ] ) ) {
187 $class = $this->registered[ $blockName ];
188 $block = new $class( $block, true, $context );
189
190 return $block;
191 }
192 }
193
194 function getParentBlock() {
195 $count = count( $this->blocksStack );
196
197 return $count > 1 ? $this->blocksStack[ $count - 2 ] : null;
198 }
199
200 function getLastBlock() {
201 return Arr::last( $this->blocksStack, null, null );
202 }
203
204 function addBlockToStack( $block ) {
205 $name = $block->block_type->name;
206 if ( ! isset( $this->lastBlocksByName[ $name ] ) ) {
207 $this->lastBlocksByName[ $name ] = array();
208 }
209 $this->lastBlocksByName[ $name ][] = $block;
210 $this->blocksStack[] = $block;
211 }
212
213 function removeBlockFromStack( $block ) {
214 $name = $block->block_type->name;
215 if ( isset( $this->lastBlocksByName[ $name ] ) ) {
216 array_pop( $this->lastBlocksByName[ $name ] );
217 }
218
219 array_pop( $this->blocksStack );
220 }
221
222 function getLastBlockOfName( $blockName ) {
223 $block_names = array();
224 if ( ! is_array( $blockName ) ) {
225 $block_names = array( $blockName );
226 } else {
227 $block_names = $blockName;
228 }
229
230 foreach ( $block_names as $blockName ) {
231 if ( isset( $this->lastBlocksByName[ $blockName ] ) ) {
232 $length = count( $this->lastBlocksByName[ $blockName ] );
233
234 if ( $length - 1 < 0 ) {
235 continue;
236 }
237
238 return $this->lastBlocksByName[ $blockName ][ $length - 1 ];
239 }
240 }
241
242 return null;
243 }
244
245 function createElement( $type, $props = array(), $children = array(), $block = null ) {
246 // $typeParts = explode(".", $type);
247 $class = $this->getClassForType( $type );
248 $tag = Element::DIV;
249 if ( is_string( $type ) && ! isset( $this->elementsByType[ $type ] ) ) {
250 $tag = $type;
251 }
252
253 return new $class( $tag, $props, $children, $block );
254 }
255
256 function getClassForType( $type ) {
257 $elementsByType = apply_filters(
258 'kubio/blocks/elements',
259 $this->elementsByType
260 );
261
262 if ( ! isset( $elementsByType[ $type ] ) ) {
263 return Element::class;
264 }
265 $constructor = $elementsByType[ $type ];
266 if ( function_exists( $constructor ) ) {
267 return call_user_func_array( $constructor, array() );
268 } else {
269 return $constructor;
270 }
271 }
272 }
273