Background
1 year ago
Blocks
1 year ago
GlobalElements
1 year ago
Layout
1 year ago
Separators
11 months ago
StyleManager
1 month ago
Styles
1 year ago
Activation.php
1 year ago
Backup.php
1 year ago
CustomizerImporter.php
11 months ago
Deactivation.php
1 year ago
EditInKubioCustomizerPanel.php
1 year ago
Element.php
1 year ago
ElementBase.php
4 years ago
Importer.php
1 month ago
InnerBlocks.php
1 year ago
KubioFrontPageRevertNotice.php
9 months ago
LodashBasic.php
1 year ago
Registry.php
1 year ago
ThirdPartyPluginAssetLoaderInEditor.php
3 months ago
Utils.php
13 hours ago
Registry.php
280 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 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 48 | throw new Exception( "Kubio register block missing metadata. Path: {$block_json}" ); |
| 49 | } |
| 50 | |
| 51 | $metadata_mixins = Arr::get( $args, 'metadata_mixins', array() ); |
| 52 | |
| 53 | foreach ( $metadata_mixins as $mixin ) { |
| 54 | $mixin_path = wp_normalize_path( "{$block_dir}/$mixin" ); |
| 55 | $mixin_data = kubio_get_block_metadata_mixin( $mixin_path ); |
| 56 | |
| 57 | if ( ! $mixin_data ) { |
| 58 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 59 | throw new Exception( "Kubio register block missing metadata mixin. Path: {$mixin_path}" ); |
| 60 | } |
| 61 | |
| 62 | $metadata = array_replace_recursive( $metadata, $mixin_data ); |
| 63 | |
| 64 | $exact_replaces = Arr::get( $args, 'mixins_exact_replace', array() ); |
| 65 | |
| 66 | if ( isset( $exact_replaces[ $mixin ] ) ) { |
| 67 | foreach ( (array) $exact_replaces[ $mixin ] as $exact_replace ) { |
| 68 | Arr::set( $metadata, $exact_replace, Arr::get( $mixin_data, $exact_replace ) ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | $metadata = array_replace_recursive( |
| 73 | $metadata, |
| 74 | array( |
| 75 | 'supports' => array( |
| 76 | 'anchor' => true, |
| 77 | 'customClassName' => true, |
| 78 | ), |
| 79 | ) |
| 80 | ); |
| 81 | $metadata = apply_filters( 'kubio/blocks/register_block_type', $metadata ); |
| 82 | |
| 83 | $block_name = Arr::get( $metadata, 'name', null ); |
| 84 | |
| 85 | if ( ! $block_name ) { |
| 86 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 87 | throw new Exception( "Kubio register block missing block name. Path: {$block_json}" ); |
| 88 | } |
| 89 | |
| 90 | self::getInstance()->registered[ $block_name ] = $handle_class; |
| 91 | |
| 92 | if ( kubio_can_register_block( $block_name ) ) { |
| 93 | |
| 94 | if ( did_action( 'init' ) ) { |
| 95 | kubio_register_block_type_from_metadata_array( |
| 96 | $metadata, |
| 97 | array( |
| 98 | 'render_callback' => 'kubio_render_block_callback', |
| 99 | 'skip_inner_blocks' => true, |
| 100 | 'editor_style' => 'kubio-block-library-editor', |
| 101 | 'style' => 'kubio-block-library', |
| 102 | ) |
| 103 | ); |
| 104 | } else { |
| 105 | add_action( |
| 106 | 'init', |
| 107 | function () use ( $block_name, $metadata ) { |
| 108 | kubio_register_block_type_from_metadata_array( |
| 109 | $metadata, |
| 110 | array( |
| 111 | 'render_callback' => 'kubio_render_block_callback', |
| 112 | 'skip_inner_blocks' => true, |
| 113 | 'editor_style' => 'kubio-block-library-editor', |
| 114 | 'style' => 'kubio-block-library', |
| 115 | ) |
| 116 | ); |
| 117 | }, |
| 118 | 20 |
| 119 | ); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * |
| 126 | * @return Registry |
| 127 | */ |
| 128 | static function getInstance() { |
| 129 | if ( ! self::$instance ) { |
| 130 | self::$instance = new self(); |
| 131 | } |
| 132 | |
| 133 | return self::$instance; |
| 134 | } |
| 135 | |
| 136 | function getRenderedFonts() { |
| 137 | $result = array(); |
| 138 | |
| 139 | foreach ( $this->fonts as $font => $weights ) { |
| 140 | $next_weights = LodashBasic::uniq( array_merge( $weights, $this->window_font_weights ) ); |
| 141 | $result[ $font ] = $next_weights; |
| 142 | } |
| 143 | |
| 144 | return $result; |
| 145 | } |
| 146 | |
| 147 | function registerFonts( $familiesStr, $weight, $style = 'normal' ) { |
| 148 | $families = explode( ',', $familiesStr ); |
| 149 | foreach ( $families as $family ) { |
| 150 | $family = trim( $family ); |
| 151 | if ( $family ) { |
| 152 | if ( ! isset( $this->fonts[ $family ] ) ) { |
| 153 | $this->fonts[ $family ] = array(); |
| 154 | } |
| 155 | |
| 156 | if ( empty( $weight ) ) { |
| 157 | $weight = '400'; |
| 158 | } |
| 159 | |
| 160 | $next_weights = array( strval( $weight ) ); |
| 161 | |
| 162 | if ( $style === 'italic' ) { |
| 163 | $next_weights[] = $weight . 'italic'; |
| 164 | } |
| 165 | |
| 166 | $this->fonts[ $family ] = LodashBasic::uniq( |
| 167 | LodashBasic::concat( |
| 168 | $this->fonts[ $family ], |
| 169 | $next_weights |
| 170 | ) |
| 171 | ); |
| 172 | } else { |
| 173 | if ( empty( $weight ) ) { |
| 174 | $weight = '400'; |
| 175 | } |
| 176 | |
| 177 | $weight = strval( $weight ); |
| 178 | |
| 179 | if ( $style === 'italic' ) { |
| 180 | $weight . 'italic'; |
| 181 | } |
| 182 | |
| 183 | if ( ! in_array( $weight, $this->window_font_weights ) ) { |
| 184 | $this->window_font_weights[] = $weight; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | function getBlock( $block, $context ) { |
| 192 | $blockName = $block['blockName']; |
| 193 | if ( isset( $this->registered[ $blockName ] ) ) { |
| 194 | $class = $this->registered[ $blockName ]; |
| 195 | $block = new $class( $block, true, $context ); |
| 196 | |
| 197 | return $block; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | function getParentBlock() { |
| 202 | $count = count( $this->blocksStack ); |
| 203 | |
| 204 | return $count > 1 ? $this->blocksStack[ $count - 2 ] : null; |
| 205 | } |
| 206 | |
| 207 | function getLastBlock() { |
| 208 | return Arr::last( $this->blocksStack, null, null ); |
| 209 | } |
| 210 | |
| 211 | function addBlockToStack( $block ) { |
| 212 | $name = $block->block_type->name; |
| 213 | if ( ! isset( $this->lastBlocksByName[ $name ] ) ) { |
| 214 | $this->lastBlocksByName[ $name ] = array(); |
| 215 | } |
| 216 | $this->lastBlocksByName[ $name ][] = $block; |
| 217 | $this->blocksStack[] = $block; |
| 218 | } |
| 219 | |
| 220 | function removeBlockFromStack( $block ) { |
| 221 | $name = $block->block_type->name; |
| 222 | if ( isset( $this->lastBlocksByName[ $name ] ) ) { |
| 223 | array_pop( $this->lastBlocksByName[ $name ] ); |
| 224 | } |
| 225 | |
| 226 | array_pop( $this->blocksStack ); |
| 227 | } |
| 228 | |
| 229 | function getLastBlockOfName( $blockName ) { |
| 230 | $block_names = array(); |
| 231 | if ( ! is_array( $blockName ) ) { |
| 232 | $block_names = array( $blockName ); |
| 233 | } else { |
| 234 | $block_names = $blockName; |
| 235 | } |
| 236 | |
| 237 | foreach ( $block_names as $blockName ) { |
| 238 | if ( isset( $this->lastBlocksByName[ $blockName ] ) ) { |
| 239 | $length = count( $this->lastBlocksByName[ $blockName ] ); |
| 240 | |
| 241 | if ( $length - 1 < 0 ) { |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | return $this->lastBlocksByName[ $blockName ][ $length - 1 ]; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return null; |
| 250 | } |
| 251 | |
| 252 | function createElement( $type, $props = array(), $children = array(), $block = null ) { |
| 253 | $class = $this->getClassForType( $type ); |
| 254 | $tag = Element::DIV; |
| 255 | if ( is_string( $type ) && ! isset( $this->elementsByType[ $type ] ) ) { |
| 256 | $tag = $type; |
| 257 | } |
| 258 | |
| 259 | return new $class( $tag, $props, $children, $block ); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | function getClassForType( $type ) { |
| 264 | $elementsByType = apply_filters( |
| 265 | 'kubio/blocks/elements', |
| 266 | $this->elementsByType |
| 267 | ); |
| 268 | |
| 269 | if ( ! isset( $elementsByType[ $type ] ) ) { |
| 270 | return Element::class; |
| 271 | } |
| 272 | $constructor = $elementsByType[ $type ]; |
| 273 | if ( function_exists( $constructor ) ) { |
| 274 | return call_user_func_array( $constructor, array() ); |
| 275 | } else { |
| 276 | return $constructor; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 |