├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── composer.lock └── src ├── Editor ├── AbstractBlock.php ├── AbstractPanel.php └── GutengoodBuilder.php ├── Providers ├── BlockServiceProvider.php └── PanelServiceProvider.php └── Traits └── Helpers.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | .idea 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Alexandr Yarovikov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Gutengood 2 | 3 | PHP base for fast building server-side gutenberg blocks and post type panels. 4 | 5 | Examples, documentation and your questions are here https://github.com/yarovikov/gutengood-examples. 6 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yarovikov/gutengood", 3 | "description": "Base for yours blocks", 4 | "license": "MIT", 5 | "type": "package", 6 | "authors": [ 7 | { 8 | "name": "Yarovikov", 9 | "email": "alex.yarovikov@gmail.com", 10 | "homepage": "https://yarovikov.com/", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": "^8.1", 16 | "ext-simplexml": "*" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Yarovikov\\Gutengood\\": "src/" 21 | } 22 | }, 23 | "extra": { 24 | "acorn": { 25 | "providers": [ 26 | "Yarovikov\\Gutengood\\Providers\\BlockServiceProvider", 27 | "Yarovikov\\Gutengood\\Providers\\PanelServiceProvider" 28 | ] 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "5c99fe9b95f2004abd5fa70c1b13fdb1", 8 | "packages": [], 9 | "packages-dev": [], 10 | "aliases": [], 11 | "minimum-stability": "stable", 12 | "stability-flags": [], 13 | "prefer-stable": false, 14 | "prefer-lowest": false, 15 | "platform": { 16 | "php": "^8.1", 17 | "ext-simplexml": "*" 18 | }, 19 | "platform-dev": [], 20 | "plugin-api-version": "2.6.0" 21 | } 22 | -------------------------------------------------------------------------------- /src/Editor/AbstractBlock.php: -------------------------------------------------------------------------------- 1 | fields(), 84 | ...$this->options(), 85 | ...$this->defaultOptions()['fields'], 86 | ]; 87 | 88 | $attributes = $this->getAttributes($fields_and_options); 89 | 90 | register_block_type($this->name, [ 91 | 'attributes' => [...$attributes], 92 | 'render_callback' => fn(array $attributes, string $content): null|View => $this->view ? view($this->view, $this->getBlockData($attributes, $content)) : null, 93 | ]); 94 | } 95 | 96 | /** 97 | * Block data 98 | * 99 | * @param array $attributes 100 | * @param string $content 101 | * 102 | * @return array 103 | */ 104 | public function getBlockData(array $attributes, string $content): array 105 | { 106 | $block_id = uniqid((str_replace(['/', '-'], '_', $this->name)) . '_'); 107 | 108 | return [ 109 | 'name' => $this->name, 110 | 'block_class' => 'gutengood-block' . (!empty($attributes['className']) ? ' ' . $attributes['className'] : ''), 111 | 'block_id' => $block_id, 112 | 'block_styles' => $this->getBlockStyles($attributes, $block_id), 113 | 'content' => $content, 114 | 'is_editor' => $this->checkIfTheEditor(), 115 | 'attributes' => (object) $attributes, 116 | ]; 117 | } 118 | 119 | /** 120 | * Enqueue js/css block 121 | * 122 | * @return void 123 | */ 124 | public function enqueue(): void 125 | { 126 | $assets = $this->getAssets(); 127 | 128 | if (empty($assets)) { 129 | return; 130 | } 131 | 132 | $post = get_post(); 133 | if (!$post instanceof WP_Post) { 134 | return; 135 | } 136 | 137 | $content = (string) apply_filters('content_with_gutengood_blocks', $post->post_content); 138 | 139 | $blocks = parse_blocks($content); 140 | if (empty($blocks)) { 141 | return; 142 | } 143 | 144 | $this->enqueueAssets($blocks, $assets); 145 | } 146 | 147 | public function enqueueAssets(array $blocks, array $assets): void 148 | { 149 | foreach ($blocks as $block) { 150 | if ($this->name === $block['blockName']) { 151 | // Get default attributes 152 | $default_attributes = $this->getBlockDefaultAttributes($block['blockName']); 153 | // Merge default and changed attributes 154 | $block['attrs'] = [...$default_attributes, ...$block['attrs'] ?? []]; 155 | 156 | array_map(function (array $asset) use ($block): void { 157 | if (empty($asset['condition']) || (is_callable($asset['condition']) && $asset['condition']($block))) { 158 | if (!empty($asset['dependencies']) && false === is_admin()) { 159 | bundle($asset['handle'])->enqueueJs(true, $asset['dependencies']); 160 | } else { 161 | bundle($asset['handle'])->enqueue(); 162 | } 163 | } 164 | }, $assets); 165 | } 166 | 167 | // Check if there are inner blocks 168 | if (!empty($block['innerBlocks'])) { 169 | $this->enqueueAssets($block['innerBlocks'], $assets); 170 | } 171 | } 172 | } 173 | 174 | private function getBlockDefaultAttributes(string $block_name): array 175 | { 176 | $block_registry = WP_Block_Type_Registry::get_instance(); 177 | $block_type = $block_registry->get_registered($block_name); 178 | 179 | if ($block_type && isset($block_type->attributes)) { 180 | return array_map(function (array $attribute): mixed { 181 | return isset($attribute['default']) ? $attribute['default'] : null; 182 | }, $block_type->attributes); 183 | } 184 | 185 | return []; 186 | } 187 | 188 | public function blockEndpoint(): void 189 | { 190 | register_rest_route("{$this->name}/v1", '/data', [ 191 | 'methods' => 'GET', 192 | 'callback' => fn(): array => $this->blockData(), 193 | 'permission_callback' => '__return_true', 194 | ]); 195 | } 196 | 197 | /** 198 | * Pass php data to js (used rest api for fetch data in the editor) 199 | * 200 | * @return array 201 | */ 202 | public function blockData(): array 203 | { 204 | return [ 205 | 'edit_mode' => $this->edit_mode, 206 | 'options' => [ 207 | ...$this->options(), 208 | $this->defaultOptions(), 209 | ], 210 | 'fields' => !empty($this->fields()[0]['fields']) ? [...$this->fields()[0]['fields']] : [...$this->fields()], 211 | ]; 212 | } 213 | 214 | /** 215 | * Available components see here resources/scripts/editor/components/block-options.js 216 | * 217 | * @return array 218 | */ 219 | public function fields(): array 220 | { 221 | return []; 222 | } 223 | 224 | /** 225 | * Available components see here resources/scripts/editor/components/block-options.js 226 | * 227 | * @return array 228 | */ 229 | public function options(): array 230 | { 231 | return []; 232 | } 233 | 234 | public function defaultOptions(): array 235 | { 236 | return [ 237 | 'name' => __('Default Options'), 238 | 'type' => 'Section', 239 | 'fields' => [ 240 | [ 241 | 'name' => 'margin_top_desktop', 242 | 'type' => 'Text', 243 | 'label' => 'Margin Top Desktop', 244 | 'value' => static::MARGIN_TOP_DESKTOP, 245 | ], 246 | [ 247 | 'name' => 'margin_top_mobile', 248 | 'type' => 'Text', 249 | 'label' => 'Margin Top Mobile', 250 | 'value' => static::MARGIN_TOP_MOBILE, 251 | ], 252 | [ 253 | 'name' => 'margin_bottom_desktop', 254 | 'type' => 'Text', 255 | 'label' => 'Margin Bottom Desktop', 256 | 'value' => static::MARGIN_BOTTOM_DESKTOP, 257 | ], 258 | [ 259 | 'name' => 'margin_bottom_mobile', 260 | 'type' => 'Text', 261 | 'label' => 'Margin Bottom Mobile', 262 | 'value' => static::MARGIN_BOTTOM_MOBILE, 263 | ], 264 | ], 265 | ]; 266 | } 267 | 268 | /** 269 | * Block meta if needed. Based on fields and options depens on meta parameter 270 | * 271 | * @return array 272 | */ 273 | public function blockMeta(): array 274 | { 275 | $components = [ 276 | ...!empty($this->options()[0]['fields']) ? $this->options()[0]['fields'] : [], 277 | ...!empty($this->fields()[0]['fields']) ? $this->fields()[0]['fields'] : [], 278 | ]; 279 | 280 | return array_filter(array_map(function (array $component): ?array { 281 | if (false === ($component['meta'] ?? false)) { 282 | return null; 283 | } 284 | 285 | return $this->buildMetaArgs($component); 286 | }, $components)); 287 | } 288 | 289 | /** 290 | * Check we are in the editor or front-end 291 | * 292 | * @return bool 293 | */ 294 | public function checkIfTheEditor(): bool 295 | { 296 | return defined('REST_REQUEST') && REST_REQUEST; 297 | } 298 | 299 | /** 300 | * Js/css block assets. Use bud config 301 | * 302 | * 303 | * @return array 304 | */ 305 | public function getAssets(): array 306 | { 307 | return []; 308 | } 309 | 310 | /** 311 | * Default inline block styles 312 | * 313 | * @return string 314 | */ 315 | public function getBlockStyles(array $attributes, string $block_id): string 316 | { 317 | $margins = [ 318 | '--block-margin-top-desktop' => (int) ($attributes['margin_top_desktop'] ?? 0), 319 | '--block-margin-bottom-desktop' => (int) ($attributes['margin_bottom_desktop'] ?? 0), 320 | '--block-margin-top-mobile' => (int) ($attributes['margin_top_mobile'] ?? 0), 321 | '--block-margin-bottom-mobile' => (int) ($attributes['margin_bottom_mobile'] ?? 0), 322 | ]; 323 | 324 | $styles = implode(';', array_map(fn($key, $value) => "{$key}:{$value}px", array_keys($margins), $margins)); 325 | $styles = ""; 326 | 327 | return wp_kses($styles, ['style' => []]); 328 | } 329 | } 330 | -------------------------------------------------------------------------------- /src/Editor/AbstractPanel.php: -------------------------------------------------------------------------------- 1 | setMetaTrue($this->options()); 39 | 40 | register_rest_route("{$this->name}/v1", '/data', [ 41 | 'methods' => 'GET', 42 | 'callback' => fn(): array => ['options' => $options], 43 | 'permission_callback' => '__return_true', 44 | ]); 45 | } 46 | 47 | /** 48 | * Options 49 | * 50 | * @return array 51 | */ 52 | public function options(): array 53 | { 54 | return []; 55 | } 56 | 57 | /** 58 | * Meta 59 | * 60 | * @return array 61 | */ 62 | public function panelMeta(): array 63 | { 64 | $components = [ 65 | ...!empty($this->options()[0]['fields']) ? $this->options()[0]['fields'] : [], 66 | ]; 67 | 68 | return array_filter(array_map(function (array $component): array { 69 | return $this->buildMetaArgs($component); 70 | }, $components)); 71 | } 72 | 73 | public function setMetaTrue(array $data): array 74 | { 75 | return array_map(function (array $item): array { 76 | if ('Section' === ($item['type'] ?? '')) { 77 | if (!empty($item['fields'])) { 78 | $item['fields'] = $this->setMetaTrue($item['fields']); 79 | } 80 | } else { 81 | $item['meta'] = true; 82 | } 83 | return $item; 84 | }, $data); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Editor/GutengoodBuilder.php: -------------------------------------------------------------------------------- 1 | $name, 17 | 'type' => $type, 18 | ...$args, 19 | ]; 20 | 21 | if (!empty($this->section)) { 22 | if (!empty($this->repeater)) { 23 | $this->repeater['fields'][] = $component; 24 | } else { 25 | $this->section['fields'][] = $component; 26 | } 27 | } else { 28 | $this->section = [ 29 | 'name' => __('Block Options'), 30 | 'type' => 'Section', 31 | 'open' => true, 32 | ]; 33 | if (!empty($this->repeater)) { 34 | $this->repeater['fields'][] = $component; 35 | } else { 36 | $this->section['fields'][] = $component; 37 | } 38 | } 39 | 40 | return $this; 41 | } 42 | 43 | /** 44 | * TimePicker control component 45 | * 46 | * @param string $name The name of the component. 47 | * @param array $args (string label, string help, bool is12hour, bool meta) 48 | * 49 | * @return self 50 | */ 51 | public function addTimePicker(string $name, array $args = []): self 52 | { 53 | return $this->addComponent($name, 'TimePicker', $args); 54 | } 55 | 56 | /** 57 | * File component 58 | * 59 | * @param string $name The name of the component. 60 | * @param array $args (string label, string help, bool meta) 61 | * 62 | * @return self 63 | */ 64 | public function addFile(string $name, array $args = []): self 65 | { 66 | return $this->addComponent($name, 'File', $args); 67 | } 68 | 69 | /** 70 | * Link component 71 | * 72 | * @param string $name The name of the component. 73 | * @param array $args (string label, string placeholder, bool use_title, bool meta) 74 | * 75 | * @return self 76 | */ 77 | public function addLink(string $name, array $args = []): self 78 | { 79 | return $this->addComponent($name, 'Link', $args); 80 | } 81 | 82 | /** 83 | * Text control component 84 | * 85 | * @param string $name The name of the component. 86 | * @param array $args (string label, string help, string value, string placeholder, bool meta) 87 | * 88 | * @return self 89 | */ 90 | public function addText(string $name, array $args = []): self 91 | { 92 | return $this->addComponent($name, 'Text', $args); 93 | } 94 | 95 | /** 96 | * Textarea control component 97 | * 98 | * @param string $name The name of the component. 99 | * @param array $args (string label, string help, string value, string placeholder, bool meta) 100 | * 101 | * @return self 102 | */ 103 | public function addTextarea(string $name, array $args = []): self 104 | { 105 | return $this->addComponent($name, 'Textarea', $args); 106 | } 107 | 108 | /** 109 | * Toggle control component 110 | * 111 | * @param string $name The name of the component. 112 | * @param array $args (string label, string help, bool value, bool meta) 113 | * 114 | * @return self 115 | */ 116 | public function addToggle(string $name, array $args = []): self 117 | { 118 | return $this->addComponent($name, 'Toggle', $args); 119 | } 120 | 121 | /** 122 | * ColorPalette control component 123 | * 124 | * @param string $name The name of the component. 125 | * @param array $args (string label, string help, array colors (string name, string color hex, string slug), string value, bool meta) 126 | * 127 | * @return self 128 | */ 129 | public function addColorPalette(string $name, array $args = []): self 130 | { 131 | return $this->addComponent($name, 'ColorPalette', $args); 132 | } 133 | 134 | /** 135 | * ColorPicker control component 136 | * 137 | * @param string $name The name of the component. 138 | * @param array $args (string label, string help, bool alfa, bool meta) 139 | * 140 | * @return self 141 | */ 142 | public function addColorPicker(string $name, array $args = []): self 143 | { 144 | return $this->addComponent($name, 'ColorPicker', $args); 145 | } 146 | 147 | /** 148 | * Select control component 149 | * 150 | * @param string $name The name of the component. 151 | * @param array $args (string label, string help, array choices (string label, string value), string value, bool meta) 152 | * 153 | * @return self 154 | */ 155 | public function addSelect(string $name, array $args = []): self 156 | { 157 | return $this->addComponent($name, 'Select', $args); 158 | } 159 | 160 | /** 161 | * Message component 162 | * 163 | * @param string $name The name of the component. 164 | * @param array $args (string label, string help) 165 | * 166 | * @return self 167 | */ 168 | public function addMessage(string $name, array $args = []): self 169 | { 170 | return $this->addComponent($name, 'Message', $args); 171 | } 172 | 173 | /** 174 | * Image component 175 | * 176 | * @param string $name The name of the component. 177 | * @param array $args (string label, string help, int value, bool meta) 178 | * 179 | * @return self 180 | */ 181 | public function addImage(string $name, array $args = []): self 182 | { 183 | return $this->addComponent($name, 'Image', $args); 184 | } 185 | 186 | /** 187 | * RichText component 188 | * 189 | * @param string $name The name of the component. 190 | * @param array $args (string label, string help, string placeholder, string value, bool meta) 191 | * 192 | * @return self 193 | */ 194 | public function addRichText(string $name, array $args = []): self 195 | { 196 | return $this->addComponent($name, 'RichText', $args); 197 | } 198 | 199 | /** 200 | * Range control component 201 | * 202 | * @param string $name The name of the component. 203 | * @param array $args (string label, string help, string placeholder, int step, int min, int max, int value, bool meta) 204 | * 205 | * @return self 206 | */ 207 | public function addRange(string $name, array $args = []): self 208 | { 209 | return $this->addComponent($name, 'Range', $args); 210 | } 211 | 212 | /** 213 | * Repeater 214 | * 215 | * @param string $name The name of the repeater component. 216 | * @param array $args (string label, string help, string button_label, bool meta) 217 | * 218 | * @return self 219 | */ 220 | public function addRepeater(string $name, array $args = []): self 221 | { 222 | $this->repeater = [ 223 | 'name' => $name, 224 | 'type' => 'Repeater', 225 | 'fields' => [], 226 | ...$args, 227 | ]; 228 | 229 | return $this; 230 | } 231 | 232 | public function endRepeater(): self 233 | { 234 | $this->section['fields'][] = $this->repeater; 235 | $this->repeater = []; 236 | 237 | return $this; 238 | } 239 | 240 | /** 241 | * Section. Use this as accordion panel for options 242 | * 243 | * @param string $name The name of the section. 244 | * @param array $args (bool open) 245 | * 246 | * @return self 247 | */ 248 | public function addSection(string $name, array $args = []): self 249 | { 250 | if (!empty($this->section)) { 251 | $this->components[] = $this->section; 252 | } 253 | 254 | $this->section = [ 255 | 'name' => $name, 256 | 'type' => 'Section', 257 | 'fields' => [], 258 | ...$args, 259 | ]; 260 | 261 | return $this; 262 | } 263 | 264 | public function endSection(): self 265 | { 266 | if (!empty($this->section)) { 267 | $this->components[] = $this->section; 268 | $this->section = []; 269 | } 270 | 271 | return $this; 272 | } 273 | 274 | public function conditional(string $name, mixed $value): self 275 | { 276 | if (!empty($this->section)) { 277 | if (!empty($this->repeater)) { 278 | $index = count($this->repeater['fields']) - 1; 279 | if ($index >= 0) { 280 | $this->repeater['fields'][$index]['condition'] = ['name' => $name, 'value' => $value]; 281 | } 282 | } else { 283 | $index = count($this->section['fields']) - 1; 284 | if ($index >= 0) { 285 | $this->section['fields'][$index]['condition'] = ['name' => $name, 'value' => $value]; 286 | } 287 | } 288 | } 289 | 290 | return $this; 291 | } 292 | 293 | public function build(): array 294 | { 295 | if (!empty($this->section)) { 296 | $this->components[] = $this->section; 297 | } 298 | 299 | return $this->components; 300 | } 301 | } 302 | -------------------------------------------------------------------------------- /src/Providers/BlockServiceProvider.php: -------------------------------------------------------------------------------- 1 | makeInstances(); 36 | } 37 | 38 | public function boot(): void 39 | { 40 | add_action('wp_enqueue_scripts', [$this, 'enqueue']); 41 | add_action('enqueue_block_editor_assets', [$this, 'enqueue']); 42 | add_action('enqueue_block_editor_assets', [$this, 'enqueueBlockEditorAssets']); 43 | add_action('init', [$this, 'registerBlocks']); 44 | add_action('init', [$this, 'registerMeta']); 45 | add_action('rest_api_init', [$this, 'blockEndpoint']); 46 | } 47 | 48 | /** 49 | * Make instances of the blocks. Make $blocks object 50 | * 51 | * @return void 52 | */ 53 | public function makeInstances(): void 54 | { 55 | $this->blocks = collect(); 56 | 57 | $directory = $this->app->basePath('app/' . $this->folder); 58 | 59 | if (is_dir($directory)) { 60 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); 61 | foreach ($iterator as $file) { 62 | if ($file->isFile() && $file->getExtension() === 'php') { 63 | $src = $this->formatFile($this->folder, $file->getPathname()); 64 | 65 | $this->app->bind("block.$src->handle", function () use ($src) { 66 | return new $src->class(); 67 | }); 68 | 69 | $this->blocks->push("block.$src->handle"); 70 | } 71 | } 72 | } 73 | } 74 | 75 | public function registerBlocks(): void 76 | { 77 | $blocks = $this->blocks; 78 | 79 | if (empty($blocks)) { 80 | return; 81 | } 82 | 83 | foreach ($blocks as $block) { 84 | if (!WP_Block_Type_Registry::get_instance()->is_registered($this->app[$block]->name)) { 85 | $this->app[$block]->registerBlockType(); 86 | } 87 | } 88 | } 89 | 90 | /** 91 | * Enqueue the block assets with the block editor and front-end. 92 | * 93 | * @return void 94 | */ 95 | public function enqueue(): void 96 | { 97 | $blocks = $this->blocks; 98 | 99 | if (empty($blocks)) { 100 | return; 101 | } 102 | 103 | foreach ($blocks as $block) { 104 | $this->app[$block]->enqueue(); 105 | } 106 | } 107 | 108 | public function enqueueBlockEditorAssets(): void 109 | { 110 | $blocks = $this->blocks; 111 | 112 | if (empty($blocks)) { 113 | return; 114 | } 115 | 116 | $gutengood_blocks = []; 117 | 118 | foreach ($blocks as $block) { 119 | if (false === $this->app[$block]->editor_script) { 120 | $gutengood_blocks[] = (object) [ 121 | 'title' => $this->app[$block]->title, 122 | 'name' => $this->app[$block]->name, 123 | 'description' => $this->app[$block]->description, 124 | 'icon' => $this->getIcon($this->app[$block]->icon), 125 | 'category' => $this->app[$block]->category, 126 | ]; 127 | } 128 | } 129 | 130 | if (empty($gutengood_blocks)) { 131 | return; 132 | } 133 | 134 | wp_localize_script('editor', 'gutengoodBlocks', $gutengood_blocks); 135 | } 136 | 137 | public function blockEndpoint(): void 138 | { 139 | $blocks = $this->blocks; 140 | 141 | if (empty($blocks)) { 142 | return; 143 | } 144 | 145 | foreach ($blocks as $block) { 146 | $this->app[$block]->blockEndpoint(); 147 | } 148 | } 149 | 150 | /** 151 | * Register post meta for using in blocks (optional) 152 | * 153 | * @return void 154 | */ 155 | public function registerMeta(): void 156 | { 157 | $blocks = $this->blocks; 158 | 159 | if (empty($blocks)) { 160 | return; 161 | } 162 | 163 | foreach ($blocks as $block) { 164 | array_map(function (array $meta): void { 165 | if (empty($meta) || empty($meta['meta_key'])) { 166 | return; 167 | } 168 | 169 | register_post_meta( 170 | '', 171 | $meta['meta_key'], 172 | [ 173 | 'show_in_rest' => $meta['show_in_rest'] ?? true, 174 | 'single' => true, 175 | 'type' => $meta['type'], 176 | 'default' => $meta['default'], 177 | 'auth_callback' => fn(): bool => current_user_can('edit_posts'), 178 | ] 179 | ); 180 | }, $this->app[$block]->blockMeta()); 181 | } 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /src/Providers/PanelServiceProvider.php: -------------------------------------------------------------------------------- 1 | makeInstances(); 35 | } 36 | 37 | public function boot(): void 38 | { 39 | add_action('enqueue_block_editor_assets', [$this, 'enqueueBlockEditorAssets']); 40 | add_action('init', [$this, 'registerMeta']); 41 | add_action('rest_api_init', [$this, 'panelEndpoint']); 42 | } 43 | 44 | /** 45 | * Make instances of the panels. Make $panels object 46 | * 47 | * @return void 48 | */ 49 | public function makeInstances(): void 50 | { 51 | $this->panels = collect(); 52 | 53 | $directory = $this->app->basePath('app/' . $this->folder); 54 | 55 | if (is_dir($directory)) { 56 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); 57 | 58 | foreach ($iterator as $file) { 59 | if ($file->isFile() && $file->getExtension() === 'php') { 60 | $src = $this->formatFile($this->folder, $file->getPathname()); 61 | 62 | $this->app->bind("panel.$src->handle", function () use ($src) { 63 | return new $src->class(); 64 | }); 65 | 66 | $this->panels->push("panel.$src->handle"); 67 | } 68 | } 69 | } 70 | } 71 | 72 | public function enqueueBlockEditorAssets(): void 73 | { 74 | $panels = $this->panels; 75 | 76 | if (empty($panels)) { 77 | return; 78 | } 79 | 80 | $gutengood_panels = []; 81 | 82 | foreach ($panels as $panel) { 83 | $gutengood_panels[] = (object) [ 84 | 'name' => "{$this->app[$panel]->name}", 85 | 'icon' => $this->getIcon($this->app[$panel]->icon), 86 | 'title' => $this->app[$panel]->title, 87 | 'post_types' => $this->app[$panel]->post_types, 88 | ]; 89 | } 90 | 91 | if (empty($gutengood_panels)) { 92 | return; 93 | } 94 | 95 | wp_localize_script('editor', 'gutengoodPanels', $gutengood_panels); 96 | } 97 | 98 | public function panelEndpoint(): void 99 | { 100 | $panels = $this->panels; 101 | 102 | if (empty($panels)) { 103 | return; 104 | } 105 | 106 | foreach ($panels as $panel) { 107 | $this->app[$panel]->panelEndpoint(); 108 | } 109 | } 110 | 111 | /** 112 | * Register post meta for using in panels (optional) 113 | * 114 | * @return void 115 | */ 116 | public function registerMeta(): void 117 | { 118 | $panels = $this->panels; 119 | 120 | if (empty($panels)) { 121 | return; 122 | } 123 | 124 | foreach ($panels as $panel) { 125 | array_map(function (array $meta): void { 126 | if (empty($meta) || empty($meta['meta_key'])) { 127 | return; 128 | } 129 | 130 | register_post_meta( 131 | '', 132 | $meta['meta_key'], 133 | [ 134 | 'show_in_rest' => $meta['show_in_rest'] ?? true, 135 | 'single' => true, 136 | 'type' => $meta['type'], 137 | 'default' => $meta['default'], 138 | 'auth_callback' => fn(): bool => current_user_can('edit_posts'), 139 | ] 140 | ); 141 | }, $this->app[$panel]->panelMeta()); 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/Traits/Helpers.php: -------------------------------------------------------------------------------- 1 | $xml]) : $icon; 21 | } 22 | return $icon; 23 | } 24 | 25 | public function formatFile(string $class, string $file): object 26 | { 27 | return (object) [ 28 | 'handle' => substr(strtolower(basename(preg_replace('/[A-Z]/', '-$0', $file), '.php')), 1), 29 | 'class' => '\\App\\' . str_replace('/', '\\', str_replace('.php', '', str_replace('/app/', '', strstr($file, '/app/Editor/')))), 30 | ]; 31 | } 32 | 33 | /** 34 | * Attributes 35 | * 36 | * Types: null, boolean, object, array, string, integer 37 | * @var array 38 | */ 39 | public function getAttributes(array $fields_and_options): array 40 | { 41 | if (empty($fields_and_options)) { 42 | return []; 43 | } 44 | 45 | $attributes = []; 46 | 47 | foreach ($fields_and_options as $field_or_option) { 48 | if ('Section' === ($field_or_option['type'] ?? '')) { 49 | foreach ($field_or_option['fields'] as $section_field_or_option) { 50 | $attributes[$section_field_or_option['name']] = $this->getDefaultAttribute($section_field_or_option['type'], $section_field_or_option['value'] ?? ''); 51 | } 52 | } else { 53 | $attributes[$field_or_option['name']] = $this->getDefaultAttribute($field_or_option['type'], $field_or_option['value'] ?? ''); 54 | } 55 | } 56 | 57 | return array_filter($attributes); 58 | } 59 | 60 | public function getDefaultAttribute(string $type, mixed $value): ?array 61 | { 62 | return match ($type) { 63 | 'TimePicker', 'Text', 'Textarea', 'Select', 'ColorPalette', 'ColorPicker', 'RichText' => [ 64 | 'type' => 'string', 65 | 'default' => (string) ($value ?? ''), 66 | ], 67 | 'Image', 'Range' => [ 68 | 'type' => 'integer', 69 | 'default' => (int) ($value ?? ''), 70 | ], 71 | 'Toggle' => [ 72 | 'type' => 'boolean', 73 | 'default' => (bool) ($value ?? ''), 74 | ], 75 | 'File', 'Link' => [ 76 | 'type' => 'object', 77 | 'default' => !empty($value) ? (object) $value : (object) [], 78 | ], 79 | 'Gallery' => [ 80 | 'type' => 'array', 81 | 'default' => array_filter((array) ($value ?? [])), 82 | ], 83 | 'Repeater' => [ 84 | 'type' => 'array', 85 | 'default' => array_map(function (array $item): array { 86 | $item['id'] = substr(hash('sha256', uniqid((string) random_int(1000000000000, 9999999999999), true)), 0, 13); 87 | return $item; 88 | }, array_filter((array) ($value ?? []))), 89 | ], 90 | default => null, 91 | }; 92 | } 93 | 94 | /** 95 | * Build meta args scheme for Repeater 96 | * 97 | * @return array 98 | */ 99 | public function buildMetaArgs(array $component): array 100 | { 101 | $args = [ 102 | 'meta_key' => $component['name'], 103 | ...$this->getDefaultAttribute($component['type'], $component['default'] ?? ''), 104 | ]; 105 | 106 | if ('File' === $component['type']) { 107 | $args ['show_in_rest'] = [ 108 | 'schema' => [ 109 | 'type' => 'object', 110 | 'properties' => [ 111 | 'id' => [ 112 | 'type' => 'integer', 113 | ], 114 | 'url' => [ 115 | 'type' => 'string', 116 | ], 117 | 'name' => [ 118 | 'type' => 'string', 119 | ], 120 | 'size' => [ 121 | 'type' => 'string', 122 | ], 123 | ], 124 | ], 125 | ]; 126 | } 127 | 128 | if ('Link' === $component['type']) { 129 | $args ['show_in_rest'] = [ 130 | 'schema' => [ 131 | 'type' => 'object', 132 | 'properties' => [ 133 | 'id' => [ 134 | 'type' => 'integer', 135 | ], 136 | 'url' => [ 137 | 'type' => 'string', 138 | ], 139 | 'title' => [ 140 | 'type' => 'string', 141 | ], 142 | 'type' => [ 143 | 'type' => 'string', 144 | ], 145 | 'kind' => [ 146 | 'type' => 'string', 147 | ], 148 | ], 149 | ], 150 | ]; 151 | } 152 | 153 | if ('Repeater' === $component['type'] && !empty($component['fields'])) { 154 | $args ['show_in_rest'] = [ 155 | 'schema' => [ 156 | 'type' => 'array', 157 | 'items' => [ 158 | 'type' => 'object', 159 | 'properties' => [ 160 | 'id' => [ 161 | 'type' => 'integer', 162 | ], 163 | ...array_reduce($component['fields'], function (array $carry, array $item): array { 164 | $carry[$item['name']] = $this->buildMetaArgs($item); 165 | return $carry; 166 | }, []), 167 | ], 168 | ], 169 | ], 170 | ]; 171 | } 172 | 173 | return $args; 174 | } 175 | } --------------------------------------------------------------------------------