PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
3.8.10 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 All 234 releases
polylang / frontend / frontend-static-pages.php

frontend-static-pages.php in Polylang 2.0.3, at frontend/frontend-static-pages.php

254 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Manages the static front page and the page for posts on frontend
5 *
6 * @since 1.8
7 */
8 class PLL_Frontend_Static_Pages extends PLL_Static_Pages {
9
10 /**
11 * Constructor: setups filters and actions
12 *
13 * @since 1.8
14 *
15 * @param object $polylang
16 */
17 public function __construct( &$polylang ) {
18 parent::__construct( $polylang );
19
20 $this->links_model = &$polylang->links_model;
21 $this->links = &$polylang->links;
22 $this->curlang = &$polylang->curlang;
23
24 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ) );
25 add_action( 'pll_home_requested', array( $this, 'pll_home_requested' ) );
26
27 // Manages the redirection of the homepage
28 add_filter( 'redirect_canonical', array( $this, 'redirect_canonical' ), 10, 2 );
29
30 add_filter( 'pll_pre_translation_url', array( $this, 'pll_pre_translation_url' ), 10, 3 );
31 add_filter( 'pll_check_canonical_url', array( $this, 'pll_check_canonical_url' ) );
32
33 add_filter( 'pll_set_language_from_query', array( $this, 'page_on_front_query' ), 10, 2 );
34 add_filter( 'pll_set_language_from_query', array( $this, 'page_for_posts_query' ), 10, 2 );
35 }
36
37 /**
38 * Init the filters
39 *
40 * @since 1.8
41 */
42 public function pll_language_defined() {
43 // Translates our page on front and page for posts properties
44 $this->init();
45
46 // Translates page for posts and page on front
47 add_filter( 'option_page_on_front', array( $this, 'translate_page_on_front' ) );
48 add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) );
49
50 // Support theme customizer
51 if ( isset( $_POST['wp_customize'], $_POST['customized'] ) ) {
52 add_filter( 'pre_option_page_on_front', 'pll_get_post', 20 );
53 add_filter( 'pre_option_page_for_post', 'pll_get_post', 20 );
54 }
55 }
56
57 /**
58 * Translates the page_id query var when the site root page is requested
59 *
60 * @since 1.8
61 */
62 public function pll_home_requested() {
63 set_query_var( 'page_id', $this->curlang->page_on_front );
64 }
65
66 /**
67 * Translates page on front
68 *
69 * @since 1.8
70 *
71 * @param int $v page on front page id
72 * @return int
73 */
74 public function translate_page_on_front( $v ) {
75 // returns the current page if there is no translation to avoid ugly notices
76 return isset( $this->curlang->page_on_front ) ? $this->curlang->page_on_front : $v;
77 }
78
79 /**
80 * Translates page for posts
81 *
82 * @since 1.8
83 *
84 * @param int $v page for posts page id
85 * @return int
86 */
87 public function translate_page_for_posts( $v ) {
88 // Returns the current page if there is no translation to avoid ugly notices
89 return isset( $this->curlang->page_for_posts ) ? $this->curlang->page_for_posts : $v;
90 }
91
92 /**
93 * Manages canonical redirection of the homepage when using page on front
94 *
95 * @since 0.1
96 *
97 * @param string $redirect_url
98 * @param string $requested_url
99 * @return bool|string modified url, false if redirection is canceled
100 */
101 public function redirect_canonical( $redirect_url, $requested_url ) {
102 global $wp_query;
103 if ( is_page() && ! is_feed() && isset( $wp_query->queried_object ) && $wp_query->queried_object->ID == $this->curlang->page_on_front ) {
104 $url = is_paged() ? $this->links_model->add_paged_to_link( $this->links->get_home_url(), $wp_query->query_vars['page'] ) : $this->links->get_home_url();
105
106 // Don't forget additional query vars
107 $query = parse_url( $redirect_url, PHP_URL_QUERY );
108 if ( ! empty( $query ) ) {
109 parse_str( $query, $query_vars );
110 $query_vars = rawurlencode_deep( $query_vars ); // WP encodes query vars values
111 $url = add_query_arg( $query_vars, $url );
112 }
113
114 return $url;
115 }
116
117 return $redirect_url;
118 }
119
120 /**
121 * Translates the url of the page on front and page for posts
122 *
123 * @since 1.8
124 *
125 * @param string $url not used
126 * @param object $language language in which we want the translation
127 * @param int $queried_object_id id of the queried object
128 * @return string
129 */
130 public function pll_pre_translation_url( $url, $language, $queried_object_id ) {
131 if ( ! empty( $queried_object_id ) ) {
132 // Page for posts
133 if ( $GLOBALS['wp_query']->is_posts_page && ( $id = $this->model->post->get( $queried_object_id, $language ) ) ) {
134 $url = get_permalink( $id );
135 }
136
137 // Page on front
138 elseif ( is_front_page() && $language->page_on_front && ( $language->page_on_front == $this->model->post->get( $queried_object_id, $language ) ) ) {
139 $url = $language->home_url;
140 }
141 }
142
143 return $url;
144 }
145
146 /**
147 * Prevents canonical redirection if we are on a static front page
148 *
149 * @since 1.8
150 *
151 * @param string $redirect_url
152 * @return bool|string
153 */
154 public function pll_check_canonical_url( $redirect_url ) {
155 return $this->options['redirect_lang'] && ! empty( $this->curlang->page_on_front ) && is_page( $this->curlang->page_on_front ) ? false : $redirect_url;
156 }
157
158 /**
159 * Setups query vars when requesting a static front page
160 *
161 * @since 1.8
162 *
163 * @param bool|object $lang
164 * @param object $query
165 * @return bool|object
166 */
167 public function page_on_front_query( $lang, $query ) {
168 if ( ! empty( $lang ) || ! $this->page_on_front ) {
169 return $lang;
170 }
171
172 // The home page is requested
173 if ( did_action( 'home_requested' ) ) {
174 $query->set( 'page_id', $lang->page_on_front );
175 }
176
177 // Redirect the language page to the homepage when using a static front page
178 elseif ( ( $this->options['redirect_lang'] || $this->options['hide_default'] ) && ( count( $query->query ) == 1 || ( is_paged() && count( $query->query ) == 2 ) ) && is_tax( 'language' ) ) {
179 $lang = $this->model->get_language( get_query_var( 'lang' ) );
180 $query->set( 'page_id', $lang->page_on_front );
181 $query->is_singular = $query->is_page = true;
182 $query->is_archive = $query->is_tax = false;
183 unset( $query->query_vars['lang'], $query->queried_object ); // Reset queried object
184 }
185
186 // Set the language when requesting a static front page
187 else {
188 $page_id = $this->get_page_id( $query );
189 $languages = $this->model->get_languages_list();
190 $pages = wp_list_pluck( $languages, 'page_on_front' );
191
192 if ( ! empty( $page_id ) && false !== $n = array_search( $page_id, $pages ) ) {
193 $lang = $languages[ $n ];
194 }
195 }
196
197 // Fix <!--nextpage--> for page_on_front
198 if ( ! empty( $lang ) ) {
199 $query->set( 'page', $query->query_vars['paged'] );
200 unset( $query->query_vars['paged'] );
201 }
202
203 return $lang;
204 }
205
206 /**
207 * Setups query vars when requesting a posts page
208 *
209 * @since 1.8
210 *
211 * @param bool|object $lang
212 * @param object $query
213 * @return bool|object
214 */
215 public function page_for_posts_query( $lang, $query ) {
216 if ( empty( $lang ) && $this->page_for_posts ) {
217 $page_id = $this->get_page_id( $query );
218
219 if ( ! empty( $page_id ) && in_array( $page_id, $pages = $this->model->get_languages_list( array( 'fields' => 'page_for_posts' ) ) ) ) {
220 // Fill the cache with all pages for posts to avoid one query per page later
221 // The posts_per_page limit is a trick to avoid splitting the query
222 get_posts( array( 'posts_per_page' => 999, 'post_type' => 'page', 'post__in' => $pages, 'lang' => '' ) );
223
224 $lang = $this->model->post->get_language( $page_id );
225 $query->is_singular = $query->is_page = false;
226 $query->is_home = $query->is_posts_page = true;
227 }
228 }
229 return $lang;
230 }
231
232 /**
233 * Get queried page_id ( if exists )
234 * If permalinks are used, WordPress does set and use $query->queried_object_id and sets $query->query_vars['page_id'] to 0
235 * and does set and use $query->query_vars['page_id'] if permalinks are not used :(
236 *
237 * @since 1.5
238 *
239 * @param object $query instance of WP_Query
240 * @return int page_id
241 */
242 protected function get_page_id( $query ) {
243 if ( ! empty( $query->query_vars['pagename'] ) && isset( $query->queried_object_id ) ) {
244 return $query->queried_object_id;
245 }
246
247 if ( isset( $query->query_vars['page_id'] ) ) {
248 return $query->query_vars['page_id'];
249 }
250
251 return 0; // No page queried
252 }
253 }
254