| 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 |
} else { |
| 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 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Assigns the current language to the default language when the sitemap url |
| 81 |
* doesn't include any language. |
| 82 |
* |
| 83 |
* @since 2.8 |
| 84 |
* |
| 85 |
* @param string|bool $lang Current language code, false if not set yet. |
| 86 |
* @param object $query Main WP query object. |
| 87 |
* @return string|bool |
| 88 |
*/ |
| 89 |
public function set_language_from_query( $lang, $query ) { |
| 90 |
if ( isset( $query->query['sitemap'] ) && empty( $query->query['lang'] ) ) { |
| 91 |
$lang = $this->options['default_lang']; |
| 92 |
} |
| 93 |
return $lang; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Whitelists the home url filter for the sitemaps |
| 98 |
* |
| 99 |
* @since 2.8 |
| 100 |
* |
| 101 |
* @param array $whitelist White list. |
| 102 |
* @return array; |
| 103 |
*/ |
| 104 |
public function home_url_white_list( $whitelist ) { |
| 105 |
$whitelist[] = array( 'file' => 'class-wp-sitemaps-posts' ); |
| 106 |
return $whitelist; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Filters the sitemaps rewrite rules to take the languages into account. |
| 111 |
* |
| 112 |
* @since 2.8 |
| 113 |
* |
| 114 |
* @param array $rules Rewrite rules. |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function rewrite_rules( $rules ) { |
| 118 |
global $wp_rewrite; |
| 119 |
|
| 120 |
$newrules = array(); |
| 121 |
|
| 122 |
$languages = $this->model->get_languages_list( array( 'fields' => 'slug' ) ); |
| 123 |
if ( $this->options['hide_default'] ) { |
| 124 |
$languages = array_diff( $languages, array( $this->options['default_lang'] ) ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( ! empty( $languages ) ) { |
| 128 |
$slug = $wp_rewrite->root . ( $this->options['rewrite'] ? '^' : '^language/' ) . '(' . implode( '|', $languages ) . ')/'; |
| 129 |
} |
| 130 |
|
| 131 |
foreach ( $rules as $key => $rule ) { |
| 132 |
if ( false !== strpos( $rule, 'sitemap=$matches[1]' ) ) { |
| 133 |
$newrules[ str_replace( '^wp-sitemap', $slug . 'wp-sitemap', $key ) ] = str_replace( |
| 134 |
array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), |
| 135 |
array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ), |
| 136 |
$rule |
| 137 |
); // Should be enough! |
| 138 |
} |
| 139 |
|
| 140 |
$newrules[ $key ] = $rule; |
| 141 |
} |
| 142 |
return $newrules; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Replaces a sitemap provider by our decorator. |
| 147 |
* |
| 148 |
* @since 2.8 |
| 149 |
* |
| 150 |
* @param WP_Sitemaps_Provider $provider Instance of a WP_Sitemaps_Provider. |
| 151 |
* @return WP_Sitemaps_Provider |
| 152 |
*/ |
| 153 |
public function replace_provider( $provider ) { |
| 154 |
if ( $provider instanceof WP_Sitemaps_Provider ) { |
| 155 |
$provider = new PLL_Multilingual_Sitemaps_Provider( $provider, $this->links_model ); |
| 156 |
} |
| 157 |
return $provider; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Filters the sitemap index entries for subdomains and multiple domains. |
| 162 |
* |
| 163 |
* @since 2.8 |
| 164 |
* |
| 165 |
* @param array $sitemap_entry Sitemap entry for the post. |
| 166 |
* return array |
| 167 |
*/ |
| 168 |
public function index_entry( $sitemap_entry ) { |
| 169 |
$sitemap_entry['loc'] = $this->links_model->site_url( $sitemap_entry['loc'] ); |
| 170 |
return $sitemap_entry; |
| 171 |
} |
| 172 |
} |
| 173 |
|