-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbb-example-acf-blocks.php
More file actions
67 lines (59 loc) · 1.77 KB
/
bb-example-acf-blocks.php
File metadata and controls
67 lines (59 loc) · 1.77 KB
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
<?php
/**
* Plugin Name: Example ACF Blocks for Beaver Builder
* Plugin URI: https://www.wpbeaverbuilder.com
* Description: Example ACF Blocks that can be used in Beaver Builder.
* Version: 0.1
* Author: Beaver Builder
* Author URI: https://www.wpbeaverbuilder.com
* Copyright: (c) 2022 Beaver Builder
* License: GNU General Public License v2.0
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: bb-example-acf-blocks
* Requires at least: 6.0
* Tested up to: 6.1
* Requires PHP: 7.4
*/
define( 'BB_EXAMPLE_ACF_BLOCKS_VERSION', '0.1' );
define( 'BB_EXAMPLE_ACF_BLOCKS_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'BB_EXAMPLE_ACF_BLOCKS_URL', esc_url( trailingslashit( plugins_url( '/', __FILE__ ) ) ) );
/**
* Bail if ACF blocks aren't available.
*/
if ( ! function_exists( 'acf_register_block_type' ) ) {
return false;
}
/**
* Add a custom block category for our example blocks.
*/
add_filter( 'block_categories_all', function( $categories ) {
return array_merge(
$categories,
[
[
'slug' => 'bb-example-acf-blocks',
'title' => __( 'Example ACF Blocks', 'bb-example-acf-blocks' ),
],
]
);
}, 10, 2 );
/**
* Load our example blocks and fields.
*/
add_action( 'acf/init', function() {
$paths = glob( BB_EXAMPLE_ACF_BLOCKS_DIR . 'blocks/*' );
foreach ( $paths as $path ) {
// Load the block
if ( file_exists( "$path/block.json" ) ) {
register_block_type( "$path/block.json" );
}
// Load the fields
if ( file_exists( "$path/fields.json" ) ) {
$fields = json_decode( file_get_contents( "$path/fields.json" ), 1 );
// Only load if no fields exist in the database with this key
if ( empty( acf_get_fields( $fields[0]['key'] ) ) ) {
acf_add_local_field_group( $fields[0] );
}
}
}
} );