PluginProbe
Polylang / 2.7
Polylang v2.7
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 2.7, at include/links-model.php

137 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( wp_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 // We should always have a default language here, except, temporarily, in PHPUnit tests. The test here protects against PHP notices.
80 if ( isset( $this->options['default_lang'] ) ) {
81 $search_url = $this->home_url( $language );
82 $home_url = empty( $language->page_on_front ) || $this->options['redirect_lang'] ? $search_url : $this->front_page_url( $language );
83 $language->set_home_url( $search_url, $home_url );
84 }
85 }
86
87 /**
88 * Sets the home urls and flags before the languages are persistently cached
89 *
90 * @since 1.8
91 *
92 * @param array $languages array of PLL_Language objects
93 * @return array
94 */
95 public function pll_languages_list( $languages ) {
96 foreach ( $languages as $language ) {
97 $this->set_home_url( $language );
98 $language->set_flag();
99 }
100 return $languages;
101 }
102
103 /**
104 * Sets the home urls when not cached
105 * Sets the home urls scheme
106 *
107 * @since 1.8
108 *
109 * @param array $languages array of PLL_Language objects
110 * @return array
111 */
112 public function pll_after_languages_cache( $languages ) {
113 foreach ( $languages as $language ) {
114 // Get the home urls when not cached
115 if ( ( defined( 'PLL_CACHE_LANGUAGES' ) && ! PLL_CACHE_LANGUAGES ) || ( defined( 'PLL_CACHE_HOME_URL' ) && ! PLL_CACHE_HOME_URL ) ) {
116 $this->set_home_url( $language );
117 }
118
119 // Ensures that the ( possibly cached ) home url uses the right scheme http or https
120 $language->set_home_url_scheme();
121 }
122 return $languages;
123 }
124
125 /**
126 * Adds our domains or subdomains to allowed hosts for safe redirection
127 *
128 * @since 1.4.3
129 *
130 * @param array $hosts allowed hosts
131 * @return array
132 */
133 public function allowed_redirect_hosts( $hosts ) {
134 return array_unique( array_merge( $hosts, array_values( $this->get_hosts() ) ) );
135 }
136 }
137