PLL_Frontend

Frontend controller accessible as $polylang global object.

Defined (1)

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

/frontend/frontend.php  
  1. class PLL_Frontend extends PLL_Base { 
  2. public $curlang; 
  3. public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate; 
  4.  
  5. /** 
  6. * constructor 
  7. * @since 1.2 
  8. * @param object $links_model 
  9. */ 
  10. public function __construct( &$links_model ) { 
  11. parent::__construct( $links_model ); 
  12.  
  13. add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 ); 
  14.  
  15. // avoids the language being the queried object when querying multiple taxonomies 
  16. add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 ); 
  17.  
  18. // filters posts by language 
  19. add_action( 'parse_query', array( $this, 'parse_query' ), 6 ); 
  20.  
  21. // not before 'check_canonical_url' 
  22. if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) { 
  23. add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 ); 
  24.  
  25. /** 
  26. * setups the language chooser based on options 
  27. * @since 1.2 
  28. */ 
  29. public function init() { 
  30. $this->links = new PLL_Frontend_Links( $this ); 
  31. $this->static_pages = new PLL_Frontend_Static_Pages( $this ); 
  32.  
  33. // setup the language chooser 
  34. $c = array( 'Content', 'Url', 'Url', 'Domain' ); 
  35. $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; 
  36. $this->choose_lang = new $class( $this ); 
  37. $this->choose_lang->init(); 
  38.  
  39. // need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content 
  40. $this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); 
  41.  
  42. /** 
  43. * setups filters and nav menus once the language has been defined 
  44. * @since 1.2 
  45. */ 
  46. public function pll_language_defined() { 
  47. // filters 
  48. $this->filters_links = new PLL_Frontend_Filters_Links( $this ); 
  49. $this->filters = new PLL_Frontend_Filters( $this ); 
  50. $this->filters_search = new PLL_Frontend_Filters_Search( $this ); 
  51.  
  52.  
  53. /** 
  54. * when querying multiple taxonomies, makes sure that the language is not the queried object 
  55. * @since 1.8 
  56. * @param object $query WP_Query object 
  57. */ 
  58. public function parse_tax_query( $query ) { 
  59. $queried_taxonomies = $this->get_queried_taxonomies( $query ); 
  60.  
  61. if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) { 
  62. $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms ); 
  63.  
  64. /** 
  65. * modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts 
  66. * @since 1.2 
  67. * @param object $query WP_Query object 
  68. */ 
  69. public function parse_query( $query ) { 
  70. $qv = $query->query_vars; 
  71. $taxonomies = $this->get_queried_taxonomies( $query ); 
  72.  
  73. // to avoid returning an empty result if the query includes a translated taxonomy in a different language 
  74. $has_tax = isset( $query->tax_query->queries ) && $this->model->have_translated_taxonomy( $query->tax_query->queries ); 
  75.  
  76. // allow filtering recent posts and secondary queries by the current language 
  77. // take care not to break queries for non visible post types such as nav_menu_items 
  78. // do not filter if lang is set to an empty value 
  79. // do not filter single page and translated taxonomies to avoid conflicts 
  80. if ( ! empty( $this->curlang ) && ! isset( $qv['lang'] ) && ! $has_tax && empty( $qv['page_id'] ) && empty( $qv['pagename'] ) ) { 
  81. if ( $taxonomies && ( empty( $qv['post_type'] ) || 'any' === $qv['post_type'] ) ) { 
  82. foreach ( $taxonomies as $taxonomy ) { 
  83. $tax_object = get_taxonomy( $taxonomy ); 
  84. if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) { 
  85. $this->choose_lang->set_lang_query_var( $query, $this->curlang ); 
  86. break; 
  87. } elseif ( empty( $qv['post_type'] ) || $this->model->is_translated_post_type( $qv['post_type'] ) ) { 
  88. $this->choose_lang->set_lang_query_var( $query, $this->curlang ); 
  89.  
  90. // modifies query vars when the language is queried 
  91. if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language') == array_values( $taxonomies ) ) ) { 
  92. // do we query a custom taxonomy? 
  93. $taxonomies = array_diff( $taxonomies , array( 'language', 'category', 'post_tag' ) ); 
  94.  
  95. // remove pages query when the language is set unless we do a search 
  96. // take care not to break the single page, attachment and taxonomies queries! 
  97. if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_page && ! $query->is_attachment && empty( $taxonomies ) ) { 
  98. $query->set( 'post_type', 'post' ); 
  99.  
  100. // unset the is_archive flag for language pages to prevent loading the archive template 
  101. // keep archive flag for comment feed otherwise the language filter does not work 
  102. if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) { 
  103. $query->is_archive = false; 
  104.  
  105. // unset the is_tax flag except if another custom tax is queried 
  106. if ( empty( $taxonomies ) && ($query->is_category || $query->is_tag || $query->is_author || $query->is_post_type_archive || $query->is_date || $query->is_search || $query->is_feed ) ) { 
  107. $query->is_tax = false; 
  108. unset( $query->queried_object ); // FIXME useless? 
  109.  
  110. /** 
  111. * auto translate posts and terms ids 
  112. * @since 1.2 
  113. */ 
  114. public function auto_translate() { 
  115. $this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); 
  116.  
  117. /** 
  118. * resets some variables when switching blog 
  119. * overrides parent method 
  120. * @since 1.5.1 
  121. */ 
  122. public function switch_blog( $new_blog, $old_blog ) { 
  123. // need to check that some languages are defined when user is logged in, has several blogs, some without any languages 
  124. if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { 
  125. static $restore_curlang; 
  126. if ( empty( $restore_curlang ) ) { 
  127. $restore_curlang = $this->curlang->slug; // to always remember the current language through blogs 
  128.  
  129. $lang = $this->model->get_language( $restore_curlang ); 
  130. $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); 
  131. $this->static_pages->init(); 
  132. $this->load_strings_translations(); 
  133.  
  134. /** 
  135. * get queried taxonomies 
  136. * @since 1.8 
  137. * @param object $query WP_Query object 
  138. * @return array queried taxonomies 
  139. */ 
  140. protected function get_queried_taxonomies( $query ) { 
  141. return isset( $query->tax_query->queried_terms ) ? array_keys( wp_list_filter( $query->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' ) ) : array();