PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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.3.6, at modules/wpml/wpml-api.php

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