PLL_Frontend_Links

Manages links filters and url of translations on frontend.

Defined (1)

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

/frontend/frontend-links.php  
  1. class PLL_Frontend_Links extends PLL_Links { 
  2. public $curlang; 
  3. public $cache; // our internal non persistent cache object 
  4.  
  5. /** 
  6. * constructor 
  7. * @since 1.2 
  8. * @param object $polylang 
  9. */ 
  10. public function __construct( &$polylang ) { 
  11. parent::__construct( $polylang ); 
  12.  
  13. $this->curlang = &$polylang->curlang; 
  14. $this->cache = new PLL_Cache(); 
  15.  
  16.  
  17. /** 
  18. * returns the url of the translation ( if exists ) of the current page 
  19. * @since 0.1 
  20. * @param object $language 
  21. * @return string 
  22. */ 
  23. public function get_translation_url( $language ) { 
  24. global $wp_query; 
  25.  
  26. if ( false !== $translation_url = $this->cache->get( 'translation_url:' . $language->slug ) ) { 
  27. return $translation_url; 
  28.  
  29. // make sure that we have the queried object 
  30. // see https://wordpress.org/support/topic/patch-for-fixing-a-notice 
  31. $queried_object_id = $wp_query->get_queried_object_id(); 
  32.  
  33. /** 
  34. * Filter the translation url before Polylang attempts to find one 
  35. * Internally used by Polylang for the static front page and posts page 
  36. * @since 1.8 
  37. * @param string $url empty or the url of the translation of teh current page 
  38. * @param object $language language of the translation 
  39. * @param int $queried_object_id queried object id 
  40. */ 
  41. if ( ! $url = apply_filters( 'pll_pre_translation_url', '', $language, $queried_object_id ) ) { 
  42. $qv = $wp_query->query_vars; 
  43. $hide = $this->options['default_lang'] == $language->slug && $this->options['hide_default']; 
  44.  
  45. // post and attachment 
  46. if ( is_single() && ( $this->options['media_support'] || ! is_attachment() ) && ( $id = $this->model->post->get( $queried_object_id, $language ) ) && $this->current_user_can_read( $id ) ) { 
  47. $url = get_permalink( $id ); 
  48.  
  49. // page 
  50. elseif ( is_page() && ( $id = $this->model->post->get( $queried_object_id, $language ) ) && $this->current_user_can_read( $id ) ) { 
  51. $url = get_page_link( $id ); 
  52.  
  53. elseif ( is_search() ) { 
  54. $url = $this->get_archive_url( $language ); 
  55.  
  56. // special case for search filtered by translated taxonomies: taxonomy terms are translated in the translation url 
  57. if ( ! empty( $wp_query->tax_query->queries ) ) { 
  58. foreach ( $wp_query->tax_query->queries as $tax_query ) { 
  59. if ( ! empty( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) ) { 
  60.  
  61. $tax = get_taxonomy( $tax_query['taxonomy'] ); 
  62. $terms = get_terms( $tax->name, array( 'fields' => 'id=>slug' ) ); // filtered by current language 
  63.  
  64. foreach ( $tax_query['terms'] as $slug ) { 
  65. $term_id = array_search( $slug, $terms ); // what is the term_id corresponding to taxonomy term? 
  66. if ( $term_id && $term_id = $this->model->term->get_translation( $term_id, $language ) ) { // get the translated term_id 
  67. $term = get_term( $term_id, $tax->name ); 
  68. $url = str_replace( $slug, $term->slug, $url ); 
  69.  
  70. // translated taxonomy 
  71. // take care that is_tax() is false for categories and tags 
  72. elseif ( ( is_category() || is_tag() || is_tax() ) && ( $term = get_queried_object() ) && $this->model->is_translated_taxonomy( $term->taxonomy ) ) { 
  73. $lang = $this->model->term->get_language( $term->term_id ); 
  74.  
  75. if ( ! $lang || $language->slug == $lang->slug ) { 
  76. $url = wpcom_vip_get_term_link( $term, $term->taxonomy ); // self link 
  77.  
  78. elseif ( $tr_id = $this->model->term->get_translation( $term->term_id, $language ) ) { 
  79. $tr_term = get_term( $tr_id, $term->taxonomy ); 
  80. // check if translated term ( or children ) have posts 
  81. if ( $tr_term && ( $tr_term->count || ( is_taxonomy_hierarchical( $term->taxonomy ) && array_sum( wp_list_pluck( get_terms( $term->taxonomy, array( 'child_of' => $tr_term->term_id, 'lang' => $language->slug ) ), 'count' ) ) ) ) ) { 
  82. $url = wpcom_vip_get_term_link( $tr_term, $term->taxonomy ); 
  83.  
  84. // post type archive 
  85. elseif ( is_post_type_archive() ) { 
  86. if ( $this->model->is_translated_post_type( $qv['post_type'] ) && $this->model->count_posts( $language, array( 'post_type' => $qv['post_type'] ) ) ) { 
  87. $url = $this->get_archive_url( $language ); 
  88.  
  89. elseif ( is_archive() ) { 
  90. $keys = array( 'post_type', 'm', 'year', 'monthnum', 'day', 'author', 'author_name' ); 
  91. $keys = array_merge( $keys, $this->model->get_filtered_taxonomies_query_vars() ); 
  92.  
  93. // check if there are existing translations before creating the url 
  94. if ( $this->model->count_posts( $language, array_intersect_key( $qv, array_flip( $keys ) ) ) ) { 
  95. $url = $this->get_archive_url( $language ); 
  96.  
  97. // front page when it is the list of posts 
  98. elseif ( is_front_page() ) { 
  99. $url = $this->get_home_url( $language ); 
  100.  
  101. /** 
  102. * Filter the translation url of the current page before Polylang caches it 
  103. * @since 1.1.2 
  104. * @param null|string $url the translation url, null if none was found 
  105. * @param string $language the language code of the translation 
  106. */ 
  107. $translation_url = apply_filters( 'pll_translation_url', ( isset( $url ) && ! is_wp_error( $url ) ? $url : null ), $language->slug ); 
  108. $this->cache->set( 'translation_url:' . $language->slug, $translation_url ); 
  109. return $translation_url; 
  110.  
  111. /** 
  112. * get the translation of the current archive url 
  113. * used also for search 
  114. * @since 1.2 
  115. * @param object $language 
  116. * @return string 
  117. */ 
  118. public function get_archive_url( $language ) { 
  119. $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
  120. $url = $this->links_model->switch_language_in_link( $url, $language ); 
  121. $url = $this->links_model->remove_paged_from_link( $url ); 
  122.  
  123. /** 
  124. * Filter the archive url 
  125. * @since 1.6 
  126. * @param string $url url of the archive 
  127. * @param object $language language of the archive 
  128. */ 
  129. return apply_filters( 'pll_get_archive_url', $url, $language ); 
  130.  
  131. /** 
  132. * returns the home url in the right language 
  133. * @since 0.1 
  134. * @param object $language optional defaults to current language 
  135. * @param bool $is_search optional wether we need the home url for a search form, defaults to false 
  136. */ 
  137. public function get_home_url( $language = '', $is_search = false ) { 
  138. if ( empty( $language ) ) { 
  139. $language = $this->curlang; 
  140.  
  141. return parent::get_home_url( $language, $is_search );