PluginProbe
Polylang / 3.1.4
Polylang v3.1.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
← All changes | include/links-model.php +120 -31 2.7.23.1.4 View file →
@@ -1,45 +1,133 @@
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 + * Constructor.
40 + *
16 41 * @since 1.5
17 42 *
18 - * @param object $model PLL_Model instance
43 + * @param PLL_Model $model PLL_Model instance.
19 44 */
20 45 public function __construct( &$model ) {
21 - $this->model = &$model;
46 + $this->model = &$model;
22 47 $this->options = &$model->options;
23 48
24 49 $this->home = home_url();
25 50
26 - add_filter( 'pll_languages_list', array( $this, 'pll_languages_list' ), 4 ); // after PLL_Static_Pages
51 + add_filter( 'pll_languages_list', array( $this, 'pll_languages_list' ), 4 ); // After PLL_Static_Pages.
27 52 add_filter( 'pll_after_languages_cache', array( $this, 'pll_after_languages_cache' ) );
28 53
29 - // adds our domains or subdomains to allowed hosts for safe redirection
54 + // Adds our domains or subdomains to allowed hosts for safe redirection.
30 55 add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
31 56 }
32 57
33 58 /**
34 - * Changes the language code in url
59 + * Adds the language code in url.
35 60 *
61 + * @since 1.2
62 + *
63 + * @param string $url The url to modify.
64 + * @param PLL_Language $lang The language object.
65 + * @return string Modified url.
66 + */
67 + abstract public function add_language_to_link( $url, $lang );
68 +
69 + /**
70 + * Returns the url without language code.
71 + *
72 + * @since 1.2
73 + *
74 + * @param string $url The url to modify.
75 + * @return string Modified url.
76 + */
77 + abstract public function remove_language_from_link( $url );
78 +
79 + /**
80 + * Returns the link to the first page.
81 + *
82 + * @since 1.2
83 + *
84 + * @param string $url The url to modify.
85 + * @return string Modified url.
86 + */
87 + abstract public function remove_paged_from_link( $url );
88 +
89 + /**
90 + * Returns the link to a paged page.
91 + *
36 92 * @since 1.5
37 93 *
38 - * @param string $url url to modify
39 - * @param object $lang language
40 - * @return string modified url
94 + * @param string $url The url to modify.
95 + * @param int $page The page number.
96 + * @return string Modified url.
41 97 */
98 + abstract public function add_paged_to_link( $url, $page );
99 +
100 + /**
101 + * Returns the language based on language code in url.
102 + *
103 + * @since 1.2
104 + * @since 2.0 add $url argument.
105 + *
106 + * @param string $url Optional, defaults to thej current url.
107 + * @return string Language slug.
108 + */
109 + abstract public function get_language_from_url( $url = '' );
110 +
111 + /**
112 + * Returns the static front page url.
113 + *
114 + * @since 1.8
115 + *
116 + * @param PLL_Language $lang The language object.
117 + * @return string The static front page url.
118 + */
119 + abstract public function front_page_url( $lang );
120 +
121 + /**
122 + * Changes the language code in url.
123 + *
124 + * @since 1.5
125 + *
126 + * @param string $url The url to modify.
127 + * @param PLL_Language $lang The language object.
128 + * @return string Modified url.
129 + */
42 130 public function switch_language_in_link( $url, $lang ) {
43 131 $url = $this->remove_language_from_link( $url );
44 132 return $this->add_language_to_link( $url, $lang );
45 133 }
@@ -44,13 +132,13 @@
44 132 return $this->add_language_to_link( $url, $lang );
45 133 }
46 134
47 135 /**
48 - * Get hosts managed on the website
136 + * Get hosts managed on the website.
49 137 *
50 138 * @since 1.5
51 139 *
52 - * @return array list of hosts
140 + * @return string[] The list of hosts.
53 141 */
54 142 public function get_hosts() {
55 143 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
56 144 }
@@ -55,13 +143,13 @@
55 143 return array( wp_parse_url( $this->home, PHP_URL_HOST ) );
56 144 }
57 145
58 146 /**
59 - * Returns the home url
147 + * Returns the home url in a given language.
60 148 *
61 149 * @since 1.3.1
62 150 *
63 - * @param object $lang PLL_Language object
151 + * @param PLL_Language $lang PLL_Language object.
64 152 * @return string
65 153 */
66 154 public function home_url( $lang ) {
67 155 $url = trailingslashit( $this->home );
@@ -68,13 +156,14 @@
68 156 return $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ? $url : $this->add_language_to_link( $url, $lang );
69 157 }
70 158
71 159 /**
72 - * Sets the home urls
160 + * Sets the home urls in PLL_Language.
73 161 *
74 162 * @since 1.8
75 163 *
76 - * @param object $language
164 + * @param PLL_Language $language PLL_Language object.
165 + * @return void
77 166 */
78 167 protected function set_home_url( $language ) {
79 168 // We should always have a default language here, except, temporarily, in PHPUnit tests. The test here protects against PHP notices.
80 169 if ( isset( $this->options['default_lang'] ) ) {
@@ -84,14 +173,14 @@
84 173 }
85 174 }
86 175
87 176 /**
88 - * Sets the home urls and flags before the languages are persistently cached
177 + * Sets the home urls and flags before the languages are persistently cached.
89 178 *
90 179 * @since 1.8
91 180 *
92 - * @param array $languages array of PLL_Language objects
93 - * @return array
181 + * @param PLL_Language[] $languages Array of PLL_Language objects.
182 + * @return PLL_Language[] Array of PLL_Language objects with home url and flag.
94 183 */
95 184 public function pll_languages_list( $languages ) {
96 185 foreach ( $languages as $language ) {
97 186 $this->set_home_url( $language );
@@ -100,36 +189,36 @@
100 189 return $languages;
101 190 }
102 191
103 192 /**
104 - * Sets the home urls when not cached
105 - * Sets the home urls scheme
193 + * Sets the home urls when not cached.
194 + * Sets the home urls scheme.
106 195 *
107 196 * @since 1.8
108 197 *
109 - * @param array $languages array of PLL_Language objects
110 - * @return array
198 + * @param PLL_Language[] $languages Array of PLL_Language objects.
199 + * @return PLL_Language[] Array of PLL_Language objects.
111 200 */
112 201 public function pll_after_languages_cache( $languages ) {
113 202 foreach ( $languages as $language ) {
114 - // Get the home urls when not cached
203 + // Get the home urls when not cached.
115 204 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || ( defined( 'PLL_CACHE_HOME_URL' ) && ! PLL_CACHE_HOME_URL ) ) {
116 205 $this->set_home_url( $language );
117 206 }
118 207
119 - // Ensures that the ( possibly cached ) home url uses the right scheme http or https
120 - $language->set_home_url_scheme();
208 + // Ensures that the ( possibly cached ) home and flag urls use the right scheme http or https.
209 + $language->set_url_scheme();
121 210 }
122 211 return $languages;
123 212 }
124 213
125 214 /**
126 - * Adds our domains or subdomains to allowed hosts for safe redirection
215 + * Adds our domains or subdomains to allowed hosts for safe redirect.
127 216 *
128 217 * @since 1.4.3
129 218 *
130 - * @param array $hosts allowed hosts
131 - * @return array
219 + * @param string[] $hosts Allowed hosts.
220 + * @return string[] Modified list of allowed hosts.
132 221 */
133 222 public function allowed_redirect_hosts( $hosts ) {
134 223 return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) );
135 224 }