first commit
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
class WPML_All_Language_Pairs {
|
||||
|
||||
public static function get( SitePress $sitepress ) {
|
||||
|
||||
$languages = array_keys( $sitepress->get_active_languages() );
|
||||
|
||||
$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,90 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
public function __construct( wpdb $wpdb, WPML_Language_Records $language_records ) {
|
||||
$this->meta_key = $wpdb->prefix . 'language_pairs';
|
||||
$this->language_records = $language_records;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user