| 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 |
$new_post_id = bogo_duplicate_post( $post, $locale ); |
| 162 |
|
| 163 |
if ( ! $new_post_id ) { |
| 164 |
return new WP_Error( 'bogo_post_duplication_failed', |
| 165 |
__( "Failed to duplicate a post.", 'bogo' ), |
| 166 |
array( 'status' => 500 ) ); |
| 167 |
} |
| 168 |
|
| 169 |
$new_post = get_post( $new_post_id ); |
| 170 |
$response = array(); |
| 171 |
|
| 172 |
$response[$locale] = array( |
| 173 |
'lang' => array( 'tag' => bogo_language_tag( $locale ) ), |
| 174 |
'id' => $new_post->ID, |
| 175 |
'link' => get_permalink( $new_post->ID ), |
| 176 |
'edit_link' => get_edit_post_link( $new_post->ID, 'raw' ), |
| 177 |
'slug' => $new_post->post_name, |
| 178 |
'type' => $new_post->post_type, |
| 179 |
'date' => mysql_to_rfc3339( $new_post->post_date ), |
| 180 |
'date_gmt' => mysql_to_rfc3339( $new_post->post_date_gmt ), |
| 181 |
'modified' => mysql_to_rfc3339( $new_post->post_modified ), |
| 182 |
'modified_gmt' => mysql_to_rfc3339( $new_post->post_modified_gmt ), |
| 183 |
'guid' => array( 'rendered' => '', 'raw' => $new_post->guid ), |
| 184 |
'title' => array( 'rendered' => '', 'raw' => $new_post->post_title ), |
| 185 |
'content' => array( 'rendered' => '', 'raw' => $new_post->post_content ), |
| 186 |
'excerpt' => array( 'rendered' => '', 'raw' => $new_post->post_excerpt ) ); |
| 187 |
|
| 188 |
$lang = bogo_get_language( $locale ); |
| 189 |
$lang = empty( $lang ) ? $locale : $lang; |
| 190 |
$response[$locale]['lang']['name'] = $lang; |
| 191 |
|
| 192 |
if ( ! empty( $new_post->guid ) ) { |
| 193 |
$response[$locale]['guid']['rendered'] = |
| 194 |
apply_filters( 'get_the_guid', $new_post->guid ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! empty( $new_post->post_title ) ) { |
| 198 |
$response[$locale]['title']['rendered'] = |
| 199 |
get_the_title( $new_post->ID ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( ! empty( $new_post->post_content ) ) { |
| 203 |
$response[$locale]['content']['rendered'] = |
| 204 |
apply_filters( 'the_content', $new_post->post_content ); |
| 205 |
} |
| 206 |
|
| 207 |
if ( ! empty( $new_post->post_excerpt ) ) { |
| 208 |
$response[$locale]['excerpt']['rendered'] = apply_filters( 'the_excerpt', |
| 209 |
apply_filters( 'get_the_excerpt', $new_post->post_excerpt ) ); |
| 210 |
} |
| 211 |
|
| 212 |
return rest_ensure_response( $response ); |
| 213 |
} |
| 214 |
|