PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
← All changes | includes/rest-api.php +374 -326 1.8.73.0.2 View file →
@@ -1,326 +1,374 @@
1 -<?php
2 -
3 -namespace Getwid;
4 -
5 -/**
6 - * Class REST API
7 - * @package Getwid
8 - */
9 -class RestAPI {
10 -
11 - protected $_namespace = 'getwid/v1';
12 -
13 - protected $remote_template_library_url;
14 -
15 - /**
16 - * RestAPI constructor.
17 - */
18 - public function __construct( ) {
19 -
20 - $this->remote_template_library_url = defined( 'GETWID_REMOTE_TEMPLATE_LIBRARY_URL' ) ?
21 - GETWID_REMOTE_TEMPLATE_LIBRARY_URL : 'https://cgw.motopress.com';
22 -
23 - add_action( 'rest_api_init', [ $this, 'register_rest_route' ] );
24 -
25 - }
26 -
27 - public function register_rest_route(){
28 -
29 - register_rest_route( $this->_namespace, '/get_remote_templates', array(
30 - array(
31 - 'methods' => 'GET',
32 - 'callback' => [ $this, 'get_remote_templates' ],
33 - 'permission_callback' => [ $this, 'permissions_check' ],
34 - )
35 - ) );
36 -
37 - register_rest_route( $this->_namespace, '/get_remote_content', array(
38 - array(
39 - 'methods' => 'GET',
40 - 'callback' => [ $this, 'get_remote_content' ],
41 - 'permission_callback' => [ $this, 'permissions_check' ],
42 - )
43 - ) );
44 -
45 - register_rest_route( $this->_namespace, '/taxonomies', array(
46 - array(
47 - 'methods' => 'GET',
48 - 'callback' => [ $this, 'get_taxonomies' ],
49 - 'permission_callback' => [ $this, 'permissions_check' ],
50 - ),
51 - 'schema' => array( $this, 'taxonomy_schema' )
52 - ) );
53 -
54 - register_rest_route( $this->_namespace, '/terms', array(
55 - array(
56 - 'methods' => 'GET',
57 - 'callback' => [ $this, 'get_terms' ],
58 - 'permission_callback' => [ $this, 'permissions_check' ],
59 - ),
60 - 'schema' => array( $this, 'terms_schema' )
61 - ) );
62 -
63 - register_rest_route( $this->_namespace, '/templates', array(
64 - array(
65 - 'methods' => 'GET',
66 - 'callback' => [ $this, 'get_templates' ],
67 - 'permission_callback' => [ $this, 'permissions_check' ],
68 - ),
69 - 'schema' => array( $this, 'templates_schema' )
70 - ) );
71 - }
72 -
73 - public function permissions_check( $request ) {
74 - if ( ! current_user_can( 'edit_posts' ) ) {
75 - return new \WP_Error(
76 - 'rest_forbidden',
77 - esc_html__( 'Forbidden.' ),
78 - array( 'status' => $this->authorization_status_code() )
79 - );
80 - }
81 -
82 - return true;
83 - }
84 -
85 - // Sets up the proper HTTP status code for authorization.
86 - public function authorization_status_code() {
87 -
88 - $status = 401;
89 -
90 - if ( is_user_logged_in() ) {
91 - $status = 403;
92 - }
93 -
94 - return $status;
95 - }
96 -
97 - /**
98 - * Schema for a taxonomy.
99 - *
100 - * @param WP_REST_Request $request Current request.
101 - */
102 - public function taxonomy_schema() {
103 - $schema = array(
104 - '$schema' => 'http://json-schema.org/draft-04/schema#',
105 - 'title' => 'taxonomy',
106 - 'type' => 'object',
107 - 'properties' => array(
108 - 'post_type_name' => array(
109 - 'description' => esc_html__( 'Post Type' ),
110 - 'type' => 'string',
111 - 'context' => array( 'view', 'edit', 'embed' ),
112 - 'readonly' => true,
113 - )
114 - )
115 - );
116 -
117 - return $schema;
118 - }
119 -
120 - /**
121 - * Schema for a terms.
122 - *
123 - * @param WP_REST_Request $request Current request.
124 - */
125 - public function terms_schema() {
126 - $schema = array(
127 - '$schema' => 'http://json-schema.org/draft-04/schema#',
128 - 'title' => 'terms',
129 - 'type' => 'object',
130 - 'properties' => array(
131 - 'taxonomy_name' => array(
132 - 'description' => esc_html__( 'Taxonomy' ),
133 - 'type' => 'string',
134 - 'context' => array( 'view', 'edit', 'embed' ),
135 - 'readonly' => true,
136 - )
137 - )
138 - );
139 -
140 - return $schema;
141 - }
142 -
143 - /**
144 - * Schema for a templates.
145 - *
146 - * @param WP_REST_Request $request Current request.
147 - */
148 - public function templates_schema() {
149 - $schema = array(
150 - '$schema' => 'http://json-schema.org/draft-04/schema#',
151 - 'title' => 'templates',
152 - 'type' => 'object',
153 - 'properties' => array(
154 - 'post_type_name' => array(
155 - 'description' => esc_html__( 'Templates' ),
156 - 'type' => 'string',
157 - 'context' => array( 'view', 'edit', 'embed' ),
158 - 'readonly' => true,
159 - )
160 - )
161 - );
162 -
163 - return $schema;
164 - }
165 -
166 - public function get_remote_templates() {
167 -
168 - $cache = isset( $_GET['cache'] ) ? sanitize_text_field( wp_unslash( $_GET['cache'] ) ) : 'refresh';
169 - $templates_data = [];
170 -
171 - if ($cache == 'cache'){
172 - $templates_data = get_transient( 'getwid_templates_response_data' ); //Get Cache response
173 - } elseif ($cache == 'refresh') {
174 - delete_transient( 'getwid_templates_response_data' ); //Delete cache data
175 - }
176 -
177 - if ( $templates_data == false || empty( $templates_data ) ) {
178 -
179 - //Get Templates from remote server
180 - $response = wp_remote_get(
181 - $this->remote_template_library_url . "/wp-json/getwid-templates-server/v1/get_templates",
182 - array(
183 - 'timeout' => 15,
184 - )
185 - );
186 - // var_dump( $response ); exit();
187 -
188 - if ( is_wp_error( $response ) ) {
189 - return '<p>' . $response->get_error_message() . '</p>';
190 - } else {
191 -
192 - $templates_data = json_decode( wp_remote_retrieve_body( $response ), false );
193 -
194 - //JSON valid
195 - if ( json_last_error() === JSON_ERROR_NONE && $templates_data ) {
196 -
197 - set_transient( 'getwid_templates_response_data', $templates_data, 24 * HOUR_IN_SECONDS ); //Cache response
198 - return $templates_data;
199 -
200 - } else {
201 - return __( 'Error in json_decode.', 'getwid' );
202 - }
203 - }
204 - } else {
205 - return $templates_data;
206 - }
207 - }
208 -
209 - public function get_remote_content() {
210 -
211 - $get_content_url = isset( $_GET['get_content_url'] ) ? esc_url_raw( $_GET['get_content_url'] ) : false;
212 -
213 - if ( ! $this->content_url_is_valid( $get_content_url ) ) {
214 - return __( 'Please provide a valid URL.', 'getwid' );
215 - }
216 -
217 - //Get Templates from remote server
218 - $response = wp_remote_get(
219 - $get_content_url,
220 - array(
221 - 'timeout' => 15,
222 - )
223 - );
224 -
225 - $templates_data = json_decode( wp_remote_retrieve_body( $response ) );
226 -
227 - //JSON valid
228 - if ( json_last_error() === JSON_ERROR_NONE ) {
229 - return $templates_data;
230 - } else {
231 - return __( 'Error in json_decode.', 'getwid' );
232 - }
233 - }
234 -
235 - public function get_taxonomies($object) {
236 - $post_type_name = sanitize_text_field( wp_unslash( $_GET['post_type_name'] ) );
237 - $taxonomies = get_object_taxonomies( $post_type_name, 'objects' );
238 -
239 - $return = [];
240 - if (!empty($taxonomies)){
241 - foreach ($taxonomies as $key => $taxonomy_name) {
242 - $return[] = array(
243 - 'value' => $key,
244 - 'label' => $taxonomy_name->labels->name.' ('.$key.')'
245 - );
246 - }
247 - }
248 - return $return;
249 - }
250 -
251 - public function get_terms($object) {
252 - $taxonomy_name = getwid_recursive_sanitize_array( wp_unslash( $_GET['taxonomy_name'] ) );
253 -
254 - $return = [];
255 - $terms = get_terms(array(
256 - 'taxonomy' => $taxonomy_name,
257 - 'hide_empty' => false,
258 - ));
259 -
260 - if (!empty($terms)){
261 - foreach ($terms as $key => $term_name) {
262 - $taxonomy_obj = get_taxonomy( $term_name->taxonomy );
263 -
264 - $return[$term_name->taxonomy]['group_name'] = $taxonomy_obj->label;
265 - $return[$term_name->taxonomy]['group_value'][] = array(
266 - 'value' => $term_name->taxonomy.'['.$term_name->term_id.']',
267 - 'label' => $term_name->name,
268 - );
269 - }
270 - }
271 - return $return;
272 - }
273 -
274 - public function get_templates($object) {
275 - $template_name = sanitize_text_field( wp_unslash( $_GET['template_name'] ) );
276 -
277 - $posts = get_posts( array(
278 - 'numberposts' => -1,
279 - 'orderby' => 'date',
280 - 'order' => 'DESC',
281 - 'post_type' => $template_name,
282 - ) );
283 -
284 - $return = [];
285 - if (!empty($posts)){
286 - foreach ($posts as $key => $post) {
287 - $return[] = array(
288 - 'value' => $post->ID,
289 - 'label' => $post->post_title
290 - );
291 - }
292 - }
293 - return $return;
294 - }
295 -
296 - private function content_url_is_valid( $url ) {
297 -
298 - $parsed_url = wp_parse_url( $url );
299 -
300 - if ( ! $parsed_url ) {
301 - return false;
302 - }
303 -
304 - $valid_hosts = array( 'elements.getwid.getmotopress.com' );
305 - $valid_path = '/wp-json/getwid-templates-server/v1/get_content';
306 -
307 - $remote_library_url = wp_parse_url( $this->remote_template_library_url );
308 -
309 - if ( $remote_library_url && isset( $remote_library_url['host'] ) ) {
310 - $valid_hosts[] = $remote_library_url['host'];
311 - }
312 -
313 - $host_is_valid = false;
314 - $path_is_valid = false;
315 -
316 - if ( isset( $parsed_url['host'] ) && in_array( $parsed_url['host'], $valid_hosts ) ) {
317 - $host_is_valid = true;
318 - }
319 -
320 - if ( isset( $parsed_url['path'] ) && $valid_path == $parsed_url['path'] ) {
321 - $path_is_valid = true;
322 - }
323 -
324 - return $host_is_valid && $path_is_valid;
325 - }
326 -}
1 +<?php
2 +
3 +namespace Getwid;
4 +
5 +/**
6 + * Class REST API
7 + * @package Getwid
8 + */
9 +class RestAPI {
10 +
11 + protected $_namespace = 'getwid/v1';
12 +
13 + protected $remote_template_library_url;
14 +
15 + /**
16 + * RestAPI constructor.
17 + */
18 + public function __construct( ) {
19 +
20 + $this->remote_template_library_url = defined( 'GETWID_REMOTE_TEMPLATE_LIBRARY_URL' ) ?
21 + GETWID_REMOTE_TEMPLATE_LIBRARY_URL : 'https://cgw.motopress.com';
22 +
23 + add_action( 'rest_api_init', [ $this, 'register_rest_route' ] );
24 +
25 + }
26 +
27 + public function register_rest_route(){
28 +
29 + register_rest_route( $this->_namespace, '/get_remote_templates', array(
30 + array(
31 + 'methods' => 'GET',
32 + 'callback' => [ $this, 'get_remote_templates' ],
33 + 'permission_callback' => [ $this, 'permissions_check' ],
34 + )
35 + ) );
36 +
37 + register_rest_route( $this->_namespace, '/get_remote_content', array(
38 + array(
39 + 'methods' => 'GET',
40 + 'callback' => [ $this, 'get_remote_content' ],
41 + 'permission_callback' => [ $this, 'permissions_check' ],
42 + )
43 + ) );
44 +
45 + register_rest_route( $this->_namespace, '/taxonomies', array(
46 + array(
47 + 'methods' => 'GET',
48 + 'callback' => [ $this, 'get_taxonomies' ],
49 + 'permission_callback' => [ $this, 'permissions_check' ],
50 + ),
51 + 'schema' => array( $this, 'taxonomy_schema' )
52 + ) );
53 +
54 + register_rest_route( $this->_namespace, '/terms', array(
55 + array(
56 + 'methods' => 'GET',
57 + 'callback' => [ $this, 'get_terms' ],
58 + 'permission_callback' => [ $this, 'permissions_check' ],
59 + ),
60 + 'schema' => array( $this, 'terms_schema' )
61 + ) );
62 +
63 + register_rest_route( $this->_namespace, '/templates', array(
64 + array(
65 + 'methods' => 'GET',
66 + 'callback' => [ $this, 'get_templates' ],
67 + 'permission_callback' => [ $this, 'permissions_check' ],
68 + ),
69 + 'schema' => array( $this, 'templates_schema' )
70 + ) );
71 +
72 + register_rest_route( $this->_namespace, '/get_meta_keys', array(
73 + array(
74 + 'methods' => 'POST',
75 + 'callback' => [ $this, 'get_meta_keys' ],
76 + 'permission_callback' => [ $this, 'permissions_check' ],
77 + )
78 + ) );
79 + }
80 +
81 + public function permissions_check( $request ) {
82 + if ( ! current_user_can( 'edit_posts' ) ) {
83 + return new \WP_Error(
84 + 'rest_forbidden',
85 + esc_html__( 'Forbidden.' ),
86 + array( 'status' => $this->authorization_status_code() )
87 + );
88 + }
89 +
90 + return true;
91 + }
92 +
93 + // Sets up the proper HTTP status code for authorization.
94 + public function authorization_status_code() {
95 +
96 + $status = 401;
97 +
98 + if ( is_user_logged_in() ) {
99 + $status = 403;
100 + }
101 +
102 + return $status;
103 + }
104 +
105 + /**
106 + * Schema for a taxonomy.
107 + *
108 + * @param WP_REST_Request $request Current request.
109 + */
110 + public function taxonomy_schema() {
111 + $schema = array(
112 + '$schema' => 'http://json-schema.org/draft-04/schema#',
113 + 'title' => 'taxonomy',
114 + 'type' => 'object',
115 + 'properties' => array(
116 + 'post_type_name' => array(
117 + 'description' => esc_html__( 'Post Type' ),
118 + 'type' => 'string',
119 + 'context' => array( 'view', 'edit', 'embed' ),
120 + 'readonly' => true,
121 + )
122 + )
123 + );
124 +
125 + return $schema;
126 + }
127 +
128 + /**
129 + * Schema for a terms.
130 + *
131 + * @param WP_REST_Request $request Current request.
132 + */
133 + public function terms_schema() {
134 + $schema = array(
135 + '$schema' => 'http://json-schema.org/draft-04/schema#',
136 + 'title' => 'terms',
137 + 'type' => 'object',
138 + 'properties' => array(
139 + 'taxonomy_name' => array(
140 + 'description' => esc_html__( 'Taxonomy' ),
141 + 'type' => 'string',
142 + 'context' => array( 'view', 'edit', 'embed' ),
143 + 'readonly' => true,
144 + )
145 + )
146 + );
147 +
148 + return $schema;
149 + }
150 +
151 + /**
152 + * Schema for a templates.
153 + *
154 + * @param WP_REST_Request $request Current request.
155 + */
156 + public function templates_schema() {
157 + $schema = array(
158 + '$schema' => 'http://json-schema.org/draft-04/schema#',
159 + 'title' => 'templates',
160 + 'type' => 'object',
161 + 'properties' => array(
162 + 'post_type_name' => array(
163 + 'description' => esc_html__( 'Templates' ),
164 + 'type' => 'string',
165 + 'context' => array( 'view', 'edit', 'embed' ),
166 + 'readonly' => true,
167 + )
168 + )
169 + );
170 +
171 + return $schema;
172 + }
173 +
174 + public function get_remote_templates() {
175 +
176 + $cache = isset( $_GET['cache'] ) ? sanitize_text_field( wp_unslash( $_GET['cache'] ) ) : 'refresh';
177 + $templates_data = [];
178 +
179 + if ($cache == 'cache'){
180 + $templates_data = get_transient( 'getwid_templates_response_data' ); //Get Cache response
181 + } elseif ($cache == 'refresh') {
182 + delete_transient( 'getwid_templates_response_data' ); //Delete cache data
183 + }
184 +
185 + if ( $templates_data == false || empty( $templates_data ) ) {
186 +
187 + //Get Templates from remote server
188 + $response = wp_remote_get(
189 + $this->remote_template_library_url . "/wp-json/getwid-templates-server/v1/get_templates",
190 + array(
191 + 'timeout' => 15,
192 + )
193 + );
194 + // var_dump( $response ); exit();
195 +
196 + if ( is_wp_error( $response ) ) {
197 + return '<p>' . $response->get_error_message() . '</p>';
198 + } else {
199 +
200 + $templates_data = json_decode( wp_remote_retrieve_body( $response ), false );
201 +
202 + //JSON valid
203 + if ( json_last_error() === JSON_ERROR_NONE && $templates_data ) {
204 +
205 + set_transient( 'getwid_templates_response_data', $templates_data, 24 * HOUR_IN_SECONDS ); //Cache response
206 + return $templates_data;
207 +
208 + } else {
209 + return __( 'Error in json_decode.', 'getwid' );
210 + }
211 + }
212 + } else {
213 + return $templates_data;
214 + }
215 + }
216 +
217 + public function get_remote_content() {
218 +
219 + $get_content_url = isset( $_GET['get_content_url'] ) ? esc_url_raw( $_GET['get_content_url'] ) : false;
220 +
221 + if ( ! $this->content_url_is_valid( $get_content_url ) ) {
222 + return __( 'Please provide a valid URL.', 'getwid' );
223 + }
224 +
225 + //Get Templates from remote server
226 + $response = wp_remote_get(
227 + $get_content_url,
228 + array(
229 + 'timeout' => 15,
230 + )
231 + );
232 +
233 + $templates_data = json_decode( wp_remote_retrieve_body( $response ) );
234 +
235 + //JSON valid
236 + if ( json_last_error() === JSON_ERROR_NONE ) {
237 + return $templates_data;
238 + } else {
239 + return __( 'Error in json_decode.', 'getwid' );
240 + }
241 + }
242 +
243 + public function get_taxonomies($object) {
244 + $post_type_name = sanitize_text_field( wp_unslash( $_GET['post_type_name'] ) );
245 + $taxonomies = get_object_taxonomies( $post_type_name, 'objects' );
246 +
247 + $return = [];
248 + if (!empty($taxonomies)){
249 + foreach ($taxonomies as $key => $taxonomy_name) {
250 + $return[] = array(
251 + 'value' => $key,
252 + 'label' => $taxonomy_name->labels->name.' ('.$key.')'
253 + );
254 + }
255 + }
256 + return $return;
257 + }
258 +
259 + public function get_terms($object) {
260 + $taxonomy_name = getwid_recursive_sanitize_array( wp_unslash( $_GET['taxonomy_name'] ) );
261 +
262 + $return = [];
263 + $terms = get_terms(array(
264 + 'taxonomy' => $taxonomy_name,
265 + 'hide_empty' => false,
266 + ));
267 +
268 + if (!empty($terms)){
269 + foreach ($terms as $key => $term_name) {
270 + $taxonomy_obj = get_taxonomy( $term_name->taxonomy );
271 +
272 + $return[$term_name->taxonomy]['group_name'] = $taxonomy_obj->label;
273 + $return[$term_name->taxonomy]['group_value'][] = array(
274 + 'value' => $term_name->taxonomy.'['.$term_name->term_id.']',
275 + 'label' => $term_name->name,
276 + );
277 + }
278 + }
279 + return $return;
280 + }
281 +
282 + public function get_templates($object) {
283 + $template_name = sanitize_text_field( wp_unslash( $_GET['template_name'] ) );
284 +
285 + $posts = get_posts( array(
286 + 'numberposts' => -1,
287 + 'orderby' => 'date',
288 + 'order' => 'DESC',
289 + 'post_type' => $template_name,
290 + ) );
291 +
292 + $return = [];
293 + if (!empty($posts)){
294 + foreach ($posts as $key => $post) {
295 + $return[] = array(
296 + 'value' => $post->ID,
297 + 'label' => $post->post_title
298 + );
299 + }
300 + }
301 + return $return;
302 + }
303 +
304 + public function get_meta_keys( $request ) {
305 +
306 + $params = $request->get_params();
307 + unset( $params['metaQuery'] );
308 +
309 + $args = getwid_build_custom_post_type_query( $params );
310 + $args['fields'] = 'ids';
311 +
312 + if ( $args['posts_per_page'] > 100 ) {
313 + $args['posts_per_page'] = 100;
314 + }
315 +
316 + $posts = get_posts( $args );
317 +
318 + $meta_keys_to_skip = [ '_oembed', '_edit_lock', '_edit_last', '_wp_old_slug', '_wxr' ];
319 + $meta_keys = [];
320 + foreach ( $posts as $postID ) {
321 + foreach ( get_post_meta( $postID, '', true ) as $meta_key => $value ) {
322 +
323 + if ( in_array( $meta_key, $meta_keys ) ) {
324 + continue;
325 + }
326 +
327 + $should_skip = false;
328 + foreach( $meta_keys_to_skip as $skip ) {
329 + if ( 0 === strpos( $meta_key, $skip ) ) {
330 + $should_skip = true;
331 + break;
332 + }
333 + }
334 +
335 + if ( ! $should_skip ) {
336 + $meta_keys[] = $meta_key;
337 + }
338 + }
339 + }
340 +
341 + return array_values( $meta_keys );
342 + }
343 +
344 + private function content_url_is_valid( $url ) {
345 +
346 + $parsed_url = wp_parse_url( $url );
347 +
348 + if ( ! $parsed_url ) {
349 + return false;
350 + }
351 +
352 + $valid_hosts = array( 'elements.getwid.getmotopress.com' );
353 + $valid_path = '/wp-json/getwid-templates-server/v1/get_content';
354 +
355 + $remote_library_url = wp_parse_url( $this->remote_template_library_url );
356 +
357 + if ( $remote_library_url && isset( $remote_library_url['host'] ) ) {
358 + $valid_hosts[] = $remote_library_url['host'];
359 + }
360 +
361 + $host_is_valid = false;
362 + $path_is_valid = false;
363 +
364 + if ( isset( $parsed_url['host'] ) && in_array( $parsed_url['host'], $valid_hosts ) ) {
365 + $host_is_valid = true;
366 + }
367 +
368 + if ( isset( $parsed_url['path'] ) && $valid_path == $parsed_url['path'] ) {
369 + $path_is_valid = true;
370 + }
371 +
372 + return $host_is_valid && $path_is_valid;
373 + }
374 +}