/**
  * @param $url
  *
  * @return mixed
  */
 public static function sanitize_url($url)
 {
     $original_url_parsed = wpml_parse_url($url, PHP_URL_QUERY);
     parse_str($original_url_parsed, $original_query_vars);
     $sanitized_query_vars = self::sanitize_data($original_query_vars);
     return add_query_arg($sanitized_query_vars, $url);
 }
 /**
  * Gets the source_language $_GET parameter from the HTTP_REFERER
  *
  * @return string|bool
  */
 public function get_source_language_from_referer()
 {
     $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
     $query = wpml_parse_url($referer, PHP_URL_QUERY);
     parse_str($query, $query_parts);
     $source_lang = isset($query_parts['source_lang']) ? $query_parts['source_lang'] : false;
     return $source_lang;
 }
 /**
  * Filters the preview links on the post edit screen so that they always point to the currently used language
  * domain. This ensures that the user can actually see the preview, as he might not have the login cookie set for
  * the actual language domain of the post.
  *
  * @param string $link
  *
  * @return mixed
  */
 public function preview_post_link_filter($link)
 {
     $original_host = filter_var($_SERVER['HTTP_HOST'], FILTER_SANITIZE_STRING);
     if ($original_host) {
         $domain = wpml_parse_url($link, PHP_URL_HOST);
         $link = str_replace('//' . $domain . '/', '//' . $original_host . '/', $link);
     }
     return $link;
 }
 /**
  * Returns the language_code of the http referrer's location from which a request originated.
  * Used to correctly determine the language code on ajax link lists for the post edit screen or
  * the flat taxonomy auto-suggest.
  *
  * @return string|null
  */
 public function get_referrer_language_code()
 {
     if (!empty($_SERVER['HTTP_REFERER'])) {
         $query_string = wpml_parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
         $query = array();
         parse_str((string) $query_string, $query);
         $language_code = isset($query['lang']) ? $query['lang'] : null;
     }
     return isset($language_code) ? $language_code : null;
 }
 /**
  * Filters links to the root page, so that they are displayed properly in the front-end.
  *
  * @param $url
  *
  * @return string
  */
 public function filter_root_permalink($url)
 {
     $root_page_utils = $this->sitepress->get_root_page_utils();
     if ($root_page_utils->get_root_page_id() > 0 && $root_page_utils->is_url_root_page($url)) {
         $url_parts = wpml_parse_url($url);
         $query = isset($url_parts['query']) ? $url_parts['query'] : '';
         $path = isset($url_parts['path']) ? $url_parts['path'] : '';
         $slugs = array_filter(explode('/', $path));
         $last_slug = array_pop($slugs);
         $new_url = $this->url_converter->get_abs_home();
         $new_url = is_numeric($last_slug) ? trailingslashit(trailingslashit($new_url) . $last_slug) : $new_url;
         $query = $this->unset_page_query_vars($query);
         $new_url = trailingslashit($new_url);
         $url = (bool) $query === true ? trailingslashit($new_url) . '?' . $query : $new_url;
     }
     return $url;
 }
 /**
  *
  * @param string $url
  * @param bool   $only_admin If set to true only language parameters on Admin Screen URLs will be recognized. The
  *                           function will return null for non-Admin Screens.
  *
  * @return null|String Language code
  */
 protected function lang_by_param($url, $only_admin = true)
 {
     if (isset($this->cache[$url])) {
         return $this->cache[$url];
     }
     $url = wpml_strip_subdir_from_url($url);
     $url_query_parts = wpml_parse_url($url);
     $url_query = ($only_admin === false || isset($url_query_parts['path']) && strpos($url_query_parts['path'], '/wp-admin') === 0) && isset($url_query_parts['query']) ? untrailingslashit($url_query_parts['query']) : null;
     if (isset($url_query)) {
         parse_str($url_query, $vars);
         if (isset($vars['lang']) && ($only_admin === true && $vars['lang'] === 'all' || in_array($vars['lang'], $this->active_languages, true))) {
             $lang = $vars['lang'];
         }
     }
     $lang = isset($lang) ? $lang : null;
     $this->cache[$url] = $lang;
     return $lang;
 }
 public function filter_include_url($result)
 {
     $domains = wpml_get_setting_filter(array(), 'language_domains');
     $domains = preg_replace('#^(http(?:s?))://#', '', array_map('untrailingslashit', $domains));
     if ((bool) $domains === true) {
         $php_host_in_domain = wpml_parse_url($result, PHP_URL_HOST);
         $domains[] = wpml_parse_url($this->get_unfiltered_home(), PHP_URL_HOST);
         foreach ($domains as $dom) {
             if (strpos(trailingslashit($php_host_in_domain), trailingslashit($dom)) === 0) {
                 $http_host_parts = explode(':', $this->requested_host);
                 unset($http_host_parts[1]);
                 $http_host_without_port = implode($http_host_parts);
                 $result = str_replace($php_host_in_domain, $http_host_without_port, $result);
                 break;
             }
         }
     }
     return $result;
 }
 public function get_hosts($hosts)
 {
     $domains = $this->sitepress->get_setting('language_domains');
     $default_language = $this->sitepress->get_default_language();
     $default_home = $this->sitepress->convert_url($this->sitepress->get_wp_api()->get_home_url(), $default_language);
     $home_schema = wpml_parse_url($default_home, PHP_URL_SCHEME) . '://';
     if (!isset($domains[$default_language])) {
         $domains[$default_language] = wpml_parse_url($default_home, PHP_URL_HOST);
     }
     $active_languages = $this->sitepress->get_active_languages();
     foreach ($domains as $code => $url) {
         if (!empty($active_languages[$code])) {
             $url = $home_schema . $url;
             $parts = wpml_parse_url($url);
             if (isset($parts['host']) && !in_array($parts['host'], $hosts)) {
                 $hosts[] = $parts['host'];
             }
         }
     }
     return $hosts;
 }
 protected function get_lang_from_url_string($url)
 {
     $url = wpml_strip_subdir_from_url($url);
     if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) {
         $url_path = wpml_parse_url($url, PHP_URL_PATH);
     } else {
         $pathparts = array_filter(explode('/', $url));
         if (count($pathparts) > 1) {
             unset($pathparts[0]);
             $url_path = implode('/', $pathparts);
         } else {
             $url_path = $url;
         }
     }
     $fragments = array_filter((array) explode("/", $url_path));
     $lang = array_shift($fragments);
     $lang_get_parts = explode('?', $lang);
     $lang = $lang_get_parts[0];
     $lang = isset($this->language_codes_reverse_map[$lang]) ? $this->language_codes_reverse_map[$lang] : $lang;
     return $lang && in_array($lang, $this->active_languages) ? $lang : ($this->dir_default ? null : $this->default_language);
 }
 public function get_trid_from_referer()
 {
     if (isset($_SERVER['HTTP_REFERER'])) {
         $query = wpml_parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
         parse_str($query, $vars);
     }
     if (isset($_SERVER['REQUEST_URI'])) {
         $request_uri = wpml_parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
         parse_str($request_uri, $request_uri_vars);
     }
     /**
      * trid from `HTTP_REFERER` should be return only if `REQUEST_URI` also has trid set.
      * @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmltm-1351
      */
     return isset($vars['trid']) && isset($request_uri_vars['trid']) ? filter_var($vars['trid'], FILTER_SANITIZE_NUMBER_INT) : false;
 }
 private function is_permalink_part_of_request($permalink, $request_uri)
 {
     $permalink_path = trailingslashit(urldecode(wpml_parse_url($permalink, PHP_URL_PATH)));
     $request_uri = trailingslashit(urldecode($request_uri));
     return 0 === strcasecmp(substr($request_uri, 0, strlen($permalink_path)), $permalink_path);
 }
 /**
  * @param $requested_url string
  *                       Takes a request_url in the format of $_SERVER['REQUEST_URI']
  *                       and returns an associative array containing its slugs ans query string.
  *
  * @return array
  */
 private static function get_slugs_and_get_query($requested_url)
 {
     $result = array();
     $request_path = wpml_parse_url($requested_url, PHP_URL_PATH);
     $request_path = wpml_strip_subdir_from_url($request_path);
     $slugs = self::get_slugs_array($request_path);
     $result['slugs'] = $slugs;
     $query_string = wpml_parse_url($requested_url, PHP_URL_QUERY);
     $result['querystring'] = !$query_string ? '' : $query_string;
     return $result;
 }
