PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/mostafame/team_website
PHP | 344 lines | 103 code | 33 blank | 208 comment | 12 complexity | acf248c07d38cddc8142332fd592aaf4 MD5 | raw file
  1. <?php
  2. /**
  3. * Template tag: displays the language switcher
  4. *
  5. * List of parameters accepted in $args:
  6. *
  7. * dropdown => displays a dropdown if set to 1, defaults to 0
  8. * echo => echoes the the switcher if set to 1 ( default )
  9. * hide_if_empty => hides languages with no posts ( or pages ) if set to 1 ( default )
  10. * show_flags => shows flags if set to 1, defaults to 0
  11. * show_names => shows languages names if set to 1 ( default )
  12. * display_names_as => whether to display the language name or code. valid options are 'slug' and 'name'
  13. * force_home => forces linking to the home page is set to 1, defaults to 0
  14. * hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0
  15. * hide_current => hides the current language if set to 1, defaults to 0
  16. * post_id => if not null, link to translations of post defined by post_id, defaults to null
  17. * raw => set this to true to build your own custom language switcher, defaults to 0
  18. *
  19. * @since 0.5
  20. *
  21. * @param array $args optional
  22. * @return null|string|array null if displaying, array if raw is requested, string otherwise
  23. */
  24. function pll_the_languages( $args = '' ) {
  25. if ( PLL_ADMIN ) {
  26. return '';
  27. }
  28. $switcher = new PLL_Switcher;
  29. return $switcher->the_languages( PLL()->links, $args );
  30. }
  31. /**
  32. * Returns the current language on frontend
  33. * Returns the language set in admin language filter on backend ( false if set to all languages )
  34. *
  35. * @since 0.8.1
  36. *
  37. * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
  38. * @return string|bool the requested field for the current language
  39. */
  40. function pll_current_language( $field = 'slug' ) {
  41. return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false;
  42. }
  43. /**
  44. * Returns the default language
  45. *
  46. * @since 1.0
  47. *
  48. * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
  49. * @return string the requested field for the default language
  50. */
  51. function pll_default_language( $field = 'slug' ) {
  52. return isset( PLL()->options['default_lang'] ) && ( $lang = PLL()->model->get_language( PLL()->options['default_lang'] ) ) && isset( $lang->$field ) ? $lang->$field : false;
  53. }
  54. /**
  55. * Among the post and its translations, returns the id of the post which is in the language represented by $slug
  56. *
  57. * @since 0.5
  58. *
  59. * @param int $post_id post id
  60. * @param string $slug optional language code, defaults to current language
  61. * @return int|null post id of the translation if exists, null otherwise
  62. */
  63. function pll_get_post( $post_id, $slug = '' ) {
  64. return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->post->get( $post_id, $slug ) : null;
  65. }
  66. /**
  67. * Among the term and its translations, returns the id of the term which is in the language represented by $slug
  68. *
  69. * @since 0.5
  70. *
  71. * @param int $term_id term id
  72. * @param string $slug optional language code, defaults to current language
  73. * @return int|null term id of the translation if exists, null otherwise
  74. */
  75. function pll_get_term( $term_id, $slug = '' ) {
  76. return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->term->get( $term_id, $slug ) : null;
  77. }
  78. /**
  79. * Returns the home url in the current language
  80. *
  81. * @since 0.8
  82. *
  83. * @param string $lang language code ( optional on frontend )
  84. * @return string
  85. */
  86. function pll_home_url( $lang = '' ) {
  87. if ( empty( $lang ) ) {
  88. $lang = pll_current_language();
  89. }
  90. return empty( $lang ) ? home_url( '/' ) : PLL()->links->get_home_url( $lang );
  91. }
  92. /**
  93. * Registers a string for translation in the "strings translation" panel
  94. *
  95. * @since 0.6
  96. *
  97. * @param string $name a unique name for the string
  98. * @param string $string the string to register
  99. * @param string $context optional the group in which the string is registered, defaults to 'polylang'
  100. * @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line
  101. */
  102. function pll_register_string( $name, $string, $context = 'polylang', $multiline = false ) {
  103. if ( PLL_ADMIN ) {
  104. PLL_Admin_Strings::register_string( $name, $string, $context, $multiline );
  105. }
  106. }
  107. /**
  108. * Translates a string ( previously registered with pll_register_string )
  109. *
  110. * @since 0.6
  111. *
  112. * @param string $string the string to translate
  113. * @return string the string translation in the current language
  114. */
  115. function pll__( $string ) {
  116. static $cache; // Cache object to avoid translating the same string several times
  117. if ( ! did_action( 'pll_language_defined' ) ) { // No need for translation
  118. return $string;
  119. }
  120. if ( empty( $cache ) ) {
  121. $cache = new PLL_Cache();
  122. }
  123. if ( false === $str = $cache->get( $string ) ) {
  124. $str = __( $string, 'pll_string' );
  125. $cache->set( $string, $str );
  126. }
  127. return $str;
  128. }
  129. /**
  130. * Echoes a translated string ( previously registered with pll_register_string )
  131. *
  132. * @since 0.6
  133. *
  134. * @param string $string the string to translate
  135. */
  136. function pll_e( $string ) {
  137. echo pll__( $string );
  138. }
  139. /**
  140. * Translates a string ( previously registered with pll_register_string )
  141. *
  142. * @since 1.5.4
  143. *
  144. * @param string $string the string to translate
  145. * @param string $lang language code
  146. * @return string the string translation in the requested language
  147. */
  148. function pll_translate_string( $string, $lang ) {
  149. if ( pll_current_language() == $lang ) {
  150. return pll__( $string );
  151. }
  152. static $cache; // Cache object to avoid loading the same translations object several times
  153. if ( empty( $cache ) ) {
  154. $cache = new PLL_Cache();
  155. }
  156. if ( false === $mo = $cache->get( $lang ) ) {
  157. $mo = new PLL_MO();
  158. $mo->import_from_db( PLL()->model->get_language( $lang ) );
  159. $cache->set( $lang, $mo );
  160. }
  161. return $mo->translate( $string );
  162. }
  163. /**
  164. * Returns true if Polylang manages languages and translations for this post type
  165. *
  166. * @since 1.0.1
  167. *
  168. * @param string post type name
  169. * @return bool
  170. */
  171. function pll_is_translated_post_type( $post_type ) {
  172. return PLL()->model->is_translated_post_type( $post_type );
  173. }
  174. /**
  175. * Returns true if Polylang manages languages and translations for this taxonomy
  176. *
  177. * @since 1.0.1
  178. *
  179. * @param string taxonomy name
  180. * @return bool
  181. */
  182. function pll_is_translated_taxonomy( $tax ) {
  183. return PLL()->model->is_translated_taxonomy( $tax );
  184. }
  185. /**
  186. * Returns the list of available languages
  187. *
  188. * List of parameters accepted in $args:
  189. *
  190. * hide_empty => hides languages with no posts if set to true ( defaults to false )
  191. * fields => return only that field if set ( see PLL_Language for a list of fields )
  192. *
  193. * @since 1.5
  194. *
  195. * @param array $args list of parameters
  196. * @return array
  197. */
  198. function pll_languages_list( $args = array() ) {
  199. $args = wp_parse_args( $args, array( 'fields' => 'slug' ) );
  200. return PLL()->model->get_languages_list( $args );
  201. }
  202. /**
  203. * Set the post language
  204. *
  205. * @since 1.5
  206. *
  207. * @param int $id post id
  208. * @param string $lang language code
  209. */
  210. function pll_set_post_language( $id, $lang ) {
  211. PLL()->model->post->set_language( $id, $lang );
  212. }
  213. /**
  214. * Set the term language
  215. *
  216. * @since 1.5
  217. *
  218. * @param int $id term id
  219. * @param string $lang language code
  220. */
  221. function pll_set_term_language( $id, $lang ) {
  222. PLL()->model->term->set_language( $id, $lang );
  223. }
  224. /**
  225. * Save posts translations
  226. *
  227. * @since 1.5
  228. *
  229. * @param array $arr an associative array of translations with language code as key and post id as value
  230. */
  231. function pll_save_post_translations( $arr ) {
  232. PLL()->model->post->save_translations( reset( $arr ), $arr );
  233. }
  234. /**
  235. * Save terms translations
  236. *
  237. * @since 1.5
  238. *
  239. * @param array $arr an associative array of translations with language code as key and term id as value
  240. */
  241. function pll_save_term_translations( $arr ) {
  242. PLL()->model->term->save_translations( reset( $arr ), $arr );
  243. }
  244. /**
  245. * Returns the post language
  246. *
  247. * @since 1.5.4
  248. *
  249. * @param int $post_id
  250. * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
  251. * @return bool|string the requested field for the post language, false if no language is associated to that post
  252. */
  253. function pll_get_post_language( $post_id, $field = 'slug' ) {
  254. return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
  255. }
  256. /**
  257. * Returns the term language
  258. *
  259. * @since 1.5.4
  260. *
  261. * @param int $term_id
  262. * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
  263. * @return bool|string the requested field for the term language, false if no language is associated to that term
  264. */
  265. function pll_get_term_language( $term_id, $field = 'slug' ) {
  266. return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
  267. }
  268. /**
  269. * Returns an array of translations of a post
  270. *
  271. * @since 1.8
  272. *
  273. * @param int $post_id
  274. * @return array an associative array of translations with language code as key and translation post_id as value
  275. */
  276. function pll_get_post_translations( $post_id ) {
  277. return PLL()->model->post->get_translations( $post_id );
  278. }
  279. /**
  280. * Returns an array of translations of a term
  281. *
  282. * @since 1.8
  283. *
  284. * @param int $term_id
  285. * @return array an associative array of translations with language code as key and translation term_id as value
  286. */
  287. function pll_get_term_translations( $term_id ) {
  288. return PLL()->model->term->get_translations( $term_id );
  289. }
  290. /**
  291. * Count posts in a language
  292. *
  293. * @since 1.5
  294. *
  295. * @param string $lang language code
  296. * @param array $args ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format )
  297. * @return int posts count
  298. */
  299. function pll_count_posts( $lang, $args = array() ) {
  300. return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
  301. }
  302. /**
  303. * Allows to access the Polylang instance
  304. * It is always preferable to use API functions
  305. * Internal methods may be changed without prior notice
  306. *
  307. * @since 1.8
  308. */
  309. function PLL() {
  310. return $GLOBALS['polylang'];
  311. }