/
MetaBox.php
171 lines (136 loc) · 4.23 KB
/
MetaBox.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
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
<?php
namespace MBBlocks;
class MetaBox extends \RW_Meta_Box {
private $storage;
public static function normalize( $meta_box ) {
$meta_box = parent::normalize( $meta_box );
$meta_box = wp_parse_args( $meta_box, [
'description' => '',
'icon' => 'schedule',
'category' => 'layout',
'keywords' => [],
'supports' => [],
] );
// Block preview.
if ( empty( $meta_box['preview'] ) ) {
return $meta_box;
}
$meta_box['example'] = [
'attributes' => [
'data' => $meta_box['preview'],
],
];
unset( $meta_box['preview'] );
return $meta_box;
}
protected function object_hooks() {
$this->add_block_data();
$this->register_block_type();
add_action( 'wp_ajax_mb_blocks_fetch', [ $this, 'fetch' ] );
}
private function add_block_data() {
$block = $this->meta_box;
// Remove unnecessary keys to keep JSON short and bug away.
$keys = ['fields', 'autosave', 'default_hidden', 'priority', 'style', 'post_types', 'type'];
foreach ( $keys as $key ) {
unset( $block[ $key ] );
}
$json = json_encode( $block );
wp_add_inline_script( 'mb-blocks', "rwmb.blocks.push({$json});", 'before' );
}
private function register_block_type() {
register_block_type( "meta-box/{$this->id}", [
'editor_script' => 'mb-blocks',
'editor_style' => 'mb-blocks',
'render_callback' => [$this, 'render'],
] );
}
public function enqueue() {
parent::enqueue();
if ( is_admin() && ! $this->is_edit_screen() ) {
return;
}
$this->enqueue_block_assests();
}
public function fetch() {
$block = filter_input( INPUT_POST, 'block', FILTER_SANITIZE_STRING );
if ( $block !== "meta-box/$this->id" ) {
return;
}
$nonce = filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING );
if ( ! wp_verify_nonce( $nonce, 'fetch' ) ) {
return;
}
$mode = filter_input( INPUT_POST, 'mode', FILTER_SANITIZE_STRING );
$attributes = isset( $_POST['attributes'] ) ? $_POST['attributes'] : [];
$attributes = wp_unslash( $attributes );
$this->set_block_data( $attributes );
if ( 'edit' === $mode ) {
$this->show();
} else {
$this->preview( $attributes );
}
die;
}
public function render( $attributes = [] ) {
$this->set_block_data( $attributes );
ob_start();
$post_id = get_the_ID();
$this->render_block( $attributes, false, $post_id );
return ob_get_clean();
}
private function preview( $attributes = [] ) {
// Alignment is handled by theme editor styles, it should not be outputted in block HTML when preview.
unset( $attributes['align'] );
$post_id = filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$this->render_block( $attributes, true, $post_id );
}
private function set_block_data( $attributes ) {
$attributes['name'] = $this->id;
$data = isset( $attributes['data'] ) ? $attributes['data'] : [];
$this->storage->set_data( $data );
ActiveBlock::set_block_name( $this->id );
}
private function render_block( $attributes = [], $is_preview = false, $post_id = null ) {
$this->enqueue_block_assests();
if ( $this->render_callback ) {
call_user_func( $this->render_callback, $attributes, $is_preview, $post_id );
return;
}
if ( file_exists( $this->render_template ) ) {
include $this->render_template;
} else {
locate_template( $this->render_template, true );
}
}
private function enqueue_block_assests() {
$handle = "meta-box/$this->id";
if ( $this->enqueue_style ) {
wp_enqueue_style( $handle, $this->enqueue_style );
}
if ( $this->enqueue_script ) {
wp_enqueue_script( $handle, $this->enqueue_script, [ 'jquery' ], '', true );
}
if ( $this->enqueue_assets && is_callable( $this->enqueue_assets ) ) {
call_user_func( $this->enqueue_assets );
}
}
public function get_storage() {
if ( null === $this->storage ) {
$this->storage = new Storage;
}
return $this->storage;
}
public function register_fields() {
$field_registry = rwmb_get_registry( 'field' );
foreach ( $this->fields as $field ) {
$field_registry->add( $field, $this->id, 'block' );
}
}
public function is_edit_screen( $screen = null ) {
if ( ! ( $screen instanceof WP_Screen ) ) {
$screen = get_current_screen();
}
return 'post' === $screen->base && use_block_editor_for_post_type( $screen->post_type );
}
}