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 +131 -61 3.03.6.7 View file →
@@ -3,9 +3,9 @@
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 {
@@ -13,9 +13,9 @@
13 13 * Id of the page on front.
14 14 *
15 15 * @var int
16 16 */
17 - public $page_on_front;
17 + public $page_on_front = 0;
18 18
19 19 /**
20 20 * Id of the page for posts.
21 21 *
@@ -20,18 +20,11 @@
20 20 * Id of the page for posts.
21 21 *
22 22 * @var int
23 23 */
24 - public $page_for_posts;
24 + public $page_for_posts = 0;
25 25
26 26 /**
27 - * Stores the plugin options.
28 - *
29 - * @var array
30 - */
31 - protected $options;
32 -
33 - /**
34 27 * @var PLL_Model
35 28 */
36 29 protected $model;
37 30
@@ -37,43 +30,47 @@
37 30
38 31 /**
39 32 * Current language.
40 33 *
41 - * @var PLL_Language
34 + * @var PLL_Language|null
42 35 */
43 36 protected $curlang;
44 37
45 38 /**
46 - * Constructor: setups filters and actions
39 + * Constructor: setups filters and actions.
47 40 *
48 41 * @since 1.8
49 42 *
50 - * @param object $polylang
43 + * @param object $polylang The Polylang object.
51 44 */
52 45 public function __construct( &$polylang ) {
53 46 $this->model = &$polylang->model;
54 - $this->options = &$polylang->options;
55 47 $this->curlang = &$polylang->curlang;
56 48
57 49 $this->init();
58 50
59 - // Modifies the page link in case the front page is not in the default language
60 - 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.
61 52
62 - // Clean the languages cache when editing page of front, page for posts
63 - add_action( 'update_option_show_on_front', array( $this->model, 'clean_languages_cache' ) );
64 - add_action( 'update_option_page_on_front', array( $this->model, 'clean_languages_cache' ) );
65 - 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' ) );
66 57
67 - // Refresh rewrite rules when the page on front is modified
58 + // Refresh rewrite rules when the page on front is modified.
68 59 add_action( 'update_option_page_on_front', 'flush_rewrite_rules' );
69 60
70 - // 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.
71 68 add_filter( 'oembed_request_post_id', array( $this, 'oembed_request_post_id' ), 10, 2 );
72 69 }
73 70
74 71 /**
75 - * Stores the page on front and page for posts ids
72 + * Stores the page on front and page for posts ids.
76 73 *
77 74 * @since 1.8
78 75 *
79 76 * @return void
@@ -78,71 +75,143 @@
78 75 *
79 76 * @return void
80 77 */
81 78 public function init() {
82 - if ( 'page' == get_option( 'show_on_front' ) ) {
83 - $this->page_on_front = get_option( 'page_on_front' );
84 - $this->page_for_posts = get_option( 'page_for_posts' );
85 - } else {
86 - $this->page_on_front = 0;
87 - $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;
88 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 + }
89 95 }
90 96
91 97 /**
92 - * Modifies the page link in case the front page is not in the default language
98 + * Returns the ID of the static page translation.
93 99 *
94 - * @since 0.7.2
100 + * @since 3.4
95 101 *
96 - * @param string $link link to the page
97 - * @param int $id post id of the page
98 - * @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
99 105 */
100 - public function page_link( $link, $id ) {
101 - $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 );
102 108
103 - if ( $lang && $id == $lang->page_on_front ) {
104 - 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 + }
105 116 }
106 - return $link;
117 +
118 + if ( ! isset( $translations[ $language['slug'] ] ) ) {
119 + return 0;
120 + }
121 +
122 + return $translations[ $language['slug'] ];
107 123 }
108 124
109 125 /**
110 - * 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.
111 127 *
112 - * @since 1.8
128 + * @since 3.4
113 129 *
114 - * @param PLL_Language[] $languages The list of languages.
115 - * @param PLL_Model $model The instance of PLL_Model.
116 - * @return PLL_Language[]
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.
117 133 */
118 - public static function pll_languages_list( $languages, $model ) {
119 - if ( 'page' === get_option( 'show_on_front' ) ) {
120 - foreach ( $languages as $k => $language ) {
121 - $languages[ $k ]->page_on_front = $model->post->get( get_option( 'page_on_front' ), $language );
122 - $languages[ $k ]->page_for_posts = $model->post->get( get_option( 'page_for_posts' ), $language );
123 - }
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;
124 179 }
125 180
126 - 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};
127 191 }
128 192
129 193 /**
130 - * Translates page for posts
194 + * Modifies the page link in case the front page is not in the default language.
131 195 *
132 - * @since 1.8
196 + * @since 0.7.2
133 197 *
134 - * @param int $v page for posts page id
135 - * @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.
136 201 */
137 - public function translate_page_for_posts( $v ) {
138 - // Don't attempt to translate in a 'switch_blog' action as there is a risk to call this function while initializing the languages cache
139 - return isset( $this->curlang->page_for_posts ) && ( $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;
140 209 }
141 210
142 211 /**
143 212 * Fixes the oembed for the translated static front page
144 - * when the language page is redirected to the front page
213 + * when the language page is redirected to the front page.
145 214 *
146 215 * @since 2.6
147 216 *
148 217 * @param int $post_id The post ID.
@@ -150,11 +219,12 @@
150 219 * @return int
151 220 */
152 221 public function oembed_request_post_id( $post_id, $url ) {
153 222 foreach ( $this->model->get_languages_list() as $lang ) {
154 - if ( trailingslashit( $url ) === trailingslashit( $lang->home_url ) ) {
155 - $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;
156 225 }
157 226 }
227 +
158 228 return $post_id;
159 229 }
160 230 }