first commit
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
class WPML_URL_Converter_Lang_Param_Helper {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $cache = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $active_languages;
|
||||
|
||||
/**
|
||||
* @param array $active_languages
|
||||
*/
|
||||
public function __construct( array $active_languages ) {
|
||||
$this->active_languages = $active_languages;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public function lang_by_param( $url, $only_admin = true ) {
|
||||
if ( isset( self::$cache[ $url ] ) ) {
|
||||
return self::$cache[ $url ];
|
||||
}
|
||||
|
||||
$lang = $this->extract_lang_param_from_url( $url, $only_admin );
|
||||
|
||||
self::$cache[ $url ] = $lang;
|
||||
|
||||
return $lang;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param bool $only_admin
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function extract_lang_param_from_url( $url, $only_admin ) {
|
||||
$url = wpml_strip_subdir_from_url( $url );
|
||||
$url_query_parts = wpml_parse_url( $url );
|
||||
$url_query = $this->has_query_part( $only_admin, $url_query_parts ) ? untrailingslashit( $url_query_parts['query'] ) : null;
|
||||
|
||||
if ( null !== $url_query ) {
|
||||
parse_str( $url_query, $vars );
|
||||
if ( $this->can_retrieve_lang_from_query( $only_admin, $vars ) ) {
|
||||
return $vars['lang'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $only_admin
|
||||
* @param array $url_query_parts
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function has_query_part( $only_admin, $url_query_parts ) {
|
||||
if ( ! isset( $url_query_parts['query'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( false === $only_admin ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( ! isset( $url_query_parts['path'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( false === strpos( $url_query_parts['path'], '/wp-admin' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $only_admin
|
||||
* @param array $vars
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function can_retrieve_lang_from_query( $only_admin, $vars ) {
|
||||
if ( ! isset( $vars['lang'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $only_admin && 'all' === $vars['lang'] ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( in_array( $vars['lang'], $this->active_languages, true ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
class WPML_URL_Converter_Url_Helper {
|
||||
/**
|
||||
* @var wpdb
|
||||
*/
|
||||
private $wpdb;
|
||||
|
||||
/**
|
||||
* @var WPML_Include_Url
|
||||
*/
|
||||
private $wpml_include_url_filter;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $absolute_home;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param wpdb $wpdb
|
||||
* @param WPML_Include_Url $wpml_include_url_filter
|
||||
*/
|
||||
public function __construct( wpdb $wpdb = null, WPML_Include_Url $wpml_include_url_filter = null ) {
|
||||
if ( ! $wpdb ) {
|
||||
global $wpdb;
|
||||
}
|
||||
|
||||
if ( ! $wpml_include_url_filter ) {
|
||||
global $wpml_include_url_filter;
|
||||
}
|
||||
|
||||
$this->wpdb = $wpdb;
|
||||
$this->wpml_include_url_filter = $wpml_include_url_filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unfiltered home_url by directly retrieving it from wp_options.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_abs_home() {
|
||||
if ( ! $this->absolute_home ) {
|
||||
$is_multisite = is_multisite();
|
||||
if ( ! $is_multisite && defined( 'WP_HOME' ) ) {
|
||||
$this->absolute_home = WP_HOME;
|
||||
} elseif ( $is_multisite && ! is_main_site() ) {
|
||||
$protocol = preg_match( '/^(https)/', get_option( 'home' ) ) === 1 ? 'https://' : 'http://';
|
||||
$sql = "
|
||||
SELECT CONCAT(b.domain, b.path)
|
||||
FROM {$this->wpdb->blogs} b
|
||||
WHERE blog_id = {$this->wpdb->blogid}
|
||||
LIMIT 1
|
||||
";
|
||||
|
||||
$this->absolute_home = $protocol . $this->wpdb->get_var( $sql );
|
||||
} else {
|
||||
$this->absolute_home = $this->get_unfiltered_home_option();
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters( 'wpml_url_converter_get_abs_home', $this->absolute_home );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a $url points to a WP Admin screen.
|
||||
*
|
||||
* @param string $url
|
||||
* @return bool True if the input $url points to an admin screen.
|
||||
*/
|
||||
public function is_url_admin( $url ) {
|
||||
$url_query_parts = wpml_parse_url( strpos( $url, 'http' ) === false ? 'http://' . $url : $url );
|
||||
|
||||
return isset( $url_query_parts['path'] )
|
||||
&& strpos( wpml_strip_subdir_from_url( $url_query_parts['path'] ), '/wp-admin' ) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unfiltered home option from the database.
|
||||
*
|
||||
* @uses \WPML_Include_Url::get_unfiltered_home in case the $wpml_include_url_filter global is loaded
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_unfiltered_home_option() {
|
||||
if ( $this->wpml_include_url_filter ) {
|
||||
return $this->wpml_include_url_filter->get_unfiltered_home();
|
||||
} else {
|
||||
$sql = "
|
||||
SELECT option_value
|
||||
FROM {$this->wpdb->options}
|
||||
WHERE option_name = 'home'
|
||||
LIMIT 1
|
||||
";
|
||||
|
||||
return $this->wpdb->get_var( $sql );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user