-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
116 lines (103 loc) · 2.8 KB
/
index.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
<?php
/**
* Plugin Name: Starter Block Plugin
* Plugin URI: https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/
* Description: This is an example plugin for setting up WordPress editor blocks using the WP Scripts package.
* Version: 1.0.0
* Author: Sky
*
* @package starter-block-plugin
*/
defined( 'ABSPATH' ) || exit;
// Add custom block category.
function gutenberg_examples_block_categories( $categories ) {
return array_merge(
$categories,
array(
array(
'slug' => 'starter-block-plugin-examples',
'title' => 'Starter Block Plugin Examples',
),
)
);
}
add_action( 'block_categories', 'gutenberg_examples_block_categories', 10, 2 );
/**
* Class to set up plugin blocks.
*/
class Starter_Block_Plugin_Block {
/**
* block.json file path.
*
* @var string
*/
public $block_json;
/**
* Block directory.
*
* @var string
*/
public $block_dir;
/**
* Get the template file for the block. Uses the directory of the block's block.json file to find the template.
*/
public function get_template() {
return $this->block_dir . 'template.php';
}
/**
* Render the block on the frontend.
*
* @param array $attributes The array of attributes for this block.
* @param string $content Rendered block output. ie. <InnerBlocks.Content />.
* @param WP_Block $block_instance The instance of the WP_Block class that represents the block being rendered.
*
* @return string Rendered block output.
*/
public function render( $attributes, $content, $block_instance ) {
ob_start();
require $this->get_template();
return ob_get_clean();
}
/**
* Register the block with WordPress.
* Always dynamic, uses template.php in the same directory as the block.json file.
* OPINION: all blocks are dynamic.
*
* @return void
*/
public function register_block() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
register_block_type(
$this->block_json,
array(
'render_callback' => array( $this, 'render' ),
)
);
}
/**
* Constructor.
*
* @param string $block_json Path to the block.json file. Will look for template.php in the same directory.
*/
public function __construct( $block_json ) {
$this->block_json = $block_json;
$this->block_dir = plugin_dir_path( $block_json );
$this->register_block( $this );
}
}
/**
* Register each plugin block available in the build directory.
*/
function starter_block_plugin_set_up_blocks() {
// Require each index.php file once in the build directory.
foreach ( glob( plugin_dir_path( __FILE__ ) . 'build/blocks/*/block.json' ) as $file ) {
if ( ! is_readable( $file ) ) {
continue;
}
new Starter_Block_Plugin_Block( $file );
}
}
// Set up blocks.
add_action( 'init', 'starter_block_plugin_set_up_blocks' );