| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Links model abstract class |
| 5 |
* |
| 6 |
* @since 1.5 |
| 7 |
*/ |
| 8 |
abstract class PLL_Links_Model { |
| 9 |
public $model, $options; |
| 10 |
public $home; // used to store the home url before it is filtered |
| 11 |
public $using_permalinks; |
| 12 |
|
| 13 |
/** |
| 14 |
* Constructor |
| 15 |
* |
| 16 |
* @since 1.5 |
| 17 |
* |
| 18 |
* @param object $model PLL_Model instance |
| 19 |
*/ |
| 20 |
public function __construct( &$model ) { |
| 21 |
$this->model = &$model; |
| 22 |
$this->options = &$model->options; |
| 23 |
|
| 24 |
$this->home = home_url(); |
| 25 |
|
| 26 |
add_filter( 'pll_languages_list', array( $this, 'pll_languages_list' ), 4 ); // after PLL_Static_Pages |
| 27 |
add_filter( 'pll_after_languages_cache', array( $this, 'pll_after_languages_cache' ) ); |
| 28 |
|
| 29 |
// adds our domains or subdomains to allowed hosts for safe redirection |
| 30 |
add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Changes the language code in url |
| 35 |
* |
| 36 |
* @since 1.5 |
| 37 |
* |
| 38 |
* @param string $url url to modify |
| 39 |
* @param object $lang language |
| 40 |
* @return string modified url |
| 41 |
*/ |
| 42 |
public function switch_language_in_link( $url, $lang ) { |
| 43 |
$url = $this->remove_language_from_link( $url ); |
| 44 |
return $this->add_language_to_link( $url, $lang ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get hosts managed on the website |
| 49 |
* |
| 50 |
* @since 1.5 |
| 51 |
* |
| 52 |
* @return array list of hosts |
| 53 |
*/ |
| 54 |
public function get_hosts() { |
| 55 |
return array( parse_url( $this->home, PHP_URL_HOST ) ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the home url |
| 60 |
* |
| 61 |
* @since 1.3.1 |
| 62 |
* |
| 63 |
* @param object $lang PLL_Language object |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function home_url( $lang ) { |
| 67 |
$url = trailingslashit( $this->home ); |
| 68 |
return $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ? $url: $this->add_language_to_link( $url, $lang ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Sets the home urls |
| 73 |
* |
| 74 |
* @since 1.8 |
| 75 |
* |
| 76 |
* @param object $language |
| 77 |
*/ |
| 78 |
protected function set_home_url( $language ) { |
| 79 |
$search_url = $this->home_url( $language ); |
| 80 |
$home_url = empty( $language->page_on_front ) || $this->options['redirect_lang'] ? $search_url : $this->front_page_url( $language ); |
| 81 |
$language->set_home_url( $search_url, $home_url ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Sets the home urls before it is persistently cached |
| 86 |
* |
| 87 |
* @since 1.8 |
| 88 |
* |
| 89 |
* @param array $languages array of PLL_Language objects |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function pll_languages_list( $languages ) { |
| 93 |
foreach ( $languages as $language ) { |
| 94 |
$this->set_home_url( $language ); |
| 95 |
} |
| 96 |
return $languages; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sets the home urls when not cached |
| 101 |
* Sets the home urls scheme |
| 102 |
* |
| 103 |
* @since 1.8 |
| 104 |
* |
| 105 |
* @param array $languages array of PLL_Language objects |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function pll_after_languages_cache( $languages ) { |
| 109 |
foreach ( $languages as $language ) { |
| 110 |
// Get the home urls when not cached |
| 111 |
if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || ( defined( 'PLL_CACHE_HOME_URL' ) && ! PLL_CACHE_HOME_URL ) ) { |
| 112 |
$this->set_home_url( $language ); |
| 113 |
} |
| 114 |
|
| 115 |
// Ensures that the ( possibly cached ) home url uses the right scheme http or https |
| 116 |
$language->set_home_url_scheme(); |
| 117 |
} |
| 118 |
return $languages; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Adds our domains or subdomains to allowed hosts for safe redirection |
| 123 |
* |
| 124 |
* @since 1.4.3 |
| 125 |
* |
| 126 |
* @param array $hosts allowed hosts |
| 127 |
* @return array |
| 128 |
*/ |
| 129 |
public function allowed_redirect_hosts( $hosts ) { |
| 130 |
return array_unique( array_merge( $hosts, $this->get_hosts() ) ); |
| 131 |
} |
| 132 |
} |
| 133 |
|