@@ -71,7 +71,7 @@ public function register_rest_route(){
71
71
}
72
72
73
73
public function permissions_check ( $ request ) {
74
- if ( ! current_user_can ( 'read ' ) ) {
74
+ if ( ! current_user_can ( 'edit_posts ' ) ) {
75
75
return new \WP_Error (
76
76
'rest_forbidden ' ,
77
77
esc_html__ ( 'Forbidden. ' ),
@@ -165,7 +165,7 @@ public function templates_schema() {
165
165
166
166
public function get_remote_templates () {
167
167
168
- $ cache = sanitize_text_field ( wp_unslash ( $ _GET ['cache ' ] ) );
168
+ $ cache = isset ( $ _GET [ ' cache ' ] ) ? sanitize_text_field ( wp_unslash ( $ _GET ['cache ' ] ) ) : ' refresh ' ;
169
169
$ templates_data = [];
170
170
171
171
if ($ cache == 'cache ' ){
@@ -207,18 +207,21 @@ public function get_remote_templates() {
207
207
}
208
208
209
209
public function get_remote_content () {
210
- $ get_content_url = esc_url_raw ( $ _GET ['get_content_url ' ] );
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
+ }
211
216
212
217
//Get Templates from remote server
213
218
$ response = wp_remote_get (
214
219
$ get_content_url ,
215
220
array (
216
221
'timeout ' => 15 ,
217
- )
222
+ )
218
223
);
219
224
220
- // var_dump( wp_remote_retrieve_body( $response ) ); exit();
221
-
222
225
$ templates_data = json_decode ( wp_remote_retrieve_body ( $ response ) );
223
226
224
227
//JSON valid
@@ -289,4 +292,35 @@ public function get_templates($object) {
289
292
}
290
293
return $ return ;
291
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
+ }
292
326
}
0 commit comments