Skip to content

Commit aaf02b7

Browse files
authored
Merge pull request #22 from spacedmonkey/widget-try
Add support for block widgets
2 parents a451988 + 8b1726e commit aaf02b7

File tree

6 files changed

+263
-109
lines changed

6 files changed

+263
-109
lines changed

languages/wp-rest-blocks.pot

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ msgstr ""
99
"MIME-Version: 1.0\n"
1010
"Content-Type: text/plain; charset=UTF-8\n"
1111
"Content-Transfer-Encoding: 8bit\n"
12-
"POT-Creation-Date: 2021-08-15T19:27:05+00:00\n"
12+
"POT-Creation-Date: 2021-09-06T17:52:03+00:00\n"
1313
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1414
"X-Generator: WP-CLI 2.5.0\n"
1515
"X-Domain: wp-rest-blocks\n"
@@ -34,19 +34,21 @@ msgstr ""
3434
msgid "https://www.spacedmonkey.com/"
3535
msgstr ""
3636

37-
#: src/blocks.php:48
37+
#: src/posts.php:51
38+
#: src/widgets.php:38
3839
msgid "Has blocks."
3940
msgstr ""
4041

41-
#: src/blocks.php:61
42+
#: src/posts.php:64
43+
#: src/widgets.php:51
4244
msgid "Blocks."
4345
msgstr ""
4446

4547
#. translators: %s: build commands.
46-
#: wp-rest-blocks.php:34
48+
#: wp-rest-blocks.php:35
4749
msgid " Please run %s to finish installation."
4850
msgstr ""
4951

50-
#: wp-rest-blocks.php:39
52+
#: wp-rest-blocks.php:40
5153
msgid "REST API Blocks plugin could not be initialized."
5254
msgstr ""

src/blocks.php renamed to src/data.php

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Add extra fields into rest api to format blocks as json data.
3+
* Data layer to process to block data.
44
*
55
* @package WP_REST_Blocks.
66
*/
@@ -11,93 +11,17 @@
1111
use pQuery;
1212