示例#13
0
 /**
  * Fix sticky link url to have ID of translated post (used in case both translations have same slug)
  *
  * @param string $url - url in sticky link form
  * @param string $original_url - url in permalink form
  * @return string  - url in sticky link form to correct translation
  */
 private function maybe_adjust_url($url, $original_url)
 {
     $parsed_url = wpml_parse_url($url);
     $query = isset($parsed_url['query']) ? $parsed_url['query'] : "";
     parse_str($query, $vars);
     $inurl = isset($vars['page_id']) ? 'page_id' : null;
     $inurl = $inurl === null && isset($vars['p']) ? 'p' : null;
     $post_id = $inurl !== null ? $vars[$inurl] : null;
     if (isset($post_id)) {
         $post_type = get_post_type($post_id);
         $post_language = $this->get_language_for_element($post_id, 'post_' . $post_type);
         $url_language = $this->get_language_from_url($original_url);
         if ($post_language !== $url_language) {
             $trid = $this->get_element_trid($post_id, 'post_' . $post_type);
             $translations = $this->get_element_translations($trid, 'post_' . $post_type);
             if (isset($translations[$url_language])) {
                 $translation = $translations[$url_language];
                 if (isset($translation->element_id)) {
                     $vars[$inurl] = $translation->element_id;
                     $new_query = http_build_query($vars);
                     $url = str_replace($query, $new_query, $url);
                 }
             }
         }
     }
     return $url;
 }
