PluginProbe
WPGet API – Connect to any external REST API / 2.2.10
WPGet API – Connect to any external REST API v2.2.10
1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.10 2.2.2 2.2.3 All 97 releases
wpgetapi / includes / block-editor / block-editor.php

block-editor.php in WPGet API – Connect to any external REST API 2.2.10, at includes/block-editor/block-editor.php

248 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Register WPGetAPI Gutenberg block
5 *
6 * @return void
7 */
8 function wpgetapi_init_block_editor_assets() {
9
10 $assets = array();
11
12 $asset_file = WPGETAPIDIR . 'includes/block-editor/index-asset.php';
13
14 if ( file_exists( $asset_file ) ) {
15 $assets = include $asset_file;
16 }
17
18 $assets = wp_parse_args(
19 $assets,
20 array(
21 'dependencies' => array(
22 'wp-api-fetch',
23 'wp-block-editor',
24 'wp-blocks',
25 'wp-components',
26 'wp-element',
27 'wp-i18n',
28 'wp-url',
29 ),
30 'version' => WPGETAPIVERSION,
31 )
32 );
33
34 wp_register_script(
35 'wpgetapi-endpoint-selector-editor-script',
36 WPGETAPIURL . 'includes/block-editor/index.js',
37 $assets['dependencies'],
38 $assets['version']
39 );
40
41 wp_set_script_translations(
42 'wpgetapi-endpoint-selector-editor-script',
43 'wpgetapi'
44 );
45
46 /**
47 * Filter attributes for the WPGetAPI Widget.
48 *
49 * @param array $attributes The default widget attributes.
50 * @return array Modified widget attributes.
51 */
52 $attributes = apply_filters(
53 'wpgetapi_gutenberg_attributes',
54 array(
55 'api_id' => array( 'type' => 'string' ),
56 'endpoint_id' => array( 'type' => 'string' ),
57 'debug' => array( 'type' => 'string' ),
58 )
59 );
60
61 register_block_type(
62 WPGETAPIDIR . 'includes/block-editor',
63 array(
64 'editor_script' => 'wpgetapi-endpoint-selector-editor-script',
65 'render_callback' => 'wpgetapi_get_gutenberg_html',
66 'attributes' => $attributes,
67 )
68 );
69 }
70
71 add_action( 'init', 'wpgetapi_init_block_editor_assets', 10, 0 );
72
73 /**
74 * Register script and Enqueue localize script data for WPGetAPI Gutenberg block.
75 *
76 * @return void
77 */
78 function wpgetapi_enqueue_block_editor_assets() {
79
80 /**
81 * Filter attributes for the WPGetAPI Widget.
82 *
83 * @param array $attributes The default widget attributes.
84 * @return array Modified widget attributes.
85 */
86 $attributes = apply_filters(
87 'wpgetapi_gutenberg_attributes',
88 array(
89 'api_id' => array( 'type' => 'string' ),
90 'endpoint_id' => array( 'type' => 'string' ),
91 'debug' => array( 'type' => 'string' ),
92 )
93 );
94
95 /**
96 * Filter select options for the WPGetAPI Widget.
97 *
98 * @param array $select_options The widget select options.
99 * @return array
100 */
101 $select_options = apply_filters(
102 'wpgetapi_gutenberg_select_options',
103 array(
104 'format' => array(
105 array(
106 'label' => __( 'Available in PRO plugin', 'wpgetapi' ),
107 'value' => '',
108 ),
109 ),
110 'html_tags' => array(
111 array(
112 'label' => __( 'Available in PRO plugin', 'wpgetapi' ),
113 'value' => '',
114 ),
115 ),
116 'html_labels' => array(
117 array(
118 'label' => __( 'Available in PRO plugin', 'wpgetapi' ),
119 'value' => '',
120 ),
121 ),
122 )
123 );
124
125 $data = array();
126 $count_api = 0;
127 $count_endpoint = 0;
128 $apis = get_option( 'wpgetapi_setup' );
129
130 if ( $apis ) {
131 foreach ( $apis as $api ) {
132 if ( $api ) {
133 foreach ( $api as $item ) {
134 $endpoints = get_option( 'wpgetapi_' . $item['id'] );
135 if ( isset( $endpoints['endpoints'] ) ) {
136 $data['apis'][ $count_api ]['api_id'] = $item['id'];
137 foreach ( $endpoints['endpoints'] as $endpoint ) {
138 $data['endpoints'][ $count_endpoint ]['id'] = $item['id'] . '_' . $endpoint['id'];
139 $data['endpoints'][ $count_endpoint ]['api_id'] = $item['id'];
140 $data['endpoints'][ $count_endpoint ]['endpoint_id'] = $endpoint['id'];
141 $data['endpoints'][ $count_endpoint ]['endpoint'] = $endpoint['endpoint'];
142 ++$count_endpoint;
143 }
144 ++$count_api;
145 }
146 }
147 }
148 }
149 }
150
151 $wpgetapi_script_data = array(
152 'logo_url' => WPGETAPIURL . 'assets/img/wpgetapi-icon.png',
153 'endpoints' => isset( $data['endpoints'] ) ? $data['endpoints'] : '',
154 'apis' => isset( $data['apis'] ) ? $data['apis'] : '',
155 'attributes' => $attributes,
156 'select_options' => $select_options,
157 );
158
159 wp_add_inline_script(
160 'wpgetapi-endpoint-selector-editor-script',
161 sprintf(
162 'window.wpgetapi_block_editor = %s;',
163 wp_json_encode( $wpgetapi_script_data )
164 ),
165 'before'
166 );
167 }
168
169 add_action( 'enqueue_block_editor_assets', 'wpgetapi_enqueue_block_editor_assets', 10, 0 );
170
171 /**
172 * Get form HTML to display in a WPGetAPI Gutenberg block.
173 *
174 * @since 2.0.0
175 *
176 * @param array $attr Attributes passed by WPGetAPI Gutenberg block.
177 *
178 * @return string
179 */
180 function wpgetapi_get_gutenberg_html( $attr, $content ) {
181
182 $api_id = ! empty( $attr['api_id'] ) ? $attr['api_id'] : 0;
183 $endpoint_id = ! empty( $attr['endpoint_id'] ) ? $attr['endpoint_id'] : 0;
184
185 if ( empty( $api_id ) || empty( $endpoint_id ) ) {
186 return __( 'The API or Endpoint should not be empty.', 'wpgetapi' );
187 }
188
189 ob_start();
190
191 /**
192 * Fires before Gutenberg block output.
193 *
194 * @since 1.5.8.2
195 */
196 do_action( 'wpgetapi_gutenberg_block_before' );
197
198 $debug = ! empty( $attr['debug'] ) ? $attr['debug'] : 'false';
199 $format = ! empty( $attr['format'] ) ? $attr['format'] : '';
200 $html_tag = ! empty( $attr['html_tag'] ) ? $attr['html_tag'] : '';
201 $html_labels = ! empty( $attr['html_labels'] ) ? $attr['html_labels'] : '';
202 $keys = ! empty( $attr['keys'] ) ? $attr['keys'] : '';
203 $query_variables = ! empty( $attr['query_variables'] ) ? $attr['query_variables'] : '';
204 $endpoint_variables = ! empty( $attr['endpoint_variables'] ) ? $attr['endpoint_variables'] : '';
205
206 $shortcode = sprintf(
207 '[wpgetapi_endpoint
208 api_id="' . $api_id . '"
209 endpoint_id="' . $endpoint_id . '"
210 debug="' . $debug . '"
211 format="' . $format . '"
212 html_tag="' . $html_tag . '"
213 html_labels="' . $html_labels . '"
214 keys="' . $keys . '"
215 query_variables="' . $query_variables . '"
216 endpoint_variables="' . $endpoint_variables . '"
217 ]'
218 );
219
220 echo do_shortcode( shortcode_unautop( $shortcode ) );
221
222 /**
223 * Fires after Gutenberg block output.
224 *
225 * @since 1.5.8.2
226 */
227 do_action( 'wpgetapi_gutenberg_block_after' );
228
229 $content = ob_get_clean();
230
231 if ( empty( $content ) ) {
232 $content = '<div className="components-placeholder"><div className="components-placeholder__label"></div>' .
233 '<div className="components-placeholder__fieldset">' .
234 esc_html__( 'The data cannot be displayed.', 'wpgetapi' ) .
235 '</div></div>';
236 }
237
238 /**
239 * Filter Gutenberg block content.
240 *
241 * @since 1.5.8.2
242 *
243 * @param string $content Block content.
244 * @param int $id Form id.
245 */
246 return apply_filters( 'wpgetapi_gutenberg_block_content', $content, $api_id, $endpoint_id, $attr );
247 }
248