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