-
Notifications
You must be signed in to change notification settings - Fork 30
/
wds-blocks.php
128 lines (114 loc) · 3.17 KB
/
wds-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
127
128
<?php
/**
* Plugin Name: WDS Blocks
* Plugin URI: https://github.com/WebDevStudios/WDS-Blocks/
* Description: WebDevStudios library of Gutenberg blocks.
* Author: WebDevStudios
* Author URI: https://webdevstudios.com
* Version: 2.2.3
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wdsblocks
* Domain Path: /languages
*
* @package WebDevStudios\Blocks
* @since 2.0.0
*/
namespace WebDevStudios\Blocks;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Register the block with WordPress.
*
* @author WebDevStudios
* @since 2.0.0
*/
function register_block() {
// Define our assets.
$editor_script = 'build/index.js';
$editor_style = 'build/index.css';
$frontend_style = 'build/style-index.css';
$frontend_script = 'build/frontend.js';
// Verify we have an editor script.
if ( ! file_exists( plugin_dir_path( __FILE__ ) . $editor_script ) ) {
wp_die( esc_html__( 'Whoops! You need to run `npm run build` for the WDS Block Starter first.', 'wdsblocks' ) );
}
// Autoload dependencies and version.
$asset_file = require plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
// Register editor script.
wp_register_script(
'wdsblocks-editor-script',
plugins_url( $editor_script, __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);
// Register editor style.
if ( file_exists( plugin_dir_path( __FILE__ ) . $editor_style ) ) {
wp_register_style(
'wdsblocks-editor-style',
plugins_url( $editor_style, __FILE__ ),
[ 'wp-edit-blocks' ],
filemtime( plugin_dir_path( __FILE__ ) . $editor_style )
);
}
// Register frontend style.
if ( file_exists( plugin_dir_path( __FILE__ ) . $frontend_style ) ) {
wp_register_style(
'wdsblocks-style',
plugins_url( $frontend_style, __FILE__ ),
[],
filemtime( plugin_dir_path( __FILE__ ) . $frontend_style )
);
}
// Register blocks with WordPress.
register_block_type(
'wdsblocks/carousel',
[
'editor_script' => 'wdsblocks-editor-script',
'editor_style' => 'wdsblocks-editor-style',
'style' => 'wdsblocks-style',
]
);
register_block_type(
'wdsblocks/carousel-slide',
[
'editor_script' => 'wdsblocks-editor-script',
'editor_style' => 'wdsblocks-editor-style',
'style' => 'wdsblocks-style',
]
);
// Register frontend script.
if ( file_exists( plugin_dir_path( __FILE__ ) . $frontend_script ) ) {
wp_enqueue_script(
'wdsblocks-frontend-script',
plugins_url( $frontend_script, __FILE__ ),
$asset_file['dependencies'],
$asset_file['version'],
true
);
}
}
add_action( 'init', __NAMESPACE__ . '\register_block' );
/**
* Adds a WDS Block category to the Gutenberg category list.
*
* @author WebDevStudios
* @since 2.0.0
*
* @param array $categories The existing categories.
* @param object $post The current post.
* @return array The updated array of categories.
*/
function register_block_category( $categories, $post ) {
return array_merge(
$categories,
[
[
'slug' => 'wds-blocks',
'title' => esc_html__( 'WDS Blocks', 'wdsblocks' ),
],
]
);
}
add_filter( 'block_categories_all', __NAMESPACE__ . '\register_block_category', 10, 2 );