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

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