Skip to content

Commit 77e3df8

Browse files
authored
[#11] Using Timber/Twig (#12)
* [#11] Using Timber/Twig * [N/A] PR Template
1 parent d70fcee commit 77e3df8

File tree

9 files changed

+761
-24
lines changed

9 files changed

+761
-24
lines changed

.github/pull_request_template.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Summary
2+
3+
<!-- Brief Description of what this PR does. -->
4+
5+
## Issues
6+
7+
* List any related issues.
8+
9+
## Testing Instructions
10+
11+
1. List out testing instructions
12+
13+
## Screenshots
14+
15+
<!-- Optional -->

wp-content/plugins/acf-blocks-toolkit/acf-blocks-toolkit.php

+3
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,8 @@
4141
// Settings functions.
4242
require_once 'includes/settings.php';
4343

44+
// Timber functions.
45+
require_once 'includes/timber.php';
46+
4447
Block_Registration::init();
4548
Settings::init();

wp-content/plugins/acf-blocks-toolkit/includes/helpers.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,12 @@ function get_field_property( string $selector, string $property, string $group_i
261261
*
262262
* @since 1.0.0
263263
*
264-
* @param array $props
265-
* @param array allowedBlocks Allowed blocks
266-
* @param array template Block Template
267-
* @param string templateLock Template Lock
268-
* @param string className Class Name
264+
* @param array $props {
265+
* @type array $allowedBlocks Allowed blocks
266+
* @type array $template Block Template
267+
* @type string $templateLock Template Lock
268+
* @type string $className Class Name
269+
* }
269270
*
270271
* @return void
271272
*/

wp-content/plugins/acf-blocks-toolkit/includes/register.php

+44-16
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77

88
namespace Viget\ACFBlocksToolkit;
99

10+
use Timber\Timber;
11+
1012
/**
1113
* Block Registration Class
1214
*/
1315
class Block_Registration {
1416

15-
/**
16-
* @var string
17-
*/
18-
// const ALL_BLOCKS_TRANSIENT = 'acfbt_all_blocks';
19-
2017
/**
2118
* @var array
2219
*/
@@ -73,7 +70,7 @@ function ( array $metadata ): array {
7370
return $metadata;
7471
}
7572

76-
$metadata['acf']['renderCallback'] = function ( array $block ): void {
73+
$metadata['acf']['renderCallback'] = function ( array $block, string $content = '', bool $is_preview = false ): void {
7774
$block_name = str_replace( 'acf/', '', $block['name'] );
7875
$block['slug'] = sanitize_title( $block_name );
7976
if ( empty( $block['path'] ) ) {
@@ -82,6 +79,14 @@ function ( array $metadata ): array {
8279
if ( empty( $block['url'] ) ) {
8380
$block['url'] = self::path_to_url( $block['path'] );
8481
}
82+
83+
$twig = $block['path'] . '/render.twig';
84+
85+
if ( class_exists( '\Timber\Timber' ) && file_exists( $twig ) ) {
86+
self::render_twig_block( $twig, $block, $content, $is_preview );
87+
return;
88+
}
89+
8590
$render = $block['path'] . '/render.php';
8691

8792
if ( ! file_exists( $render ) ) {
@@ -96,7 +101,6 @@ function ( array $metadata ): array {
96101
};
97102

98103
return $metadata;
99-
100104
},
101105
5
102106
);
@@ -112,21 +116,16 @@ public static function get_all_blocks(): array {
112116
return self::$blocks;
113117
}
114118

115-
// $transient = get_transient( self::ALL_BLOCKS_TRANSIENT );
116-
// if ( $transient ) {
117-
// self::$blocks = $transient;
118-
// return $transient;
119-
// }
120-
121119
$locations = self::get_block_locations();
122120

123121
foreach ( $locations as $location ) {
122+
if ( ! is_dir( $location ) ) {
123+
continue;
124+
}
125+
124126
self::get_blocks_in_dir( $location, self::$blocks );
125127
}
126128

127-
// Cache for 30min
128-
// set_transient( ALL_BLOCKS_TRANSIENT, self::$blocks, MINUTE_IN_SECONDS * 30 );
129-
130129
return self::$blocks;
131130
}
132131

@@ -268,4 +267,33 @@ function ( bool $wrap, string $name ): bool {
268267
2
269268
);
270269
}
270+
271+
/**
272+
* Render Twig block
273+
*
274+
* @param string $template
275+
* @param array $block
276+
* @param string $content
277+
* @param bool $is_preview
278+
*
279+
* @return void
280+
*/
281+
public static function render_twig_block( string $template, array $block = [], string $content = '', bool $is_preview = false, int $post_id = 0 ): void {
282+
$context = Timber::context();
283+
284+
// Store block attributes.
285+
$context['attributes'] = $block;
286+
287+
// Store field values. These are the fields from your ACF field group for the block.
288+
$context['fields'] = get_fields();
289+
290+
// Store whether the block is being rendered in the editor or on the frontend.
291+
$context['is_preview'] = $is_preview;
292+
293+
// Store the current post ID.
294+
$context['post_id'] = $post_id;
295+
296+
// Render the block.
297+
Timber::render( $template, $context );
298+
}
271299
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Timber Integration
4+
*
5+
* @package ACFBlocksToolkit
6+
*/
7+
8+
add_action(
9+
'after_setup_theme',
10+
function () {
11+
if ( ! class_exists( 'Timber\Timber' ) ) {
12+
return;
13+
}
14+
15+
add_filter(
16+
'timber/twig/functions',
17+
function ( array $functions ): array {
18+
$functions['inner_blocks'] = [
19+
'callable' => 'inner_blocks',
20+
];
21+
$functions['block_attrs'] = [
22+
'callable' => 'block_attrs',
23+
];
24+
25+
return $functions;
26+
}
27+
);
28+
29+
}
30+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{#
2+
Block: CTA
3+
#}
4+
5+
{% set template = [
6+
[
7+
'core/pattern',
8+
{
9+
'slug': 'wp-starter/cta-dynamic'
10+
}
11+
]
12+
] %}
13+
{% set inner = { template: template } %}
14+
<section {{ block_attrs( attributes ) }}>
15+
{{ inner_blocks( inner ) }}
16+
</section>
+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"require": {
3-
"idleberg/wordpress-vite-assets": "^1.0"
3+
"idleberg/wordpress-vite-assets": "^1.0",
4+
"timber/timber": "^2.1"
5+
},
6+
"config": {
7+
"allow-plugins": {
8+
"composer/installers": true
9+
}
410
}
5-
}
11+
}

0 commit comments

Comments
 (0)