sitepress = $sitepress; $this->url_converter = $url_converter; $this->is_current_request_root_callback = $is_current_request_root_callback; } public function add_hooks() { $urls = $this->sitepress->get_setting( 'urls' ); if ( WPML_LANGUAGE_NEGOTIATION_TYPE_DIRECTORY === (int) $this->sitepress->get_setting( 'language_negotiation_type' ) && ! empty( $urls['directory_for_default_language'] ) ) { add_action( 'template_redirect', array( $this, 'redirect_pages_from_root_to_default_lang_dir' ) ); } if ( isset( $_SERVER['SERVER_SOFTWARE'] ) && strpos( strtolower( $_SERVER['SERVER_SOFTWARE'] ), 'nginx' ) !== false ) { add_action( 'redirect_canonical', array( $this, 'maybe_fix_nginx_redirection_callback' ) ); } } public function redirect_pages_from_root_to_default_lang_dir() { if ( is_page() && ! call_user_func( $this->is_current_request_root_callback ) ) { $lang = $this->sitepress->get_current_language(); $current_uri = $_SERVER['REQUEST_URI']; $abs_home = $this->url_converter->get_abs_home(); $install_subdir = wpml_parse_url( $abs_home, PHP_URL_PATH ); $actual_uri = preg_replace( '#^' . $install_subdir . '#', '', $current_uri ); $actual_uri = '/' . ltrim( $actual_uri, '/' ); if ( 0 !== strpos( $actual_uri, '/' . $lang . '/' ) ) { $canonical_uri = trailingslashit( $install_subdir ) . $lang . $actual_uri; $canonical_uri = user_trailingslashit( $canonical_uri ); $this->sitepress->get_wp_api()->wp_safe_redirect( $canonical_uri, 301 ); } } } /** * @param string $redirect * * @return bool|string */ public function maybe_fix_nginx_redirection_callback( $redirect ) { if ( is_front_page() ) { $redirect = false; } return $redirect; } }