forked from johnbillion/query-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_editor.php
223 lines (184 loc) · 5.4 KB
/
block_editor.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
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
<?php declare(strict_types = 1);
/**
* Block editor (née Gutenberg) collector.
*
* @package query-monitor
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @extends QM_DataCollector<QM_Data_Block_Editor>
*/
class QM_Collector_Block_Editor extends QM_DataCollector {
public $id = 'block_editor';
/**
* @var array<int, mixed[]>
*/
protected $block_context = array();
/**
* @var array<int, QM_Timer|false>
*/
protected $block_timing = array();
/**
* @var QM_Timer|null
*/
protected $block_timer = null;
public function get_storage(): QM_Data {
return new QM_Data_Block_Editor();
}
/**
* @return void
*/
public function set_up() {
parent::set_up();
add_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ), 9999, 2 );
add_filter( 'render_block_context', array( $this, 'filter_render_block_context' ), -9999, 2 );
add_filter( 'render_block_data', array( $this, 'filter_render_block_data' ), -9999 );
add_filter( 'render_block', array( $this, 'filter_render_block' ), 9999, 2 );
}
/**
* @return void
*/
public function tear_down() {
remove_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ), 9999 );
remove_filter( 'render_block_context', array( $this, 'filter_render_block_context' ), -9999 );
remove_filter( 'render_block_data', array( $this, 'filter_render_block_data' ), -9999 );
remove_filter( 'render_block', array( $this, 'filter_render_block' ), 9999 );
parent::tear_down();
}
/**
* @return array<int, string>
*/
public function get_concerned_filters() {
return array(
'allowed_block_types',
'allowed_block_types_all',
'block_editor_settings_all',
'block_type_metadata',
'block_type_metadata_settings',
'block_parser_class',
'pre_render_block',
'register_block_type_args',
'render_block_context',
'render_block_data',
'render_block',
'use_widgets_block_editor',
);
}
/**
* @param string|null $pre_render
* @param mixed[] $block
* @return string|null
*/
public function filter_pre_render_block( $pre_render, array $block ) {
if ( null !== $pre_render ) {
$this->block_timing[] = false;
}
return $pre_render;
}
/**
* @param mixed[] $context
* @param mixed[] $block
* @return mixed[]
*/
public function filter_render_block_context( array $context, array $block ) {
$this->block_context[] = $context;
return $context;
}
/**
* @param mixed[] $block
* @return mixed[]
*/
public function filter_render_block_data( array $block ) {
$this->block_timer = new QM_Timer();
$this->block_timer->start();
return $block;
}
/**
* @param string $block_content
* @param mixed[] $block
* @return string
*/
public function filter_render_block( $block_content, array $block ) {
if ( isset( $this->block_timer ) ) {
$this->block_timing[] = $this->block_timer->stop();
}
return $block_content;
}
public function process() {
global $_wp_current_template_content;
if ( ! empty( $_wp_current_template_content ) ) {
// Full site editor:
$content = $_wp_current_template_content;
} elseif ( is_singular() ) {
// Post editor:
$post = get_post( get_queried_object_id() );
if ( ! $post ) {
return;
}
$content = $post->post_content;
} else {
// Nada:
return;
}
$this->data->post_has_blocks = has_blocks( $content );
$this->data->post_blocks = array_values( parse_blocks( $content ) );
$this->data->all_dynamic_blocks = get_dynamic_block_names();
$this->data->total_blocks = 0;
$this->data->has_block_context = false;
$this->data->has_block_timing = false;
if ( $this->data->post_has_blocks ) {
$this->data->post_blocks = array_values( array_filter( array_map( array( $this, 'process_block' ), $this->data->post_blocks ) ) );
}
}
/**
* @param mixed[] $block
* @return mixed[]|null
*/
protected function process_block( array $block ) {
$context = array_shift( $this->block_context );
$timing = array_shift( $this->block_timing );
// Remove empty blocks caused by two consecutive line breaks in content
if ( ! $block['blockName'] && ! trim( $block['innerHTML'] ) ) {
return null;
}
$this->data->total_blocks++;
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$dynamic = false;
$callback = null;
if ( $block_type && $block_type->is_dynamic() ) {
$dynamic = true;
$callback = QM_Util::populate_callback( array(
'function' => $block_type->render_callback,
) );
}
$timing = array_shift( $this->block_timing );
$block['dynamic'] = $dynamic;
$block['callback'] = $callback;
$block['innerHTML'] = trim( $block['innerHTML'] );
$block['size'] = strlen( $block['innerHTML'] );
if ( $context ) {
$block['context'] = $context;
$this->data->has_block_context = true;
}
if ( $timing ) {
$block['timing'] = $timing->get_time();
$this->data->has_block_timing = true;
}
if ( ! empty( $block['innerBlocks'] ) ) {
$block['innerBlocks'] = array_values( array_filter( array_map( array( $this, 'process_block' ), $block['innerBlocks'] ) ) );
}
return $block;
}
}
/**
* @param array<string, QM_Collector> $collectors
* @param QueryMonitor $qm
* @return array<string, QM_Collector>
*/
function register_qm_collector_block_editor( array $collectors, QueryMonitor $qm ) {
$collectors['block_editor'] = new QM_Collector_Block_Editor();
return $collectors;
}
add_filter( 'qm/collectors', 'register_qm_collector_block_editor', 10, 2 );