-
Notifications
You must be signed in to change notification settings - Fork 7
/
acf-slider-block.php
106 lines (100 loc) · 2.6 KB
/
acf-slider-block.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
<?php
/**
* Plugin Name: ACF Slider Block
* Description: A slider carousel block.
* Version: 0.1.2
* Author: ACF
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: null
* Text Domain: wpe
*
* @package slider-acf
*/
/**
* Register blocks
*
* @return void
*/
function wpe_slider_register_blocks() {
/**
* We register our block's with WordPress's handy
* register_block_type();
*
* @link https://developer.wordpress.org/reference/functions/register_block_type/
*/
register_block_type( __DIR__ . '/blocks/slider/' );
register_block_type( __DIR__ . '/blocks/slide/' );
}
add_action( 'init', 'wpe_slider_register_blocks', 5 );
/**
* Check for JavaScript modules and set
* type="module" based on the registered handle.
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $handle The script's registered handle.
* @return string $tag The <script> tag for the enqueued script.
*/
function wpe_script_attrs( $tag, $handle ) {
if ( str_contains( $handle, 'module' ) ) {
$tag = str_replace( '<script ', '<script type="module" ', $tag );
}
return $tag;
}
add_filter( 'script_loader_tag', 'wpe_script_attrs', 10, 2 );
/**
* Register Swiper scripts.
*
* @return void
*/
function wpe_slider_register_scripts() {
/**
* Front end modules.
*
* @see "viewScript" in
* blocks/slider/block.json
*/
wp_register_script(
'swiper-module-front', // "viewScript" entry.
plugin_dir_url( __FILE__ ) . 'blocks/slider/view.js',
array(),
'11.0.4', // Using Swiper's current version.
true
);
/**
* Editor only modules.
*
* @see "editorScript" in
* blocks/slider/block.json
*/
wp_register_script(
'swiper-module-editor', // editorScript entry.
plugin_dir_url( __FILE__ ) . 'blocks/slider/editor.js',
array(),
'11.0.4', // Using Swiper's current version.
true
);
}
add_action( 'init', 'wpe_slider_register_scripts' );
/**
* Register ACF field group through JSON.
*
* @return void
*/
function wpe_register_acf_fields() {
$path = __DIR__ . '/acf-json/acf-fields.json';
$fields_json = json_decode( file_get_contents( $path ), true ); // phpcs:ignore
$fields_json['location'] = array(
array(
array(
'param' => 'block',
'operator' => '==',
'value' => 'wpe/slide', // Assign to desired block.
),
),
);
$fields_json['local'] = 'json';
$fields_json['local_file'] = $path;
acf_add_local_field_group( $fields_json );
}
add_action( 'acf/include_fields', 'wpe_register_acf_fields' );