PLL_Choose_Lang_Content

Choose the language when it is set from content The language is set either in parse_query with priority 2 or in wp with priority 5.

Defined (1)

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

/frontend/choose-lang-content.php  
  1. class PLL_Choose_Lang_Content extends PLL_Choose_lang { 
  2.  
  3. /** 
  4. * defers the language choice to the 'wp' action (when the content is known) 
  5. * @since 1.8 
  6. */ 
  7. public function init() { 
  8. parent::init(); 
  9.  
  10. if ( ! did_action( 'pll_language_defined' ) ) { 
  11. // set the languages from content 
  12. add_action( 'wp', array( $this, 'wp' ), 5 ); // priority 5 for post types and taxonomies registered in wp hook with default priority 
  13.  
  14. // if no language found, choose the preferred one 
  15. add_filter( 'pll_get_current_language', array( $this, 'pll_get_current_language' ) ); 
  16.  
  17. /** 
  18. * overwrites parent::set_language to remove the 'wp' action if the language is set before 
  19. * @since 1.2 
  20. * @param object $curlang current language 
  21. */ 
  22. protected function set_language( $curlang ) { 
  23. parent::set_language( $curlang ); 
  24. remove_action( 'wp', array( $this, 'wp' ), 5 ); // won't attempt to set the language a 2nd time 
  25.  
  26. /** 
  27. * returns the language based on the queried content 
  28. * @since 1.2 
  29. * @return object|bool detected language, false if none was found 
  30. */ 
  31. protected function get_language_from_content() { 
  32. // no language set for 404 
  33. if ( is_404() || ( is_attachment() && ! $this->options['media_support'] ) ) { 
  34. return $this->get_preferred_language(); 
  35.  
  36. if ( $var = get_query_var( 'lang' ) ) { 
  37. $lang = explode( ', ', $var ); 
  38. $lang = $this->model->get_language( reset( $lang ) ); // choose the first queried language 
  39.  
  40. elseif ( ( is_single() || is_page() || ( is_attachment() && $this->options['media_support'] ) ) && ( ( $var = get_queried_object_id() ) || ( $var = get_query_var( 'p' ) ) || ( $var = get_query_var( 'page_id' ) ) || ( $var = get_query_var( 'attachment_id' ) ) ) ) { 
  41. $lang = $this->model->post->get_language( $var ); 
  42.  
  43. else { 
  44. foreach ( $this->model->get_translated_taxonomies() as $taxonomy ) { 
  45. if ( $var = get_query_var( get_taxonomy( $taxonomy )->query_var ) ) { 
  46. $lang = $this->model->term->get_language( $var, $taxonomy ); 
  47.  
  48. /** 
  49. * Filter the language before it is set from the content 
  50. * @since 0.9 
  51. * @param bool|object $lang language object or false if none was found 
  52. */ 
  53. return apply_filters( 'pll_get_current_language', isset( $lang ) ? $lang : false ); 
  54.  
  55. /** 
  56. * sets the language for home page 
  57. * add the lang query var when querying archives with no language code 
  58. * @since 1.2 
  59. * @param object $query instance of WP_Query 
  60. */ 
  61. public function parse_main_query( $query ) { 
  62. if ( $query !== $GLOBALS['wp_the_query'] ) { 
  63. return; 
  64.  
  65. $qv = $query->query_vars; 
  66.  
  67. // homepage is requested, let's set the language 
  68. // take care to avoid posts page for which is_home = 1 
  69. if ( empty( $query->query ) && ( is_home() || is_page() ) ) { 
  70. $this->home_language(); 
  71. $this->home_requested(); 
  72.  
  73. parent::parse_main_query( $query ); 
  74.  
  75. $is_archive = ( count( $query->query ) == 1 && ! empty( $qv['paged'] ) ) || 
  76. $query->is_date || 
  77. $query->is_author || 
  78. ( ! empty( $qv['post_type'] ) && $query->is_post_type_archive && $this->model->is_translated_post_type( $qv['post_type'] ) ); 
  79.  
  80. // sets the language in case we hide the default language 
  81. // use $query->query['s'] as is_search is not set when search is empty 
  82. // http://wordpress.org/support/topic/search-for-empty-string-in-default-language 
  83. if ( $this->options['hide_default'] && ! isset( $qv['lang'] ) && ( $is_archive || isset( $query->query['s'] ) || ( count( $query->query ) == 1 && ! empty( $qv['feed'] ) ) ) ) { 
  84. $this->set_language( $this->model->get_language( $this->options['default_lang'] ) ); 
  85. $this->set_lang_query_var( $query, $this->curlang ); 
  86.  
  87. /** 
  88. * sets the language from content 
  89. * @since 1.2 
  90. */ 
  91. public function wp() { 
  92. // nothing to do if the language has already been set ( although normally the filter has been removed ) 
  93. if ( ! $this->curlang && $curlang = $this->get_language_from_content() ) { 
  94. parent::set_language( $curlang ); 
  95.  
  96. /** 
  97. * if no language found by get_language_from_content, return the preferred one 
  98. * @since 0.9 
  99. * @param object|bool language found in get_language_from_content 
  100. * @return object language 
  101. */ 
  102. public function pll_get_current_language( $lang ) { 
  103. return ! $lang ? $this->get_preferred_language() : $lang;