-
Notifications
You must be signed in to change notification settings - Fork 0
/
sage-acf-gutenberg-blocks.php
126 lines (107 loc) · 4.7 KB
/
sage-acf-gutenberg-blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Gutenberg Blocks for Roots/Sage theme 10
* Version: 1.0.7
* Author: Łukasz Górski / TotalDigital
*/
namespace App;
if ( function_exists( 'add_action' ) ) {
add_action( 'after_setup_theme', function () {
if ( ! function_exists( 'acf_register_block_type' ) ) {
return;
}
if ( ! function_exists( 'add_filter' ) ) {
return;
}
if ( ! function_exists( 'add_action' ) ) {
return;
}
add_filter( 'sage-acf-gutenberg-blocks-templates', function () {
return [ 'views/blocks' ];
} );
}, 30 );
}
if ( function_exists( 'add_action' ) ) {
add_action( 'acf/init', function () {
global $sage_error;
$directories = apply_filters( 'sage-acf-gutenberg-blocks-templates', [] );
foreach ( $directories as $directory ) {
$dir = \Roots\resource_path( 'views/blocks' );
$blocks_path = ( 'resources/views/blocks' );
if ( ! file_exists( $dir ) ) {
return;
}
$blocks_directory = new \DirectoryIterator( $dir );
foreach ( $blocks_directory as $directory ) {
if ( ! $directory->isDot() ) {
$block_name = $directory->getFilename();
$block_path = "{$dir}/{$block_name}";
$file = "{$block_path}/{$block_name}.blade.php";
$config = "{$block_path}/config.php";
$theme_url = get_template_directory_uri();
$theme_path = get_template_directory();
$file_headers = get_file_data( $file, [
'title' => 'Title',
'category' => 'Category',
] );
$options = [
'name' => $block_name,
'title' => __( $file_headers['title'] ),
'category' => $file_headers['category'],
'render_callback' => __NAMESPACE__ . '\\blocks_callback',
'supports' => [
'align' => [ 'full', 'center', 'wide' ],
],
'align' => empty( $file_headers['align'] ) ? 'full' : $file_headers['align'],
'theme_url' => $theme_url,
'theme_path' => $theme_path,
'block_name' => $block_name,
'example' => [
'attributes' => [
'mode' => 'preview',
'data' => [
'preview_image' => "{$theme_url}/{$blocks_path}/{$block_name}/screenshot.png",
"is_preview" => 1
],
]
],
];
\acf_register_block_type( apply_filters( "sage/blocks/$block_name/register-data", $options ) );
if ( ! file_exists( $config ) ) {
continue;
}
require_once( $config );
acf_add_local_field_group( [
"key" => "group_{$block_name}",
"title" => "BLOCK: {$file_headers['title']}",
"fields" => $fields,
'location' => [ [ [ 'param' => 'block', 'operator' => '==', 'value' => "acf/{$block_name}" ] ] ],
] );
}
}
}
} );
}
function blocks_callback( $block, $content = '', $is_preview = false, $post_id = 0 ) {
$slug = str_replace( 'acf/', '', $block['name'] );
$block['post_id'] = $post_id;
$block['preview'] = $is_preview;
$block['content'] = $content;
$block['slug'] = $slug;
$block['anchor'] = isset( $block['anchor'] ) ? $block['anchor'] : '';
$block['classes'] = [
$slug,
$block['preview'] ? 'is-preview' : null,
'align' . $block['align']
];
$block['preview_image'] = $block['example']['attributes']['data']['preview_image'];
$block = apply_filters( "sage/blocks/$slug/data", $block );
$block['classes'] = implode( ' ', array_filter( $block['classes'] ) );
$directories = apply_filters( 'sage-acf-gutenberg-blocks-templates', [] );
foreach ( $directories as $directory ) {
$view = ltrim( $directory, 'views/' ) . '/' . $slug . '/' . $slug;
if ( \Roots\view()->exists( $view ) ) {
echo \Roots\view( $view, [ 'block' => $block ] );
}
}
}