| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages the compatibility with WordPress MU Domain Mapping. |
| 8 |
* |
| 9 |
* @since 2.8 |
| 10 |
*/ |
| 11 |
class PLL_Domain_Mapping { |
| 12 |
|
| 13 |
/** |
| 14 |
* Setups actions. |
| 15 |
* |
| 16 |
* @since 2.8 |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
if ( function_exists( 'redirect_to_mapped_domain' ) ) { |
| 20 |
$options = get_option( 'polylang' ); |
| 21 |
|
| 22 |
if ( is_array( $options ) && $options['force_lang'] < 2 ) { |
| 23 |
pll_set_constant( 'PLL_CACHE_HOME_URL', false ); |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! get_site_option( 'dm_no_primary_domain' ) ) { |
| 27 |
remove_action( 'template_redirect', 'redirect_to_mapped_domain' ); |
| 28 |
add_action( 'template_redirect', array( $this, 'dm_redirect_to_mapped_domain' ) ); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Fix primary domain check which forces only one domain per blog. |
| 35 |
* Accept only known domains/subdomains for the current blog. |
| 36 |
* |
| 37 |
* @since 2.2 |
| 38 |
*/ |
| 39 |
public function dm_redirect_to_mapped_domain() { |
| 40 |
// Don't go further if we stopped loading the plugin early (for example when deactivate-polylang=1). |
| 41 |
if ( ! function_exists( 'PLL' ) ) { |
| 42 |
// Rely on MU Domain Mapping. |
| 43 |
redirect_to_mapped_domain(); |
| 44 |
} |
| 45 |
|
| 46 |
// The language is set from the subdomain or domain name |
| 47 |
if ( PLL()->options['force_lang'] > 1 ) { |
| 48 |
// Don't redirect the main site |
| 49 |
if ( is_main_site() ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Don't redirect post previews |
| 54 |
if ( isset( $_GET['preview'] ) && 'true' === $_GET['preview'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Don't redirect theme customizer |
| 59 |
if ( isset( $_POST['customize'] ) && isset( $_POST['theme'] ) && 'on' === $_POST['customize'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// If we can't associate the requested domain to a language, redirect to the default domain |
| 64 |
$requested_url = pll_get_requested_url(); |
| 65 |
$requested_host = wp_parse_url( $requested_url, PHP_URL_HOST ); |
| 66 |
|
| 67 |
$hosts = PLL()->links_model->get_hosts(); |
| 68 |
$lang = array_search( $requested_host, $hosts ); |
| 69 |
|
| 70 |
if ( empty( $lang ) ) { |
| 71 |
$status = get_site_option( 'dm_301_redirect' ) ? '301' : '302'; // Honor status redirect option |
| 72 |
$redirect = str_replace( '://' . $requested_host, '://' . $hosts[ PLL()->options['default_lang'] ], $requested_url ); |
| 73 |
wp_safe_redirect( $redirect, $status ); |
| 74 |
exit; |
| 75 |
} |
| 76 |
} else { |
| 77 |
// Otherwise rely on MU Domain Mapping |
| 78 |
redirect_to_mapped_domain(); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|