first commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\Languages;
|
||||
|
||||
use WPML\API\PostTypes;
|
||||
use WPML\Core\WP\App\Resources;
|
||||
use WPML\Element\API\Languages;
|
||||
use WPML\FP\Fns;
|
||||
use WPML\FP\Lst;
|
||||
use WPML\FP\Obj;
|
||||
use WPML\LIB\WP\Hooks;
|
||||
use WPML\LIB\WP\PostType;
|
||||
use WPML\Posts\CountPerPostType;
|
||||
use WPML\Posts\DeleteTranslatedContentOfLanguages;
|
||||
use WPML\Posts\UntranslatedCount;
|
||||
use WPML\Setup\Option;
|
||||
use WPML\TM\ATE\AutoTranslate\Endpoint\ActivateLanguage;
|
||||
use WPML\TM\ATE\AutoTranslate\Endpoint\CheckLanguageSupport;
|
||||
use WPML\UIPage;
|
||||
use function WPML\FP\partial;
|
||||
|
||||
class UI implements \IWPML_Backend_Action {
|
||||
|
||||
public function add_hooks() {
|
||||
if ( UIPage::isLanguages( $_GET ) ) {
|
||||
|
||||
if ( self::isEditLanguagePage() ) {
|
||||
Hooks::onAction( 'admin_enqueue_scripts' )
|
||||
->then( Fns::unary( partial( [ self::class, 'getData' ], true ) ) )
|
||||
->then( Resources::enqueueApp( 'languages' ) );
|
||||
} else {
|
||||
Hooks::onAction( 'admin_enqueue_scripts' )
|
||||
->then( Fns::unary( partial( [ self::class, 'getData' ], false ) ) )
|
||||
->then( Resources::enqueueApp( 'languages' ) );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static function getData( $editPage ) {
|
||||
$getPostTypeName = function ( $postType ) {
|
||||
return PostType::getPluralName( $postType )->getOrElse( $postType );
|
||||
};
|
||||
|
||||
$data = [
|
||||
'endpoints' => [
|
||||
'checkSupportOfLanguages' => CheckLanguageSupport::class,
|
||||
'skipTranslationOfExistingContent' => ActivateLanguage::class,
|
||||
'postsToTranslatePerTypeCount' => CountPerPostType::class,
|
||||
'deleteTranslatedContentOfLanguage' => DeleteTranslatedContentOfLanguages::class
|
||||
],
|
||||
'postTypes' => Fns::map( $getPostTypeName, PostTypes::getAutomaticTranslatable() ),
|
||||
'shouldTranslateEverything' => Option::shouldTranslateEverything(),
|
||||
];
|
||||
|
||||
if ( $editPage ) {
|
||||
$existingLanguages = Obj::values( Fns::map( function ( $language ) {
|
||||
return Lst::concat( $language, [
|
||||
'mapping' => [
|
||||
'targetId' => Obj::pathOr( '', [ 'mapping', 'targetId' ], $language ),
|
||||
'targetCode' => Obj::pathOr( '', [ 'mapping', 'targetCode' ], $language ),
|
||||
]
|
||||
] );
|
||||
}, \SitePress_EditLanguages::get_active_languages() ) );
|
||||
|
||||
$data = Lst::concat( $data, [ 'existingLanguages' => $existingLanguages ] );
|
||||
} else {
|
||||
$data = Lst::concat( $data, [
|
||||
'existingLangs' => Languages::getSecondaryCodes(),
|
||||
'defaultLang' => Languages::getDefaultCode(),
|
||||
'settingsUrl' => admin_url( UIPage::getSettings() ),
|
||||
] );
|
||||
}
|
||||
|
||||
return [ 'name' => 'wpmlLanguagesUI', 'data' => $data ];
|
||||
}
|
||||
|
||||
|
||||
private static function isEditLanguagePage() {
|
||||
return (int) Obj::prop( 'trop', $_GET ) === 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
class WPML_Language_Collection {
|
||||
|
||||
/** @var SitePress $sitepress */
|
||||
private $sitepress;
|
||||
|
||||
/** @var array $languages */
|
||||
private $languages = array();
|
||||
|
||||
/**
|
||||
* WPML_Language_Collection constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
* @param array $initial_languages Array of language codes
|
||||
*/
|
||||
public function __construct( SitePress $sitepress, $initial_languages = array() ) {
|
||||
$this->sitepress = $sitepress;
|
||||
foreach ( $initial_languages as $lang ) {
|
||||
$this->add( $lang );
|
||||
}
|
||||
}
|
||||
|
||||
public function add( $code ) {
|
||||
if ( ! isset( $this->languages[ $code ] ) ) {
|
||||
$language = new WPML_Language( $this->sitepress, $code );
|
||||
if ( $language->is_valid() ) {
|
||||
$this->languages[ $code ] = $language;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function get( $code ) {
|
||||
if ( ! isset( $this->languages[ $code ] ) ) {
|
||||
$this->add( $code );
|
||||
}
|
||||
return $this->languages[ $code ];
|
||||
}
|
||||
|
||||
public function get_codes() {
|
||||
return array_keys( $this->languages );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
class WPML_Language_Records {
|
||||
|
||||
private $wpdb;
|
||||
|
||||
private $languages;
|
||||
|
||||
/** @var null|array $locale_lang_map */
|
||||
private $locale_lang_map;
|
||||
|
||||
public function __construct( wpdb $wpdb ) {
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
public function is_valid( $code ) {
|
||||
if ( ! $this->languages ) {
|
||||
$this->load();
|
||||
}
|
||||
|
||||
return in_array( $code, $this->languages );
|
||||
}
|
||||
|
||||
private function load() {
|
||||
$this->languages = $this->wpdb->get_col( "SELECT code FROM {$this->get_table()}" );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $lang_code
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_locale( $lang_code ) {
|
||||
$this->init_locale_lang_map();
|
||||
$locale = array_search( $lang_code, $this->locale_lang_map, true );
|
||||
return $locale ? $locale : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_language_code( $locale ) {
|
||||
$this->init_locale_lang_map();
|
||||
return isset( $this->locale_lang_map[ $locale ] ) ? $this->locale_lang_map[ $locale ] : null;
|
||||
}
|
||||
|
||||
private function init_locale_lang_map() {
|
||||
if ( null === $this->locale_lang_map ) {
|
||||
$this->locale_lang_map = array();
|
||||
|
||||
$sql = "SELECT default_locale, code FROM {$this->get_table()}";
|
||||
$rowset = $this->wpdb->get_results( $sql );
|
||||
|
||||
foreach ( $rowset as $row ) {
|
||||
$this->locale_lang_map[ $row->default_locale ?: $row->code ] = $row->code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_locale_lang_map() {
|
||||
$this->init_locale_lang_map();
|
||||
return $this->locale_lang_map;
|
||||
}
|
||||
|
||||
private function get_table() {
|
||||
return $this->wpdb->prefix . 'icl_languages';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
class WPML_Language {
|
||||
|
||||
/** @var SitePress $sitepress */
|
||||
private $sitepress;
|
||||
|
||||
/** @var string $code */
|
||||
private $code;
|
||||
|
||||
private $lang_details;
|
||||
|
||||
public function __construct( SitePress $sitepress, $code ) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->code = $code;
|
||||
$this->lang_details = $sitepress->get_language_details( $code );
|
||||
}
|
||||
|
||||
public function is_valid() {
|
||||
return (bool) $this->lang_details;
|
||||
}
|
||||
|
||||
public function get_code() {
|
||||
return $this->lang_details['code'];
|
||||
}
|
||||
|
||||
public function get_display_name() {
|
||||
return $this->lang_details['display_name'];
|
||||
}
|
||||
|
||||
public function get_flag_url() {
|
||||
return $this->sitepress->get_flag_url( $this->code );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
use WPML\API\Sanitize;
|
||||
|
||||
/**
|
||||
* @author OnTheGo Systems
|
||||
*/
|
||||
class WPML_Languages_AJAX {
|
||||
private $sitepress;
|
||||
private $default_language;
|
||||
|
||||
/**
|
||||
* WPML_Languages_AJAX constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
*/
|
||||
public function __construct( SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->default_language = $this->sitepress->get_default_language();
|
||||
}
|
||||
|
||||
public function ajax_hooks() {
|
||||
add_action( 'wp_ajax_wpml_set_active_languages', array( $this, 'set_active_languages_action' ) );
|
||||
add_action( 'wp_ajax_wpml_set_default_language', array( $this, 'set_default_language_action' ) );
|
||||
}
|
||||
|
||||
private function validate_ajax_action() {
|
||||
$action = Sanitize::stringProp( 'action', $_POST );
|
||||
$nonce = Sanitize::stringProp( 'nonce', $_POST );
|
||||
|
||||
return $action && $nonce && wp_verify_nonce( $nonce, $action );
|
||||
}
|
||||
|
||||
public function set_active_languages_action() {
|
||||
$failed = true;
|
||||
|
||||
$response = array();
|
||||
if ( $this->validate_ajax_action() ) {
|
||||
$old_active_languages = $this->sitepress->get_active_languages();
|
||||
$old_active_languages_count = count( (array) $old_active_languages );
|
||||
$lang_codes = filter_var( $_POST['languages'], FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY );
|
||||
$setup_instance = wpml_get_setup_instance();
|
||||
if ( $lang_codes && $setup_instance->set_active_languages( $lang_codes ) ) {
|
||||
$active_languages = $this->sitepress->get_active_languages();
|
||||
$html_response = '';
|
||||
|
||||
foreach ( (array) $active_languages as $lang ) {
|
||||
$is_default = ( $this->default_language === $lang['code'] );
|
||||
$html_response .= '<li ';
|
||||
if ( $is_default ) {
|
||||
$html_response .= 'class="default_language"';
|
||||
}
|
||||
$html_response .= '><label><input type="radio" name="default_language" value="' . $lang['code'] . '" ';
|
||||
if ( $is_default ) {
|
||||
$html_response .= 'checked="checked"';
|
||||
}
|
||||
$html_response .= '>' . $lang['display_name'];
|
||||
if ( $is_default ) {
|
||||
$html_response .= ' (' . __( 'default', 'sitepress' ) . ')';
|
||||
}
|
||||
$html_response .= '</label></li>';
|
||||
$response['enabledLanguages'] = $html_response;
|
||||
}
|
||||
|
||||
$response['noLanguages'] = 1;
|
||||
if ( ( count( $lang_codes ) > 1 ) || ( $old_active_languages_count > 1 && count( $lang_codes ) < 2 ) ) {
|
||||
$response['noLanguages'] = 0;
|
||||
}
|
||||
$updated_active_languages = $this->sitepress->get_active_languages();
|
||||
if ( $updated_active_languages ) {
|
||||
$wpml_localization = new WPML_Download_Localization( $updated_active_languages, $this->default_language );
|
||||
$wpml_localization->download_language_packs();
|
||||
|
||||
$wpml_languages_notices = new WPML_Languages_Notices( wpml_get_admin_notices() );
|
||||
$wpml_languages_notices->maybe_create_notice_missing_menu_items( count( $lang_codes ) );
|
||||
$wpml_languages_notices->missing_languages( $wpml_localization->get_not_founds() );
|
||||
|
||||
if ( \WPML\Setup\Option::isTMAllowed() && $this->sitepress->is_setup_complete() ) {
|
||||
WPML_TM_Translation_Priorities::insert_missing_default_terms();
|
||||
}
|
||||
}
|
||||
$failed = false;
|
||||
}
|
||||
|
||||
icl_cache_clear();
|
||||
|
||||
/** @deprecated Use `wpml_update_active_languages` instead */
|
||||
do_action( 'icl_update_active_languages' );
|
||||
do_action( 'wpml_update_active_languages', $old_active_languages );
|
||||
}
|
||||
|
||||
if ( $failed ) {
|
||||
wp_send_json_error( $response );
|
||||
} else {
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
}
|
||||
|
||||
public function set_default_language_action() {
|
||||
$failed = true;
|
||||
$response = array();
|
||||
|
||||
if ( $this->validate_ajax_action() ) {
|
||||
$previous_default = $this->default_language;
|
||||
$new_default_language = filter_var( $_POST['language'], FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_NULL_ON_FAILURE );
|
||||
|
||||
$active_languages = $this->sitepress->get_active_languages();
|
||||
$active_languages_codes = array_keys( $active_languages );
|
||||
|
||||
if ( $new_default_language && in_array( $new_default_language, $active_languages_codes, true ) ) {
|
||||
$status = $this->sitepress->set_default_language( $new_default_language );
|
||||
if ( $status ) {
|
||||
$response['previousLanguage'] = $previous_default;
|
||||
$failed = false;
|
||||
}
|
||||
if ( 1 === $status ) {
|
||||
$response['message'] = __( 'WordPress language file (.mo) is missing. Keeping existing display language.', 'sitepress' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $failed ) {
|
||||
wp_send_json_error( $response );
|
||||
} else {
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
interface IWPML_Current_Language {
|
||||
public function get_current_language();
|
||||
public function get_default_language();
|
||||
public function get_admin_language();
|
||||
}
|
||||
Reference in New Issue
Block a user