|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* class for Rest API Routes within FooGallery |
|
4
|
|
|
* |
|
5
|
|
|
* @since 1.6.0 |
|
6
|
|
|
*/ |
|
7
|
|
|
if ( ! class_exists( 'FooGallery_Rest_Routes' ) ) { |
|
8
|
|
|
|
|
9
|
|
|
class FooGallery_Rest_Routes { |
|
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Constructs the class. |
|
12
|
|
|
* |
|
13
|
|
|
* @access public |
|
14
|
|
|
*/ |
|
15
|
|
|
public function __construct() { |
|
16
|
|
|
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Registers the necessary REST API routes for FooGallery |
|
21
|
|
|
* |
|
22
|
|
|
* @access public |
|
23
|
|
|
*/ |
|
24
|
|
|
public function register_routes() { |
|
25
|
|
|
$nonce = wp_create_nonce( 'wp_rest' ); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
register_rest_route( |
|
29
|
|
|
'foogallery/v1', |
|
30
|
|
|
'galleries', |
|
31
|
|
|
array( |
|
32
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
33
|
|
|
'callback' => array( $this, 'get_galleries' ), |
|
34
|
|
|
'permission_callback' => array( $this, 'get_galleries_permissions_check' ), |
|
35
|
|
|
'schema' => array( $this, 'get_galleries_schema' ), |
|
36
|
|
|
) |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Checks if a given request has access to get galleries. |
|
44
|
|
|
* |
|
45
|
|
|
* @access public |
|
46
|
|
|
* |
|
47
|
|
|
* @param WP_REST_Request $request Request. |
|
48
|
|
|
* |
|
49
|
|
|
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function get_galleries_permissions_check( $request ) { |
|
|
|
|
|
|
52
|
|
|
if ( ! current_user_can( 'edit_posts' ) ) { |
|
53
|
|
|
return new WP_Error( |
|
54
|
|
|
'foogallery_galleries_cannot_read', |
|
55
|
|
|
__( 'Sorry, you are not allowed to read galleries as this user.', 'foogallery' ), |
|
56
|
|
|
array( |
|
57
|
|
|
'status' => rest_authorization_required_code(), |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns a list of all galleries. |
|
67
|
|
|
* |
|
68
|
|
|
* @since 2.8.0 |
|
69
|
|
|
* @access public |
|
70
|
|
|
* |
|
71
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
72
|
|
|
* |
|
73
|
|
|
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function get_galleries( $request ) { |
|
|
|
|
|
|
76
|
|
|
$args = array( |
|
77
|
|
|
'post_type' => FOOGALLERY_CPT_GALLERY, |
|
78
|
|
|
'post_status' => array( 'publish', 'draft' ), |
|
79
|
|
|
'cache_results' => false, |
|
80
|
|
|
'nopaging' => true, |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$gallery_posts = get_posts( $args ); |
|
84
|
|
|
|
|
85
|
|
|
$galleries = array(); |
|
86
|
|
|
|
|
87
|
|
|
if ( !empty( $gallery_posts ) ) { |
|
88
|
|
|
foreach ( $gallery_posts as $post ) { |
|
89
|
|
|
$galleries[] = array( |
|
90
|
|
|
'ID' => $post->ID, |
|
91
|
|
|
'Name' => $post->post_title |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return rest_ensure_response( $galleries ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieves block's output schema, conforming to JSON Schema. |
|
101
|
|
|
* |
|
102
|
|
|
* @since 2.8.0 |
|
103
|
|
|
* @access public |
|
104
|
|
|
* |
|
105
|
|
|
* @return array Item schema data. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function get_item_schema() { |
|
108
|
|
|
return array( |
|
109
|
|
|
'$schema' => 'http://json-schema.org/schema#', |
|
110
|
|
|
'title' => 'foogallery', |
|
111
|
|
|
'type' => 'object', |
|
112
|
|
|
'properties' => array( |
|
113
|
|
|
'ID' => array( |
|
114
|
|
|
'description' => __( 'The FooGallery ID.', 'foogallery' ), |
|
115
|
|
|
'type' => 'int', |
|
116
|
|
|
'required' => true, |
|
117
|
|
|
'context' => array( 'edit' ), |
|
118
|
|
|
), |
|
119
|
|
|
'Name' => array( |
|
120
|
|
|
'description' => __( 'The FooGallery Name.', 'foogallery' ), |
|
121
|
|
|
'type' => 'string', |
|
122
|
|
|
'required' => false, |
|
123
|
|
|
'context' => array( 'edit' ), |
|
124
|
|
|
), |
|
125
|
|
|
), |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.