PluginProbe
Polylang / 3.4.5
Polylang v3.4.5
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 / include / static-pages.php

static-pages.php in Polylang 3.4.5, at include/static-pages.php

231 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Base class to manage the static front page and the page for posts.
8 *
9 * @since 1.8
10 */
11 class PLL_Static_Pages {
12 /**
13 * Id of the page on front.
14 *
15 * @var int
16 */
17 public $page_on_front = 0;
18
19 /**
20 * Id of the page for posts.
21 *
22 * @var int
23 */
24 public $page_for_posts = 0;
25
26 /**
27 * @var PLL_Model
28 */
29 protected $model;
30
31 /**
32 * Current language.
33 *
34 * @var PLL_Language|null
35 */
36 protected $curlang;
37
38 /**
39 * Constructor: setups filters and actions.
40 *
41 * @since 1.8
42 *
43 * @param object $polylang
44 */
45 public function __construct( &$polylang ) {
46 $this->model = &$polylang->model;
47 $this->curlang = &$polylang->curlang;
48
49 $this->init();
50
51 add_filter( 'pll_additional_language_data', array( $this, 'set_static_pages' ), 5, 2 ); // Before PLL_Links_Model.
52
53 // Clean the languages cache when editing page of front, page for posts.
54 add_action( 'update_option_show_on_front', array( $this, 'clean_cache' ) );
55 add_action( 'update_option_page_on_front', array( $this, 'clean_cache' ) );
56 add_action( 'update_option_page_for_posts', array( $this, 'clean_cache' ) );
57
58 // Refresh rewrite rules when the page on front is modified.
59 add_action( 'update_option_page_on_front', 'flush_rewrite_rules' );
60
61 // Add option filters when the current language is defined
62 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ) );
63
64 // Modifies the page link in case the front page is not in the default language.
65 add_filter( 'page_link', array( $this, 'page_link' ), 20, 2 );
66
67 // OEmbed.
68 add_filter( 'oembed_request_post_id', array( $this, 'oembed_request_post_id' ), 10, 2 );
69 }
70
71 /**
72 * Stores the page on front and page for posts ids.
73 *
74 * @since 1.8
75 *
76 * @return void
77 */
78 public function init() {
79 $this->page_on_front = 0;
80 $this->page_for_posts = 0;
81
82 if ( 'page' !== get_option( 'show_on_front' ) ) {
83 return;
84 }
85
86 $page_on_front = get_option( 'page_on_front' );
87 if ( is_numeric( $page_on_front ) ) {
88 $this->page_on_front = (int) $page_on_front;
89 }
90
91 $page_for_posts = get_option( 'page_for_posts' );
92 if ( is_numeric( $page_for_posts ) ) {
93 $this->page_for_posts = (int) $page_for_posts;
94 }
95 }
96
97 /**
98 * Returns the ID of the static page translation.
99 *
100 * @since 3.4
101 *
102 * @param string $static_page Static page option name; `page_on_front` or `page_for_posts`.
103 * @param array $language Language data.
104 * @return int
105 */
106 protected function get_translation( $static_page, $language ) {
107 $translations = $this->model->post->get_raw_translations( $this->$static_page );
108
109 // When the current static page doesn't have any translation, we must return itself for its language.
110 if ( empty( $translations ) ) {
111 $page_lang = $this->model->post->get_object_term( $this->$static_page, $this->model->post->get_tax_language() );
112
113 if ( ! empty( $page_lang ) && $page_lang->slug === $language['slug'] ) {
114 return $this->$static_page;
115 }
116 }
117
118 if ( ! isset( $translations[ $language['slug'] ] ) ) {
119 return 0;
120 }
121
122 return $translations[ $language['slug'] ];
123 }
124
125 /**
126 * Adds `page_on_front` and `page_for_posts` properties to language data before the object is created.
127 *
128 * @since 3.4
129 *
130 * @param array $additional_data Array of language additional data.
131 * @param array $language Language data.
132 * @return array Language data with additional `page_on_front` and `page_for_posts` options added.
133 */
134 public function set_static_pages( $additional_data, $language ) {
135 $additional_data['page_on_front'] = $this->get_translation( 'page_on_front', $language );
136 $additional_data['page_for_posts'] = $this->get_translation( 'page_for_posts', $language );
137
138 return $additional_data;
139 }
140
141 /**
142 * Cleans the language cache and resets the internal properties when options are updated.
143 *
144 * @since 3.4
145 *
146 * @return void
147 */
148 public function clean_cache() {
149 $this->model->clean_languages_cache();
150 $this->init();
151 }
152
153 /**
154 * Init the hooks that filter the "page on front" and "page for posts" options.
155 *
156 * @since 3.3
157 *
158 * @return void
159 */
160 public function pll_language_defined() {
161 // Translates page for posts and page on front.
162 add_filter( 'option_page_on_front', array( $this, 'translate_page_on_front' ) );
163 add_filter( 'option_page_for_posts', array( $this, 'translate_page_for_posts' ) );
164 }
165
166 /**
167 * Translates the page on front option.
168 *
169 * @since 1.8
170 * @since 3.3 Was previously defined in PLL_Frontend_Static_Pages.
171 *
172 * @param int $page_id ID of the page on front.
173 * @return int
174 */
175 public function translate_page_on_front( $page_id ) {
176 // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache.
177 return ! empty( $this->curlang->page_on_front ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_on_front : $page_id;
178 }
179
180 /**
181 * Translates the page for posts option.
182 *
183 * @since 1.8
184 *
185 * @param int $page_id ID of the page for posts.
186 * @return int
187 */
188 public function translate_page_for_posts( $page_id ) {
189 // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache.
190 return ! empty( $this->curlang->page_for_posts ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_for_posts : $page_id;
191 }
192
193 /**
194 * Modifies the page link in case the front page is not in the default language.
195 *
196 * @since 0.7.2
197 *
198 * @param string $link The link to the page.
199 * @param int $id The post ID of the page.
200 * @return string Modified link.
201 */
202 public function page_link( $link, $id ) {
203 $lang = $this->model->post->get_language( $id );
204
205 if ( $lang && $id == $lang->page_on_front ) {
206 return $lang->get_home_url();
207 }
208 return $link;
209 }
210
211 /**
212 * Fixes the oembed for the translated static front page
213 * when the language page is redirected to the front page.
214 *
215 * @since 2.6
216 *
217 * @param int $post_id The post ID.
218 * @param string $url The requested URL.
219 * @return int
220 */
221 public function oembed_request_post_id( $post_id, $url ) {
222 foreach ( $this->model->get_languages_list() as $lang ) {
223 if ( is_string( $lang->get_home_url() ) && trailingslashit( $url ) === trailingslashit( $lang->get_home_url() ) ) {
224 return (int) $lang->page_on_front;
225 }
226 }
227
228 return $post_id;
229 }
230 }
231