├── .editorconfig ├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Blocks ├── Block.php └── BlockServiceProvider.php └── config └── blocks.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 4 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Brandon Nifong 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 | # ACF Blocks 2 | 3 | ACF Blocks is a small package for Sage 10 to assist you in easily creating Gutenberg Blocks with Advanced Custom Fields. 4 | 5 | **ABANDONED: USE [acf-composer](https://github.com/Log1x/acf-composer).** 6 | 7 | ## Installation 8 | 9 | ```sh 10 | $ composer require log1x/blocks 11 | ``` 12 | 13 | Add `App\Blocks\BlockServiceProvider::class` to the providers array in `config/app.php`. 14 | 15 | ## Usage 16 | 17 | - Create a directory to store your Blocks. 18 | 19 | ``` 20 | app/Blocks 21 | └── Example # Your Block 22 | ├── Example.php # Where you register your block, fields, and data passed to it's view. 23 | └── views 24 | ├── example.blade.php # Block view 25 | ├── example.css # Block CSS file (optional) 26 | └── example.js # Block JS file (optional) 27 | ``` 28 | 29 | - Register your Block, attach it's fields, and provide data for your view (similar to Sage 10's Composers). 30 | 31 | ```php 32 | # Blocks/Example/Example.php 33 | 'Example', 51 | 'description' => 'Lorem ipsum', 52 | 'category' => 'formatting', 53 | ]; 54 | } 55 | 56 | /** 57 | * Fields to be attached to the block. 58 | * 59 | * @return array 60 | */ 61 | public function fields() 62 | { 63 | $example = new FieldsBuilder('example'); 64 | 65 | $example 66 | ->setLocation('block', '==', 'acf/example'); 67 | 68 | $example 69 | ->addText('label') 70 | ->addTextarea('description') 71 | ->addRepeater('items') 72 | ->addText('item') 73 | ->endRepeater(); 74 | 75 | return $example->build(); 76 | } 77 | 78 | /** 79 | * Data to be passed to the rendered block. 80 | * 81 | * @return array 82 | */ 83 | public function with() 84 | { 85 | return [ 86 | 'label' => $this->label(), 87 | 'description' => $this->description(), 88 | 'items' => $this->items(), 89 | ]; 90 | } 91 | 92 | /** 93 | * Returns the label field. 94 | * 95 | * @return string 96 | */ 97 | public function label() 98 | { 99 | return get_field('label'); 100 | } 101 | 102 | /** 103 | * Returns the description field. 104 | * 105 | * @return string 106 | */ 107 | public function description() 108 | { 109 | return get_field('description'); 110 | } 111 | 112 | /** 113 | * Returns the items field. 114 | * 115 | * @return array 116 | */ 117 | public function items() 118 | { 119 | return get_field('items') ?? []; 120 | } 121 | } 122 | ``` 123 | 124 | - Create your Block's view. 125 | 126 | ```php 127 | # Blocks/Example/views/example.blade.php 128 |
129 |

{{ $label }}

130 |

{{ $description }}

