-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblocks.php
81 lines (71 loc) · 1.85 KB
/
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
<?php
/**
* Gutenberg blocks related function
*
* @package W4_Post_List
*/
defined( 'ABSPATH' ) || exit;
/**
* Register gutenberg block function
*/
function w4pl_register_block() {
if ( ! function_exists( 'register_block_type' ) ) {
// Gutenberg is not active.
return;
}
// automatically load dependencies and version.
$asset_file = include W4PL_DIR . 'assets/block/build.asset.php';
wp_register_script(
'w4pl_block',
W4PL_URL . 'assets/block/build.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);
register_block_type(
'w4-post-list/postlist',
array(
'title' => __( 'W4 Post List', 'w4-post-list' ),
'description' => __( 'Display a list from w4 post list plugin.', 'w4-post-list' ),
'supports' => array(
'align' => false,
'html' => false,
),
'editor_script' => 'w4pl_block',
'attributes' => array(
'listId' => array(
'type' => 'string',
'default' => '',
),
'className' => array(
'type' => 'string',
'default' => '',
),
),
'render_callback' => 'w4pl_render_block_postlist',
)
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'w4pl_block', 'w4-post-list' );
}
}
add_action( 'init', 'w4pl_register_block' );
/**
* Render postlist function
*
* @param array $attributes List Attributes for rendering postlist.
*/
function w4pl_render_block_postlist( $attributes ) {
if ( ! empty( $attributes['listId'] ) ) {
$before = '';
$after = '';
// include className if used.
if ( ! empty( $attributes['className'] ) ) {
$before = sprintf( '<div class="%s">', esc_attr( $attributes['className'] ) );
$after = '</div>';
}
return $before . do_shortcode( '[postlist ' . $attributes['listId'] . ']' ) . $after;
} else {
return __( 'No list selected.', 'w4-post-list' );
}
}