first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Language\Detection;
|
||||
|
||||
use WPML\FP\Maybe;
|
||||
use WPML\FP\Obj;
|
||||
use WPML\FP\Lst;
|
||||
use WPML\FP\Str;
|
||||
use WPML\FP\Fns;
|
||||
use \WPML_Request;
|
||||
|
||||
class Ajax extends WPML_Request {
|
||||
|
||||
public function get_requested_lang() {
|
||||
return Maybe::of( $_REQUEST )
|
||||
->map( Obj::prop( 'lang' ) )
|
||||
->filter( Lst::includes( Fns::__, $this->active_languages ) )
|
||||
->map( 'sanitize_text_field' )
|
||||
->getOrElse(
|
||||
function () {
|
||||
return $this->get_cookie_lang();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function get_cookie_name() {
|
||||
return $this->cookieLanguage->getAjaxCookieName( $this->is_admin_action_from_referer() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function is_admin_action_from_referer() {
|
||||
return (bool) Maybe::of( $_SERVER )
|
||||
->map( Obj::prop( 'HTTP_REFERER' ) )
|
||||
->map( Str::pos( '/wp-admin/' ) )
|
||||
->getOrElse( false );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Language\Detection;
|
||||
|
||||
use WPML\FP\Fns;
|
||||
use WPML\FP\Logic;
|
||||
use WPML\FP\Maybe;
|
||||
use WPML\FP\Obj;
|
||||
use WPML\FP\Relation;
|
||||
use function WPML\FP\System\filterVar;
|
||||
use function \WPML\FP\partialRight;
|
||||
|
||||
/**
|
||||
* Class WPML_Backend_Request
|
||||
*
|
||||
* @package wpml-core
|
||||
* @subpackage wpml-requests
|
||||
*/
|
||||
class Backend extends \WPML_Request {
|
||||
/**
|
||||
* Determines the requested language in the WP Admin backend from URI, $_POST, $_GET and cookies.
|
||||
*
|
||||
* @return string The requested language code.
|
||||
*/
|
||||
public function get_requested_lang() {
|
||||
$findFromSystemVars = Fns::until(
|
||||
Logic::isNotNull(),
|
||||
[
|
||||
$this->getForPage(),
|
||||
$this->getFromParam( [ 'get', 'lang' ], true ),
|
||||
$this->getFromParam( [ 'post', 'icl_post_language' ], false ),
|
||||
$this->getPostElementLanguage(),
|
||||
]
|
||||
);
|
||||
|
||||
return Maybe::of(
|
||||
[
|
||||
'get' => $_GET,
|
||||
'post' => $_POST,
|
||||
]
|
||||
)
|
||||
->map( $findFromSystemVars )
|
||||
->getOrElse(
|
||||
function () {
|
||||
return $this->get_cookie_lang();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private function getForPage() {
|
||||
return function ( $system ) {
|
||||
return $this->is_string_translation_or_translation_queue_page( $system ) ? $this->default_language : null;
|
||||
};
|
||||
}
|
||||
|
||||
protected function get_cookie_name() {
|
||||
return $this->cookieLanguage->getBackendCookieName();
|
||||
}
|
||||
|
||||
private function getFromParam( $path, $allowAllValue ) {
|
||||
return function ( $system ) use ( $path, $allowAllValue ) {
|
||||
/** @var \WPML_Language_Resolution $wpml_language_resolution */
|
||||
global $wpml_language_resolution;
|
||||
|
||||
return Maybe::of( $system )
|
||||
->map( Obj::path( $path ) )
|
||||
->map( filterVar( FILTER_SANITIZE_FULL_SPECIAL_CHARS ) )
|
||||
->filter( partialRight( [ $wpml_language_resolution, 'is_language_active' ], $allowAllValue ) )
|
||||
->getOrElse( null );
|
||||
};
|
||||
}
|
||||
|
||||
private function getPostElementLanguage() {
|
||||
return function ( $system ) {
|
||||
/** @var \WPML_Post_Translation $wpml_post_translations */
|
||||
global $wpml_post_translations;
|
||||
|
||||
return Maybe::of( $system )
|
||||
->map( Obj::path( [ 'get', 'p' ] ) )
|
||||
->filter( Relation::gt( Fns::__, 0 ) )
|
||||
->map( [ $wpml_post_translations, 'get_element_lang_code' ] )
|
||||
->getOrElse( null );
|
||||
};
|
||||
}
|
||||
|
||||
private function is_string_translation_or_translation_queue_page( $system ) {
|
||||
$page = Obj::path( [ 'get', 'page' ], $system );
|
||||
|
||||
return ( defined( 'WPML_ST_FOLDER' ) && $page === WPML_ST_FOLDER . '/menu/string-translation.php' )
|
||||
||
|
||||
( defined( 'WPML_TM_FOLDER' ) && $page === WPML_TM_FOLDER . '/menu/translations-queue.php' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Language\Detection;
|
||||
|
||||
class CookieLanguage {
|
||||
/** @var \WPML_Cookie */
|
||||
private $cookie;
|
||||
|
||||
/** @var string */
|
||||
private $defaultLanguage;
|
||||
|
||||
/**
|
||||
* @param \WPML_Cookie $cookie
|
||||
* @param string $defaultLanguage
|
||||
*/
|
||||
public function __construct( \WPML_Cookie $cookie, $defaultLanguage ) {
|
||||
$this->cookie = $cookie;
|
||||
$this->defaultLanguage = $defaultLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isBackend
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAjaxCookieName( $isBackend ) {
|
||||
return $isBackend ? $this->getBackendCookieName() : $this->getFrontendCookieName();
|
||||
}
|
||||
|
||||
public function getBackendCookieName() {
|
||||
return 'wp-wpml_current_admin_language_' . md5( $this->get_cookie_domain() );
|
||||
}
|
||||
|
||||
public function getFrontendCookieName() {
|
||||
return 'wp-wpml_current_language';
|
||||
}
|
||||
|
||||
public function get( $cookieName ) {
|
||||
global $wpml_language_resolution;
|
||||
|
||||
$cookie_value = esc_attr( $this->cookie->get_cookie( $cookieName ) );
|
||||
$lang = $cookie_value ? substr( $cookie_value, 0, 10 ) : null;
|
||||
$lang = $wpml_language_resolution->is_language_active( $lang ) ? $lang : $this->defaultLanguage;
|
||||
|
||||
return $lang;
|
||||
}
|
||||
|
||||
|
||||
public function set( $cookieName, $lang_code ) {
|
||||
global $sitepress;
|
||||
|
||||
if ( is_user_logged_in() ) {
|
||||
if ( ! $this->cookie->headers_sent() ) {
|
||||
if ( preg_match(
|
||||
'@\.(css|js|png|jpg|gif|jpeg|bmp|ico)@i',
|
||||
basename( preg_replace( '@\?.*$@', '', $_SERVER['REQUEST_URI'] ) )
|
||||
)
|
||||
|| isset( $_POST['icl_ajx_action'] ) || isset( $_POST['_ajax_nonce'] ) || defined( 'DOING_AJAX' )
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current_cookie_value = $this->cookie->get_cookie( $cookieName );
|
||||
if ( ! $current_cookie_value || $current_cookie_value !== $lang_code ) {
|
||||
$cookie_domain = $this->get_cookie_domain();
|
||||
$cookie_path = defined( 'COOKIEPATH' ) ? COOKIEPATH : '/';
|
||||
$this->cookie->set_cookie(
|
||||
$cookieName,
|
||||
$lang_code,
|
||||
time() + DAY_IN_SECONDS,
|
||||
$cookie_path,
|
||||
$cookie_domain
|
||||
);
|
||||
}
|
||||
}
|
||||
} elseif ( $sitepress->get_setting( \WPML_Cookie_Setting::COOKIE_SETTING_FIELD ) ) {
|
||||
$wpml_cookie_scripts = new \WPML_Cookie_Scripts( $cookieName, $sitepress->get_current_language() );
|
||||
$wpml_cookie_scripts->add_hooks();
|
||||
}
|
||||
|
||||
$_COOKIE[ $cookieName ] = $lang_code;
|
||||
|
||||
do_action( 'wpml_language_cookie_added', $lang_code );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|string
|
||||
*/
|
||||
public function get_cookie_domain() {
|
||||
|
||||
return defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : self::get_server_host_name();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns SERVER_NAME, or HTTP_HOST if the first is not available
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function get_server_host_name() {
|
||||
$host = '';
|
||||
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
|
||||
$host = $_SERVER['HTTP_HOST'];
|
||||
} elseif ( isset( $_SERVER['SERVER_NAME'] ) ) {
|
||||
$host = $_SERVER['SERVER_NAME'] . self::get_port();
|
||||
// Removes standard ports 443 (80 should be already omitted in all cases)
|
||||
$host = preg_replace( '@:[443]+([/]?)@', '$1', $host );
|
||||
}
|
||||
|
||||
return $host;
|
||||
}
|
||||
|
||||
private static function get_port() {
|
||||
return isset( $_SERVER['SERVER_PORT'] ) && ! in_array( $_SERVER['SERVER_PORT'], [ 80, 443 ] )
|
||||
? ':' . $_SERVER['SERVER_PORT']
|
||||
: '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Language\Detection;
|
||||
|
||||
use WPML\FP\Maybe;
|
||||
use WPML\FP\Obj;
|
||||
use \WPML_Request;
|
||||
use \WPML_WP_Comments;
|
||||
use function WPML\FP\System\filterVar;
|
||||
|
||||
/**
|
||||
* @package wpml-core
|
||||
* @subpackage wpml-requests
|
||||
*/
|
||||
class Frontend extends WPML_Request {
|
||||
/** @var \WPML_WP_API */
|
||||
private $wp_api;
|
||||
|
||||
public function __construct(
|
||||
\WPML_URL_Converter $url_converter,
|
||||
$active_languages,
|
||||
$default_language,
|
||||
CookieLanguage $cookieLanguage,
|
||||
\WPML_WP_API $wp_api
|
||||
) {
|
||||
parent::__construct( $url_converter, $active_languages, $default_language, $cookieLanguage );
|
||||
$this->wp_api = $wp_api;
|
||||
}
|
||||
|
||||
|
||||
public function get_requested_lang() {
|
||||
return $this->wp_api->is_comments_post_page() ? $this->get_comment_language() : $this->get_request_uri_lang();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function get_comment_language() {
|
||||
return Maybe::of( $_POST )
|
||||
->map( Obj::prop( WPML_WP_Comments::LANG_CODE_FIELD ) )
|
||||
->map( filterVar( FILTER_SANITIZE_SPECIAL_CHARS ) )
|
||||
->getOrElse( $this->default_language );
|
||||
|
||||
}
|
||||
|
||||
protected function get_cookie_name() {
|
||||
return $this->cookieLanguage->getFrontendCookieName();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Language\Detection;
|
||||
|
||||
use \WPML_Request;
|
||||
|
||||
class Rest extends WPML_Request {
|
||||
/** @var Backend */
|
||||
private $backend;
|
||||
|
||||
public function __construct(
|
||||
$url_converter,
|
||||
$active_languages,
|
||||
$default_language,
|
||||
$cookieLanguage,
|
||||
$backend
|
||||
) {
|
||||
parent::__construct( $url_converter, $active_languages, $default_language, $cookieLanguage );
|
||||
$this->backend = $backend;
|
||||
}
|
||||
|
||||
|
||||
protected function get_cookie_name() {
|
||||
return $this->cookieLanguage->getAjaxCookieName( ! $this->getFrontendLanguage() );
|
||||
}
|
||||
|
||||
public function get_requested_lang() {
|
||||
return $this->getFrontendLanguage() ?: $this->backend->get_requested_lang();
|
||||
}
|
||||
|
||||
/**
|
||||
* It tries to detect language in FRONTEND manner.
|
||||
*
|
||||
* We ignore a default language due to fallback mechanism in WPML_URL_Converter_Subdir_Strategy which never returns
|
||||
* NULL when `use_directory_for_default_lang` option is enabled.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getFrontendLanguage() {
|
||||
$language = $this->get_request_uri_lang();
|
||||
|
||||
return $language && $language !== $this->default_language ? $language : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
class WPML_All_Language_Pairs {
|
||||
|
||||
public static function get() {
|
||||
|
||||
$languages = array_keys( \WPML\Element\API\Languages::getActive() );
|
||||
|
||||
$lang_pairs = array();
|
||||
|
||||
foreach ( $languages as $from_lang ) {
|
||||
$lang_pairs[ $from_lang ] = array();
|
||||
foreach ( $languages as $to_lang ) {
|
||||
if ( $from_lang !== $to_lang ) {
|
||||
$lang_pairs[ $from_lang ][] = $to_lang;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $lang_pairs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
use WPML\Element\API\Languages;
|
||||
use WPML\FP\Logic;
|
||||
use WPML\FP\Lst;
|
||||
|
||||
/**
|
||||
* Class WPML_Language_Pair_Records
|
||||
*
|
||||
* Language pairs are stored as user meta as an array of the form
|
||||
* array( $from_lang => array( $to_lang_1 => '1', $to_lang_2 => '1' )
|
||||
*/
|
||||
class WPML_Language_Pair_Records {
|
||||
|
||||
private $meta_key;
|
||||
|
||||
/** @var WPML_Language_Records $language_records */
|
||||
private $language_records;
|
||||
|
||||
/** @var array|null */
|
||||
private $active_language_codes;
|
||||
|
||||
/**
|
||||
* @param wpdb $wpdb
|
||||
* @param WPML_Language_Records $language_records
|
||||
* @param array|null $active_language_codes
|
||||
*/
|
||||
public function __construct( wpdb $wpdb, WPML_Language_Records $language_records, $active_language_codes = null ) {
|
||||
$this->meta_key = $wpdb->prefix . 'language_pairs';
|
||||
$this->language_records = $language_records;
|
||||
$this->active_language_codes = $active_language_codes ?: Lst::pluck( 'code', Languages::getActive() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $user_id
|
||||
* @param array $language_pairs
|
||||
*
|
||||
* Language pairs are an array of the form
|
||||
* array( $from_lang => array( $to_lang_1, $to_lang_2 )
|
||||
*/
|
||||
public function store( $user_id, $language_pairs ) {
|
||||
$language_pairs = $this->convert_to_storage_format( $language_pairs );
|
||||
update_user_meta( $user_id, $this->meta_key, $language_pairs );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $user_id
|
||||
* @param array $language_pairs
|
||||
*
|
||||
* Stores only the language pairs that are active.
|
||||
*/
|
||||
public function store_active( $user_id, $language_pairs ) {
|
||||
$language_pairs = wpml_collect( $language_pairs )
|
||||
->mapWithKeys(
|
||||
function( $to, $from ) {
|
||||
if ( ! $this->active_language_codes || ! in_array( $from, $this->active_language_codes, true ) ) {
|
||||
return [];
|
||||
}
|
||||
return [ $from => array_intersect( $to, $this->active_language_codes ) ];
|
||||
}
|
||||
)
|
||||
->filter( Logic::complement( Logic::isEmpty() ) )
|
||||
->toArray();
|
||||
$this->store( $user_id, $language_pairs );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $user_id
|
||||
*/
|
||||
public function remove_all( $user_id ) {
|
||||
delete_user_meta( $user_id, $this->meta_key );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $user_id
|
||||
* @return array
|
||||
*
|
||||
* Language pairs are returned in an array of the form
|
||||
* array( $from_lang => array( $to_lang_1, $to_lang_2 )
|
||||
*/
|
||||
public function get( $user_id ) {
|
||||
$language_pairs = get_user_meta( $user_id, $this->meta_key, true );
|
||||
if ( ! $language_pairs ) {
|
||||
$language_pairs = array();
|
||||
}
|
||||
return $this->convert_from_storage_format( $language_pairs );
|
||||
}
|
||||
|
||||
private function convert_to_storage_format( $language_pairs ) {
|
||||
if ( $this->is_in_storage_format( $language_pairs ) ) {
|
||||
return $language_pairs;
|
||||
}
|
||||
|
||||
foreach ( $language_pairs as $from => $to_langs ) {
|
||||
$targets = array();
|
||||
foreach ( $to_langs as $lang ) {
|
||||
$targets[ $lang ] = 1;
|
||||
}
|
||||
$language_pairs[ $from ] = $targets;
|
||||
}
|
||||
|
||||
return $language_pairs;
|
||||
}
|
||||
|
||||
private function is_in_storage_format( $language_pairs ) {
|
||||
$first_from = reset( $language_pairs );
|
||||
|
||||
if ( $first_from && ! is_numeric( key( $first_from ) ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function convert_from_storage_format( array $language_pairs ) {
|
||||
foreach ( $language_pairs as $from => $to_langs ) {
|
||||
if ( $this->language_records->is_valid( $from ) ) {
|
||||
$language_pairs[ $from ] = array();
|
||||
foreach ( array_keys( $to_langs ) as $lang ) {
|
||||
if ( $this->language_records->is_valid( $lang ) ) {
|
||||
$language_pairs[ $from ][] = $lang;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
unset( $language_pairs[ $from ] );
|
||||
}
|
||||
}
|
||||
return $language_pairs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $user_id
|
||||
*/
|
||||
public function remove_invalid_language_pairs( $user_id ) {
|
||||
$language_pairs = $this->get( $user_id );
|
||||
$this->store( $user_id, $language_pairs );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user