PluginProbe
Polylang / 3.6.7
Polylang v3.6.7
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
← All changes | include/static-pages.php +156 -52 2.83.6.7 View file →
@@ -3,124 +3,228 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * Base class to manage the static front page and the page for posts
7 + * Base class to manage the static front page and the page for posts.
8 8 *
9 9 * @since 1.8
10 10 */
11 11 class PLL_Static_Pages {
12 - public $model, $options;
13 - public $page_on_front, $page_for_posts;
12 + /**
13 + * Id of the page on front.
14 + *
15 + * @var int
16 + */
17 + public $page_on_front = 0;
14 18
15 19 /**
16 - * Constructor: setups filters and actions
20 + * Id of the page for posts.
17 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 + *
18 41 * @since 1.8
19 42 *
20 - * @param object $polylang
43 + * @param object $polylang The Polylang object.
21 44 */
22 45 public function __construct( &$polylang ) {
23 46 $this->model = &$polylang->model;
24 - $this->options = &$polylang->options;
25 47 $this->curlang = &$polylang->curlang;
26 48
27 49 $this->init();
28 50
29 - // Modifies the page link in case the front page is not in the default language
30 - add_filter( 'page_link', array( $this, 'page_link' ), 20, 2 );
51 + add_filter( 'pll_additional_language_data', array( $this, 'set_static_pages' ), 5, 2 ); // Before PLL_Links_Model.
31 52
32 - // Clean the languages cache when editing page of front, page for posts
33 - add_action( 'update_option_show_on_front', array( $this->model, 'clean_languages_cache' ) );
34 - add_action( 'update_option_page_on_front', array( $this->model, 'clean_languages_cache' ) );
35 - add_action( 'update_option_page_for_posts', array( $this->model, 'clean_languages_cache' ) );
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' ) );
36 57
37 - // Refresh rewrite rules when the page on front is modified
58 + // Refresh rewrite rules when the page on front is modified.
38 59 add_action( 'update_option_page_on_front', 'flush_rewrite_rules' );
39 60
40 - // OEmbed
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.
41 68 add_filter( 'oembed_request_post_id', array( $this, 'oembed_request_post_id' ), 10, 2 );
42 69 }
43 70
44 71 /**
45 - * Stores the page on front and page for posts ids
72 + * Stores the page on front and page for posts ids.
46 73 *
47 74 * @since 1.8
75 + *
76 + * @return void
48 77 */
49 78 public function init() {
50 - if ( 'page' == get_option( 'show_on_front' ) ) {
51 - $this->page_on_front = get_option( 'page_on_front' );
52 - $this->page_for_posts = get_option( 'page_for_posts' );
53 - } else {
54 - $this->page_on_front = 0;
55 - $this->page_for_posts = 0;
79 + $this->page_on_front = 0;
80 + $this->page_for_posts = 0;
81 +
82 + if ( 'page' !== get_option( 'show_on_front' ) ) {
83 + return;
56 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 + }
57 95 }
58 96
59 97 /**
60 - * Modifies the page link in case the front page is not in the default language
98 + * Returns the ID of the static page translation.
61 99 *
62 - * @since 0.7.2
100 + * @since 3.4
63 101 *
64 - * @param string $link link to the page
65 - * @param int $id post id of the page
66 - * @return string modified link
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
67 105 */
68 - public function page_link( $link, $id ) {
69 - $lang = $this->model->post->get_language( $id );
106 + protected function get_translation( $static_page, $language ) {
107 + $translations = $this->model->post->get_raw_translations( $this->$static_page );
70 108
71 - if ( $lang && $id == $lang->page_on_front ) {
72 - return $lang->home_url;
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 + }
73 116 }
74 - return $link;
117 +
118 + if ( ! isset( $translations[ $language['slug'] ] ) ) {
119 + return 0;
120 + }
121 +
122 + return $translations[ $language['slug'] ];
75 123 }
76 124
77 125 /**
78 - * Adds page_on_front and page_for_posts properties to the language objects
126 + * Adds `page_on_front` and `page_for_posts` properties to language data before the object is created.
79 127 *
80 - * @since 1.8
128 + * @since 3.4
81 129 *
82 - * @param array $languages
83 - * @param object $model
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.
84 133 */
85 - public static function pll_languages_list( $languages, $model ) {
86 - if ( 'page' === get_option( 'show_on_front' ) ) {
87 - foreach ( $languages as $k => $language ) {
88 - $languages[ $k ]->page_on_front = $model->post->get( get_option( 'page_on_front' ), $language );
89 - $languages[ $k ]->page_for_posts = $model->post->get( get_option( 'page_for_posts' ), $language );
90 - }
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_id' ), 10, 2 );
163 + add_filter( 'option_page_for_posts', array( $this, 'translate_page_id' ), 10, 2 );
164 + }
165 +
166 + /**
167 + * Translates the page on front or page for posts option.
168 + *
169 + * @since 3.6 Replaces `translate_page_on_front()` and `translate_page_on_front()` methods.
170 + *
171 + * @param int $page_id ID of the page on front or page for posts.
172 + * @param string $option Option name: `page_on_front` or `page_for_posts`.
173 + * @return int
174 + */
175 + public function translate_page_id( $page_id, $option ) {
176 +
177 + if ( empty( $this->curlang->{$option} ) ) {
178 + return $page_id;
91 179 }
92 180
93 - return $languages;
181 + if ( doing_action( "update_option_{$option}" ) || doing_action( 'switch_blog' ) || doing_action( 'before_delete_post' ) || doing_action( 'wp_trash_post' ) ) {
182 + /*
183 + * Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache.
184 + * Don't translate while deleting a post or it will mess up `_reset_front_page_settings_for_post()`.
185 + * Don't translate while updating the option itself.
186 + */
187 + return $page_id;
188 + }
189 +
190 + return $this->curlang->{$option};
94 191 }
95 192
96 193 /**
97 - * Translates page for posts
194 + * Modifies the page link in case the front page is not in the default language.
98 195 *
99 - * @since 1.8
196 + * @since 0.7.2
100 197 *
101 - * @param int $v page for posts page id
102 - * @return int
198 + * @param string $link The link to the page.
199 + * @param int $id The post ID of the page.
200 + * @return string Modified link.
103 201 */
104 - public function translate_page_for_posts( $v ) {
105 - // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache
106 - return isset( $this->curlang->page_for_posts ) && ! doing_action( 'switch_blog' ) ? $this->curlang->page_for_posts : $v;
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;
107 209 }
108 210
109 211 /**
110 212 * Fixes the oembed for the translated static front page
111 - * when the language page is redirected to the front page
213 + * when the language page is redirected to the front page.
112 214 *
113 215 * @since 2.6
114 216 *
115 217 * @param int $post_id The post ID.
116 218 * @param string $url The requested URL.
219 + * @return int
117 220 */
118 221 public function oembed_request_post_id( $post_id, $url ) {
119 222 foreach ( $this->model->get_languages_list() as $lang ) {
120 - if ( trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) {
121 - $post_id = $lang->page_on_front;
223 + if ( is_string( $lang->get_home_url() ) && trailingslashit( $url ) === trailingslashit( $lang->get_home_url() ) ) {
224 + return (int) $lang->page_on_front;
122 225 }
123 226 }
227 +
124 228 return $post_id;
125 229 }
126 230 }