PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
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.6.2, at frontend/frontend-links.php

215 lines 7.4 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 $hide = $this->options['default_lang'] == $language->slug && $this->options['hide_default'];
59
60 // Post and attachment
61 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 ) ) {
62 $url = get_permalink( $id );
63 }
64
65 // Page
66 elseif ( is_page() && ( $id = $this->model->post->get( $queried_object_id, $language ) ) && $this->model->post->current_user_can_read( $id ) ) {
67 $url = get_page_link( $id );
68 }
69
70 elseif ( is_search() ) {
71 $url = $this->get_archive_url( $language );
72
73 // Special case for search filtered by translated taxonomies: taxonomy terms are translated in the translation url
74 if ( ! empty( $wp_query->tax_query->queries ) ) {
75 foreach ( $wp_query->tax_query->queries as $tax_query ) {
76 if ( ! empty( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) ) {
77
78 $tax = get_taxonomy( $tax_query['taxonomy'] );
79 $terms = get_terms( $tax->name, array( 'fields' => 'id=>slug' ) ); // Filtered by current language
80
81 foreach ( $tax_query['terms'] as $slug ) {
82 $term_id = array_search( $slug, $terms ); // What is the term_id corresponding to taxonomy term?
83 if ( $term_id && $term_id = $this->model->term->get_translation( $term_id, $language ) ) { // Get the translated term_id
84 $term = get_term( $term_id, $tax->name );
85 $url = str_replace( $slug, $term->slug, $url );
86 }
87 }
88 }
89 }
90 }
91 }
92
93 // Translated taxonomy
94 // Take care that is_tax() is false for categories and tags
95 elseif ( ( is_category() || is_tag() || is_tax() ) && ( $term = get_queried_object() ) && $this->model->is_translated_taxonomy( $term->taxonomy ) ) {
96 $lang = $this->model->term->get_language( $term->term_id );
97
98 if ( ! $lang || $language->slug == $lang->slug ) {
99 $url = wpcom_vip_get_term_link( $term, $term->taxonomy ); // Self link
100 }
101
102 elseif ( $tr_id = $this->model->term->get_translation( $term->term_id, $language ) ) {
103 if ( $tr_term = get_term( $tr_id, $term->taxonomy ) ) {
104 // Check if translated term ( or children ) have posts
105 $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' ) ) );
106
107 /**
108 * Filter whether to hide an archive translation url
109 *
110 * @since 2.2.4
111 *
112 * @param bool $hide True to hide the translation url.
113 * defaults to true when the translated archive is empty, false otherwise.
114 * @param string $lang The language code of the translation
115 * @param array $args Arguments used to evaluated the number of posts in the archive
116 */
117 if ( ! apply_filters( 'pll_hide_archive_translation_url', ! $count, $language->slug, array( 'taxonomy' => $term->taxonomy ) ) ) {
118 $url = wpcom_vip_get_term_link( $tr_term, $term->taxonomy );
119 }
120 }
121 }
122 }
123
124 // Post type archive
125 elseif ( is_post_type_archive() ) {
126 if ( $this->model->is_translated_post_type( $qv['post_type'] ) ) {
127 $args = array( 'post_type' => $qv['post_type'] );
128 $count = $this->model->count_posts( $language, $args );
129
130 /** This filter is documented in frontend/frontend-links.php */
131 if ( ! apply_filters( 'pll_hide_archive_translation_url', ! $count, $language->slug, $args ) ) {
132 $url = $this->get_archive_url( $language );
133 }
134 }
135 }
136
137 // Other archives
138 elseif ( is_archive() ) {
139 $keys = array( 'post_type', 'm', 'year', 'monthnum', 'day', 'author', 'author_name' );
140 $keys = array_merge( $keys, $this->model->get_filtered_taxonomies_query_vars() );
141 $args = array_intersect_key( $qv, array_flip( $keys ) );
142 $count = $this->model->count_posts( $language, $args );
143
144 /** This filter is documented in frontend/frontend-links.php */
145 if ( ! apply_filters( 'pll_hide_archive_translation_url', ! $count, $language->slug, $args ) ) {
146 $url = $this->get_archive_url( $language );
147 }
148 }
149
150 // Front page when it is the list of posts
151 elseif ( is_front_page() ) {
152 $url = $this->get_home_url( $language );
153 }
154 }
155
156 /**
157 * Filter the translation url of the current page before Polylang caches it
158 *
159 * @since 1.1.2
160 *
161 * @param null|string $url The translation url, null if none was found
162 * @param string $language The language code of the translation
163 */
164 $translation_url = apply_filters( 'pll_translation_url', ( isset( $url ) && ! is_wp_error( $url ) ? $url : null ), $language->slug );
165
166 // Don't cache before template_redirect to avoid a conflict with Barrel + WP Bakery Page Builder
167 if ( did_action( 'template_redirect' ) ) {
168 $this->cache->set( 'translation_url:' . $language->slug, $translation_url );
169 }
170
171 return $translation_url;
172 }
173
174 /**
175 * Get the translation of the current archive url
176 * used also for search
177 *
178 * @since 1.2
179 *
180 * @param object $language
181 * @return string
182 */
183 public function get_archive_url( $language ) {
184 $url = pll_get_requested_url();
185 $url = $this->links_model->switch_language_in_link( $url, $language );
186 $url = $this->links_model->remove_paged_from_link( $url );
187
188 /**
189 * Filter the archive url
190 *
191 * @since 1.6
192 *
193 * @param string $url Url of the archive
194 * @param object $language Language of the archive
195 */
196 return apply_filters( 'pll_get_archive_url', $url, $language );
197 }
198
199 /**
200 * Returns the home url in the right language
201 *
202 * @since 0.1
203 *
204 * @param object $language Optional, defaults to current language
205 * @param bool $is_search Optional, whether we need the home url for a search form, defaults to false
206 */
207 public function get_home_url( $language = '', $is_search = false ) {
208 if ( empty( $language ) ) {
209 $language = $this->curlang;
210 }
211
212 return parent::get_home_url( $language, $is_search );
213 }
214 }
215