-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathclass-wp-bootstrap-blocks.php
More file actions
executable file
·267 lines (233 loc) · 6.4 KB
/
class-wp-bootstrap-blocks.php
File metadata and controls
executable file
·267 lines (233 loc) · 6.4 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Main class
*
* @package wp-bootstrap-blocks
*/
namespace WP_Bootstrap_Blocks;
use WP_Bootstrap_Blocks\Button\Button_Block_Type;
use WP_Bootstrap_Blocks\Column\Column_Block_Type;
use WP_Bootstrap_Blocks\Container\Container_Block_Type;
use WP_Bootstrap_Blocks\Row\Row_Block_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class WP_Bootstrap_Blocks
*/
class WP_Bootstrap_Blocks {
/**
* WP_Bootstrap_Blocks instance.
*
* @var WP_Bootstrap_Blocks
*/
protected static $instance = null;
/**
* The plugin version number.
*
* @var string
*/
public $version = '1.2.0';
/**
* The plugin token.
*
* @var string
*/
public $token = 'wp-bootstrap-blocks';
/**
* The plugin assets directory.
*
* @var string
*/
public $assets_dir;
/**
* The plugin assets URL.
*
* @var string
*/
public $assets_url;
/**
* WP_Bootstrap_Blocks constructor.
*/
public function __construct() {
$this->define_constants();
$this->init_plugin_environment();
$this->includes();
$this->init_hooks();
$this->register_block_types();
}
/**
* Define plugin constants.
*/
protected function define_constants() {
if ( ! defined( 'WP_BOOTSTRAP_BLOCKS_ABSPATH' ) ) {
define( 'WP_BOOTSTRAP_BLOCKS_ABSPATH', trailingslashit( dirname( WP_BOOTSTRAP_BLOCKS_PLUGIN_FILE ) ) );
}
}
/**
* Initializes plugin environment variables
*/
protected function init_plugin_environment() {
// Load plugin environment variables
$this->assets_dir = WP_BOOTSTRAP_BLOCKS_ABSPATH . 'dist';
$this->assets_url = esc_url( trailingslashit( plugins_url( '/dist/', WP_BOOTSTRAP_BLOCKS_PLUGIN_FILE ) ) );
}
/**
* Include required core files.
*/
public function includes() {
// Load plugin class files
require_once WP_BOOTSTRAP_BLOCKS_ABSPATH . 'src/wp-bootstrap-blocks-functions.php';
require_once WP_BOOTSTRAP_BLOCKS_ABSPATH . 'src/class-block-type.php';
require_once WP_BOOTSTRAP_BLOCKS_ABSPATH . 'src/container/class-container-block-type.php';
require_once WP_BOOTSTRAP_BLOCKS_ABSPATH . 'src/row/class-row-block-type.php';
require_once WP_BOOTSTRAP_BLOCKS_ABSPATH . 'src/column/class-column-block-type.php';
require_once WP_BOOTSTRAP_BLOCKS_ABSPATH . 'src/button/class-button-block-type.php';
}
/**
* Initializes hooks.
*/
protected function init_hooks() {
// Hook: Frontend assets.
add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) );
// Hook: Editor assets.
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
// Register custom block category
add_filter( 'block_categories', array( $this, 'register_custom_block_category' ), 10, 2 );
// Load textdomain
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
// check version number on each request
add_action( 'init', array( $this, 'check_version' ) );
}
/**
* Load frontend block assets.
*/
public function enqueue_block_assets() {
$enqueue_block_assets = apply_filters( 'wp_bootstrap_blocks_enqueue_block_assets', true );
if ( ! $enqueue_block_assets ) {
return;
}
// Styles.
wp_enqueue_style(
$this->token . '-styles', // Handle.
esc_url( $this->assets_url ) . 'blocks.style.build.css', // Block style CSS.
array(),
$this->version
);
}
/**
* Load editor block assets.
*/
public function enqueue_block_editor_assets() {
// Scripts.
wp_register_script(
$this->token . '-js', // Handle.
esc_url( $this->assets_url ) . 'blocks.build.js', // block.build.js: We register the block here. Built with Webpack.
array(
'wp-blocks',
'wp-i18n',
'wp-element',
'wp-editor',
'wp-components',
'wp-data',
'wp-hooks',
),
$this->version,
true // Enqueue the script in the footer.
);
global $wp_version;
wp_localize_script(
$this->token . '-js',
'wpBootstrapBlocks',
array(
'gutenbergVersion' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : false,
'wpVersion' => $wp_version,
)
);
wp_enqueue_script( $this->token . '-js' );
// Styles.
wp_enqueue_style(
$this->token . '-editor-styles', // Handle.
esc_url( $this->assets_url ) . 'blocks.editor.build.css', // Block editor CSS.
array( 'wp-edit-blocks' ), // Dependency to include the CSS after it.
$this->version
);
}
/**
* Register custom block category
*
* @param array $categories List of all registered categories.
* @param \WP_Post $post Current post object.
*
* @return array
*/
public function register_custom_block_category( $categories, $post ) {
return array_merge(
$categories,
array(
array(
'slug' => 'wp-bootstrap-blocks',
'title' => __( 'Bootstrap Blocks', 'wp-bootstrap-blocks' ),
),
)
);
}
/**
* Load plugin textdomain
*/
public function load_plugin_textdomain() {
$domain = 'wp-bootstrap-blocks'; // textdomain can't be stored in class variable since it must be a single string literal
load_plugin_textdomain( $domain, false, dirname( plugin_basename( WP_BOOTSTRAP_BLOCKS_PLUGIN_FILE ) ) . '/languages/' );
}
/**
* Register block types
*/
public function register_block_types() {
new Container_Block_Type();
new Row_Block_Type();
new Column_Block_Type();
new Button_Block_Type();
}
/**
* Main WP_Bootstrap_Blocks Instance
* Ensures only one instance of WP_Bootstrap_Blocks is loaded or can be loaded.
*
* @return WP_Bootstrap_Blocks Plugin instance
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Cloning is forbidden.
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?' ), esc_attr( $this->version ) );
}
/**
* Unserializing instances of this class is forbidden.
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?' ), esc_attr( $this->version ) );
}
/**
* Checks plugin version.
*
* This check is done on all requests and runs if the versions do not match.
*/
public function check_version() {
if ( ! defined( 'IFRAME_REQUEST' ) && get_option( $this->token . '_version' ) !== $this->version ) {
$this->log_version_number();
do_action( $this->token . '_updated' );
}
}
/**
* Log the plugin version number in database.
*/
protected function log_version_number() {
delete_option( $this->token . '_version' );
update_option( $this->token . '_version', $this->version );
}
}