├── .editorconfig ├── .gitignore ├── README.md ├── composer.json ├── config └── blocks.php └── src ├── AcfBlock.php ├── AcfBlockServiceProvider.php ├── Block.php └── Facades └── AcfBlock.php /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = space 7 | indent_size = 2 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | 13 | [*.php] 14 | indent_size = 4 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sage ACF Blocks 2 | 3 | A Sage 10 helper package for building ACF blocks rendered using blade templates. 4 | 5 | The main difference between this and others similar packages is that it's designed for also rendering the blocks directly from templates that do not have a Block Editor. It also supports rendering different templates per block style. 6 | 7 | _WordPress 5.3 or more is required. A Bedrock installation with Acorn is also required._ 8 | 9 | ### Installation 10 | 11 | 1. If not installed already, add Acorn at the site level in your Bedrocks setup installation 12 | 13 | ```sh 14 | composer require roots/acorn 15 | ```` 16 | 17 | 2. Install the package in yor theme 18 | 19 | ```sh 20 | composer config repositories.sage-acfblocks vcs https://github.com/generoi/sage-acfblocks.git 21 | composer require generoi/sage-acfblocks:dev-master 22 | ``` 23 | 24 | 3. Add `Generoi\Sage\AcfBlocks\BlockServiceProvider::class` to the providers in `config/app.php` or add automatically with: 25 | 26 | ```sh 27 | wp acorn package:discover 28 | ``` 29 | 30 | 3. Publish the default `config/blocks.php` file. 31 | 32 | ```sh 33 | wp acorn vendor:publish 34 | ``` 35 | 36 | ### Getting started example 37 | 38 | 1. Create a directory to store your blocks. 39 | 40 | ```sh 41 | app/Blocks 42 | └── ContentListing 43 | ├── ContentListing.php 44 | ├── assets 45 | │ ├── content-listing.css 46 | │ └── content-listing.js 47 | └── views 48 | ├── content-listing-accordion.blade.php 49 | └── content-listing.blade.php 50 | ``` 51 | 52 | 2. Configure your block in `app/Blocks/ContentListing/ContentListing.php` 53 | 54 | ```php 55 | 'content-listing', 67 | 'title' => 'Content listing', 68 | 'description' => 'A block listing content based on filters', 69 | 'category' => 'sage', 70 | 'align' => 'wide', 71 | 'mode' => 'preview', 72 | 'icon' => 'excerpt-view', 73 | 'keywords' => ['post', 'query'], 74 | 'supports' => [], 75 | 76 | // Unless set dynamically to a URL using the static register() method 77 | // these will be loaded from in the root diretory of the block. 78 | 'enqueue_style' => 'assets/content-listing.css', 79 | 'enqueue_script' => 'assets/content-listing.js', 80 | 81 | // Define block styles which will be automatically added and used 82 | // when looking for block templates. 83 | 'styles' => [ 84 | 'accordion' => 'Accordion', 85 | ], 86 | ]; 87 | 88 | /** 89 | * Data to be passed to the rendered block. 90 | */ 91 | public function with($data, $view) 92 | { 93 | $data['fields'] = (object) array_merge([ 94 | 'posts_per_page' => 3, 95 | 'order_by' => ['date'], 96 | 'order' => 'DESC', 97 | 'post_type' => 'post', 98 | ], $this->block['fields'] ?? get_fields() ?: []); 99 | 100 | $data['posts'] = get_posts($this->query($data['fields'])); 101 | 102 | return $data; 103 | } 104 | 105 | protected function query($data): array 106 | { 107 | $query = [ 108 | 'posts_per_page' => $data->posts_per_page, 109 | 'orderby' => implode(' ', $data->order_by), 110 | 'order' => $data->order, 111 | 'post_type' => $data->post_type, 112 | 'post_status' => 'publish', 113 | ]; 114 | 115 | return $query; 116 | } 117 | 118 | /** 119 | * {@inhertiDoc} 120 | */ 121 | public function render(View $view): string 122 | { 123 | if (empty($view->posts)) { 124 | if ($this->isPreview()) { 125 | return '
' . __('No results found...') . '
'; 126 | } 127 | return ''; 128 | } 129 | 130 | return parent::render($view); 131 | } 132 | } 133 | ``` 134 | 135 | 3. Add the block to `config/block.php` 136 | 137 | ```php 138 | 'blocks' => [ 139 | App\Blocks\ContentListing\ContentListing::class, 140 | ], 141 | ``` 142 | 143 | 4. Create your default template: `views/content-listing.blade.php` 144 | 145 | ```blade 146 |
147 | @foreach ($posts as $post) 148 |
149 |

{{ get_the_title($post) }}

150 | 151 | {{ get_the_excerpt($post) }} 152 |
153 | @endforeach 154 |
155 | ``` 156 | 157 | 5. Create your style variation template: `views/content-listing-