PluginProbe
Polylang / 3.2.2
Polylang v3.2.2
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 3.2.2, at modules/sitemaps/sitemaps.php

141 lines 3.3 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 for sites using a single domain.
8 *
9 * @since 2.8
10 */
11 class PLL_Sitemaps extends PLL_Abstract_Sitemaps {
12 /**
13 * @var PLL_Links_Model
14 */
15 protected $links_model;
16
17 /**
18 * @var PLL_Model
19 */
20 protected $model;
21
22 /**
23 * Stores the plugin options.
24 *
25 * @var array
26 */
27 protected $options;
28
29 /**
30 * Constructor.
31 *
32 * @since 2.8
33 *
34 * @param object $polylang Main Polylang object.
35 */
36 public function __construct( &$polylang ) {
37 $this->links_model = &$polylang->links_model;
38 $this->model = &$polylang->model;
39 $this->options = &$polylang->options;
40 }
41
42 /**
43 * Setups actions and filters.
44 *
45 * @since 2.8
46 *
47 * @return void
48 */
49 public function init() {
50 parent::init();
51
52 add_filter( 'pll_set_language_from_query', array( $this, 'set_language_from_query' ), 10, 2 );
53 add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules' ) );
54 add_filter( 'wp_sitemaps_add_provider', array( $this, 'replace_provider' ) );
55 }
56
57 /**
58 * Assigns the current language to the default language when the sitemap url
59 * doesn't include any language.
60 *
61 * @since 2.8
62 *
63 * @param string|bool $lang Current language code, false if not set yet.
64 * @param WP_Query $query Main WP query object.
65 * @return string|bool
66 */
67 public function set_language_from_query( $lang, $query ) {
68 if ( isset( $query->query['sitemap'] ) && empty( $query->query['lang'] ) ) {
69 $lang = $this->options['default_lang'];
70 }
71 return $lang;
72 }
73
74 /**
75 * Whitelists the home url filter for the sitemaps
76 *
77 * @since 2.8
78 *
79 * @param array $whitelist White list.
80 * @return array
81 */
82 public function home_url_white_list( $whitelist ) {
83 $whitelist[] = array( 'file' => 'class-wp-sitemaps-posts' );
84 return $whitelist;
85 }
86
87 /**
88 * Filters the sitemaps rewrite rules to take the languages into account.
89 *
90 * @since 2.8
91 *
92 * @param string[] $rules Rewrite rules.
93 * @return string[] Modified rewrite rules.
94 */
95 public function rewrite_rules( $rules ) {
96 global $wp_rewrite;
97
98 $languages = $this->model->get_languages_list( array( 'fields' => 'slug' ) );
99
100 if ( empty( $languages ) ) {
101 return $rules;
102 }
103
104 if ( $this->options['hide_default'] ) {
105 $languages = array_diff( $languages, array( $this->options['default_lang'] ) );
106 }
107
108 $slug = $wp_rewrite->root . ( $this->options['rewrite'] ? '^' : '^language/' ) . '(' . implode( '|', $languages ) . ')/';
109
110 $newrules = array();
111
112 foreach ( $rules as $key => $rule ) {
113 if ( false !== strpos( $rule, 'sitemap=$matches[1]' ) ) {
114 $newrules[ str_replace( '^wp-sitemap', $slug . 'wp-sitemap', $key ) ] = str_replace(
115 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
116 array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
117 $rule
118 ); // Should be enough!
119 }
120
121 $newrules[ $key ] = $rule;
122 }
123 return $newrules;
124 }
125
126 /**
127 * Replaces a sitemap provider by our decorator.
128 *
129 * @since 2.8
130 *
131 * @param WP_Sitemaps_Provider $provider Instance of a WP_Sitemaps_Provider.
132 * @return WP_Sitemaps_Provider
133 */
134 public function replace_provider( $provider ) {
135 if ( $provider instanceof WP_Sitemaps_Provider ) {
136 $provider = new PLL_Multilingual_Sitemaps_Provider( $provider, $this->links_model );
137 }
138 return $provider;
139 }
140 }
141