1313
/**
14-
* Bootstrap filters and actions.
14+
* Get blocks from html string.
1515
*
16-
* @return void
17-
*/
18-
function bootstrap() {
19-
add_action( 'rest_api_init', __NAMESPACE__ . '\\wp_rest_blocks_init' );
20-
}
21-
22-
/**
23-
* Add rest api fields.
24-
*
25-
* @return void
26-
*/
27-
function wp_rest_blocks_init() {
28-
$post_types = get_post_types(
29-
[
30-
'show_in_rest' => true,
31-
],
32-
'names'
33-
);
34-
35-
if ( ! function_exists( 'use_block_editor_for_post_type' ) ) {
36-
require_once ABSPATH . 'wp-admin/includes/post.php';
37-
}
38-
39-
$types = array_filter( $post_types, 'use_block_editor_for_post_type' );
40-
41-
register_rest_field(
42-
$types,
43-
'has_blocks',
44-
[
45-
'get_callback' => __NAMESPACE__ . '\\has_blocks_get_callback',
46-
'update_callback' => null,
47-
'schema' => [
48-
'description' => __( 'Has blocks.', 'wp-rest-blocks' ),
49-
'type' => 'boolean',
50-
],
51-
]
52-
);
53-
54-
register_rest_field(
55-
$types,
56-
'blocks',
57-
[
58-
'get_callback' => __NAMESPACE__ . '\\blocks_get_callback',
59-
'update_callback' => null,
60-
'schema' => [
61-
'description' => __( 'Blocks.', 'wp-rest-blocks' ),
62-
'type' => 'object',
63-
],
64-
]
65-
);
66-
}
67-
68-
69-
/**
70-
* Callback to get if post content has block data.
71-
*
72-
* @param array $object Array of data rest api request.
73-
*
74-
* @return bool
75-
*/
76-
function has_blocks_get_callback( array $object ) {
77-
$post = get_post( $object['id'] );
78-
if ( ! $post ) {
79-
return false;
80-
}
81-
82-
return has_blocks( $post );
83-
}
84-
85-
/**
86-
* Loop around all blocks and get block data.
87-
*
88-
* @param array $object Array of data rest api request.
16+
* @param string $content Content to parse.
17+
* @param int $post_id Post int.
8918
*
9019
* @return array
9120
*/
92-
function blocks_get_callback( array $object ) {
93-
$post = get_post( $object['id'] );
21+
function get_blocks( $content, $post_id = 0 ) {
9422
$output = [];
95-
if ( ! $post ) {
96-
return $output;
97-
}
23+
$blocks = parse_blocks( $content );
9824

99-
$post_id = $post->ID;
100-
$blocks = parse_blocks( $post->post_content );
10125
foreach ( $blocks as $block ) {
10226
$block_data = handle_do_block( $block, $post_id );
10327
if ( $block_data ) {
@@ -207,7 +131,7 @@ function get_attribute( $attribute, $html, $post_id = 0 ) {
207131
}
208132
}
209133

210-
if ( 'meta' === $attribute['source'] && isset( $attribute['meta'] ) ) {
134+
if ( $post_id && 'meta' === $attribute['source'] && isset( $attribute['meta'] ) ) {
211135
$value = get_post_meta( $post_id, $attribute['meta'], true );
212136
}
213137
}

src/posts.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Posts.
4+
*
5+
* @package WP_REST_Blocks.
6+
*/
7+
8+
namespace WP_REST_Blocks\Posts;
9+
10+
use function WP_REST_Blocks\Data\get_blocks;
11+
12+
/**
13+
* Bootstrap filters and actions.
14+
*
15+
* @return void
16+
*/
17+
function bootstrap() {
18+
add_action( 'rest_api_init', __NAMESPACE__ . '\\wp_rest_blocks_init' );
19+
}
20+
21+
/**
22+
* Get post types with editor.
23+
*
24+
* @return array
25+
*/
26+
function get_post_types_with_editor() {
27+
$post_types = get_post_types( [ 'show_in_rest' => true ], 'names' );
28+
$post_types = array_values( $post_types );
29+
30+
if ( ! function_exists( 'use_block_editor_for_post_type' ) ) {
31+
require_once ABSPATH . 'wp-admin/includes/post.php';
32+
}
33+
34+
return array_filter( $post_types, 'use_block_editor_for_post_type' );
35+
}
36+
37+
/**
38+
* Add rest api fields.
39+
*
40+
* @return void
41+
*/
42+
function wp_rest_blocks_init() {
43+
$types = get_post_types_with_editor();
44+
if ( ! $types ) {
45+
return;
46+
}
47+
48+
register_rest_field(
49+
$types,
50+
'has_blocks',
51+
[
52+
'get_callback' => __NAMESPACE__ . '\\has_blocks_get_callback',
53+
'update_callback' => null,
54+
'schema' => [
55+
'description' => __( 'Has blocks.', 'wp-rest-blocks' ),
56+
'type' => 'boolean',
57+
'context' => [ 'embed', 'view', 'edit' ],
58+
'readonly' => true,
59+
],
60+
]
61+
);
62+
63+
register_rest_field(
64+
$types,
65+
'blocks',
66+
[
67+
'get_callback' => __NAMESPACE__ . '\\blocks_get_callback',
68+
'update_callback' => null,
69+
'schema' => [
70+
'description' => __( 'Blocks.', 'wp-rest-blocks' ),
71+
'type' => 'object',
72+
'context' => [ 'embed', 'view', 'edit' ],
73+
'readonly' => true,
74+
],
75+
]
76+
);
77+
}
78+
79+
/**
80+
* Callback to get if post content has block data.
81+
*
82+
* @param array $object Array of data rest api request.
83+
*
84+
* @return bool
85+
*/
86+
function has_blocks_get_callback( array $object ) {
87+
$post = get_post( $object['id'] );
88+
if ( ! $post ) {
89+
return false;
90+
}
91+
92+
return has_blocks( $post );
93+
}
94+
95+
/**
96+
* Loop around all blocks and get block data.
97+
*
98+
* @param array $object Array of data rest api request.
99+
*
100+
* @return array
101+
*/
102+
function blocks_get_callback( array $object ) {
103+
$post = get_post( $object['id'] );
104+
$output = [];
105+
if ( ! $post ) {
106+
return $output;
107+
}
108+
109+
return get_blocks( $post->post_content, $post->ID );
110+
}

src/widgets.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Widgets.
4+
*
5+
* @package WP_REST_Blocks.
6+
*/
7+
8+
namespace WP_REST_Blocks\Widgets;
9+
10+
use function WP_REST_Blocks\Data\get_blocks;
11+
12+
/**
13+
* Bootstrap filters and actions.
14+
*
15+
* @return void
16+
*/
17+
function bootstrap() {
18+
add_action( 'rest_api_init', __NAMESPACE__ . '\\wp_rest_blocks_init' );
19+
}
20+
21+
/**
22+
* Add rest api fields.
23+
*
24+
* @return void
25+
*/
26+
function wp_rest_blocks_init() {
27+
if ( ! function_exists( 'wp_use_widgets_block_editor' ) || ! wp_use_widgets_block_editor() ) {
28+
return;
29+
}
30+
31+
register_rest_field(
32+
'widget',
33+
'has_blocks',
34+
[
35+
'get_callback' => __NAMESPACE__ . '\\has_blocks_widget_get_callback',
36+
'update_callback' => null,
37+
'schema' => [
38+
'description' => __( 'Has blocks.', 'wp-rest-blocks' ),
39+
'type' => 'boolean',
40+
'context' => [ 'embed', 'view', 'edit' ],
41+
'readonly' => true,
42+
],
43+
]
44+
);
45+
46+
register_rest_field(
47+
'widget',
48+
'blocks',
49+
[
50+
'get_callback' => __NAMESPACE__ . '\\blocks_widget_get_callback',
51+
'update_callback' => null,
52+
'schema' => [
53+
'description' => __( 'Blocks.', 'wp-rest-blocks' ),
54+
'type' => 'object',
55+
'context' => [ 'embed', 'view', 'edit' ],
56+
'readonly' => true,
57+
],
58+
]
59+
);
60+
}
61+
62+
/**
63+
* Get widget
64+
*
65+
* @param array $object Object data.
66+
*
67+
* @return mixed
68+
*/
69+
function get_widget( array $object ) {
70+
global $wp_widget_factory;
71+
72+
$widget_object = $wp_widget_factory->get_widget_object( $object['id_base'] );
73+
$parsed_id = wp_parse_widget_id( $object['id'] );
74+
$all_instances = $widget_object->get_settings();
75+
76+
return $all_instances[ $parsed_id['number'] ];
77+
}
78+
79+
/**
80+
* Callback to get if post content has block data.
81+
*
82+
* @param array $object Array of data rest api request.
83+
*
84+
* @return bool
85+
*/
86+
function has_blocks_widget_get_callback( array $object ) {
87+
if ( ! isset( $object['id_base'] ) || 'block' !== $object['id_base'] ) {
88+
return false;
89+
}
90+
91+
$instance = get_widget( $object );
92+
if ( ! isset( $instance['content'] ) || ! $instance['content'] ) {
93+
return false;
94+
}
95+
96+
return has_blocks( $instance['content'] );
97+
}
98+
99+
/**
100+
* Loop around all blocks and get block data.
101+
*
102+
* @param array $object Array of data rest api request.
103+
*
104+
* @return array
105+
*/
106+
function blocks_widget_get_callback( array $object ) {
107+
if ( ! has_blocks_widget_get_callback( $object ) ) {
108+
return [];
109+
}
110+
111+
$instance = get_widget( $object );
112+
113+
return get_blocks( $instance['content'] );
114+
}

0 commit comments

Comments
 (0)