-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-grf-wp-rest-api.php
executable file
·55 lines (48 loc) · 1.2 KB
/
class-grf-wp-rest-api.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
<?php
/**
* Register custom WP Rest API endpoints and handle their endpoints.
*
* @since 0.1
*/
class GRF_WP_Rest_API {
/**
* Register a custom WP REST API endpoint for validating an added FEED URL to the back-end.
*
* @since 0.1
*/
static function validate_feed_url() {
add_action( 'rest_api_init' , function () {
$route_settings = array(
'path' => 'gutenbergrssfeed/v2',
'name' => '/validateFeedUrl/'
);
register_rest_route( $route_settings[ 'path' ], $route_settings[ 'name' ], array(
'methods' => 'GET',
'args' => array(
'url'
),
'callback' => array( __CLASS__, 'validate_feed_url_endpoint' ),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
}
) );
} );
}
/**
* Validate a given URL if it is a correct RSS feed.
*
* @param array $data
* @return boolean
* @since 0.1
*/
static function validate_feed_url_endpoint( $data ) {
$validated[ 'success' ] = false;
if( ! empty( $data[ 'url' ] ) ) {
$feed = GRF_Helper::fetch_feed( $data[ 'url' ] );
if( ! is_wp_error( $feed ) && ! isset( $feed->errors ) ) {
$validated[ 'success' ] = true;
}
}
return wp_send_json( $validated );
}
}