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

189 lines 7.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 // wpml_display_language_names => not implemented
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( PLL()->links_model, 'switch_language_in_link' ), 10, 2 );
49 // wpml_elements_without_translations => not implemented
50 // wpml_get_translated_slug => not implemented
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 }
87
88 /**
89 * Get a list of the languages enabled for a site
90 *
91 * @since 2.0
92 *
93 * @param mixed $null not used
94 * @param array| string $args see arguments of icl_get_languages()
95 * @return array array of arrays per language
96 */
97 public function wpml_active_languages( $null, $args = '' ) {
98 return icl_get_languages( $args );
99 }
100
101 /**
102 * Returns an HTML hidden input field with name=”lang” and as value the current language
103 *
104 * @since 2.0
105 */
106 public function wpml_add_language_form_field() {
107 $lang = pll_current_language();
108 $field = sprintf( '<input type="hidden" name="lang" value="%s" />', esc_attr( $lang ) );
109 $field = apply_filters( 'wpml_language_form_input_field', $field, $lang );
110 echo $field;
111 }
112
113 /**
114 * Find out if a specific language is enabled for the site
115 *
116 * @since 2.0
117 *
118 * @param mixed $null not used
119 * @param string $slug language code
120 * @return bool
121 */
122 public function wpml_language_is_active( $null, $slug ) {
123 $language = PLL()->model->get_language( $slug );
124 return empty( $language->active ) || true === $language->active;
125 }
126
127 /**
128 * Find out whether the current language text direction is RTL or not
129 *
130 * @since 2.0
131 *
132 * @param mixed $null not used
133 * @return bool
134 */
135 public function wpml_is_rtl( $null ) {
136 return pll_current_language( 'is_rtl' );
137 }
138
139 /**
140 * Get the language code for a translatable element
141 *
142 * @since 2.0
143 *
144 * @param mixed $language_code
145 * @param array $args an array with two keys element_id => post_id or term_taxonomy_id, element_type => post type or taxonomy
146 * @return string
147 */
148 public function wpml_element_language_code( $language_code, $args ) {
149 $type = $args['element_type'];
150 $id = $args['element_id'];
151 $pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
152 if ( 'term' === $pll_type && $term = wpcom_vip_get_term_by( 'term_taxonomy_id', $id ) ) {
153 $id = $term->term_id;
154 }
155 return $pll_type ? call_user_func( "pll_get_{$pll_type}_language", $id ) : $language_code;
156 }
157
158 /**
159 * Translates a string
160 *
161 * @since 2.0
162 *
163 * @param string $string the string's original value
164 * @param string $context the string's registered context
165 * @param string $name the string's registered name
166 * @param null|string $lang optional, return the translation in this language, defaults to current language
167 * @return string the translated string
168 */
169 public function wpml_translate_single_string( $string, $context, $name, $lang = null ) {
170 $has_translation = null; // Passed by reference
171 return icl_translate( $context, $name, $string, false, $has_translation, $lang );
172 }
173
174 /**
175 * Find out whether a post type or a taxonomy term is translated
176 *
177 * @since 2.0
178 *
179 * @param mixed $null
180 * @param int $id post_id or term_id
181 * @param string $type post type or taxonomy
182 * @return bool
183 */
184 public function wpml_element_has_translations( $null, $id, $type ) {
185 $pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false );
186 return ( $pll_type && $translations = call_user_func( "pll_get_{$pll_type}_translations", $id ) ) ? count( $translations ) > 1 : false;
187 }
188 }
189