-
Notifications
You must be signed in to change notification settings - Fork 0
/
block-report.php
225 lines (194 loc) · 5.07 KB
/
block-report.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
224
225
<?php
namespace plugish\com\cli;
use WP_CLI\Formatter;
class BlockReport {
/**
* @var bool The CSV flag.
*/
private bool $csv = false;
/**
* Stores block data.
* @var array
*/
private array $blocks = [];
/**
* @var array The post types to query for.
*/
private array $post_types = [];
/**
* @var int The post count in the query.
*/
private int $post_count = 0;
/**
* @var array The post status to check.
*/
private array $post_status = [];
/**
* @param array $args The arguments.
* @param array $assoc_args Flags/associative arguments.
*
* @return void
*/
public function __invoke( array $args, array $assoc_args ) {
$this->csv = isset( $assoc_args['csv'] );
$this->post_types = ! empty( $assoc_args['post-type'] ) ? explode(',', $assoc_args['post-type'] ) : [ 'post', 'page' ];
$this->post_status = ! empty( $assoc_args['post-status'] ) ? explode(',', $assoc_args['post-status'] ) : ['any'];
if ( empty( $assoc_args['fields'] ) ) {
\WP_CLI::error( 'You cannot list empty fields, why run the command then?' );
}
$page = 1;
$posts = $this->query_posts( $page );
while( ! empty( $posts ) ) {
foreach( $posts as $post ) {
$this->parse_posts( $post );
}
$page++;
$this->cleanup();
$posts = $this->query_posts( $page );
}
$fields = ! empty( $assoc_args['fields'] ) ? explode( ',', $assoc_args['fields'] ) : [];
$args = [
'format' => $this->csv ? 'csv' : 'table',
'fields' => array_map( 'trim', $fields ),
];
$formatter = new Formatter( $args );
$formatter->display_items( $this->blocks );
}
/**
* Queries the posts for a loop.
*
* @param int $page The page.
*
* @return array
*/
private function query_posts( int $page = 1 ): array {
$post_query = new \WP_Query( [
'post_type' => $this->post_types,
'post_status' => $this->post_status,
'posts_per_page' => 100,
'paged' => $page,
] );
if ( ! $post_query->have_posts() ) {
return [];
}
if ( ! $this->post_count ) {
$this->post_count = absint( $post_query->found_posts );
}
return $post_query->posts;
}
/**
* Parses a blocks array and adds it to the block report.
*
* @param array $blocks
* @param int $post_id
*
* @return void
*/
private function parse_blocks( array $blocks, int $post_id ): void {
foreach( $blocks as $block ) {
$parsed = $this->parse_block( $block, $post_id );
if ( empty( $parsed ) ) {
continue;
}
$this->blocks[] = $parsed;
}
}
/**
* Parses a block and returns the resulting array.
*
* @param array $block
* @param int $post_id
*
* @return array|null
*/
private function parse_block( array $block, int $post_id ): ?array {
if ( empty( $block['blockName'] ) ) {
return null;
}
$innerBlockList = [];
if ( ! empty( $block['innerBlocks'] ) ) {
$innerBlockList = wp_list_pluck( $block['innerBlocks'], 'blockName' );
$this->parse_blocks( $block['innerBlocks'], $post_id );
}
return [
'post_id' => $post_id,
'name' => $block['blockName'],
'attributes' => ! empty( $block['attrs'] )
? implode( ', ', array_keys( $block['attrs'] ) )
: '-',
'innerHtml' => ! empty( $block['innerHTML'] ) ? 'yes' : '-',
'innerContent' => ! empty( $block['innerContent'] ) ? 'yes' : '-',
'innerBlocks' => ! empty( $innerBlockList )
? implode( ', ', $innerBlockList )
: '-',
];
}
/**
* Parses posts
*
* @param \WP_Post $post
*
* @return void
*/
private function parse_posts( \WP_Post $post ): void {
$blocks = parse_blocks( $post->post_content );
if ( empty( $blocks ) ) {
return;
}
$this->parse_blocks( $blocks, $post->ID );
}
/**
* Cleans up memory after every operation.
*
* Borrowed from WP VIP
*
* @link https://github.com/Automattic/vip-go-mu-plugins/blob/develop/vip-helpers/vip-caching.php#L733
*
* @return void
*/
private function cleanup(): void {
global $wp_object_cache, $wpdb;
$wpdb->queries = [];
if ( ! is_object( $wp_object_cache ) ) {
return;
}
$wp_object_cache->group_ops = [];
$wp_object_cache->memcache_debug = [];
$wp_object_cache->cache = [];
if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
$wp_object_cache->__remoteset(); // important
}
}
}
\WP_CLI::add_command( 'jwcli block report', __NAMESPACE__ . '\BlockReport', [
'shortdesc' => 'Scrapes post content for blocks and provides a report on screen or in CSV format.',
'synopsis' => [
[
'type' => 'assoc',
'name' => 'post-type',
'description' => 'The post type slugs to check, separated by commas.',
'optional' => true,
'default' => 'post,page',
],
[
'type' => 'assoc',
'name' => 'fields',
'description' => 'A comma-separated list of fields to return.',
'default' => 'post_id,name,attributes,innerHtml,innerContent,innerBlocks',
'optional' => true,
],
[
'type' => 'assoc',
'name' => 'post-status',
'description' => 'The post statuses to check.',
'optional' => true,
'default' => 'any',
],
[
'type' => 'flag',
'description' => 'Outputs the CSV data to STDOUT.',
'name' => 'csv',
'optional' => true,
],
],
] );