PluginProbe
Bogo / 3.9.3
Bogo v3.9.3
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
bogo / includes / rest-api.php

rest-api.php in Bogo 3.9.3, at includes/rest-api.php

302 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 add_action( 'rest_api_init', 'bogo_rest_api_init', 10, 0 );
4
5 function bogo_rest_api_init() {
6 register_rest_route( 'bogo/v1',
7 '/languages',
8 array(
9 'methods' => WP_REST_Server::READABLE,
10 'callback' => 'bogo_rest_languages',
11 'permission_callback' => '__return_true',
12 )
13 );
14
15 register_rest_route( 'bogo/v1',
16 '/posts/(?P<id>\d+)/translations',
17 array(
18 'methods' => WP_REST_Server::READABLE,
19 'callback' => 'bogo_rest_post_translations',
20 'permission_callback' => '__return_true',
21 )
22 );
23
24 $locale_pattern = '[a-z]{2,3}(?:_[A-Z]{2}(?:_[A-Za-z0-9]+)?)?';
25
26 register_rest_route( 'bogo/v1',
27 '/posts/(?P<id>\d+)/translations/(?P<locale>' . $locale_pattern . ')',
28 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 );
59 }
60
61
62 function bogo_rest_languages( WP_REST_Request $request ) {
63 if ( ! function_exists( 'wp_get_available_translations' ) ) {
64 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
65 }
66
67 $available_translations = wp_get_available_translations();
68
69 $local_available_locales = bogo_available_locales();
70
71 $available_translations = array_intersect_key(
72 $available_translations,
73 array_flip( $local_available_locales )
74 );
75
76 return rest_ensure_response( $available_translations );
77 }
78
79
80 function bogo_rest_post_translations( WP_REST_Request $request ) {
81 $post_id = $request->get_param( 'id' );
82
83 $post = get_post( $post_id );
84
85 if ( ! $post ) {
86 return new WP_Error( 'bogo_post_not_found',
87 __( 'The requested post was not found.', 'bogo' ),
88 array( 'status' => 404 )
89 );
90 }
91
92 $post_type_object = get_post_type_object( $post->post_type );
93 $edit_post_cap = $post_type_object->cap->edit_post;
94
95 if (
96 ! current_user_can( $edit_post_cap, $post->ID ) and
97 'publish' !== get_post_status( $post )
98 ) {
99 return new WP_Error( 'bogo_post_not_found',
100 __( 'The requested post was not found.', 'bogo' ),
101 array( 'status' => 404 )
102 );
103 }
104
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 $response = array();
113 $translations = bogo_get_post_translations( $post );
114
115 foreach ( $translations as $locale => $translation ) {
116 if (
117 ! current_user_can( 'edit_post', $translation->ID ) and
118 'publish' !== get_post_status( $translation )
119 ) {
120 continue;
121 }
122
123 $response[$locale] = array(
124 'lang' => array( 'tag' => bogo_language_tag( $locale ) ),
125 'id' => $translation->ID,
126 'link' => get_permalink( $translation->ID ),
127 'slug' => $translation->post_name,
128 'type' => $translation->post_type,
129 'date' => mysql_to_rfc3339( $translation->post_date ),
130 'date_gmt' => mysql_to_rfc3339( $translation->post_date_gmt ),
131 'modified' => mysql_to_rfc3339( $translation->post_modified ),
132 'modified_gmt' => mysql_to_rfc3339( $translation->post_modified_gmt ),
133 'guid' => array( 'rendered' => '', 'raw' => '' ),
134 'title' => array( 'rendered' => '', 'raw' => '' ),
135 'content' => array( 'rendered' => '', 'raw' => '' ),
136 'excerpt' => array( 'rendered' => '', 'raw' => '' ),
137 );
138
139 $lang = bogo_get_language( $locale );
140 $lang = empty( $lang ) ? $locale : $lang;
141 $response[$locale]['lang']['name'] = $lang;
142
143 if ( ! empty( $translation->guid ) ) {
144 $response[$locale]['guid']['rendered'] =
145 apply_filters( 'get_the_guid', $translation->guid );
146
147 if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
148 $response[$locale]['guid']['raw'] = $translation->guid;
149 }
150 }
151
152 if ( ! empty( $translation->post_title ) ) {
153 $response[$locale]['title']['rendered'] =
154 get_the_title( $translation->ID );
155
156 if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
157 $response[$locale]['title']['raw'] = $translation->post_title;
158 }
159 }
160
161 if ( ! empty( $translation->post_content ) ) {
162 $response[$locale]['content']['rendered'] = apply_filters(
163 'the_content',
164 $translation->post_content
165 );
166
167 if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
168 $response[$locale]['content']['raw'] = $translation->post_content;
169 }
170 }
171
172 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 );
177
178 if ( current_user_can( $edit_post_cap, $translation->ID ) ) {
179 $response[$locale]['excerpt']['raw'] = $translation->post_excerpt;
180 }
181 }
182 }
183
184 return rest_ensure_response( $response );
185 }
186
187
188 function bogo_rest_create_post_translation( WP_REST_Request $request ) {
189 $post_id = $request->get_param( 'id' );
190
191 $post = get_post( $post_id );
192
193 if ( ! $post ) {
194 return new WP_Error( 'bogo_post_not_found',
195 __( 'The requested post was not found.', 'bogo' ),
196 array( 'status' => 404 )
197 );
198 }
199
200 $post_type_object = get_post_type_object( $post->post_type );
201 $edit_post_cap = $post_type_object->cap->edit_post;
202
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 );
211 }
212
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 );
218 }
219
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 );
227 }
228
229 $new_post_id = bogo_duplicate_post( $post, $locale );
230
231 if ( ! $new_post_id ) {
232 return new WP_Error( 'bogo_post_duplication_failed',
233 __( 'Failed to duplicate a post.', 'bogo' ),
234 array( 'status' => 500 )
235 );
236 }
237
238 $new_post = get_post( $new_post_id );
239 $response = array();
240
241 $response[$locale] = array(
242 'lang' => array( 'tag' => bogo_language_tag( $locale ) ),
243 'id' => $new_post->ID,
244 'link' => get_permalink( $new_post->ID ),
245 'edit_link' => get_edit_post_link( $new_post->ID, 'raw' ),
246 'slug' => $new_post->post_name,
247 'type' => $new_post->post_type,
248 'date' => mysql_to_rfc3339( $new_post->post_date ),
249 'date_gmt' => mysql_to_rfc3339( $new_post->post_date_gmt ),
250 'modified' => mysql_to_rfc3339( $new_post->post_modified ),
251 '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 );
257
258 $lang = bogo_get_language( $locale );
259 $lang = empty( $lang ) ? $locale : $lang;
260 $response[$locale]['lang']['name'] = $lang;
261
262 if ( ! empty( $new_post->guid ) ) {
263 $response[$locale]['guid']['rendered'] =
264 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 }
270
271 if ( ! empty( $new_post->post_title ) ) {
272 $response[$locale]['title']['rendered'] =
273 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 }
279
280 if ( ! empty( $new_post->post_content ) ) {
281 $response[$locale]['content']['rendered'] =
282 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 }
288
289 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 }
298 }
299
300 return rest_ensure_response( $response );
301 }
302