示例#14
0
/**
 * @param string $url url either with or without schema
 *                    Removes the subdirectory in which WordPress is installed from a url.
 *                    If WordPress is not installed in a subdirectory, then the input is returned unaltered.
 *
 * @return string the url input without the blog's subdirectory. Potentially existing schemata on the input are kept intact.
 */
function wpml_strip_subdir_from_url($url)
{
    /** @var WPML_URL_Converter $wpml_url_converter */
    global $wpml_url_converter;
    $subdir = wpml_parse_url($wpml_url_converter->get_abs_home(), PHP_URL_PATH);
    $subdir_slugs = array_values(array_filter(explode('/', $subdir)));
    $url_path_expl = explode('/', preg_replace('#^(http|https)://#', '', $url));
    array_shift($url_path_expl);
    $url_slugs = array_values(array_filter($url_path_expl));
    $url_slugs_before = $url_slugs;
    $url_slugs = array_diff_assoc($url_slugs, $subdir_slugs);
    $url = str_replace('/' . join('/', $url_slugs_before), '/' . join('/', $url_slugs), $url);
    return untrailingslashit($url);
}
 /**
  * Checks that the input url is valid in so far that it contains schema
  * and host at least.
  *
  * @return bool
  */
 private function validate_url_string()
 {
     $url_parts = wpml_parse_url($this->url);
     return isset($url_parts['scheme']) && isset($url_parts['host']);
 }
    public function render()
    {
        $active_languages = $this->sitepress->get_active_languages();
        $default_language = $this->sitepress->get_default_language();
        $language_domains = $this->sitepress->get_setting('language_domains', array());
        $default_home = $this->sitepress->convert_url($this->sitepress->get_wp_api()->get_home_url(), $default_language);
        $home_schema = wpml_parse_url($default_home, PHP_URL_SCHEME) . '://';
        $home_path = wpml_parse_url($default_home, PHP_URL_PATH);
        ob_start();
        ?>
		<table class="language_domains">
			<?php 
        foreach ($active_languages as $code => $lang) {
            ?>
				<?php 
            $textbox_id = esc_attr('language_domain_' . $code);
            ?>
				<tr>
					<td>
						<label
							for="<?php 
            echo $textbox_id;
            ?>
">
							<?php 
            echo $lang['display_name'];
            ?>
						</label>
					</td>
					<?php 
            if ($code === $default_language) {
                ?>
						<td id="icl_ln_home">
							<code>
								<?php 
                echo $default_home;
                ?>
							</code>
						</td>
						<td>&nbsp;</td>
					<?php 
            } else {
                ?>
						<td style="white-space: nowrap">
							<code><?php 
                echo $home_schema;
                ?>
</code>
							<input
								type="text"
								id="<?php 
                echo $textbox_id;
                ?>
"
								name="language_domains[<?php 
                echo esc_attr($code);
                ?>
]"
								value="<?php 
                echo isset($language_domains[$code]) ? preg_replace(array('#^' . $home_schema . '#', '#' . $home_path . '$#'), '', $language_domains[$code]) : $this->render_suggested_url($default_home, $code);
                ?>
"
								data-language="<?php 
                echo esc_attr($code);
                ?>
"
								size="30"/>
							<?php 
                if (isset($home_path[1])) {
                    ?>
								<code><?php 
                    echo $home_path;
                    ?>
</code>
							<?php 
                }
                ?>
						</td>
						<td>
							<p style="white-space: nowrap"><input
									class="validate_language_domain"
									type="checkbox"
									id="validate_language_domains_<?php 
                echo esc_attr($code);
                ?>
"
									name="validate_language_domains[]"
									value="<?php 
                echo esc_attr($code);
                ?>
"
									checked="checked"/>
								<label
									for="validate_language_domains_<?php 
                echo esc_attr($code);
                ?>
">
									<?php 
                esc_html_e('Validate on save', 'sitepress');
                ?>
								</label>
							</p>
							<p style="white-space: nowrap">
								<span class="spinner spinner-<?php 
                echo esc_attr($code);
                ?>
"></span>
								<span id="ajx_ld_<?php 
                echo esc_attr($code);
                ?>
"></span>
							</p>
						</td>
					<?php 
            }
            ?>
				</tr>
			<?php 
        }
        ?>
		</table>
		<?php 
        return ob_get_clean();
    }
 /**
  * This function tries to determine the terms language from the HTTP Referer. This is used in case of ajax actions
  * that save the term.
  *
  * @param string $taxonomy
  * @param string $post_action
  *
  * @return null|string
  */
 public function get_term_lang_ajax($taxonomy, $post_action)
 {
     if (isset($_POST['_ajax_nonce']) && filter_var($_POST['_ajax_nonce']) !== false && $post_action === 'add-' . $taxonomy) {
         $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
         parse_str((string) wpml_parse_url($referrer, PHP_URL_QUERY), $qvars);
         $term_lang = !empty($qvars['post']) && $this->sitepress->is_translated_post_type(get_post_type($qvars['post'])) ? $this->post_translations->get_element_lang_code($qvars['post']) : (isset($qvars['lang']) ? $qvars['lang'] : null);
     }
     return isset($term_lang) ? $term_lang : null;
 }
 /**
  * @param $source_url
  *
  * @return mixed|string
  */
 private function fix_trailingslashit($source_url)
 {
     $query = wpml_parse_url($source_url, PHP_URL_QUERY);
     if (!empty($query)) {
         $source_url = str_replace('?' . $query, '', $source_url);
     }
     $source_url = trailingslashit($source_url);
     if (!empty($query)) {
         $source_url .= '?' . $query;
     }
     return $source_url;
 }