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

188 lines 6.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->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->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 $tr_term = get_term( $tr_id, $term->taxonomy );
104 // check if translated term ( or children ) have posts
105 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' ) ) ) ) ) {
106 $url = wpcom_vip_get_term_link( $tr_term, $term->taxonomy );
107 }
108 }
109 }
110
111 // post type archive
112 elseif ( is_post_type_archive() ) {
113 if ( $this->model->is_translated_post_type( $qv['post_type'] ) && $this->model->count_posts( $language, array( 'post_type' => $qv['post_type'] ) ) ) {
114 $url = $this->get_archive_url( $language );
115 }
116 }
117
118 elseif ( is_archive() ) {
119 $keys = array( 'post_type', 'm', 'year', 'monthnum', 'day', 'author', 'author_name' );
120 $keys = array_merge( $keys, $this->model->get_filtered_taxonomies_query_vars() );
121
122 // check if there are existing translations before creating the url
123 if ( $this->model->count_posts( $language, array_intersect_key( $qv, array_flip( $keys ) ) ) ) {
124 $url = $this->get_archive_url( $language );
125 }
126 }
127
128 // front page when it is the list of posts
129 elseif ( is_front_page() ) {
130 $url = $this->get_home_url( $language );
131 }
132 }
133
134 /**
135 * Filter the translation url of the current page before Polylang caches it
136 *
137 * @since 1.1.2
138 *
139 * @param null|string $url the translation url, null if none was found
140 * @param string $language the language code of the translation
141 */
142 $translation_url = apply_filters( 'pll_translation_url', ( isset( $url ) && ! is_wp_error( $url ) ? $url : null ), $language->slug );
143 $this->cache->set( 'translation_url:' . $language->slug, $translation_url );
144 return $translation_url;
145 }
146
147 /**
148 * get the translation of the current archive url
149 * used also for search
150 *
151 * @since 1.2
152 *
153 * @param object $language
154 * @return string
155 */
156 public function get_archive_url( $language ) {
157 $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
158 $url = $this->links_model->switch_language_in_link( $url, $language );
159 $url = $this->links_model->remove_paged_from_link( $url );
160
161 /**
162 * Filter the archive url
163 *
164 * @since 1.6
165 *
166 * @param string $url url of the archive
167 * @param object $language language of the archive
168 */
169 return apply_filters( 'pll_get_archive_url', $url, $language );
170 }
171
172 /**
173 * returns the home url in the right language
174 *
175 * @since 0.1
176 *
177 * @param object $language optional defaults to current language
178 * @param bool $is_search optional wether we need the home url for a search form, defaults to false
179 */
180 public function get_home_url( $language = '', $is_search = false ) {
181 if ( empty( $language ) ) {
182 $language = $this->curlang;
183 }
184
185 return parent::get_home_url( $language, $is_search );
186 }
187 }
188