-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks-to-rest.php
99 lines (84 loc) · 2.63 KB
/
blocks-to-rest.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
<?php
/**
* Plugin Name: Blocks to Rest
* Description: Sends structured Gutenberg block data to REST.
* Version: 2.0
*
* @package blocks-to-rest
*/
namespace JohnWatkins\BlocksToRest;
const EDITOR_BLOCKS_META_FIELD = 'editor_blocks';
add_action( 'init', __NAMESPACE__ . '\filter_post_rest_response', 99 );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_script' );
/**
* Returns whether WP supports array schema types for post meta fields. Nonscalar types are introduced in WP 5.3.
*
* @see https://make.wordpress.org/core/2019/10/03/wp-5-3-supports-object-and-array-meta-types-in-the-rest-api/
*
* @return bool
*/
function this_wordpress_supports_nonscalar_post_meta() {
global $wp_version;
return 5.3 <= floatval( $wp_version );
}
/**
* Enqueues block editor script.
*/
function enqueue_script() {
$asset_data_file = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'build/index.asset.php';
if ( ! file_exists( $asset_data_file ) ) {
return;
}
$script_data = include $asset_data_file;
wp_enqueue_script(
'blocks-to-rest',
trailingslashit( plugin_dir_url( __FILE__ ) ) . 'build/index.js',
$script_data['dependencies'],
$script_data['version'],
true
);
wp_localize_script(
'blocks-to-rest',
'BLOCKS_TO_REST',
[
'EDITOR_BLOCKS_META_FIELD' => EDITOR_BLOCKS_META_FIELD,
'EDITOR_BLOCKS_META_TYPE' => this_wordpress_supports_nonscalar_post_meta() ? 'array' : 'string',
]
);
}
/**
* Registers the editor_blocks meta field.
*/
function filter_post_rest_response() {
if ( ! function_exists( 'use_block_editor_for_post_type' ) ) {
require ABSPATH . 'wp-admin/includes/post.php';
}
foreach ( array_filter( get_post_types_by_support( 'editor' ), 'use_block_editor_for_post_type' ) as $post_type ) {
add_filter( sprintf( 'rest_prepare_%s', $post_type ), __NAMESPACE__ . '\\convert_content_to_array', 10, 3 );
}
}
/**
* Converts post contenti nto an array of blocks.
*
* @param WP_REST_Response $response
* @param WP_Post $post
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
function convert_content_to_array( $response, $post, $request ) {
if ( 'edit' === $request['context'] ) {
return $response;
}
$content = $post->post_content;
if ( false !== strpos( $content, 'EDITOR_BLOCKS' ) ) {
$content = explode( '/EDITOR_BLOCKS', $content )[0];
$content = explode( 'EDITOR_BLOCKS', $content )[1];
$response->data['content']['blocks'] = json_decode( trim( $content ) );
} else {
$editor_blocks = get_post_meta( $post->ID, 'editor_blocks', true );
if ( is_array( $editor_blocks ) ) {
$response->data['content']['blocks'] = $editor_blocks;
}
}
return $response;
}