PluginProbe
Polylang / 2.7.3
Polylang v2.7.3
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / frontend / frontend-links.php

frontend-links.php in Polylang 2.7.3, at frontend/frontend-links.php

214 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages links filters and url of translations on frontend
5 *
6 * @since 1.2
7 */
8 class PLL_Frontend_Links extends PLL_Links {
9 public $curlang;
10 public $cache; // Our internal non persistent cache object
11
12 /**
13 * Constructor
14 *
15 * @since 1.2
16 *
17 * @param object $polylang
18 */
19 public function __construct( &$polylang ) {
20 parent::__construct( $polylang );
21
22 $this->curlang = &$polylang->curlang;
23 $this->cache = new PLL_Cache();
24
25 }
26
27 /**
28 * Returns the url of the translation ( if exists ) of the current page
29 *
30 * @since 0.1
31 *
32 * @param object $language
33 * @return string
34 */
35 public function get_translation_url( $language ) {
36 global $wp_query;
37
38 if ( false !== $translation_url = $this->cache->get( 'translation_url:' . $language->slug ) ) {
39 return $translation_url;
40 }
41
42 // Make sure that we have the queried object
43 // See https://wordpress.org/support/topic/patch-for-fixing-a-notice
44 $queried_object_id = $wp_query->get_queried_object_id();
45
46 /**
47 * Filter the translation url before Polylang attempts to find one
48 * Internally used by Polylang for the static front page and posts page
49 *
50 * @since 1.8
51 *
52 * @param string $url Empty or the url of the translation of teh current page
53 * @param object $language Language of the translation
54 * @param int $queried_object_id Queried object id
55 */
56 if ( ! $url = apply_filters( 'pll_pre_translation_url', '', $language, $queried_object_id ) ) {
57 $qv = $wp_query->query_vars;
58
59 // Post and attachment
60 if ( is_single() && ( $this->options['media_support'] || ! is_attachment() ) && ( $id = $this->model->post->get( $queried_object_id, $language ) ) && $this->model->post->current_user_can_read( $id ) ) {
61 $url = get_permalink( $id );
62 }
63
64 // Page
65 elseif ( is_page() && ( $id = $this->model->post->get( $queried_object_id, $language ) ) && $this->model->post->current_user_can_read( $id ) ) {
66 $url = get_page_link( $id );
67 }
68
69 elseif ( is_search() ) {
70 $url = $this->get_archive_url( $language );
71
72 // Special case for search filtered by translated taxonomies: taxonomy terms are translated in the translation url
73 if ( ! empty( $wp_query->tax_query->queries ) ) {
74 foreach ( $wp_query->tax_query->queries as $tax_query ) {
75 if ( ! empty( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) ) {
76
77 $tax = get_taxonomy( $tax_query['taxonomy'] );
78 $terms = get_terms( $tax->name, array( 'fields' => 'id=>slug' ) ); // Filtered by current language
79
80 foreach ( $tax_query['terms'] as $slug ) {
81 $term_id = array_search( $slug, $terms ); // What is the term_id corresponding to taxonomy term?
82 if ( $term_id && $term_id = $this->model->term->get_translation( $term_id, $language ) ) { // Get the translated term_id
83 $term = get_term( $term_id, $tax->name );
84 $url = str_replace( $slug, $term->slug, $url );
85 }
86 }
87 }
88 }
89 }
90 }
91
92 // Translated taxonomy
93 // Take care that is_tax() is false for categories and tags
94 elseif ( ( is_category() || is_tag() || is_tax() ) && ( $term = get_queried_object() ) && $this->model->is_translated_taxonomy( $term->taxonomy ) ) {
95 $lang = $this->model->term->get_language( $term->term_id );
96
97 if ( ! $lang || $language->slug == $lang->slug ) {
98 $url = get_term_link( $term, $term->taxonomy ); // Self link
99 }
100
101 elseif ( $tr_id = $this->model->term->get_translation( $term->term_id, $language ) ) {
102 if ( $tr_term = get_term( $tr_id, $term->taxonomy ) ) {
103 // Check if translated term ( or children ) have posts
104 $count = $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' ) ) );
105
106 /**
107 * Filter whether to hide an archive translation url
108 *
109 * @since 2.2.4
110 *
111 * @param bool $hide True to hide the translation url.
112 * defaults to true when the translated archive is empty, false otherwise.
113 * @param string $lang The language code of the translation
114 * @param array $args Arguments used to evaluated the number of posts in the archive
115 */
116 if ( ! apply_filters( 'pll_hide_archive_translation_url', ! $count, $language->slug, array( 'taxonomy' => $term->taxonomy ) ) ) {
117 $url = get_term_link( $tr_term, $term->taxonomy );
118 }
119 }
120 }
121 }
122
123 // Post type archive
124 elseif ( is_post_type_archive() ) {
125 if ( $this->model->is_translated_post_type( $qv['post_type'] ) ) {
126 $args = array( 'post_type' => $qv['post_type'] );
127 $count = $this->model->count_posts( $language, $args );
128
129 /** This filter is documented in frontend/frontend-links.php */
130 if ( ! apply_filters( 'pll_hide_archive_translation_url', ! $count, $language->slug, $args ) ) {
131 $url = $this->get_archive_url( $language );
132 }
133 }
134 }
135
136 // Other archives
137 elseif ( is_archive() ) {
138 $keys = array( 'post_type', 'm', 'year', 'monthnum', 'day', 'author', 'author_name' );
139 $keys = array_merge( $keys, $this->model->get_filtered_taxonomies_query_vars() );
140 $args = array_intersect_key( $qv, array_flip( $keys ) );
141 $count = $this->model->count_posts( $language, $args );
142
143 /** This filter is documented in frontend/frontend-links.php */
144 if ( ! apply_filters( 'pll_hide_archive_translation_url', ! $count, $language->slug, $args ) ) {
145 $url = $this->get_archive_url( $language );
146 }
147 }
148
149 // Front page when it is the list of posts
150 elseif ( is_front_page() ) {
151 $url = $this->get_home_url( $language );
152 }
153 }
154
155 /**
156 * Filter the translation url of the current page before Polylang caches it
157 *
158 * @since 1.1.2
159 *
160 * @param null|string $url The translation url, null if none was found
161 * @param string $language The language code of the translation
162 */
163 $translation_url = apply_filters( 'pll_translation_url', ( isset( $url ) && ! is_wp_error( $url ) ? $url : null ), $language->slug );
164
165 // Don't cache before template_redirect to avoid a conflict with Barrel + WP Bakery Page Builder
166 if ( did_action( 'template_redirect' ) ) {
167 $this->cache->set( 'translation_url:' . $language->slug, $translation_url );
168 }
169
170 return $translation_url;
171 }
172
173 /**
174 * Get the translation of the current archive url
175 * used also for search
176 *
177 * @since 1.2
178 *
179 * @param object $language
180 * @return string
181 */
182 public function get_archive_url( $language ) {
183 $url = pll_get_requested_url();
184 $url = $this->links_model->switch_language_in_link( $url, $language );
185 $url = $this->links_model->remove_paged_from_link( $url );
186
187 /**
188 * Filter the archive url
189 *
190 * @since 1.6
191 *
192 * @param string $url Url of the archive
193 * @param object $language Language of the archive
194 */
195 return apply_filters( 'pll_get_archive_url', $url, $language );
196 }
197
198 /**
199 * Returns the home url in the right language
200 *
201 * @since 0.1
202 *
203 * @param object $language Optional, defaults to current language
204 * @param bool $is_search Optional, whether we need the home url for a search form, defaults to false
205 */
206 public function get_home_url( $language = '', $is_search = false ) {
207 if ( empty( $language ) ) {
208 $language = $this->curlang;
209 }
210
211 return parent::get_home_url( $language, $is_search );
212 }
213 }
214