-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
$slug-blocks-integration.php.mustache
149 lines (131 loc) · 3.64 KB
/
$slug-blocks-integration.php.mustache
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
/**
* Class for integrating with WooCommerce Blocks
*/
class {{slugPascalCase}}_Blocks_Integration implements IntegrationInterface {
/**
* The name of the integration.
*
* @return string
*/
public function get_name() {
return '{{slugSnakeCase}}';
}
/**
* When called invokes any initialization/setup for the integration.
*/
public function initialize() {
$this->register_block_frontend_scripts();
$this->register_block_editor_scripts();
$this->register_block_editor_styles();
}
/**
* Returns an array of script handles to enqueue in the frontend context.
*
* @return string[]
*/
public function get_script_handles() {
return array( '{{slug}}-block-frontend' );
}
/**
* Returns an array of script handles to enqueue in the editor context.
*
* @return string[]
*/
public function get_editor_script_handles() {
return array( '{{slug}}-block-editor' );
}
/**
* An array of key, value pairs of data made available to the block on the client side.
*
* @return array
*/
public function get_script_data() {
$data = array(
'optInDefaultText' => __( 'I want to receive updates about products and promotions.', '{{slug}}' ),
);
return $data;
}
/**
* Register block editor styles.
*
* @return void
*/
public function register_block_editor_styles() {
$style_url = {{slugConstantCase}}_BLOCK_PLUGIN_URL . 'build/style-{{slug}}-block.css';
$style_path = {{slugConstantCase}}_BLOCK_PLUGIN_DIR . 'build/style-{{slug}}-block.css';
wp_enqueue_style(
'{{slug}}-block',
$style_url,
[],
$this->get_file_version( $style_path )
);
}
/**
* Register block editor scripts.
*
* @return void
*/
public function register_block_editor_scripts() {
$script_url = {{slugConstantCase}}_BLOCK_PLUGIN_URL . 'build/{{slug}}-block.js';
$script_asset_path = {{slugConstantCase}}_BLOCK_PLUGIN_DIR . 'build/{{slug}}-block.asset.php';
$script_asset = file_exists( $script_asset_path )
? require $script_asset_path
: array(
'dependencies' => array(),
'version' => $this->get_file_version( $script_asset_path ),
);
wp_register_script(
'{{slug}}-block-editor',
$script_url,
$script_asset['dependencies'],
$script_asset['version'],
true
);
wp_set_script_translations(
'{{slug}}-block-editor',
'{{slug}}',
{{slugConstantCase}}_BLOCK_PLUGIN_DIR . 'languages'
);
}
/**
* Register block frontend scripts.
*
* @return void
*/
public function register_block_frontend_scripts() {
$script_url = {{slugConstantCase}}_BLOCK_PLUGIN_URL . 'build/{{slug}}-block-frontend.js';
$script_asset_path = {{slugConstantCase}}_BLOCK_PLUGIN_DIR . 'build/{{slug}}-block-frontend.asset.php';
$script_asset = file_exists( $script_asset_path )
? require $script_asset_path
: array(
'dependencies' => array(),
'version' => $this->get_file_version( $script_asset_path ),
);
wp_register_script(
'{{slug}}-block-frontend',
$script_url,
$script_asset['dependencies'],
$script_asset['version'],
true
);
wp_set_script_translations(
'{{slug}}-block-frontend',
'{{slug}}',
{{slugConstantCase}}_BLOCK_PLUGIN_DIR . 'languages'
);
}
/**
* Get the file modified time as a cache buster if we're in dev mode.
*
* @param string $file Local path to the file.
* @return string The cache buster value to use for the given file.
*/
protected function get_file_version( $file ) {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) {
return filemtime( $file );
}
return {{slugConstantCase}}_BLOCK_PLUGIN_VERSION;
}
}