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
|
|
|
if ( !apply_filters( 'foogallery_gutenberg_enabled', true ) ) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
register_rest_route( |
30
|
|
|
'foogallery/v1', |
31
|
|
|
'galleries', |
32
|
|
|
array( |
33
|
|
|
'methods' => WP_REST_Server::READABLE, |
34
|
|
|
'callback' => array( $this, 'get_galleries' ), |
35
|
|
|
'permission_callback' => array( $this, 'get_galleries_permissions_check' ), |
36
|
|
|
'schema' => array( $this, 'get_galleries_schema' ), |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Checks if a given request has access to get galleries. |
43
|
|
|
* |
44
|
|
|
* @access public |
45
|
|
|
* |
46
|
|
|
* @param WP_REST_Request $request Request. |
47
|
|
|
* |
48
|
|
|
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
49
|
|
|
*/ |
50
|
|
|
public function get_galleries_permissions_check( $request ) { |
|
|
|
|
51
|
|
|
if ( ! current_user_can( 'edit_posts' ) ) { |
52
|
|
|
return new WP_Error( |
53
|
|
|
'foogallery_galleries_cannot_read', |
54
|
|
|
__( 'Sorry, you are not allowed to read galleries as this user.', 'foogallery' ), |
55
|
|
|
array( |
56
|
|
|
'status' => rest_authorization_required_code(), |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns a list of all galleries. |
66
|
|
|
* |
67
|
|
|
* @since 2.8.0 |
68
|
|
|
* @access public |
69
|
|
|
* |
70
|
|
|
* @param WP_REST_Request $request Full details about the request. |
71
|
|
|
* |
72
|
|
|
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
73
|
|
|
*/ |
74
|
|
|
public function get_galleries( $request ) { |
|
|
|
|
75
|
|
|
|
76
|
|
|
$galleries = foogallery_get_all_galleries(); |
77
|
|
|
|
78
|
|
|
$result = array(); |
79
|
|
|
|
80
|
|
|
if ( !empty( $galleries ) ) { |
81
|
|
|
foreach ( $galleries as $gallery ) { |
82
|
|
|
$args = array( |
83
|
|
|
'width' => 150, |
84
|
|
|
'height' => 150 |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$featuredAttachment = $gallery->featured_attachment(); |
88
|
|
|
if ( $featuredAttachment ) { |
89
|
|
|
$img = $featuredAttachment->html_img_src( $args ); |
90
|
|
|
} else { |
91
|
|
|
//if we have no featured attachment, then use the built-in image placeholder |
92
|
|
|
$img = foogallery_image_placeholder_src(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
$result[] = array( |
97
|
|
|
'id' => $gallery->ID, |
98
|
|
|
'name' => $gallery->name, |
99
|
|
|
'thumbnail' => $img |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return rest_ensure_response( $result ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Retrieves block's output schema, conforming to JSON Schema. |
109
|
|
|
* |
110
|
|
|
* @since 2.8.0 |
111
|
|
|
* @access public |
112
|
|
|
* |
113
|
|
|
* @return array Item schema data. |
114
|
|
|
*/ |
115
|
|
|
public function get_item_schema() { |
116
|
|
|
return array( |
117
|
|
|
'$schema' => 'http://json-schema.org/schema#', |
118
|
|
|
'title' => 'foogallery', |
119
|
|
|
'type' => 'object', |
120
|
|
|
'properties' => array( |
121
|
|
|
'id' => array( |
122
|
|
|
'description' => __( 'The FooGallery ID.', 'foogallery' ), |
123
|
|
|
'type' => 'int', |
124
|
|
|
'required' => true, |
125
|
|
|
'context' => array( 'edit' ), |
126
|
|
|
), |
127
|
|
|
'name' => array( |
128
|
|
|
'description' => __( 'The FooGallery Name.', 'foogallery' ), |
129
|
|
|
'type' => 'string', |
130
|
|
|
'required' => false, |
131
|
|
|
'context' => array( 'edit' ), |
132
|
|
|
), |
133
|
|
|
'thumbnail' => array( |
134
|
|
|
'description' => __( 'The FooGallery Thumbnail.', 'foogallery' ), |
135
|
|
|
'type' => 'string', |
136
|
|
|
'required' => false, |
137
|
|
|
'context' => array( 'edit' ), |
138
|
|
|
), |
139
|
|
|
), |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
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.