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

196 lines 4.8 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.
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
58 /**
59 * Adds the language code in url.
60 *
61 * @since 1.2
62 * @since 3.4 Accepts now a language slug.
63 *
64 * @param string $url The url to modify.
65 * @param PLL_Language|string|false $lang Language object or slug.
66 * @return string The modified url.
67 */
68 abstract public function add_language_to_link( $url, $lang );
69
70 /**
71 * Returns the url without language code.
72 *
73 * @since 1.2
74 *
75 * @param string $url The url to modify.
76 * @return string The modified url.
77 */
78 abstract public function remove_language_from_link( $url );
79
80 /**
81 * Returns the link to the first page.
82 *
83 * @since 1.2
84 *
85 * @param string $url The url to modify.
86 * @return string The modified url.
87 */
88 abstract public function remove_paged_from_link( $url );
89
90 /**
91 * Returns the link to a paged page.
92 *
93 * @since 1.5
94 *
95 * @param string $url The url to modify.
96 * @param int $page The page number.
97 * @return string The modified url.
98 */
99 abstract public function add_paged_to_link( $url, $page );
100
101 /**
102 * Returns the language based on the language code in the url.
103 *
104 * @since 1.2
105 * @since 2.0 Add the $url argument.
106 *
107 * @param string $url Optional, defaults to the current url.
108 * @return string The language slug.
109 */
110 abstract public function get_language_from_url( $url = '' );
111
112 /**
113 * Returns the static front page url in a given language.
114 *
115 * @since 1.8
116 * @since 3.4 Accepts now an array of language properties.
117 *
118 * @param PLL_Language|array $language Language object or array of language properties.
119 * @return string The static front page url.
120 */
121 abstract public function front_page_url( $language );
122
123 /**
124 * Changes the language code in url.
125 *
126 * @since 1.5
127 *
128 * @param string $url The url to modify.
129 * @param PLL_Language $lang The language object.
130 * @return string The modified url.
131 */
132 public function switch_language_in_link( $url, $lang ) {
133 $url = $this->remove_language_from_link( $url );
134 return $this->add_language_to_link( $url, $lang );
135 }
136
137 /**
138 * Get the hosts managed on the website.
139 *
140 * @since 1.5
141 *
142 * @return string[] The list of hosts.
143 */
144 public function get_hosts() {
145 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
146 }
147
148 /**
149 * Returns the home url in a given language.
150 *
151 * @since 1.3.1
152 * @since 3.4 Accepts now a language slug.
153 *
154 * @param PLL_Language|string $language Language object or slug.
155 * @return string
156 */
157 public function home_url( $language ) {
158 if ( $language instanceof PLL_Language ) {
159 $language = $language->slug;
160 }
161
162 $url = trailingslashit( $this->home );
163
164 return $this->options['hide_default'] && $language === $this->options['default_lang'] ? $url : $this->add_language_to_link( $url, $language );
165 }
166
167 /**
168 * Adds home and search URLs to language data before the object is created.
169 *
170 * @since 3.4
171 *
172 * @param array $additional_data Array of language additional data.
173 * @param array $language Language data.
174 * @return array Language data with home and search URLs added.
175 */
176 public function set_language_home_urls( $additional_data, $language ) {
177 $language = array_merge( $language, $additional_data );
178 $additional_data['search_url'] = $this->home_url( $language['slug'] );
179 $additional_data['home_url'] = empty( $language['page_on_front'] ) || $this->options['redirect_lang'] ? $additional_data['search_url'] : $this->front_page_url( $language );
180
181 return $additional_data;
182 }
183
184 /**
185 * Adds our domains or subdomains to allowed hosts for safe redirect.
186 *
187 * @since 1.4.3
188 *
189 * @param string[] $hosts Allowed hosts.
190 * @return string[] Modified list of allowed hosts.
191 */
192 public function allowed_redirect_hosts( $hosts ) {
193 return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) );
194 }
195 }
196