PluginProbe
Polylang / 3.5
Polylang v3.5
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.5, at include/links-model.php

256 lines 6.3 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 * Constructor.
40 *
41 * @since 1.5
42 *
43 * @param PLL_Model $model PLL_Model instance.
44 */
45 public function __construct( &$model ) {
46 $this->model = &$model;
47 $this->options = &$model->options;
48
49 $this->home = home_url();
50
51 // 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).
52 add_filter( 'pll_additional_language_data', array( $this, 'set_language_home_urls' ), 10, 2 );
53
54 // Adds our domains or subdomains to allowed hosts for safe redirection.
55 add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
56
57 // Allows secondary domains for home and search URLs in `PLL_Language`.
58 add_filter( 'pll_language_home_url', array( $this, 'set_language_home_url' ), 10, 2 );
59 add_filter( 'pll_language_search_url', array( $this, 'set_language_search_url' ), 10, 2 );
60
61 if ( did_action( 'pll_init' ) ) {
62 $this->init();
63 } else {
64 add_action( 'pll_init', array( $this, 'init' ) );
65 }
66 }
67
68 /**
69 * Initializes the links model.
70 * Does nothing by default.
71 *
72 * @since 3.5
73 *
74 * @return void
75 */
76 public function init() {}
77
78 /**
79 * Adds the language code in url.
80 *
81 * @since 1.2
82 * @since 3.4 Accepts now a language slug.
83 *
84 * @param string $url The url to modify.
85 * @param PLL_Language|string|false $lang Language object or slug.
86 * @return string The modified url.
87 */
88 abstract public function add_language_to_link( $url, $lang );
89
90 /**
91 * Returns the url without language code.
92 *
93 * @since 1.2
94 *
95 * @param string $url The url to modify.
96 * @return string The modified url.
97 */
98 abstract public function remove_language_from_link( $url );
99
100 /**
101 * Returns the link to the first page.
102 *
103 * @since 1.2
104 *
105 * @param string $url The url to modify.
106 * @return string The modified url.
107 */
108 abstract public function remove_paged_from_link( $url );
109
110 /**
111 * Returns the link to a paged page.
112 *
113 * @since 1.5
114 *
115 * @param string $url The url to modify.
116 * @param int $page The page number.
117 * @return string The modified url.
118 */
119 abstract public function add_paged_to_link( $url, $page );
120
121 /**
122 * Returns the language based on the language code in the url.
123 *
124 * @since 1.2
125 * @since 2.0 Add the $url argument.
126 *
127 * @param string $url Optional, defaults to the current url.
128 * @return string The language slug.
129 */
130 abstract public function get_language_from_url( $url = '' );
131
132 /**
133 * Returns the static front page url in a given language.
134 *
135 * @since 1.8
136 * @since 3.4 Accepts now an array of language properties.
137 *
138 * @param PLL_Language|array $language Language object or array of language properties.
139 * @return string The static front page url.
140 */
141 abstract public function front_page_url( $language );
142
143 /**
144 * Changes the language code in url.
145 *
146 * @since 1.5
147 *
148 * @param string $url The url to modify.
149 * @param PLL_Language $lang The language object.
150 * @return string The modified url.
151 */
152 public function switch_language_in_link( $url, $lang ) {
153 $url = $this->remove_language_from_link( $url );
154 return $this->add_language_to_link( $url, $lang );
155 }
156
157 /**
158 * Get the hosts managed on the website.
159 *
160 * @since 1.5
161 *
162 * @return string[] The list of hosts.
163 */
164 public function get_hosts() {
165 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
166 }
167
168 /**
169 * Returns the home url in a given language.
170 *
171 * @since 1.3.1
172 * @since 3.4 Accepts now a language slug.
173 *
174 * @param PLL_Language|string $language Language object or slug.
175 * @return string
176 */
177 public function home_url( $language ) {
178 if ( $language instanceof PLL_Language ) {
179 $language = $language->slug;
180 }
181
182 $url = trailingslashit( $this->home );
183
184 return $this->options['hide_default'] && $language === $this->options['default_lang'] ? $url : $this->add_language_to_link( $url, $language );
185 }
186
187 /**
188 * Adds home and search URLs to language data before the object is created.
189 *
190 * @since 3.4
191 *
192 * @param array $additional_data Array of language additional data.
193 * @param array $language Language data.
194 * @return array Language data with home and search URLs added.
195 */
196 public function set_language_home_urls( $additional_data, $language ) {
197 $language = array_merge( $language, $additional_data );
198 $additional_data['search_url'] = $this->set_language_search_url( '', $language );
199 $additional_data['home_url'] = $this->set_language_home_url( '', $language );
200
201 return $additional_data;
202 }
203
204 /**
205 * Adds our domains or subdomains to allowed hosts for safe redirect.
206 *
207 * @since 1.4.3
208 *
209 * @param string[] $hosts Allowed hosts.
210 * @return string[] Modified list of allowed hosts.
211 */
212 public function allowed_redirect_hosts( $hosts ) {
213 return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) );
214 }
215
216 /**
217 * Returns language home URL property according to the current domain.
218 *
219 * @since 3.4.4
220 *
221 * @param string $url Home URL.
222 * @param array $language Array of language props.
223 * @return string Filtered home URL.
224 */
225 public function set_language_home_url( $url, $language ) {
226 if ( empty( $language['page_on_front'] ) || $this->options['redirect_lang'] ) {
227 return $this->home_url( $language['slug'] );
228 }
229
230 return $this->front_page_url( $language );
231 }
232
233 /**
234 * Returns language search URL property according to the current domain.
235 *
236 * @since 3.4.4
237 *
238 * @param string $url Search URL.
239 * @param array $language Array of language props.
240 * @return string Filtered search URL.
241 */
242 public function set_language_search_url( $url, $language ) {
243 return $this->home_url( $language['slug'] );
244 }
245
246 /**
247 * Used to remove hooks in child classes, called when switching blog @see {PLL_Base::switch_blog()}.
248 * Does nothing by default.
249 *
250 * @since 3.5
251 *
252 * @return void
253 */
254 public function remove_filters() {}
255 }
256