PluginProbe
Bogo / 2.9
Bogo v2.9
3.9.3 trunk 1.0 1.1 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.8 2.8.1 2.9 3.0 3.0.1 All 52 releases
← All changes | includes/rest-api.php +56 -138 trunk2.9 View file →
@@ -1,65 +1,30 @@
1 1 <?php
2 2
3 -add_action( 'rest_api_init', 'bogo_rest_api_init', 10, 0 );
3 +add_action( 'rest_api_init', 'bogo_rest_api_init' );
4 4
5 5 function bogo_rest_api_init() {
6 6 register_rest_route( 'bogo/v1',
7 7 '/languages',
8 8 array(
9 - 'methods' => WP_REST_Server::READABLE,
10 - 'callback' => 'bogo_rest_languages',
11 - 'permission_callback' => '__return_true',
12 - )
13 - );
9 + 'methods' => 'GET',
10 + 'callback' => 'bogo_rest_languages' ) );
14 11
15 12 register_rest_route( 'bogo/v1',
16 13 '/posts/(?P<id>\d+)/translations',
17 14 array(
18 - 'methods' => WP_REST_Server::READABLE,
19 - 'callback' => 'bogo_rest_post_translations',
20 - 'permission_callback' => '__return_true',
21 - )
22 - );
15 + 'methods' => 'GET',
16 + 'callback' => 'bogo_rest_post_translations' ) );
23 17
24 - $locale_pattern = '[a-z]{2,3}(?:_[A-Z]{2}(?:_[A-Za-z0-9]+)?)?';
18 + $locale_pattern = '[a-z]{2}(?:_[A-Z]{2}(?:_[A-Za-z]+)?)?';
25 19
26 20 register_rest_route( 'bogo/v1',
27 21 '/posts/(?P<id>\d+)/translations/(?P<locale>' . $locale_pattern . ')',
28 22 array(
29 - 'methods' => WP_REST_Server::CREATABLE,
30 - 'callback' => 'bogo_rest_create_post_translation',
31 - 'permission_callback' => static function( WP_REST_Request $request ) {
32 - $locale = $request->get_param( 'locale' );
33 -
34 - if ( ! current_user_can( 'bogo_access_locale', $locale ) ) {
35 - return new WP_Error( 'bogo_locale_forbidden',
36 - __( 'You are not allowed to access posts in the requested locale.', 'bogo' ),
37 - array( 'status' => rest_authorization_required_code() )
38 - );
39 - }
40 -
41 - $post_id = $request->get_param( 'id' );
42 -
43 - if ( $post = get_post( $post_id ) ) {
44 - if (
45 - ! $post_type_object = get_post_type_object( $post->post_type ) or
46 - ! current_user_can( $post_type_object->cap->create_posts )
47 - ) {
48 - return new WP_Error( 'bogo_post_cannot_create',
49 - __( 'You are not allowed to create posts as this user.', 'bogo' ),
50 - array( 'status' => rest_authorization_required_code() )
51 - );
52 - }
53 - }
54 -
55 - return true;
56 - },
57 - )
58 - );
23 + 'methods' => 'POST',
24 + 'callback' => 'bogo_rest_create_post_translation' ) );
59 25 }
60 26
61 -
62 27 function bogo_rest_languages( WP_REST_Request $request ) {
63 28 if ( ! function_exists( 'wp_get_available_translations' ) ) {
64 29 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
65 30 }
@@ -65,19 +30,18 @@
65 30 }
66 31
67 32 $available_translations = wp_get_available_translations();
68 33
69 - $local_available_locales = bogo_available_locales();
34 + $local_available_locales = bogo_available_locales( array(
35 + 'exclude_enus_if_inactive' => true ) );
70 36
71 37 $available_translations = array_intersect_key(
72 38 $available_translations,
73 - array_flip( $local_available_locales )
74 - );
39 + array_flip( $local_available_locales ) );
75 40
76 41 return rest_ensure_response( $available_translations );
77 42 }
78 43
79 -
80 44 function bogo_rest_post_translations( WP_REST_Request $request ) {
81 45 $post_id = $request->get_param( 'id' );
82 46
83 47 $post = get_post( $post_id );
@@ -83,41 +47,28 @@
83 47 $post = get_post( $post_id );
84 48
85 49 if ( ! $post ) {
86 50 return new WP_Error( 'bogo_post_not_found',
87 - __( 'The requested post was not found.', 'bogo' ),
88 - array( 'status' => 404 )
89 - );
51 + __( "The requested post was not found.", 'bogo' ),
52 + array( 'status' => 404 ) );
90 53 }
91 54
92 55 $post_type_object = get_post_type_object( $post->post_type );
93 - $edit_post_cap = $post_type_object->cap->edit_post;
56 + $user_can_edit = current_user_can(
57 + $post_type_object->cap->edit_post, $post->ID );
94 58
95 - if (
96 - ! current_user_can( $edit_post_cap, $post->ID ) and
97 - 'publish' !== get_post_status( $post )
98 - ) {
59 + if ( ! $user_can_edit && 'publish' != get_post_status( $post ) ) {
99 60 return new WP_Error( 'bogo_post_not_found',
100 - __( 'The requested post was not found.', 'bogo' ),
101 - array( 'status' => 404 )
102 - );
61 + __( "The requested post was not found.", 'bogo' ),
62 + array( 'status' => 404 ) );
103 63 }
104 64
105 - if ( ! bogo_is_localizable_post_type( $post->post_type ) ) {
106 - return new WP_Error( 'bogo_post_type_invalid',
107 - __( 'The requested post type is not localizable.', 'bogo' ),
108 - array( 'status' => 400 )
109 - );
110 - }
111 -
112 65 $response = array();
113 66 $translations = bogo_get_post_translations( $post );
114 67
115 68 foreach ( $translations as $locale => $translation ) {
116 - if (
117 - ! current_user_can( 'edit_post', $translation->ID ) and
118 - 'publish' !== get_post_status( $translation )
119 - ) {
69 + if ( ! current_user_can( 'edit_post', $translation->ID )
70 + && 'publish' != get_post_status( $translation ) ) {
120 71 continue;
121 72 }
122 73
123 74 $response[$locale] = array(
@@ -132,10 +83,9 @@
132 83 'modified_gmt' => mysql_to_rfc3339( $translation->post_modified_gmt ),
133 84 'guid' => array( 'rendered' => '', 'raw' => '' ),
134 85 'title' => array( 'rendered' => '', 'raw' => '' ),
135 86 'content' => array( 'rendered' => '', 'raw' => '' ),
136 - 'excerpt' => array( 'rendered' => '', 'raw' => '' ),
137 - );
87 + 'excerpt' => array( 'rendered' => '', 'raw' => '' ) );
138 88
139 89 $lang = bogo_get_language( $locale );
140 90 $lang = empty( $lang ) ? $locale : $lang;
141 91 $response[$locale]['lang']['name'] = $lang;
@@ -143,9 +93,9 @@
143 93 if ( ! empty( $translation->guid ) ) {
144 94 $response[$locale]['guid']['rendered'] =
145 95 apply_filters( 'get_the_guid', $translation->guid );
146 96
147 - if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
97 + if ( $user_can_edit ) {
148 98 $response[$locale]['guid']['raw'] = $translation->guid;
149 99 }
150 100 }
151 101
@@ -152,31 +102,27 @@
152 102 if ( ! empty( $translation->post_title ) ) {
153 103 $response[$locale]['title']['rendered'] =
154 104 get_the_title( $translation->ID );
155 105
156 - if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
106 + if ( $user_can_edit ) {
157 107 $response[$locale]['title']['raw'] = $translation->post_title;
158 108 }
159 109 }
160 110
161 111 if ( ! empty( $translation->post_content ) ) {
162 - $response[$locale]['content']['rendered'] = apply_filters(
163 - 'the_content',
164 - $translation->post_content
165 - );
112 + $response[$locale]['content']['rendered'] =
113 + apply_filters( 'the_content', $translation->post_content );
166 114
167 - if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
115 + if ( $user_can_edit ) {
168 116 $response[$locale]['content']['raw'] = $translation->post_content;
169 117 }
170 118 }
171 119
172 120 if ( ! empty( $translation->post_excerpt ) ) {
173 - $response[$locale]['excerpt']['rendered'] = apply_filters(
174 - 'the_excerpt',
175 - apply_filters( 'get_the_excerpt', $translation->post_excerpt )
176 - );
121 + $response[$locale]['excerpt']['rendered'] = apply_filters( 'the_excerpt',
122 + apply_filters( 'get_the_excerpt', $translation->post_excerpt ) );
177 123
178 - if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
124 + if ( $user_can_edit ) {
179 125 $response[$locale]['excerpt']['raw'] = $translation->post_excerpt;
180 126 }
181 127 }
182 128 }
@@ -183,48 +129,40 @@
183 129
184 130 return rest_ensure_response( $response );
185 131 }
186 132
187 -
188 133 function bogo_rest_create_post_translation( WP_REST_Request $request ) {
189 134 $post_id = $request->get_param( 'id' );
190 135
191 136 $post = get_post( $post_id );
192 137
193 - if ( ! $post ) {
138 + if ( ! $post
139 + || ! current_user_can( 'edit_post', $post->ID )
140 + && 'publish' != get_post_status( $post ) ) {
194 141 return new WP_Error( 'bogo_post_not_found',
195 - __( 'The requested post was not found.', 'bogo' ),
196 - array( 'status' => 404 )
197 - );
142 + __( "The requested post was not found.", 'bogo' ),
143 + array( 'status' => 404 ) );
198 144 }
199 145
200 - $post_type_object = get_post_type_object( $post->post_type );
201 - $edit_post_cap = $post_type_object->cap->edit_post;
146 + $locale = $request->get_param( 'locale' );
202 147
203 - if (
204 - ! current_user_can( $edit_post_cap, $post->ID ) and
205 - 'publish' !== get_post_status( $post )
206 - ) {
207 - return new WP_Error( 'bogo_post_not_found',
208 - __( 'The requested post was not found.', 'bogo' ),
209 - array( 'status' => 404 )
210 - );
148 + if ( ! bogo_is_available_locale( $locale ) ) {
149 + return new WP_Error( 'bogo_locale_invalid',
150 + __( "The requested locale is not available.", 'bogo' ),
151 + array( 'status' => 400 ) );
211 152 }
212 153
213 - if ( ! bogo_is_localizable_post_type( $post->post_type ) ) {
214 - return new WP_Error( 'bogo_post_type_invalid',
215 - __( 'The requested post type is not localizable.', 'bogo' ),
216 - array( 'status' => 400 )
217 - );
154 + if ( ( $post_type_object = get_post_type_object( $post->post_type ) )
155 + && ! current_user_can( $post_type_object->cap->edit_posts ) ) {
156 + return new WP_Error( 'bogo_post_type_forbidden',
157 + __( "You are not allowed to edit posts in this post type.", 'bogo' ),
158 + array( 'status' => 403 ) );
218 159 }
219 160
220 - $locale = $request->get_param( 'locale' );
221 -
222 - if ( ! bogo_is_available_locale( $locale ) ) {
223 - return new WP_Error( 'bogo_locale_invalid',
224 - __( 'The requested locale is not available.', 'bogo' ),
225 - array( 'status' => 400 )
226 - );
161 + if ( ! current_user_can( 'bogo_access_locale', $locale ) ) {
162 + return new WP_Error( 'bogo_locale_forbidden',
163 + __( "You are not allowed to create posts in the requested locale.", 'bogo' ),
164 + array( 'status' => 403 ) );
227 165 }
228 166
229 167 $new_post_id = bogo_duplicate_post( $post, $locale );
230 168
@@ -229,11 +167,10 @@
229 167 $new_post_id = bogo_duplicate_post( $post, $locale );
230 168
231 169 if ( ! $new_post_id ) {
232 170 return new WP_Error( 'bogo_post_duplication_failed',
233 - __( 'Failed to duplicate a post.', 'bogo' ),
234 - array( 'status' => 500 )
235 - );
171 + __( "Failed to duplicate a post.", 'bogo' ),
172 + array( 'status' => 500 ) );
236 173 }
237 174
238 175 $new_post = get_post( $new_post_id );
239 176 $response = array();
@@ -248,13 +185,12 @@
248 185 'date' => mysql_to_rfc3339( $new_post->post_date ),
249 186 'date_gmt' => mysql_to_rfc3339( $new_post->post_date_gmt ),
250 187 'modified' => mysql_to_rfc3339( $new_post->post_modified ),
251 188 'modified_gmt' => mysql_to_rfc3339( $new_post->post_modified_gmt ),
252 - 'guid' => array( 'rendered' => '', 'raw' => '' ),
253 - 'title' => array( 'rendered' => '', 'raw' => '' ),
254 - 'content' => array( 'rendered' => '', 'raw' => '' ),
255 - 'excerpt' => array( 'rendered' => '', 'raw' => '' ),
256 - );
189 + 'guid' => array( 'rendered' => '', 'raw' => $new_post->guid ),
190 + 'title' => array( 'rendered' => '', 'raw' => $new_post->post_title ),
191 + 'content' => array( 'rendered' => '', 'raw' => $new_post->post_content ),
192 + 'excerpt' => array( 'rendered' => '', 'raw' => $new_post->post_excerpt ) );
257 193
258 194 $lang = bogo_get_language( $locale );
259 195 $lang = empty( $lang ) ? $locale : $lang;
260 196 $response[$locale]['lang']['name'] = $lang;
@@ -261,41 +197,23 @@
261 197
262 198 if ( ! empty( $new_post->guid ) ) {
263 199 $response[$locale]['guid']['rendered'] =
264 200 apply_filters( 'get_the_guid', $new_post->guid );
265 -
266 - if ( current_user_can( $edit_post_cap, $new_post->ID ) ) {
267 - $response[$locale]['guid']['raw'] = $new_post->guid;
268 - }
269 201 }
270 202
271 203 if ( ! empty( $new_post->post_title ) ) {
272 204 $response[$locale]['title']['rendered'] =
273 205 get_the_title( $new_post->ID );
274 -
275 - if ( current_user_can( $edit_post_cap, $new_post->ID ) ) {
276 - $response[$locale]['title']['raw'] = $new_post->post_title;
277 - }
278 206 }
279 207
280 208 if ( ! empty( $new_post->post_content ) ) {
281 209 $response[$locale]['content']['rendered'] =
282 210 apply_filters( 'the_content', $new_post->post_content );
283 -
284 - if ( current_user_can( $edit_post_cap, $new_post->ID ) ) {
285 - $response[$locale]['content']['raw'] = $new_post->post_content;
286 - }
287 211 }
288 212
289 213 if ( ! empty( $new_post->post_excerpt ) ) {
290 - $response[$locale]['excerpt']['rendered'] = apply_filters(
291 - 'the_excerpt',
292 - apply_filters( 'get_the_excerpt', $new_post->post_excerpt )
293 - );
294 -
295 - if ( current_user_can( $edit_post_cap, $new_post->ID ) ) {
296 - $response[$locale]['excerpt']['raw'] = $new_post->post_excerpt;
297 - }
214 + $response[$locale]['excerpt']['rendered'] = apply_filters( 'the_excerpt',
215 + apply_filters( 'get_the_excerpt', $new_post->post_excerpt ) );
298 216 }
299 217
300 218 return rest_ensure_response( $response );
301 219 }