7
7
8
8
namespace Viget \ACFBlocksToolkit ;
9
9
10
+ use Timber \Timber ;
11
+
10
12
/**
11
13
* Block Registration Class
12
14
*/
13
15
class Block_Registration {
14
16
15
- /**
16
- * @var string
17
- */
18
- // const ALL_BLOCKS_TRANSIENT = 'acfbt_all_blocks';
19
-
20
17
/**
21
18
* @var array
22
19
*/
@@ -73,7 +70,7 @@ function ( array $metadata ): array {
73
70
return $ metadata ;
74
71
}
75
72
76
- $ metadata ['acf ' ]['renderCallback ' ] = function ( array $ block ): void {
73
+ $ metadata ['acf ' ]['renderCallback ' ] = function ( array $ block, string $ content = '' , bool $ is_preview = false ): void {
77
74
$ block_name = str_replace ( 'acf/ ' , '' , $ block ['name ' ] );
78
75
$ block ['slug ' ] = sanitize_title ( $ block_name );
79
76
if ( empty ( $ block ['path ' ] ) ) {
@@ -82,6 +79,14 @@ function ( array $metadata ): array {
82
79
if ( empty ( $ block ['url ' ] ) ) {
83
80
$ block ['url ' ] = self ::path_to_url ( $ block ['path ' ] );
84
81
}
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
+
85
90
$ render = $ block ['path ' ] . '/render.php ' ;
86
91
87
92
if ( ! file_exists ( $ render ) ) {
@@ -96,7 +101,6 @@ function ( array $metadata ): array {
96
101
};
97
102
98
103
return $ metadata ;
99
-
100
104
},
101
105
5
102
106
);
@@ -112,21 +116,16 @@ public static function get_all_blocks(): array {
112
116
return self ::$ blocks ;
113
117
}
114
118
115
- // $transient = get_transient( self::ALL_BLOCKS_TRANSIENT );
116
- // if ( $transient ) {
117
- // self::$blocks = $transient;
118
- // return $transient;
119
- // }
120
-
121
119
$ locations = self ::get_block_locations ();
122
120
123
121
foreach ( $ locations as $ location ) {
122
+ if ( ! is_dir ( $ location ) ) {
123
+ continue ;
124
+ }
125
+
124
126
self ::get_blocks_in_dir ( $ location , self ::$ blocks );
125
127
}
126
128
127
- // Cache for 30min
128
- // set_transient( ALL_BLOCKS_TRANSIENT, self::$blocks, MINUTE_IN_SECONDS * 30 );
129
-
130
129
return self ::$ blocks ;
131
130
}
132
131
@@ -268,4 +267,33 @@ function ( bool $wrap, string $name ): bool {
268
267
2
269
268
);
270
269
}
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
+ }
271
299
}
0 commit comments