131 | @if ($items) 132 | 137 | @endif 138 |
139 | ``` 140 | 141 | - Load your Block inside of `config/blocks.php`. 142 | 143 | ```php 144 | 'blocks' => [ 145 | App\Blocks\Example\Example::class, 146 | ], 147 | ``` 148 | 149 | and that's it! 150 | 151 | If a CSS or JS file exists inside of your block's `views` folder, they will automatically be enqueued with your block on both the frontend and backend. 152 | 153 | ## License 154 | 155 | ACF Blocks is provided under the [MIT License](https://github.com/log1x/acf-blocks/blob/master/LICENSE.md). 156 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "log1x/acf-blocks", 3 | "type": "library", 4 | "description": "ACF Blocks is a small package for Sage 10 to assist you in easily creating Gutenberg Blocks with Advanced Custom Fields.", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Brandon", 9 | "email": "brandon@tendency.me" 10 | } 11 | ], 12 | "autoload": { 13 | "psr-4": { 14 | "App\\": "src/" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Blocks/Block.php: -------------------------------------------------------------------------------- 1 | register() || ! function_exists('acf')) { 95 | return; 96 | } 97 | 98 | collect($this->register())->each(function ($value, $name) { 99 | $this->{$name} = $value; 100 | }); 101 | 102 | $this->slug = Str::slug($this->name); 103 | $this->fields = $this->fields(); 104 | 105 | if (! $this->enabled) { 106 | return; 107 | } 108 | 109 | add_action('init', function () { 110 | acf_register_block([ 111 | 'name' => $this->slug, 112 | 'title' => $this->name, 113 | 'description' => $this->description, 114 | 'category' => $this->category, 115 | 'icon' => $this->icon, 116 | 'keywords' => $this->keywords, 117 | 'post_types' => $this->post_types, 118 | 'mode' => $this->mode, 119 | 'align' => $this->align, 120 | 'supports' => $this->supports, 121 | 'enqueue_assets' => [$this, 'assets'], 122 | 'render_callback' => [$this, 'view'], 123 | ]); 124 | 125 | if (! empty($this->fields)) { 126 | if (! Arr::has($this->fields, 'location.0.0')) { 127 | Arr::set($this->fields, 'location.0.0', [ 128 | 'param' => 'block', 129 | 'operator' => '==', 130 | 'value' => $this->prefix . $this->slug, 131 | ]); 132 | } 133 | 134 | acf_add_local_field_group($this->fields); 135 | } 136 | }, 20); 137 | } 138 | 139 | /** 140 | * Path for the block. 141 | * 142 | * @return string 143 | */ 144 | protected function path() 145 | { 146 | return dirname((new \ReflectionClass($this))->getFileName()); 147 | } 148 | 149 | /** 150 | * URI for the block. 151 | * 152 | * @return string 153 | */ 154 | protected function uri($path = '') 155 | { 156 | return str_replace( 157 | get_theme_file_path(), 158 | get_theme_file_uri(), 159 | home_url($path) 160 | ); 161 | } 162 | 163 | /** 164 | * View used for rendering the block. 165 | * 166 | * @return \Roots\view 167 | */ 168 | public function view() 169 | { 170 | echo view($this->path() . "/views/{$this->slug}.blade.php", $this->with()); 171 | } 172 | 173 | /** 174 | * Assets used when rendering the block. 175 | * 176 | * @return void 177 | */ 178 | public function assets() 179 | { 180 | if (file_exists($style = $this->path() . "/views/{$this->slug}.css")) { 181 | wp_enqueue_style($this->prefix . $this->slug, $this->uri($style), false, null); 182 | } 183 | 184 | if (file_exists($script = $this->path() . "/views/{$this->slug}.js")) { 185 | wp_enqueue_script($this->prefix . $this->slug, $this->uri($script), null, null, true); 186 | } 187 | } 188 | 189 | /** 190 | * Data to be passed to the block before registering. 191 | * 192 | * @return array 193 | */ 194 | public function register() 195 | { 196 | return []; 197 | } 198 | 199 | /** 200 | * Fields to be attached to the block. 201 | * 202 | * @return array 203 | */ 204 | public function fields() 205 | { 206 | return []; 207 | } 208 | 209 | /** 210 | * Data to be passed to the rendered block. 211 | * 212 | * @return array 213 | */ 214 | public function with() 215 | { 216 | return []; 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/Blocks/BlockServiceProvider.php: -------------------------------------------------------------------------------- 1 | copy(realpath(__DIR__ . '/../config/blocks.php'), app()->configPath('blocks.php')); 20 | } 21 | 22 | collect(config('blocks.blocks')) 23 | ->each(function ($block) { 24 | if (is_string($block)) { 25 | $block = new $block($this); 26 | } 27 | 28 | $block->compose(); 29 | }); 30 | } 31 | 32 | /** 33 | * Bootstrap any application services. 34 | * 35 | * @return void 36 | */ 37 | public function boot() 38 | { 39 | // 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/config/blocks.php: -------------------------------------------------------------------------------- 1 | [ 16 | // App\Blocks\Example\Example::class, 17 | ], 18 | ]; 19 | --------------------------------------------------------------------------------