PluginProbe
Polylang / 3.7.7
Polylang v3.7.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 +188 -60 2.7.33.7.7 View file →
@@ -1,45 +1,162 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 - * Links model abstract class
7 + * Links model abstract class.
5 8 *
6 9 * @since 1.5
7 10 */
8 11 abstract class PLL_Links_Model {
9 - public $model, $options;
10 - 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 + */
11 17 public $using_permalinks;
12 18
13 19 /**
14 - * Constructor
20 + * Stores the plugin options.
15 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 + *
16 48 * @since 1.5
17 49 *
18 - * @param object $model PLL_Model instance
50 + * @param PLL_Model $model PLL_Model instance.
19 51 */
20 52 public function __construct( &$model ) {
21 - $this->model = &$model;
53 + $this->model = &$model;
22 54 $this->options = &$model->options;
23 55
24 56 $this->home = home_url();
25 57
26 - add_filter( 'pll_languages_list', array( $this, 'pll_languages_list' ), 4 ); // after PLL_Static_Pages
27 - 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 );
28 60
29 - // adds our domains or subdomains to allowed hosts for safe redirection
61 + // Adds our domains or subdomains to allowed hosts for safe redirection.
30 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 + }
31 73 }
32 74
33 75 /**
34 - * Changes the language code in url
76 + * Initializes the links model.
77 + * Does nothing by default.
35 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 + *
36 120 * @since 1.5
37 121 *
38 - * @param string $url url to modify
39 - * @param object $lang language
40 - * @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.
41 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 + */
42 159 public function switch_language_in_link( $url, $lang ) {
43 160 $url = $this->remove_language_from_link( $url );
44 161 return $this->add_language_to_link( $url, $lang );
45 162 }
@@ -44,13 +161,13 @@
44 161 return $this->add_language_to_link( $url, $lang );
45 162 }
46 163
47 164 /**
48 - * Get hosts managed on the website
165 + * Get the hosts managed on the website.
49 166 *
50 167 * @since 1.5
51 168 *
52 - * @return array list of hosts
169 + * @return string[] The list of hosts.
53 170 */
54 171 public function get_hosts() {
55 172 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
56 173 }
@@ -55,82 +172,93 @@
55 172 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
56 173 }
57 174
58 175 /**
59 - * Returns the home url
176 + * Returns the home url in a given language.
60 177 *
61 178 * @since 1.3.1
179 + * @since 3.4 Accepts now a language slug.
62 180 *
63 - * @param object $lang PLL_Language object
181 + * @param PLL_Language|string $language Language object or slug.
64 182 * @return string
65 183 */
66 - public function home_url( $lang ) {
184 + public function home_url( $language ) {
185 + if ( $language instanceof PLL_Language ) {
186 + $language = $language->slug;
187 + }
188 +
67 189 $url = trailingslashit( $this->home );
68 - 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 );
69 192 }
70 193
71 194 /**
72 - * Sets the home urls
195 + * Adds home and search URLs to language data before the object is created.
73 196 *
74 - * @since 1.8
197 + * @since 3.4
75 198 *
76 - * @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.
77 202 */
78 - protected function set_home_url( $language ) {
79 - // We should always have a default language here, except, temporarily, in PHPUnit tests. The test here protects against PHP notices.
80 - if ( isset( $this->options['default_lang'] ) ) {
81 - $search_url = $this->home_url( $language );
82 - $home_url = empty( $language->page_on_front ) || $this->options['redirect_lang'] ? $search_url : $this->front_page_url( $language );
83 - $language->set_home_url( $search_url, $home_url );
84 - }
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;
85 209 }
86 210
87 211 /**
88 - * Sets the home urls and flags before the languages are persistently cached
212 + * Adds our domains or subdomains to allowed hosts for safe redirect.
89 213 *
90 - * @since 1.8
214 + * @since 1.4.3
91 215 *
92 - * @param array $languages array of PLL_Language objects
93 - * @return array
216 + * @param string[] $hosts Allowed hosts.
217 + * @return string[] Modified list of allowed hosts.
94 218 */
95 - public function pll_languages_list( $languages ) {
96 - foreach ( $languages as $language ) {
97 - $this->set_home_url( $language );
98 - $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'] );
99 235 }
100 - return $languages;
236 +
237 + return $this->front_page_url( $language );
101 238 }
102 239
103 240 /**
104 - * Sets the home urls when not cached
105 - * Sets the home urls scheme
241 + * Returns language search URL property according to the current domain.
106 242 *
107 - * @since 1.8
243 + * @since 3.4.4
108 244 *
109 - * @param array $languages array of PLL_Language objects
110 - * @return array
245 + * @param string $url Search URL.
246 + * @param array $language Array of language props.
247 + * @return string Filtered search URL.
111 248 */
112 - public function pll_after_languages_cache( $languages ) {
113 - foreach ( $languages as $language ) {
114 - // Get the home urls when not cached
115 - if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || ( defined( 'PLL_CACHE_HOME_URL' ) && ! PLL_CACHE_HOME_URL ) ) {
116 - $this->set_home_url( $language );
117 - }
118 -
119 - // Ensures that the ( possibly cached ) home url uses the right scheme http or https
120 - $language->set_home_url_scheme();
121 - }
122 - return $languages;
249 + public function set_language_search_url( $url, $language ) {
250 + return $this->home_url( $language['slug'] );
123 251 }
124 252
125 253 /**
126 - * 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.
127 256 *
128 - * @since 1.4.3
257 + * @since 3.5
129 258 *
130 - * @param array $hosts allowed hosts
131 - * @return array
259 + * @return void
132 260 */
133 - public function allowed_redirect_hosts( $hosts ) {
134 - return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) );
261 + public function remove_filters() {
262 + self::$can_filter_rewrite_rules = false;
135 263 }
136 264 }