This repository was archived by the owner on Feb 18, 2024. It is now read-only.
forked from shawnhooper/wpml-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpml-rest-api.php
156 lines (132 loc) · 4.38 KB
/
wpml-rest-api.php
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/*
Plugin Name: WPML REST API
Version: 1.1.1
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
*/
namespace ShawnHooper\WPML;
class WPML_REST_API {
public function wordpress_hooks() {
add_action( 'rest_api_init', array($this, 'init'), 1000 );
}
public function 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 ) {
$this->register_api_field($post_type);
}
}
public function register_api_field($post_type) {
register_rest_field( $post_type,
'wpml_current_locale',
array(
'get_callback' => array($this, 'get_current_locale'),
'update_callback' => null,
'schema' => null,
)
);
register_rest_field( $post_type,
'wpml_translations',
array(
'get_callback' => array($this, 'get_translations'),
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Calculate the relative path for this post, supports also nested pages
*
* @param WP_Post $thisPost
* @return string the relative path for this page e.g. `root-page/child-page`
*/
public function calculate_rel_path(WP_Post $thisPost): string
{
$post_name = $thisPost->post_name;
if ($thisPost->post_parent > 0) {
$cur_post = get_post($thisPost->post_parent);
if (isset($cur_post)) {
$rel_path = $this->calculate_rel_path($cur_post);
return $rel_path . "/" . $post_name;
}
}
return $post_name;
}
/**
* 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
*/
public function get_translations( $object, $field_name, $request ) {
global $sitepress;
$languages = apply_filters('wpml_active_languages', null);
$translations = [];
$show_on_front = get_option( 'show_on_front' );
$page_on_front = get_option( 'page_on_front' );
foreach ($languages as $language) {
$post_id = apply_filters( 'wpml_object_id', $object['id'], 'post', false, $language['language_code'] );
if ($post_id === null || $post_id == $object['id']) continue;
$thisPost = get_post($post_id);
// Only show published posts
if ( 'publish' !== $thisPost->post_status) {
continue;
}
$href = apply_filters( 'wpmlrestapi_translations_href', $language['url'], $thisPost );
if ( 'page' == $show_on_front && $object['id'] == $page_on_front ) {
$href = trailingslashit( $href );
} else {
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
*/
public function get_current_locale( $object, $field_name, $request ) {
$langInfo = wpml_get_language_information($object);
if (!is_wp_error($langInfo)) {
return $langInfo['locale'];
}
}
}
$GLOBALS['WPML_REST_API'] = new WPML_REST_API();
$GLOBALS['WPML_REST_API']->wordpress_hooks();