PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.4.3
Kubio AI Page Builder v2.4.3
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 1 year ago Blocks 1 year ago GlobalElements 3 years ago Layout 4 years ago License 2 years ago Separators 4 years ago StyleManager 1 year ago Styles 4 years ago Activation.php 1 year ago Backup.php 4 years ago CustomizerImporter.php 1 year ago Deactivation.php 4 years ago EditInKubioCustomizerPanel.php 4 years ago Element.php 2 years ago ElementBase.php 4 years ago Importer.php 2 years ago InnerBlocks.php 4 years ago LodashBasic.php 2 years ago Registry.php 3 years ago Utils.php 1 year ago
Registry.php
277 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 /**
122 *
123 * @return Registry
124 */
125 static function getInstance() {
126 if ( ! self::$instance ) {
127 self::$instance = new self();
128 }
129
130 return self::$instance;
131 }
132
133 function getRenderedFonts() {
134 $result = array();
135
136 foreach ( $this->fonts as $font => $weights ) {
137 $next_weights = LodashBasic::uniq( array_merge( $weights, $this->window_font_weights ) );
138 $result[ $font ] = $next_weights;
139 }
140
141 return $result;
142 }
143
144 function registerFonts( $familiesStr, $weight, $style = 'normal' ) {
145 $families = explode( ',', $familiesStr );
146 foreach ( $families as $family ) {
147 $family = trim( $family );
148 if ( $family ) {
149 if ( ! isset( $this->fonts[ $family ] ) ) {
150 $this->fonts[ $family ] = array();
151 }
152
153 if ( empty( $weight ) ) {
154 $weight = '400';
155 }
156
157 $next_weights = array( strval( $weight ) );
158
159 if ( $style === 'italic' ) {
160 $next_weights[] = $weight . 'italic';
161 }
162
163 $this->fonts[ $family ] = LodashBasic::uniq(
164 LodashBasic::concat(
165 $this->fonts[ $family ],
166 $next_weights
167 )
168 );
169 } else {
170 if ( empty( $weight ) ) {
171 $weight = '400';
172 }
173
174 $weight = strval( $weight );
175
176 if ( $style === 'italic' ) {
177 $weight . 'italic';
178 }
179
180 if ( ! in_array( $weight, $this->window_font_weights ) ) {
181 $this->window_font_weights[] = $weight;
182 }
183 }
184 }
185 }
186
187
188 function getBlock( $block, $context ) {
189 $blockName = $block['blockName'];
190 if ( isset( $this->registered[ $blockName ] ) ) {
191 $class = $this->registered[ $blockName ];
192 $block = new $class( $block, true, $context );
193
194 return $block;
195 }
196 }
197
198 function getParentBlock() {
199 $count = count( $this->blocksStack );
200
201 return $count > 1 ? $this->blocksStack[ $count - 2 ] : null;
202 }
203
204 function getLastBlock() {
205 return Arr::last( $this->blocksStack, null, null );
206 }
207
208 function addBlockToStack( $block ) {
209 $name = $block->block_type->name;
210 if ( ! isset( $this->lastBlocksByName[ $name ] ) ) {
211 $this->lastBlocksByName[ $name ] = array();
212 }
213 $this->lastBlocksByName[ $name ][] = $block;
214 $this->blocksStack[] = $block;
215 }
216
217 function removeBlockFromStack( $block ) {
218 $name = $block->block_type->name;
219 if ( isset( $this->lastBlocksByName[ $name ] ) ) {
220 array_pop( $this->lastBlocksByName[ $name ] );
221 }
222
223 array_pop( $this->blocksStack );
224 }
225
226 function getLastBlockOfName( $blockName ) {
227 $block_names = array();
228 if ( ! is_array( $blockName ) ) {
229 $block_names = array( $blockName );
230 } else {
231 $block_names = $blockName;
232 }
233
234 foreach ( $block_names as $blockName ) {
235 if ( isset( $this->lastBlocksByName[ $blockName ] ) ) {
236 $length = count( $this->lastBlocksByName[ $blockName ] );
237
238 if ( $length - 1 < 0 ) {
239 continue;
240 }
241
242 return $this->lastBlocksByName[ $blockName ][ $length - 1 ];
243 }
244 }
245
246 return null;
247 }
248
249 function createElement( $type, $props = array(), $children = array(), $block = null ) {
250 $class = $this->getClassForType( $type );
251 $tag = Element::DIV;
252 if ( is_string( $type ) && ! isset( $this->elementsByType[ $type ] ) ) {
253 $tag = $type;
254 }
255
256 return new $class( $tag, $props, $children, $block );
257 }
258
259
260 function getClassForType( $type ) {
261 $elementsByType = apply_filters(
262 'kubio/blocks/elements',
263 $this->elementsByType
264 );
265
266 if ( ! isset( $elementsByType[ $type ] ) ) {
267 return Element::class;
268 }
269 $constructor = $elementsByType[ $type ];
270 if ( function_exists( $constructor ) ) {
271 return call_user_func_array( $constructor, array() );
272 } else {
273 return $constructor;
274 }
275 }
276 }
277