PluginProbe
Polylang / 2.5
Polylang v2.5
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / modules / wpml / wpml-api.php

wpml-api.php in Polylang 2.5, at modules/wpml/wpml-api.php

268 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A class to handle the WPML API based on hooks, introduced since WPML 3.2
5 * It partly relies on the legacy API
6 *
7 * @see https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
8 *
9 * @since 2.0
10 */
11 class PLL_WPML_API {
12
13 /**
14 * Constructor
15 *
16 * @since 2.0
17 */
18 public function __construct() {
19 // Site Wide Language informations
20
21 add_filter( 'wpml_active_languages', array( $this, 'wpml_active_languages' ), 10, 2 );
22 add_filter( 'wpml_display_language_names', array( $this, 'wpml_display_language_names' ), 10, 5 );
23 // wpml_translated_language_name => not applicable
24 add_filter( 'wpml_current_language', 'pll_current_language', 10, 0 );
25 add_filter( 'wpml_default_language', 'pll_default_language', 10, 0 );
26 // wpml_add_language_selector => not implemented
27 // wpml_footer_language_selector => not applicable
28 add_action( 'wpml_add_language_form_field', array( $this, 'wpml_add_language_form_field' ) );
29 add_filter( 'wpml_language_is_active', array( $this, 'wpml_language_is_active' ), 10, 2 );
30 add_filter( 'wpml_is_rtl', array( $this, 'wpml_is_rtl' ) );
31 // wpml_language_form_input_field => See wpml_add_language_form_field
32 // wpml_language_has_switched => not implemented
33 // wpml_element_trid => not implemented
34 // wpml_get_element_translations => not implemented
35 // wpml_language_switcher => not implemented
36 // wpml_browser_redirect_language_params => not implemented
37 // wpml_enqueue_browser_redirect_language => not applicable
38 // wpml_enqueued_browser_redirect_language => not applicable
39 // wpml_encode_string => not applicable
40 // wpml_decode_string => not applicable
41
42 // Retrieving Language Information for Content
43
44 add_filter( 'wpml_post_language_details', 'wpml_get_language_information', 10, 2 );
45 // wpml_switch_language => not implemented
46 add_filter( 'wpml_element_language_code', array( $this, 'wpml_element_language_code' ), 10, 3 );
47 // wpml_element_language_details => not applicable
48
49 // Retrieving Localized Content
50
51 add_filter( 'wpml_home_url', 'pll_home_url', 10, 0 );
52 add_filter( 'wpml_element_link', 'icl_link_to_element', 10, 7 );
53 add_filter( 'wpml_object_id', 'icl_object_id', 10, 4 );
54 add_filter( 'wpml_translate_single_string', array( $this, 'wpml_translate_single_string' ), 10, 4 );
55 // wpml_translate_string => not applicable
56 // wpml_unfiltered_admin_string => not implemented
57 add_filter( 'wpml_permalink', array( $this, 'wpml_permalink' ), 10, 2 );
58 // wpml_elements_without_translations => not implemented
59 add_filter( 'wpml_get_translated_slug', array( $this, 'wpml_get_translated_slug' ), 10, 3 );
60
61 // Finding the Translation State of Content
62
63 // wpml_element_translation_type => not implemented
64 add_filter( 'wpml_element_has_translations', array( $this, 'wpml_element_has_translations' ), 10, 3 );
65 // wpml_master_post_from_duplicate => not applicable
66 // wpml_post_duplicates => not applicable
67
68 // Inserting Content
69
70 // wpml_admin_make_post_duplicates => not applicable
71 // wpml_make_post_duplicates => not applicable
72 add_action( 'wpml_register_single_string', 'icl_register_string', 10, 3 );
73 // wpml_register_string => not applicable
74 // wpml_register_string_packages => not applicable
75 // wpml_delete_package_action => not applicable
76 // wpml_show_package_language_ui => not applicable
77 // wpml_set_element_language_details => not implemented
78 // wpml_multilingual_options => not applicable
79
80 // Miscellaneous
81
82 // wpml_element_type => not applicable
83 // wpml_setting => not applicable
84 // wpml_sub_setting => not applicable
85 // wpml_editor_cf_to_display => not applicable
86 // wpml_tm_save_translation_cf => not implemented
87 // wpml_tm_xliff_export_translated_cf => not applicable
88 // wpml_tm_xliff_export_original_cf => not applicable
89 // wpml_duplicate_generic_string => not applicable
90 // wpml_translatable_user_meta_fields => not implemented
91 // wpml_cross_domain_language_data => not applicable
92 // wpml_get_cross_domain_language_data => not applicable
93 // wpml_loaded => not applicable
94 // wpml_st_loaded => not applicable
95 // wpml_tm_loaded => not applicable
96 // wpml_hide_management_column (3.4.1) => not applicable
97 // wpml_ls_directories_to_scan => not applicable
98 // wpml_ls_model_css_classes => not applicable
99 // wpml_ls_model_language_css_classes => not applicable
100 // wpml_tf_feedback_open_link => not applicable
101 // wpml_sync_custom_field => not implemented
102 // wpml_sync_all_custom_fields => not implemented
103 // wpml_is_redirected => not implemented
104
105 // Updating Content
106
107 // wpml_set_translation_mode_for_post_type => not implemented
108 }
109
110 /**
111 * Get a list of the languages enabled for a site
112 *
113 * @since 2.0
114 *
115 * @param mixed $null Not used
116 * @param array| string $args See arguments of icl_get_languages()
117 * @return array Array of arrays per language
118 */
119 public function wpml_active_languages( $null, $args = '' ) {
120 return icl_get_languages( $args );
121 }
122
123 /**
124 * In WPML, get a language's native and translated name for display in a custom language switcher
125 * Since Polylang does not implement the translated name, always returns only the native name
126 *
127 * @since 2.2
128 *
129 * @param mixed $null Not used.
130 * @param string $native_name The language native name.
131 * @param string|bool $translated_name The language translated name. Not used.
132 * @param bool $native_hidden Whether to hide the language native name or not. Not used.
133 * @param bool $translated_hidden Whether to hide the language translated name or not. Not used.
134 * @return string
135 */
136 public function wpml_display_language_names( $null, $native_name, $translated_name = false, $native_hidden = false, $translated_hidden = false ) {
137 return $native_name;
138 }
139
140 /**
141 * Returns an HTML hidden input field with name=”lang” and as value the current language
142 *
143 * @since 2.0
144 */
145 public function wpml_add_language_form_field() {
146 $lang = pll_current_language();
147 $field = sprintf( '<input type="hidden" name="lang" value="%s" />', esc_attr( $lang ) );
148 $field = apply_filters( 'wpml_language_form_input_field', $field, $lang );
149 echo $field;
150 }
151
152 /**
153 * Find out if a specific language is enabled for the site
154 *
155 * @since 2.0
156 *
157 * @param mixed $null Not used
158 * @param string $slug Language code
159 * @return bool
160 */
161 public function wpml_language_is_active( $null, $slug ) {
162 $language = PLL()->model->get_language( $slug );
163 return empty( $language->active ) || true === $language->active;
164 }
165
166 /**
167 * Find out whether the current language text direction is RTL or not
168 *
169 * @since 2.0
170 *
171 * @param mixed $null Not used
172 * @return bool
173 */
174 public function wpml_is_rtl( $null ) {
175 return pll_current_language( 'is_rtl' );
176 }
177
178 /**
179 * Get the language code for a translatable element
180 *
181 * @since 2.0
182 *
183 * @param mixed $language_code
184 * @param array $args An array with two keys element_id => post_id or term_taxonomy_id, element_type => post type or taxonomy
185 * @return string
186 */
187 public function wpml_element_language_code( $language_code, $args ) {
188 $type = $args['element_type'];
189 $id = $args['element_id'];
190 $pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
191 if ( 'term' === $pll_type && $term = wpcom_vip_get_term_by( 'term_taxonomy_id', $id ) ) {
192 $id = $term->term_id;
193 }
194 return $pll_type ? call_user_func( "pll_get_{$pll_type}_language", $id ) : $language_code;
195 }
196
197 /**
198 * Translates a string
199 *
200 * @since 2.0
201 *
202 * @param string $string The string's original value
203 * @param string $context The string's registered context
204 * @param string $name The string's registered name
205 * @param null|string $lang Optional, return the translation in this language, defaults to current language
206 * @return string The translated string
207 */
208 public function wpml_translate_single_string( $string, $context, $name, $lang = null ) {
209 $has_translation = null; // Passed by reference
210 return icl_translate( $context, $name, $string, false, $has_translation, $lang );
211 }
212
213 /**
214 * Converts a permalink to a language specific permalink
215 *
216 * @since 2.2
217 *
218 * @param string $url The url to filter
219 * @param null|string $lang Langage code, optional, defaults to the current language
220 * @return string
221 */
222 public function wpml_permalink( $url, $lang = '' ) {
223 $lang = PLL()->model->get_language( $lang );
224
225 if ( empty( $lang ) && ! empty( PLL()->curlang ) ) {
226 $lang = PLL()->curlang;
227 }
228
229 return empty( $lang ) ? $url : PLL()->links_model->switch_language_in_link( $url, $lang );
230 }
231
232 /**
233 * Translates a post type slug
234 *
235 * @since 2.2
236 *
237 * @param string $slug Post type slug
238 * @param string $post_type Post type name
239 * @param string $lang Optional language code (defaults to current language)
240 * @return string
241 */
242 public function wpml_get_translated_slug( $slug, $post_type, $lang = null ) {
243 if ( isset( PLL()->translate_slugs ) ) {
244 if ( empty( $lang ) ) {
245 $lang = pll_current_language();
246 }
247
248 $slug = PLL()->translate_slugs->slugs_model->get_translated_slug( $post_type, $lang );
249 }
250 return $slug;
251 }
252
253 /**
254 * Find out whether a post type or a taxonomy term is translated
255 *
256 * @since 2.0
257 *
258 * @param mixed $null
259 * @param int $id The post_id or term_id
260 * @param string $type The post type or taxonomy
261 * @return bool
262 */
263 public function wpml_element_has_translations( $null, $id, $type ) {
264 $pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
265 return ( $pll_type && $translations = call_user_func( "pll_get_{$pll_type}_translations", $id ) ) ? count( $translations ) > 1 : false;
266 }
267 }
268