/** * Set the current language: if not found in the URL or REFERER, then keep the default * @since 1.1.1 */ public function init_current_language() { /** * Keep the default language if any of the code before does not detect another one. */ $this->language = $this->default_language; /** * Theoretically, we might not have any URL to get the language info from. */ $url_to_check = ''; if (WPGlobus_WP::is_doing_ajax()) { /** * If DOING_AJAX, we cannot retrieve the language information from the URL, * because it's always `admin-ajax`. * Therefore, we'll rely on the HTTP_REFERER (if it exists). */ if (!empty($_SERVER['HTTP_REFERER'])) { $url_to_check = $_SERVER['HTTP_REFERER']; } } else { /** * If not AJAX and not ADMIN then we are at the front. Will use the current URL. */ if (!is_admin()) { $url_to_check = WPGlobus_Utils::current_url(); } } /** * If we have an URL, extract language from it. * If extracted, set it as a current. */ if ($url_to_check) { $language_from_url = WPGlobus_Utils::extract_language_from_url($url_to_check); if ($language_from_url) { $this->language = $language_from_url; } } }
/** * @covers WPGlobus_Utils::extract_language_from_url */ public function test_extract_language_from_url() { /** * Mock object sent as a parameter, because we do now have access to the actual config. * * @var WPGlobus_Config $config */ $config = $this->getMock('WPGlobus_Config'); /** * These languages are enabled */ $config->enabled_languages = array('en', 'ru', 'pt'); /** * This is the current language */ $config->language = 'pt'; /** * This is the default language */ $config->default_language = 'en'; /** * This says "Do not use language code in the default URL" * So, no /en/page/, just /page/ */ $config->hide_default_language = true; self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('http://example.com/ru/page/', $config)); self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('https://example.com/ru/page/', $config)); self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('https://develop.example.com/ru/page/', $config)); self::assertEquals('pt', WPGlobus_Utils::extract_language_from_url('http://www.example.com/pt/page/', $config)); // Unknown language self::assertEquals('', WPGlobus_Utils::extract_language_from_url('http://www.example.com/ar/page/', $config)); // Default language or no language self::assertEquals('', WPGlobus_Utils::extract_language_from_url('http://www.example.com/page/', $config)); // Default language, but specified in the URL for some reason - returns it self::assertEquals('en', WPGlobus_Utils::extract_language_from_url('http://www.example.com/en/page/', $config)); // Wrong position self::assertEquals('', WPGlobus_Utils::extract_language_from_url('http://www.example.com/page/ru/something', $config)); // TODO Not sure about this. PHP manual says it should not work. self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('/ru/something', $config)); self::assertEquals('', WPGlobus_Utils::extract_language_from_url(3.14, $config)); self::assertEquals('', WPGlobus_Utils::extract_language_from_url(array(1, 'pi'), $config)); // No trailing slash self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('/ru', $config)); self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('/ru?a=b', $config)); self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('/ru/?a=b', $config)); self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('/ru/#hash', $config)); self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('/ru#hash', $config)); // Site in subfolder self::$option_home = 'http://www.example.com/subfolder'; self::assertEquals('ru', WPGlobus_Utils::extract_language_from_url('http://www.example.com/subfolder/ru/something', $config)); }