PluginProbe
Polylang / 2.9.1
Polylang v2.9.1
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 / modules / sitemaps / sitemaps.php

sitemaps.php in Polylang 2.9.1, at modules/sitemaps/sitemaps.php

192 lines 4.6 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 * Handles the core sitemaps.
8 *
9 * @since 2.8
10 */
11 class PLL_Sitemaps {
12 /**
13 * A reference to the current language.
14 *
15 * @since 2.8
16 *
17 * @var PLL_Language
18 */
19 protected $curlang;
20
21 /**
22 * A reference to the PLL_Links_Model instance.
23 *
24 * @since 2.8
25 *
26 * @var PLL_Links_Model
27 */
28 protected $links_model;
29
30 /**
31 * A reference to the PLL_Model instance.
32 *
33 * @since 2.8
34 *
35 * @var PLL_Model
36 */
37 protected $model;
38
39 /**
40 * Stores the plugin options.
41 *
42 * @var array
43 */
44 protected $options;
45
46 /**
47 * Constructor.
48 *
49 * @since 2.8
50 *
51 * @param object $polylang Main Polylang object.
52 */
53 public function __construct( &$polylang ) {
54 $this->curlang = &$polylang->curlang;
55 $this->links_model = &$polylang->links_model;
56 $this->model = &$polylang->model;
57 $this->options = &$polylang->options;
58 }
59
60 /**
61 * Setups actions and filters.
62 *
63 * @since 2.8
64 */
65 public function init() {
66 add_filter( 'pll_home_url_white_list', array( $this, 'home_url_white_list' ) );
67
68 if ( $this->options['force_lang'] < 2 ) {
69 add_filter( 'pll_set_language_from_query', array( $this, 'set_language_from_query' ), 10, 2 );
70 add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules' ) );
71 add_filter( 'wp_sitemaps_add_provider', array( $this, 'replace_provider' ) );
72 } elseif ( method_exists( $this->links_model, 'site_url' ) ) {
73 add_filter( 'wp_sitemaps_index_entry', array( $this, 'index_entry' ) );
74 add_filter( 'wp_sitemaps_stylesheet_url', array( $this->links_model, 'site_url' ) );
75 add_filter( 'wp_sitemaps_stylesheet_index_url', array( $this->links_model, 'site_url' ) );
76 add_filter( 'home_url', array( $this, 'sitemap_url' ) );
77 }
78 }
79
80 /**
81 * Assigns the current language to the default language when the sitemap url
82 * doesn't include any language.
83 *
84 * @since 2.8
85 *
86 * @param string|bool $lang Current language code, false if not set yet.
87 * @param object $query Main WP query object.
88 * @return string|bool
89 */
90 public function set_language_from_query( $lang, $query ) {
91 if ( isset( $query->query['sitemap'] ) && empty( $query->query['lang'] ) ) {
92 $lang = $this->options['default_lang'];
93 }
94 return $lang;
95 }
96
97 /**
98 * Whitelists the home url filter for the sitemaps
99 *
100 * @since 2.8
101 *
102 * @param array $whitelist White list.
103 * @return array;
104 */
105 public function home_url_white_list( $whitelist ) {
106 $whitelist[] = array( 'file' => 'class-wp-sitemaps-posts' );
107 return $whitelist;
108 }
109
110 /**
111 * Filters the sitemaps rewrite rules to take the languages into account.
112 *
113 * @since 2.8
114 *
115 * @param array $rules Rewrite rules.
116 * @return array
117 */
118 public function rewrite_rules( $rules ) {
119 global $wp_rewrite;
120
121 $languages = $this->model->get_languages_list( array( 'fields' => 'slug' ) );
122
123 if ( empty( $languages ) ) {
124 return $rules;
125 }
126
127 if ( $this->options['hide_default'] ) {
128 $languages = array_diff( $languages, array( $this->options['default_lang'] ) );
129 }
130
131 $slug = $wp_rewrite->root . ( $this->options['rewrite'] ? '^' : '^language/' ) . '(' . implode( '|', $languages ) . ')/';
132
133 $newrules = array();
134
135 foreach ( $rules as $key => $rule ) {
136 if ( isset( $slug ) && false !== strpos( $rule, 'sitemap=$matches[1]' ) ) {
137 $newrules[ str_replace( '^wp-sitemap', $slug . 'wp-sitemap', $key ) ] = str_replace(
138 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
139 array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
140 $rule
141 ); // Should be enough!
142 }
143
144 $newrules[ $key ] = $rule;
145 }
146 return $newrules;
147 }
148
149 /**
150 * Replaces a sitemap provider by our decorator.
151 *
152 * @since 2.8
153 *
154 * @param WP_Sitemaps_Provider $provider Instance of a WP_Sitemaps_Provider.
155 * @return WP_Sitemaps_Provider
156 */
157 public function replace_provider( $provider ) {
158 if ( $provider instanceof WP_Sitemaps_Provider ) {
159 $provider = new PLL_Multilingual_Sitemaps_Provider( $provider, $this->links_model );
160 }
161 return $provider;
162 }
163
164 /**
165 * Filters the sitemap index entries for subdomains and multiple domains.
166 *
167 * @since 2.8
168 *
169 * @param array $sitemap_entry Sitemap entry for the post.
170 * return array
171 */
172 public function index_entry( $sitemap_entry ) {
173 $sitemap_entry['loc'] = $this->links_model->site_url( $sitemap_entry['loc'] );
174 return $sitemap_entry;
175 }
176
177 /**
178 * Makes sure that the sitemap urls are always evaluated on the current domain.
179 *
180 * @since 2.8.4
181 *
182 * @param string $url A sitemap url.
183 * @return string
184 */
185 public function sitemap_url( $url ) {
186 if ( false !== strpos( $url, '/wp-sitemap' ) ) {
187 $url = $this->links_model->site_url( $url );
188 }
189 return $url;
190 }
191 }
192