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/links-model.php +185 -60 2.93.6.7 View file →
@@ -3,46 +3,160 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * Links model abstract class
7 + * Links model abstract class.
8 8 *
9 9 * @since 1.5
10 10 */
11 11 abstract class PLL_Links_Model {
12 - public $model, $options;
13 - public $home; // used to store the home url before it is filtered
12 + /**
13 + * True if the child class uses pretty permalinks, false otherwise.
14 + *
15 + * @var bool
16 + */
14 17 public $using_permalinks;
15 18
16 19 /**
17 - * Constructor
20 + * Stores the plugin options.
18 21 *
22 + * @var array
23 + */
24 + public $options;
25 +
26 + /**
27 + * @var PLL_Model
28 + */
29 + public $model;
30 +
31 + /**
32 + * Stores the home url before it is filtered.
33 + *
34 + * @var string
35 + */
36 + public $home;
37 +
38 + /**
39 + * Whether rewrite rules can be filtered or not. Default to `false`.
40 + *
41 + * @var boolean
42 + */
43 + protected static $can_filter_rewrite_rules = false;
44 +
45 + /**
46 + * Constructor.
47 + *
19 48 * @since 1.5
20 49 *
21 - * @param object $model PLL_Model instance
50 + * @param PLL_Model $model PLL_Model instance.
22 51 */
23 52 public function __construct( &$model ) {
24 - $this->model = &$model;
53 + $this->model = &$model;
25 54 $this->options = &$model->options;
26 55
27 56 $this->home = home_url();
28 57
29 - add_filter( 'pll_languages_list', array( $this, 'pll_languages_list' ), 4 ); // after PLL_Static_Pages
30 - add_filter( 'pll_after_languages_cache', array( $this, 'pll_after_languages_cache' ) );
58 + // Hooked with normal priority because it needs to be run after static pages is set in language data. Must be done early (before languages objects are created).
59 + add_filter( 'pll_additional_language_data', array( $this, 'set_language_home_urls' ), 10, 2 );
31 60
32 - // adds our domains or subdomains to allowed hosts for safe redirection
61 + // Adds our domains or subdomains to allowed hosts for safe redirection.
33 62 add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
63 +
64 + // Allows secondary domains for home and search URLs in `PLL_Language`.
65 + add_filter( 'pll_language_home_url', array( $this, 'set_language_home_url' ), 10, 2 );
66 + add_filter( 'pll_language_search_url', array( $this, 'set_language_search_url' ), 10, 2 );
67 +
68 + if ( did_action( 'pll_init' ) ) {
69 + $this->init();
70 + } else {
71 + add_action( 'pll_init', array( $this, 'init' ) );
72 + }
34 73 }
35 74
36 75 /**
37 - * Changes the language code in url
76 + * Initializes the links model.
77 + * Does nothing by default.
38 78 *
79 + * @since 3.5
80 + *
81 + * @return void
82 + */
83 + public function init() {}
84 +
85 + /**
86 + * Adds the language code in url.
87 + *
88 + * @since 1.2
89 + * @since 3.4 Accepts now a language slug.
90 + *
91 + * @param string $url The url to modify.
92 + * @param PLL_Language|string|false $lang Language object or slug.
93 + * @return string The modified url.
94 + */
95 + abstract public function add_language_to_link( $url, $lang );
96 +
97 + /**
98 + * Returns the url without language code.
99 + *
100 + * @since 1.2
101 + *
102 + * @param string $url The url to modify.
103 + * @return string The modified url.
104 + */
105 + abstract public function remove_language_from_link( $url );
106 +
107 + /**
108 + * Returns the link to the first page.
109 + *
110 + * @since 1.2
111 + *
112 + * @param string $url The url to modify.
113 + * @return string The modified url.
114 + */
115 + abstract public function remove_paged_from_link( $url );
116 +
117 + /**
118 + * Returns the link to a paged page.
119 + *
39 120 * @since 1.5
40 121 *
41 - * @param string $url url to modify
42 - * @param object $lang language
43 - * @return string modified url
122 + * @param string $url The url to modify.
123 + * @param int $page The page number.
124 + * @return string The modified url.
44 125 */
126 + abstract public function add_paged_to_link( $url, $page );
127 +
128 + /**
129 + * Returns the language based on the language code in the url.
130 + *
131 + * @since 1.2
132 + * @since 2.0 Add the $url argument.
133 + *
134 + * @param string $url Optional, defaults to the current url.
135 + * @return string The language slug.
136 + */
137 + abstract public function get_language_from_url( $url = '' );
138 +
139 + /**
140 + * Returns the static front page url in a given language.
141 + *
142 + * @since 1.8
143 + * @since 3.4 Accepts now an array of language properties.
144 + *
145 + * @param PLL_Language|array $language Language object or array of language properties.
146 + * @return string The static front page url.
147 + */
148 + abstract public function front_page_url( $language );
149 +
150 + /**
151 + * Changes the language code in url.
152 + *
153 + * @since 1.5
154 + *
155 + * @param string $url The url to modify.
156 + * @param PLL_Language $lang The language object.
157 + * @return string The modified url.
158 + */
45 159 public function switch_language_in_link( $url, $lang ) {
46 160 $url = $this->remove_language_from_link( $url );
47 161 return $this->add_language_to_link( $url, $lang );
48 162 }
@@ -47,13 +161,13 @@
47 161 return $this->add_language_to_link( $url, $lang );
48 162 }
49 163
50 164 /**
51 - * Get hosts managed on the website
165 + * Get the hosts managed on the website.
52 166 *
53 167 * @since 1.5
54 168 *
55 - * @return array list of hosts
169 + * @return string[] The list of hosts.
56 170 */
57 171 public function get_hosts() {
58 172 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
59 173 }
@@ -58,82 +172,93 @@
58 172 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
59 173 }
60 174
61 175 /**
62 - * Returns the home url
176 + * Returns the home url in a given language.
63 177 *
64 178 * @since 1.3.1
179 + * @since 3.4 Accepts now a language slug.
65 180 *
66 - * @param object $lang PLL_Language object
181 + * @param PLL_Language|string $language Language object or slug.
67 182 * @return string
68 183 */
69 - public function home_url( $lang ) {
184 + public function home_url( $language ) {
185 + if ( $language instanceof PLL_Language ) {
186 + $language = $language->slug;
187 + }
188 +
70 189 $url = trailingslashit( $this->home );
71 - return $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ? $url : $this->add_language_to_link( $url, $lang );
190 +
191 + return $this->options['hide_default'] && $language === $this->options['default_lang'] ? $url : $this->add_language_to_link( $url, $language );
72 192 }
73 193
74 194 /**
75 - * Sets the home urls
195 + * Adds home and search URLs to language data before the object is created.
76 196 *
77 - * @since 1.8
197 + * @since 3.4
78 198 *
79 - * @param object $language
199 + * @param array $additional_data Array of language additional data.
200 + * @param array $language Language data.
201 + * @return array Language data with home and search URLs added.
80 202 */
81 - protected function set_home_url( $language ) {
82 - // We should always have a default language here, except, temporarily, in PHPUnit tests. The test here protects against PHP notices.
83 - if ( isset( $this->options['default_lang'] ) ) {
84 - $search_url = $this->home_url( $language );
85 - $home_url = empty( $language->page_on_front ) || $this->options['redirect_lang'] ? $search_url : $this->front_page_url( $language );
86 - $language->set_home_url( $search_url, $home_url );
87 - }
203 + public function set_language_home_urls( $additional_data, $language ) {
204 + $language = array_merge( $language, $additional_data );
205 + $additional_data['search_url'] = $this->set_language_search_url( '', $language );
206 + $additional_data['home_url'] = $this->set_language_home_url( '', $language );
207 +
208 + return $additional_data;
88 209 }
89 210
90 211 /**
91 - * Sets the home urls and flags before the languages are persistently cached
212 + * Adds our domains or subdomains to allowed hosts for safe redirect.
92 213 *
93 - * @since 1.8
214 + * @since 1.4.3
94 215 *
95 - * @param array $languages array of PLL_Language objects
96 - * @return array
216 + * @param string[] $hosts Allowed hosts.
217 + * @return string[] Modified list of allowed hosts.
97 218 */
98 - public function pll_languages_list( $languages ) {
99 - foreach ( $languages as $language ) {
100 - $this->set_home_url( $language );
101 - $language->set_flag();
219 + public function allowed_redirect_hosts( $hosts ) {
220 + return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) );
221 + }
222 +
223 + /**
224 + * Returns language home URL property according to the current domain.
225 + *
226 + * @since 3.4.4
227 + *
228 + * @param string $url Home URL.
229 + * @param array $language Array of language props.
230 + * @return string Filtered home URL.
231 + */
232 + public function set_language_home_url( $url, $language ) {
233 + if ( empty( $language['page_on_front'] ) || $this->options['redirect_lang'] ) {
234 + return $this->home_url( $language['slug'] );
102 235 }
103 - return $languages;
236 +
237 + return $this->front_page_url( $language );
104 238 }
105 239
106 240 /**
107 - * Sets the home urls when not cached
108 - * Sets the home urls scheme
241 + * Returns language search URL property according to the current domain.
109 242 *
110 - * @since 1.8
243 + * @since 3.4.4
111 244 *
112 - * @param array $languages array of PLL_Language objects
113 - * @return array
245 + * @param string $url Search URL.
246 + * @param array $language Array of language props.
247 + * @return string Filtered search URL.
114 248 */
115 - public function pll_after_languages_cache( $languages ) {
116 - foreach ( $languages as $language ) {
117 - // Get the home urls when not cached
118 - if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || ( defined( 'PLL_CACHE_HOME_URL' ) && ! PLL_CACHE_HOME_URL ) ) {
119 - $this->set_home_url( $language );
120 - }
121 -
122 - // Ensures that the ( possibly cached ) home and flag urls use the right scheme http or https.
123 - $language->set_url_scheme();
124 - }
125 - return $languages;
249 + public function set_language_search_url( $url, $language ) {
250 + return $this->home_url( $language['slug'] );
126 251 }
127 252
128 253 /**
129 - * Adds our domains or subdomains to allowed hosts for safe redirection
254 + * Used to remove hooks in child classes, called when switching blog @see {PLL_Base::switch_blog()}.
255 + * Does nothing by default.
130 256 *
131 - * @since 1.4.3
257 + * @since 3.5
132 258 *
133 - * @param array $hosts allowed hosts
134 - * @return array
259 + * @return void
135 260 */
136 - public function allowed_redirect_hosts( $hosts ) {
137 - return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) );
261 + public function remove_filters() {
262 + self::$can_filter_rewrite_rules = false;
138 263 }
139 264 }