| 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' ); |
| 24 |
add_filter( 'wpml_default_language', 'pll_default_language' ); |
| 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 => not implemented |
| 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' ); |
| 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, 2 ); |
| 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 |
printf( '<input type="hidden" name="lang" value="%s" />', esc_attr( pll_current_language() ) ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Find out if a specific language is enabled for the site |
| 112 |
* |
| 113 |
* @since 2.0 |
| 114 |
* |
| 115 |
* @param mixed $null not used |
| 116 |
* @param string $slug language code |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
public function wpml_language_is_active( $null, $slug ) { |
| 120 |
$language = PLL()->model->get_language( $slug ); |
| 121 |
return empty( $language->active ) || true === $language->active; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Find out whether the current language text direction is RTL or not |
| 126 |
* |
| 127 |
* @since 2.0 |
| 128 |
* |
| 129 |
* @param mixed $null not used |
| 130 |
* @return bool |
| 131 |
*/ |
| 132 |
public function wpml_is_rtl( $null ) { |
| 133 |
return pll_current_language( 'is_rtl' ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get the language code for a translatable element |
| 138 |
* |
| 139 |
* @since 2.0 |
| 140 |
* |
| 141 |
* @param mixed $language_code |
| 142 |
* @param array $args an array with two keys element_id => post_id or term_taxonomy_id, element_type => post type or taxonomy |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
public function wpml_element_language_code( $language_code, $args ) { |
| 146 |
$type = $args['element_type']; |
| 147 |
$id = $args['element_id']; |
| 148 |
$pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ); |
| 149 |
if ( 'term' === $pll_type && $term = wpcom_vip_get_term_by( 'term_taxonomy_id', $id ) ) { |
| 150 |
$id = $term->term_id; |
| 151 |
} |
| 152 |
return $pll_type ? call_user_func( "pll_get_$pll_type_language", $id ) : $language_code; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Translates a string |
| 157 |
* |
| 158 |
* @since 2.0 |
| 159 |
* |
| 160 |
* @param string $string the string's original value |
| 161 |
* @param string $context the string's registered context |
| 162 |
* @param string $name the string's registered name |
| 163 |
* @param null|string $lang optional, return the translation in this language, defaults to current language |
| 164 |
* @return string the translated string |
| 165 |
*/ |
| 166 |
public function wpml_translate_single_string( $string, $context, $name, $lang = null ) { |
| 167 |
$has_translation = null; // passed by reference |
| 168 |
return icl_translate( $context, $name, $original_value, false, $has_translation, $lang ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Find out whether a post type or a taxonomy term is translated |
| 173 |
* |
| 174 |
* @since 2.0 |
| 175 |
* |
| 176 |
* @param mixed $null |
| 177 |
* @param int $id post_id or term_id |
| 178 |
* @param string $type post type or taxonomy |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
public function wpml_element_has_translations( $null, $id, $type ) { |
| 182 |
$pll_type = ( 'post' == $type || pll_is_translated_post_type( $type ) ) ? 'post' : ( 'term' == $type || pll_is_translated_taxonomy( $type ) ? 'term' : false ); |
| 183 |
return ( $pll_type && $translations = call_user_func( "pll_get_$pll_type_translations", $id ) ) ? count( $translations ) > 1 : false; |
| 184 |
} |
| 185 |
} |
| 186 |
|