PluginProbe
Bogo / 2.8
Bogo v2.8
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 2.8, at includes/rest-api.php

220 lines 6.9 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' );
4
5 function bogo_rest_api_init() {
6 register_rest_route( 'bogo/v1',
7 '/languages',
8 array(
9 'methods' => 'GET',
10 'callback' => 'bogo_rest_languages' ) );
11
12 register_rest_route( 'bogo/v1',
13 '/posts/(?P<id>\d+)/translations',
14 array(
15 'methods' => 'GET',
16 'callback' => 'bogo_rest_post_translations' ) );
17
18 $locale_pattern = '[a-z]{2}(?:_[A-Z]{2}(?:_[A-Za-z]+)?)?';
19
20 register_rest_route( 'bogo/v1',
21 '/posts/(?P<id>\d+)/translations/(?P<locale>' . $locale_pattern . ')',
22 array(
23 'methods' => 'POST',
24 'callback' => 'bogo_rest_create_post_translation' ) );
25 }
26
27 function bogo_rest_languages( WP_REST_Request $request ) {
28 if ( ! function_exists( 'wp_get_available_translations' ) ) {
29 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
30 }
31
32 $available_translations = wp_get_available_translations();
33
34 $local_available_locales = bogo_available_locales( array(
35 'exclude_enus_if_inactive' => true ) );
36
37 $available_translations = array_intersect_key(
38 $available_translations,
39 array_flip( $local_available_locales ) );
40
41 return rest_ensure_response( $available_translations );
42 }
43
44 function bogo_rest_post_translations( WP_REST_Request $request ) {
45 $post_id = $request->get_param( 'id' );
46
47 $post = get_post( $post_id );
48
49 if ( ! $post ) {
50 return new WP_Error( 'bogo_post_not_found',
51 __( "The requested post was not found.", 'bogo' ),
52 array( 'status' => 404 ) );
53 }
54
55 $post_type_object = get_post_type_object( $post->post_type );
56 $user_can_edit = current_user_can(
57 $post_type_object->cap->edit_post, $post->ID );
58
59 if ( ! $user_can_edit && 'publish' != get_post_status( $post ) ) {
60 return new WP_Error( 'bogo_post_not_found',
61 __( "The requested post was not found.", 'bogo' ),
62 array( 'status' => 404 ) );
63 }
64
65 $response = array();
66 $translations = bogo_get_post_translations( $post );
67
68 foreach ( $translations as $locale => $translation ) {
69 if ( ! current_user_can( 'edit_post', $translation->ID )
70 && 'publish' != get_post_status( $translation ) ) {
71 continue;
72 }
73
74 $response[$locale] = array(
75 'lang' => array( 'tag' => bogo_language_tag( $locale ) ),
76 'id' => $translation->ID,
77 'link' => get_permalink( $translation->ID ),
78 'slug' => $translation->post_name,
79 'type' => $translation->post_type,
80 'date' => mysql_to_rfc3339( $translation->post_date ),
81 'date_gmt' => mysql_to_rfc3339( $translation->post_date_gmt ),
82 'modified' => mysql_to_rfc3339( $translation->post_modified ),
83 'modified_gmt' => mysql_to_rfc3339( $translation->post_modified_gmt ),
84 'guid' => array( 'rendered' => '', 'raw' => '' ),
85 'title' => array( 'rendered' => '', 'raw' => '' ),
86 'content' => array( 'rendered' => '', 'raw' => '' ),
87 'excerpt' => array( 'rendered' => '', 'raw' => '' ) );
88
89 $lang = bogo_get_language( $locale );
90 $lang = empty( $lang ) ? $locale : $lang;
91 $response[$locale]['lang']['name'] = $lang;
92
93 if ( ! empty( $translation->guid ) ) {
94 $response[$locale]['guid']['rendered'] =
95 apply_filters( 'get_the_guid', $translation->guid );
96
97 if ( $user_can_edit ) {
98 $response[$locale]['guid']['raw'] = $translation->guid;
99 }
100 }
101
102 if ( ! empty( $translation->post_title ) ) {
103 $response[$locale]['title']['rendered'] =
104 get_the_title( $translation->ID );
105
106 if ( $user_can_edit ) {
107 $response[$locale]['title']['raw'] = $translation->post_title;
108 }
109 }
110
111 if ( ! empty( $translation->post_content ) ) {
112 $response[$locale]['content']['rendered'] =
113 apply_filters( 'the_content', $translation->post_content );
114
115 if ( $user_can_edit ) {
116 $response[$locale]['content']['raw'] = $translation->post_content;
117 }
118 }
119
120 if ( ! empty( $translation->post_excerpt ) ) {
121 $response[$locale]['excerpt']['rendered'] = apply_filters( 'the_excerpt',
122 apply_filters( 'get_the_excerpt', $translation->post_excerpt ) );
123
124 if ( $user_can_edit ) {
125 $response[$locale]['excerpt']['raw'] = $translation->post_excerpt;
126 }
127 }
128 }
129
130 return rest_ensure_response( $response );
131 }
132
133 function bogo_rest_create_post_translation( WP_REST_Request $request ) {
134 $post_id = $request->get_param( 'id' );
135
136 $post = get_post( $post_id );
137
138 if ( ! $post
139 || ! current_user_can( 'edit_post', $post->ID )
140 && 'publish' != get_post_status( $post ) ) {
141 return new WP_Error( 'bogo_post_not_found',
142 __( "The requested post was not found.", 'bogo' ),
143 array( 'status' => 404 ) );
144 }
145
146 $locale = $request->get_param( 'locale' );
147
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 ) );
152 }
153
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 ) );
159 }
160
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 ) );
165 }
166
167 $new_post_id = bogo_duplicate_post( $post, $locale );
168
169 if ( ! $new_post_id ) {
170 return new WP_Error( 'bogo_post_duplication_failed',
171 __( "Failed to duplicate a post.", 'bogo' ),
172 array( 'status' => 500 ) );
173 }
174
175 $new_post = get_post( $new_post_id );
176 $response = array();
177
178 $response[$locale] = array(
179 'lang' => array( 'tag' => bogo_language_tag( $locale ) ),
180 'id' => $new_post->ID,
181 'link' => get_permalink( $new_post->ID ),
182 'edit_link' => get_edit_post_link( $new_post->ID, 'raw' ),
183 'slug' => $new_post->post_name,
184 'type' => $new_post->post_type,
185 'date' => mysql_to_rfc3339( $new_post->post_date ),
186 'date_gmt' => mysql_to_rfc3339( $new_post->post_date_gmt ),
187 'modified' => mysql_to_rfc3339( $new_post->post_modified ),
188 'modified_gmt' => mysql_to_rfc3339( $new_post->post_modified_gmt ),
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 ) );
193
194 $lang = bogo_get_language( $locale );
195 $lang = empty( $lang ) ? $locale : $lang;
196 $response[$locale]['lang']['name'] = $lang;
197
198 if ( ! empty( $new_post->guid ) ) {
199 $response[$locale]['guid']['rendered'] =
200 apply_filters( 'get_the_guid', $new_post->guid );
201 }
202
203 if ( ! empty( $new_post->post_title ) ) {
204 $response[$locale]['title']['rendered'] =
205 get_the_title( $new_post->ID );
206 }
207
208 if ( ! empty( $new_post->post_content ) ) {
209 $response[$locale]['content']['rendered'] =
210 apply_filters( 'the_content', $new_post->post_content );
211 }
212
213 if ( ! empty( $new_post->post_excerpt ) ) {
214 $response[$locale]['excerpt']['rendered'] = apply_filters( 'the_excerpt',
215 apply_filters( 'get_the_excerpt', $new_post->post_excerpt ) );
216 }
217
218 return rest_ensure_response( $response );
219 }
220