PLL_Language

A language object is made of two terms in 'language' and 'term_language' taxonomies manipulating only one object per language instead of two terms should make things easier.

Defined (1)

The class is defined in the following location(s).

/include/language.php  
  1. class PLL_Language { 
  2. public $term_id, $name, $slug, $term_group, $term_taxonomy_id, $taxonomy, $description, $parent, $count; 
  3. public $tl_term_id, $tl_term_taxonomy_id, $tl_count; 
  4. public $locale, $is_rtl; 
  5. public $flag_url, $flag; 
  6. public $home_url, $search_url; 
  7. public $host, $mo_id; 
  8. public $page_on_front, $page_for_posts; 
  9.  
  10. /** 
  11. * constructor: builds a language object given its two corresponding terms in language and term_language taxonomies 
  12. * @since 1.2 
  13. * @param object|array $language 'language' term or language object properties stored as an array 
  14. * @param object $term_language corresponding 'term_language' term 
  15. */ 
  16. public function __construct( $language, $term_language = null ) { 
  17. // build the object from all properties stored as an array 
  18. if ( empty( $term_language ) ) { 
  19. foreach ( $language as $prop => $value ) { 
  20. $this->$prop = $value; 
  21.  
  22. // build the object from taxonomies 
  23. else { 
  24. foreach ( $language as $prop => $value ) { 
  25. $this->$prop = in_array( $prop, array( 'term_id', 'term_taxonomy_id', 'count' ) ) ? (int) $language->$prop : $language->$prop; 
  26.  
  27. // although it would be convenient here, don't assume the term is shared between taxonomies as it may not be the case in future 
  28. // http://make.wordpress.org/core/2013/07/28/potential-roadmap-for-taxonomy-meta-and-post-relationships/ 
  29. $this->tl_term_id = (int) $term_language->term_id; 
  30. $this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id; 
  31. $this->tl_count = (int) $term_language->count; 
  32.  
  33. // the description field can contain any property 
  34. // backward compatibility for is_rtl 
  35. $description = maybe_unserialize( $language->description ); 
  36. foreach ( $description as $prop => $value ) { 
  37. 'rtl' == $prop ? $this->is_rtl = $value : $this->$prop = $value; 
  38.  
  39. $this->description = &$this->locale; // backward compatibility with Polylang < 1.2 
  40.  
  41. $this->mo_id = PLL_MO::get_id( $this ); 
  42.  
  43. /** 
  44. * sets flag_url and flag properties 
  45. * @since 1.2 
  46. */ 
  47. public function set_flag() { 
  48. $flags['flag']['url'] = ''; 
  49.  
  50. // Polylang builtin flags 
  51. if ( ! empty( $this->flag_code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $this->flag_code . '.png' ) ) ) { 
  52. $flags['flag']['url'] = esc_url_raw( plugins_url( $file, POLYLANG_FILE ) ); 
  53.  
  54. // if base64 encoded flags are preferred 
  55. if ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) { 
  56. $flags['flag']['src'] = 'data:image/png;base64, ' . base64_encode( file_get_contents( POLYLANG_DIR . $file ) ); 
  57. } else { 
  58. $flags['flag']['src'] = esc_url( plugins_url( $file, POLYLANG_FILE ) ); 
  59.  
  60. // custom flags ? 
  61. if ( file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.png' ) ) || file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.jpg' ) ) ) { 
  62. $url = content_url( '/polylang' . $file ); 
  63. $flags['custom_flag']['url'] = esc_url_raw( $url ); 
  64. $flags['custom_flag']['src'] = esc_url( $url ); 
  65.  
  66. /** 
  67. * Filter the flag title attribute 
  68. * Defaults to the language name 
  69. * @since 0.7 
  70. * @param string $title the flag title attribute 
  71. * @param string $slug the language code 
  72. * @param string $locale the language locale 
  73. */ 
  74. $title = apply_filters( 'pll_flag_title', $this->name, $this->slug, $this->locale ); 
  75.  
  76. foreach ( $flags as $key => $flag ) { 
  77. $this->{$key . '_url'} = empty( $flag['url'] ) ? '' : $flag['url']; 
  78.  
  79. /** 
  80. * Filter the html markup of a flag 
  81. * @since 1.0.2 
  82. * @param string $flag html markup of the flag or empty string 
  83. * @param string $slug language code 
  84. */ 
  85. $this->{$key} = apply_filters( 'pll_get_flag', empty( $flag['src'] ) ? '' : 
  86. sprintf( 
  87. '<img src="%s" title="%s" alt="%s" />',  
  88. $flag['src'],  
  89. esc_attr( $title ),  
  90. esc_attr( $this->name ) 
  91. ),  
  92. $this->slug 
  93. ); 
  94.  
  95. /** 
  96. * replace flag by custom flag 
  97. * takes care of url scheme 
  98. * @since 1.7 
  99. */ 
  100. public function set_custom_flag() { 
  101. // overwrite with custom flags on frontend only 
  102. if ( ! empty( $this->custom_flag ) ) { 
  103. $this->flag = $this->custom_flag; 
  104. $this->flag_url = $this->custom_flag_url; 
  105. unset( $this->custom_flag, $this->custom_flag_url ); // hide this 
  106.  
  107. // set url scheme, also for default flags 
  108. if ( is_ssl() ) { 
  109. $this->flag = str_replace( 'http://', 'https://', $this->flag ); 
  110. $this->flag_url = str_replace( 'http://', 'https://', $this->flag_url ); 
  111. } else { 
  112. $this->flag = str_replace( 'https://', 'http://', $this->flag ); 
  113. $this->flag_url = str_replace( 'https://', 'http://', $this->flag_url ); 
  114.  
  115. /** 
  116. * updates post and term count 
  117. * @since 1.2 
  118. */ 
  119. public function update_count() { 
  120. wp_update_term_count( $this->term_taxonomy_id, 'language' ); // posts count 
  121. wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // terms count 
  122.  
  123. /** 
  124. * set home_url and search_url properties 
  125. * @since 1.3 
  126. * @param string $search_url 
  127. * @param string $home_url 
  128. */ 
  129. public function set_home_url( $search_url, $home_url ) { 
  130. $this->search_url = $search_url; 
  131. $this->home_url = $home_url; 
  132.  
  133. /** 
  134. * set home_url scheme 
  135. * this can't be cached accross pages 
  136. * @since 1.6.4 
  137. */ 
  138. public function set_home_url_scheme() { 
  139. if ( is_ssl() ) { 
  140. $this->home_url = str_replace( 'http://', 'https://', $this->home_url ); 
  141. $this->search_url = str_replace( 'http://', 'https://', $this->search_url ); 
  142.  
  143. else { 
  144. $this->home_url = str_replace( 'https://', 'http://', $this->home_url ); 
  145. $this->search_url = str_replace( 'https://', 'http://', $this->search_url ); 
  146.  
  147. /** 
  148. * returns the language locale 
  149. * converts WP locales to W3C valid locales for display 
  150. * @see #33511 
  151. * @since 1.8 
  152. * @param string $filter either 'display' or 'raw', defaults to raw 
  153. * @return string 
  154. */ 
  155. public function get_locale( $filter = 'raw' ) { 
  156. if ( 'display' == $filter ) { 
  157. static $valid_locales = array( 
  158. 'bel' => 'be',  
  159. 'bre' => 'br',  
  160. 'de_CH_informal' => 'de_CH',  
  161. 'de_DE_formal' => 'de_DE',  
  162. 'dzo' => 'dz',  
  163. 'ido' => 'io',  
  164. 'kin' => 'rw',  
  165. 'oci' => 'oc',  
  166. 'mri' => 'mi',  
  167. 'nl_NL_formal' => 'nl_NL',  
  168. 'roh' => 'rm',  
  169. 'srd' => 'sc',  
  170. 'tuk' => 'tk',  
  171. ); 
  172. $locale = isset( $valid_locales[ $this->locale ] ) ? $valid_locales[ $this->locale ] : $this->locale; 
  173. return str_replace( '_', '-', $locale ); 
  174. return $this->locale;