Repository navigation
Expand file tree
/
Copy pathwpml-rest-api.php
More file actions
115 lines (95 loc) · 3.23 KB
/
Copy pathwpml-rest-api.php
File metadata and controls
115 lines (95 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
Plugin Name: WPML REST API
Version: 1.0
Description: Adds links to posts in other languages into the results of a WP REST API query for sites running the WPML plugin.
Author: Shawn Hooper
Author URI: https://profiles.wordpress.org/shooper
*/
add_action( 'rest_api_init', 'wpmlrestapi_init', 1000);
function wpmlrestapi_init() {
// Check if WPML is installed
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if (!is_plugin_active('sitepress-multilingual-cms/sitepress.php')) {
return;
}
$available_langs = wpml_get_active_languages_filter('', array('skip_missing' => false, ) );
if ( ! empty( $available_langs ) && ! isset( $GLOBALS['icl_language_switched'] ) || ! $GLOBALS['icl_language_switched'] ) {
if ( isset( $_REQUEST['wpml_lang'] ) ) {
$lang = $_REQUEST['wpml_lang'];
} else if ( isset( $_REQUEST['lang'] ) ) {
$lang = $_REQUEST['lang'];
}
if ( isset( $lang ) && in_array( $lang, array_keys( $available_langs ) ) ) {
do_action( 'wpml_switch_language', $lang );
}
}
// Add WPML fields to all post types
// Thanks to Roy Sivan for this trick.
// http://www.roysivan.com/wp-api-v2-adding-fields-to-all-post-types/#.VsH0e5MrLcM
$post_types = get_post_types( array( 'public' => true, 'exclude_from_search' => false ), 'names' );
foreach( $post_types as $post_type ) {
wpmlrestapi_register_api_field($post_type);
}
}
function wpmlrestapi_register_api_field($post_type) {
register_rest_field( $post_type,
'wpml_current_locale',
array(
'get_callback' => 'wpmlrestapi_slug_get_current_locale',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field( $post_type,
'wpml_translations',
array(
'get_callback' => 'wpmlrestapi_slug_get_translations',
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Retrieve available translations
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @return mixed
*/
function wpmlrestapi_slug_get_translations( $object, $field_name, $request ) {
global $sitepress;
$languages = apply_filters('wpml_active_languages', null);
$translations = [];
foreach ($languages as $language) {
$post_id = wpml_object_id_filter($object['id'], 'post', false, $language['language_code']);
if ($post_id === null || $post_id == $object['id']) continue;
$thisPost = get_post($post_id);
$href= apply_filters( 'WPML_filter_link', $language[ 'url' ], $language );
if (strpos($href, '?') !== false) {
$href = str_replace('?', '/' . $thisPost->post_name . '/?', $href);
} else {
if (substr($href, -1) !== '/') {
$href .= '/';
}
$href .= $thisPost->post_name . '/';
}
$translations[] = array('locale' => $language['default_locale'], 'id' => $thisPost->ID, 'post_title' => $thisPost->post_title, 'href' => $href);
}
return $translations;
}
/**
* Retrieve the current locale
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @return mixed
*/
function wpmlrestapi_slug_get_current_locale( $object, $field_name, $request ) {
$langInfo = wpml_get_language_information($object);
return $langInfo['locale'];
}