PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/polylang/include/language.php

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