-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunrise.php
75 lines (62 loc) · 1.99 KB
/
sunrise.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Class MultilanguageSubsiteMapper
*/
class MultilanguageSubsiteMapper {
/**
* @var array
*/
private $sites_language_urls = [];
/**
* MultilanguageSubsiteMapper constructor
*/
public function __construct() {
add_filter( 'pre_get_site_by_path', [ $this, 'allow_language_subdomains' ], 10, 3 );
}
/**
* Associate non-default language domains with site IDs
*/
private function setup_sites_and_languages() {
global $wpdb;
foreach ( get_sites() as $site ) {
$prefix = $wpdb->get_blog_prefix( $site->blog_id ); // e.g. 'wp_'
$polylang_options = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$prefix}options WHERE option_name = %s LIMIT 1", 'polylang' ) );
if ( ! empty( $polylang_options->option_value ) ) {
$polylang_options = unserialize( $polylang_options->option_value );
if ( ! empty( $polylang_options['domains'] ) ) {
unset( $polylang_options['domains'][ $polylang_options['default_lang'] ] );
foreach ( $polylang_options['domains'] as $domain ) {
$this->sites_language_urls[ str_replace( [
'https://',
'http://',
], '', $domain ) ] = intval( $site->blog_id );
}
}
}
}
}
/**
* Associate language domains with multisite domain
*
* The multisite uses the default language domain.
* If the request is for a non-default language-specific domain,
* get the site object and overwrite the default domain with the language specific one
*
* @param $site
* @param string $domain
* @param string $path
*
* @return false|WP_Site
*/
public function allow_language_subdomains( $site, string $domain, string $path ) {
if ( empty( $this->sites_language_urls ) ) {
$this->setup_sites_and_languages();
}
if ( ( ! is_admin() || $path === '/wp/' ) && ! empty( $this->sites_language_urls[ $domain ] ) ) {
$site = WP_Site::get_instance( $this->sites_language_urls[ $domain ] );
$site->domain = $domain;
}
return $site;
}
}
new MultilanguageSubsiteMapper();