PluginProbe
Polylang / 3.7.3
Polylang v3.7.3
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 / links-model.php

links-model.php in Polylang 3.7.3, at include/links-model.php

265 lines 6.5 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 * Links model abstract class.
8 *
9 * @since 1.5
10 */
11 abstract class PLL_Links_Model {
12 /**
13 * True if the child class uses pretty permalinks, false otherwise.
14 *
15 * @var bool
16 */
17 public $using_permalinks;
18
19 /**
20 * Stores the plugin options.
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 *
48 * @since 1.5
49 *
50 * @param PLL_Model $model PLL_Model instance.
51 */
52 public function __construct( &$model ) {
53 $this->model = &$model;
54 $this->options = &$model->options;
55
56 $this->home = home_url();
57
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 );
60
61 // Adds our domains or subdomains to allowed hosts for safe redirection.
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 }
73 }
74
75 /**
76 * Initializes the links model.
77 * Does nothing by default.
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 *
120 * @since 1.5
121 *
122 * @param string $url The url to modify.
123 * @param int $page The page number.
124 * @return string The modified url.
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 */
159 public function switch_language_in_link( $url, $lang ) {
160 $url = $this->remove_language_from_link( $url );
161 return $this->add_language_to_link( $url, $lang );
162 }
163
164 /**
165 * Get the hosts managed on the website.
166 *
167 * @since 1.5
168 *
169 * @return string[] The list of hosts.
170 */
171 public function get_hosts() {
172 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
173 }
174
175 /**
176 * Returns the home url in a given language.
177 *
178 * @since 1.3.1
179 * @since 3.4 Accepts now a language slug.
180 *
181 * @param PLL_Language|string $language Language object or slug.
182 * @return string
183 */
184 public function home_url( $language ) {
185 if ( $language instanceof PLL_Language ) {
186 $language = $language->slug;
187 }
188
189 $url = trailingslashit( $this->home );
190
191 return $this->options['hide_default'] && $language === $this->options['default_lang'] ? $url : $this->add_language_to_link( $url, $language );
192 }
193
194 /**
195 * Adds home and search URLs to language data before the object is created.
196 *
197 * @since 3.4
198 *
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.
202 */
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;
209 }
210
211 /**
212 * Adds our domains or subdomains to allowed hosts for safe redirect.
213 *
214 * @since 1.4.3
215 *
216 * @param string[] $hosts Allowed hosts.
217 * @return string[] Modified list of allowed hosts.
218 */
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'] );
235 }
236
237 return $this->front_page_url( $language );
238 }
239
240 /**
241 * Returns language search URL property according to the current domain.
242 *
243 * @since 3.4.4
244 *
245 * @param string $url Search URL.
246 * @param array $language Array of language props.
247 * @return string Filtered search URL.
248 */
249 public function set_language_search_url( $url, $language ) {
250 return $this->home_url( $language['slug'] );
251 }
252
253 /**
254 * Used to remove hooks in child classes, called when switching blog @see {PLL_Base::switch_blog()}.
255 * Does nothing by default.
256 *
257 * @since 3.5
258 *
259 * @return void
260 */
261 public function remove_filters() {
262 self::$can_filter_rewrite_rules = false;
263 }
264 }
265