first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,501 @@
<?php
/**
* @package wpml-core
*/
function icl_reset_language_data() {
global $wpdb, $sitepress;
$active = $wpdb->get_col( "SELECT code FROM {$wpdb->prefix}icl_languages WHERE active = 1" );
$wpdb->query( "TRUNCATE TABLE `{$wpdb->prefix}icl_languages`" );
SitePress_Setup::fill_languages();
$wpdb->query( "TRUNCATE TABLE `{$wpdb->prefix}icl_languages_translations`" );
SitePress_Setup::fill_languages_translations();
$wpdb->query( "TRUNCATE TABLE `{$wpdb->prefix}icl_flags`" );
SitePress_Setup::fill_flags();
// restore active
$wpdb->query( "UPDATE {$wpdb->prefix}icl_languages SET active=1 WHERE code IN(" . wpml_prepare_in( $active ) . ')' );
$wpdb->update( $wpdb->prefix . 'icl_flags', array( 'from_template' => 0 ), null );
$codes = $wpdb->get_col( "SELECT code FROM {$wpdb->prefix}icl_languages" );
foreach ( $codes as $code ) {
if ( ! $code || $wpdb->get_var(
$wpdb->prepare( "SELECT lang_code FROM {$wpdb->prefix}icl_flags WHERE lang_code = %s", $code )
)
) {
continue;
}
if ( ! file_exists( WPML_PLUGIN_PATH . '/res/flags/' . $code . '.png' ) ) {
$file = 'nil.png';
} else {
$file = $code . '.png';
}
$wpdb->insert(
$wpdb->prefix . 'icl_flags',
array(
'lang_code' => $code,
'flag' => $file,
'from_template' => 0,
)
);
}
$last_default_language = $sitepress !== null ? $sitepress->get_default_language() : 'en';
if ( ! in_array( $last_default_language, $codes ) ) {
$allowed_langs = array_intersect( array_keys( $sitepress->get_active_languages() ), $codes );
$sitepress->set_default_language( array_pop( $allowed_langs ) );
}
icl_cache_clear();
do_action( 'wpml_translation_update', array( 'type' => 'reset' ) );
}
function icl_sitepress_activate() {
global $wpdb;
$charset_collate = SitePress_Setup::get_charset_collate();
try {
SitePress_Setup::fill_languages();
SitePress_Setup::fill_languages_translations();
SitePress_Setup::fill_flags();
// translations
$table_name = $wpdb->prefix . 'icl_translations';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`translation_id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`element_type` VARCHAR( 60 ) NOT NULL DEFAULT 'post_post',
`element_id` BIGINT NULL DEFAULT NULL ,
`trid` BIGINT NOT NULL ,
`language_code` VARCHAR( 7 ) NOT NULL,
`source_language_code` VARCHAR( 7 ),
UNIQUE KEY `el_type_id` (`element_type`,`element_id`),
UNIQUE KEY `trid_lang` (`trid`,`language_code`),
KEY `trid` (`trid`),
KEY `id_type_language` (`element_id`, `element_type`, `language_code`)
) {$charset_collate}";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
// translation_status table
$table_name = $wpdb->prefix . 'icl_translation_status';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`rid` bigint(20) NOT NULL AUTO_INCREMENT,
`translation_id` bigint(20) NOT NULL,
`status` tinyint(4) NOT NULL,
`translator_id` bigint(20) NOT NULL,
`needs_update` tinyint(4) NOT NULL,
`md5` varchar(32) NOT NULL,
`translation_service` varchar(16) NOT NULL,
`batch_id` int DEFAULT 0 NOT NULL,
`translation_package` longtext NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`links_fixed` tinyint(4) NOT NULL DEFAULT 0,
`_prevstate` longtext,
`uuid` varchar(36) NULL,
`tp_id` INT NULL DEFAULT NULL,
`tp_revision` INT NOT NULL DEFAULT 1,
`ts_status` TEXT NULL DEFAULT NULL,
`review_status` ENUM('NEEDS_REVIEW', 'EDITING', 'ACCEPTED'),
`ate_comm_retry_count` INT(11) UNSIGNED DEFAULT 0,
PRIMARY KEY (`rid`),
UNIQUE KEY `translation_id` (`translation_id`)
) {$charset_collate}
";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
// translation jobs
$table_name = $wpdb->prefix . 'icl_translate_job';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`job_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`rid` BIGINT UNSIGNED NOT NULL ,
`translator_id` INT UNSIGNED NOT NULL ,
`translated` TINYINT UNSIGNED NOT NULL DEFAULT 0,
`manager_id` INT UNSIGNED NOT NULL ,
`revision` INT UNSIGNED NULL,
`title` VARCHAR(160) NULL,
`deadline_date` DATETIME NULL,
`completed_date` DATETIME NULL,
`editor` VARCHAR(16) NULL,
`editor_job_id` BIGINT(20) UNSIGNED NULL,
`edit_timestamp` INT(11) UNSIGNED NULL,
`automatic` TINYINT UNSIGNED NOT NULL DEFAULT 0,
INDEX ( `rid` , `translator_id` )
) {$charset_collate}
";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
// translate table
$table_name = $wpdb->prefix . 'icl_translate';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`tid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`job_id` BIGINT UNSIGNED NOT NULL ,
`content_id` BIGINT UNSIGNED NOT NULL ,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`field_type` VARCHAR( 160 ) NOT NULL ,
`field_wrap_tag` VARCHAR( 16 ) NOT NULL ,
`field_format` VARCHAR( 16 ) NOT NULL ,
`field_translate` TINYINT NOT NULL ,
`field_data` longtext NOT NULL ,
`field_data_translated` longtext NOT NULL ,
`field_finished` TINYINT NOT NULL DEFAULT 0,
INDEX ( `job_id` )
) {$charset_collate}
";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
// batches table
$table_name = $wpdb->prefix . 'icl_translation_batches';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS {$wpdb->prefix}icl_translation_batches (
`id` int(11) NOT NULL AUTO_INCREMENT,
`batch_name` text NOT NULL,
`tp_id` int NULL,
`ts_url` text NULL,
`last_update` DATETIME NULL,
PRIMARY KEY (`id`)
) {$charset_collate}
";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
// languages locale file names
$table_name = $wpdb->prefix . 'icl_locale_map';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`code` VARCHAR( 7 ) NOT NULL ,
`locale` VARCHAR( 35 ) NOT NULL ,
PRIMARY KEY (`code` ,`locale`)
) {$charset_collate}";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
/* general string translation */
$translation_priority_default = __( 'Optional', 'sitepress' );
$table_name = $wpdb->prefix . 'icl_strings';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`language` varchar(7) NOT NULL,
`context` varchar(" . WPML_STRING_TABLE_NAME_CONTEXT_LENGTH . ') NOT NULL,
`name` varchar(' . WPML_STRING_TABLE_NAME_CONTEXT_LENGTH . ") NOT NULL,
`value` longtext NOT NULL,
`string_package_id` BIGINT unsigned NULL,
`location` BIGINT unsigned NULL,
`wrap_tag` VARCHAR( 16 ) NOT NULL,
`type` VARCHAR(40) NOT NULL DEFAULT 'LINE',
`title` VARCHAR(160) NULL,
`status` TINYINT NOT NULL,
`gettext_context` TEXT NOT NULL,
`domain_name_context_md5` VARCHAR(32) NOT NULL,
`translation_priority` varchar(160) NOT NULL,
`word_count` int unsigned NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uc_domain_name_context_md5` (`domain_name_context_md5`),
KEY `language_context` (`language`, `context`),
KEY `icl_strings_name` (`name` ASC),
KEY `icl_strings_translation_priority` ( `translation_priority` ASC ),
KEY `context` ( `context` )
) {$charset_collate}
";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
$table_name = $wpdb->prefix . 'icl_string_translations';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`string_id` bigint(20) unsigned NOT NULL,
`language` varchar(10) NOT NULL,
`status` tinyint(4) NOT NULL,
`value` longtext NULL DEFAULT NULL,
`mo_string` longtext NULL DEFAULT NULL,
`translator_id` bigint(20) unsigned DEFAULT NULL,
`translation_service` varchar(16) DEFAULT '' NOT NULL,
`batch_id` int DEFAULT 0 NOT NULL,
`translation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `string_language` (`string_id`,`language`)
) {$charset_collate}";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
$table_name = $wpdb->prefix . 'icl_string_status';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`rid` BIGINT NOT NULL ,
`string_translation_id` BIGINT NOT NULL ,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`md5` VARCHAR( 32 ) NOT NULL,
INDEX ( `string_translation_id` )
) {$charset_collate}";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
$table_name = $wpdb->prefix . 'icl_string_positions';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`string_id` BIGINT NOT NULL ,
`kind` TINYINT,
`position_in_page` VARCHAR( 255 ) NOT NULL,
INDEX ( `string_id` )
) {$charset_collate}";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
// message status table
$table_name = $wpdb->prefix . 'icl_message_status';
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
if ( 0 !== strcasecmp( $found_table, $table_name ) ) {
$sql = "
CREATE TABLE IF NOT EXISTS `{$table_name}` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`rid` bigint(20) unsigned NOT NULL,
`object_id` bigint(20) unsigned NOT NULL,
`from_language` varchar(10) NOT NULL,
`to_language` varchar(10) NOT NULL,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
`md5` varchar(32) NOT NULL,
`object_type` varchar(64) NOT NULL,
`status` smallint(6) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `rid` (`rid`),
KEY `object_id` (`object_id`)
) {$charset_collate}";
if ( $wpdb->query( $sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
}
/* string translation - start */
$icl_translation_sql = "
CREATE TABLE IF NOT EXISTS {$wpdb->prefix}icl_core_status (
`id` BIGINT NOT NULL auto_increment,
`rid` BIGINT NOT NULL,
`module` VARCHAR( 16 ) NOT NULL ,
`origin` VARCHAR( 64 ) NOT NULL ,
`target` VARCHAR( 64 ) NOT NULL ,
`status` SMALLINT NOT NULL,
`tp_revision` INT NOT NULL DEFAULT 1,
`ts_status` TEXT NULL DEFAULT NULL,
PRIMARY KEY ( `id` ) ,
INDEX ( `rid` )
) {$charset_collate}
";
if ( $wpdb->query( $icl_translation_sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
$icl_translation_sql = "
CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}icl_content_status` (
`rid` BIGINT NOT NULL ,
`nid` BIGINT NOT NULL ,
`timestamp` DATETIME NOT NULL ,
`md5` VARCHAR( 32 ) NOT NULL ,
PRIMARY KEY ( `rid` ) ,
INDEX ( `nid` )
) {$charset_collate}
";
if ( $wpdb->query( $icl_translation_sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
$icl_translation_sql = "
CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}icl_node` (
`nid` BIGINT NOT NULL ,
`md5` VARCHAR( 32 ) NOT NULL ,
`links_fixed` TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY ( `nid` )
) {$charset_collate}
";
if ( $wpdb->query( $icl_translation_sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
$icl_translation_sql = "
CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}icl_reminders` (
`id` BIGINT NOT NULL ,
`message` TEXT NOT NULL ,
`url` TEXT NOT NULL ,
`can_delete` TINYINT NOT NULL ,
`show` TINYINT NOT NULL ,
PRIMARY KEY ( `id` )
) {$charset_collate}
";
if ( $wpdb->query( $icl_translation_sql ) === false ) {
throw new Exception( $wpdb->last_error );
}
} catch ( Exception $e ) {
trigger_error( $e->getMessage(), E_USER_ERROR );
exit;
}
// don't set the new version if a multi-step upgrade is in progress
if ( ! defined( 'ICL_MULTI_STEP_UPGRADE' ) ) {
delete_option( 'icl_sitepress_version' );
add_option( 'icl_sitepress_version', ICL_SITEPRESS_VERSION, '', true );
}
$iclsettings = get_option( 'icl_sitepress_settings' );
if ( $iclsettings === false ) {
$short_v = implode( '.', array_slice( explode( '.', ICL_SITEPRESS_VERSION ), 0, 3 ) );
$settings = array(
'hide_upgrade_notice' => $short_v,
);
add_option( 'icl_sitepress_settings', $settings, '', true );
} else {
// reset ajx_health_flag
$iclsettings['ajx_health_checked'] = 0;
$iclsettings['just_reactivated'] = 1;
update_option( 'icl_sitepress_settings', $iclsettings );
}
// Set new caps for all administrator role
wpml_enable_capabilities();
repair_el_type_collate();
WPML_Media_Duplication_Setup::initialize_settings();
do_action( 'wpml_activated' );
}
function icl_sitepress_deactivate() {
wp_clear_scheduled_hook( 'update_wpml_config_index' );
require_once WPML_PLUGIN_PATH . '/inc/cache.php';
icl_cache_clear();
do_action( 'wpml_deactivated' );
}
function icl_enable_capabilities() {
global $wp_roles;
if ( ! isset( $wp_roles ) || ! is_object( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
$iclsettings = get_option( 'icl_sitepress_settings' );
$icl_capabilities = icl_sitepress_get_capabilities();
// Set WPML capabilities to all roles with cap:"".
$roles = $wp_roles->get_names();
foreach ( $roles as $current_role => $role_name ) {
if ( isset( $wp_roles->roles[ $current_role ]['capabilities']['manage_options'] ) ) {
$role = get_role( $current_role );
if ( isset( $role ) && is_object( $role ) ) {
for ( $i = 0, $caps_limit = count( $icl_capabilities ); $i < $caps_limit; $i ++ ) {
if ( ! isset( $wp_roles->roles[ $current_role ]['capabilities'][ $icl_capabilities[ $i ] ] ) ) {
$role->add_cap( $icl_capabilities[ $i ] );
}
}
}
}
}
$user_admins = get_users(
array(
'role' => 'administrator',
)
);
if ( is_multisite() ) {
$super_admins = get_super_admins();
foreach ( $super_admins as $admin ) {
$super_admin = new WP_User( $admin );
if ( ! in_array( $super_admin, $user_admins, true ) ) {
$user_admins[] = $super_admin;
}
}
}
foreach ( $user_admins as $user ) {
if ( $user->exists() ) {
for ( $i = 0, $caps_limit = count( $icl_capabilities ); $i < $caps_limit; $i ++ ) {
$user->add_cap( $icl_capabilities[ $i ] );
}
}
}
$iclsettings['icl_capabilities_verified'] = true;
update_option( 'icl_sitepress_settings', $iclsettings );
}
/**
* Fires at plugins_loaded action, to call icl_enable_capabilities().
* https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlcore-5695
*/
function wpml_enable_capabilities() {
global $sitepress_settings;
/**
* In case of multisite, in network activation,
* including of pluggable.php before muplugins_loaded event trigger errors -
* we postpone executing of icl_enable_capabilities to after plugins_loaded event.
*
* In other cases we include pluggable.php earlier than in wp-settings.php
*/
if ( ! did_action( 'muplugins_loaded' ) ) {
add_action( 'plugins_loaded', 'wpml_enable_capabilities' );
} else {
if ( ! function_exists( 'get_user_by' ) ) {
require ABSPATH . WPINC . '/pluggable.php';
}
icl_enable_capabilities();
$sitepress_settings = get_option( 'icl_sitepress_settings' );
}
}

View File

@@ -0,0 +1,386 @@
<?php
class SitePress_Setup {
static function setup_complete() {
global $sitepress;
return $sitepress->get_setting( 'setup_complete' );
}
static function languages_complete() {
return self::active_languages_complete() && self::languages_table_is_complete();
}
private static function active_languages_complete() {
static $result = null;
if ( $result === null ) {
global $sitepress;
$result = $sitepress && 1 < count( $sitepress->get_active_languages() );
}
return $result;
}
/**
* @return array
*/
private static function get_languages_codes() {
static $languages_codes = array();
if ( ! $languages_codes ) {
$languages_codes = icl_get_languages_codes();
}
return $languages_codes;
}
/**
* @return array
*/
private static function get_languages_names() {
static $languages_names = array();
if ( ! $languages_names ) {
$languages_names = icl_get_languages_names();
}
return $languages_names;
}
private static function get_languages_names_count() {
return count( self::get_languages_names() );
}
static function get_charset_collate() {
static $charset_collate = null;
if ( $charset_collate == null ) {
$charset_collate = '';
global $wpdb;
if ( method_exists( $wpdb, 'has_cap' ) && $wpdb->has_cap( 'collation' ) ) {
$schema = wpml_get_upgrade_schema();
$charset = $schema->get_default_charset();
$collate = $schema->get_default_collate();
if ( $charset ) {
$charset_collate = "DEFAULT CHARACTER SET $charset";
}
if ( $collate ) {
$charset_collate .= " COLLATE $collate";
}
}
}
return $charset_collate;
}
private static function create_languages() {
$sql = "( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`code` VARCHAR( 7 ) NOT NULL ,
`english_name` VARCHAR( 128 ) NOT NULL ,
`major` TINYINT NOT NULL DEFAULT '0',
`active` TINYINT NOT NULL ,
`default_locale` VARCHAR( 35 ),
`tag` VARCHAR( 35 ),
`encode_url` TINYINT( 1 ) NOT NULL DEFAULT 0,
`country` VARCHAR(10) NULL DEFAULT NULL,
UNIQUE KEY `code` (`code`),
UNIQUE KEY `english_name` (`english_name`)
) ";
return self::create_table( 'icl_languages', $sql );
}
static function languages_table_is_complete() {
global $wpdb;
$table_name = $wpdb->prefix . 'icl_languages';
$sql = "SELECT count(id) FROM {$table_name}";
$records_count = $wpdb->get_var( $sql );
$languages_names_count = self::get_languages_names_count();
if ( $records_count < $languages_names_count ) {
return false;
}
$languages_codes = self::get_languages_codes();
$language_pairs = self::get_language_translations();
foreach ( self::get_languages_names() as $lang => $val ) {
foreach ( $val['tr'] as $k => $display ) {
$k = self::fix_language_name( $k );
$code = $languages_codes[ $lang ];
if ( ! array_key_exists( $code, $language_pairs ) || ! in_array( $languages_codes[ $k ], $language_pairs[ $code ], true ) ) {
return false;
}
}
}
return true;
}
/**
* @param string $language_name
*
* @return string
*/
protected static function fix_language_name( $language_name ) {
if ( strpos( $language_name, 'Norwegian Bokm' ) === 0 ) {
$language_name = 'Norwegian Bokmål';
}
return $language_name;
}
private static function get_language_translations() {
$result = array();
global $wpdb;
$table_name = $wpdb->prefix . 'icl_languages_translations';
$sql = "SELECT language_code, display_language_code FROM {$table_name}";
$rowset = $wpdb->get_results( $sql );
if ( is_array( $rowset ) ) {
foreach ( $rowset as $row ) {
$result[ $row->language_code ][] = $row->display_language_code;
}
}
return $result;
}
static function fill_languages() {
global $wpdb, $sitepress;
$languages_codes = icl_get_languages_codes();
$lang_locales = icl_get_languages_locales();
$table_name = $wpdb->prefix . 'icl_languages';
if ( ! self::create_languages() ) {
return false;
}
if ( ! self::languages_table_is_complete() ) {
// First truncate the table
$active_languages = ( $sitepress !== null
&& $sitepress->is_setup_complete() ) ? $sitepress->get_active_languages() : array();
$wpdb->hide_errors();
$sql = 'TRUNCATE ' . $table_name;
$truncate_result = $wpdb->query( $sql );
$wpdb->show_errors();
if ( false !== $truncate_result ) {
foreach ( self::get_languages_names() as $key => $val ) {
$language_code = $languages_codes[ $key ];
$default_locale = isset( $lang_locales[ $language_code ] ) ? $lang_locales[ $language_code ] : '';
$language_tag = strtolower( str_replace( '_', '-', $language_code ) );
$args = array(
'english_name' => $key,
'code' => $language_code,
'major' => $val['major'],
'active' => isset( $active_languages[ $language_code ] ) ? 1 : 0,
'default_locale' => $default_locale,
'tag' => $language_tag,
);
if ( $wpdb->insert( $table_name, $args ) === false ) {
return false;
}
}
}
}
return true;
}
private static function create_languages_translations() {
$sql = '(`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`language_code` VARCHAR( 7 ) NOT NULL ,
`display_language_code` VARCHAR( 7 ) NOT NULL ,
`name` VARCHAR( 255 ) NOT NULL,
UNIQUE(`language_code`, `display_language_code`)
)';
return self::create_table( 'icl_languages_translations', $sql );
}
static function fill_languages_translations() {
global $wpdb;
$languages_codes = icl_get_languages_codes();
$table_name = $wpdb->prefix . 'icl_languages_translations';
if ( ! self::create_languages_translations() ) {
return false;
}
if ( ! self::languages_table_is_complete() ) {
// First truncate the table
$wpdb->hide_errors();
$sql = 'TRUNCATE ' . $table_name;
$truncate_result = $wpdb->query( $sql );
$wpdb->show_errors();
if ( false !== $truncate_result ) {
$index = 1;
$insert_sql_parts = array();
$languages = self::get_languages_names();
if ( $languages ) {
foreach ( $languages as $lang => $val ) {
foreach ( $val['tr'] as $k => $display ) {
$k = self::fix_language_name( $k );
if ( ! trim( $display ) ) {
$display = $lang;
}
$inserts_language_data = array(
'id' => $index,
'language_code' => $languages_codes[ $lang ],
'display_language_code' => $languages_codes[ $k ],
'name' => $display,
);
$insert_sql_parts[] = $wpdb->prepare( '(%d, %s, %s, %s)', $inserts_language_data );
$index ++;
}
}
$insert_sql = implode( ",\n", $insert_sql_parts );
$insert_sql = "INSERT INTO {$table_name} (id, language_code, display_language_code, name) VALUES "
. $insert_sql;
if ( $wpdb->query( $insert_sql ) === false ) {
return false;
}
}
}
}
return true;
}
private static function create_table( $name, $table_sql ) {
global $wpdb;
$table_name = $wpdb->prefix . $name;
$found_table = (string) $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" );
return 0 === strcasecmp( $found_table, $table_name )
|| ( $wpdb->query(
sprintf( 'CREATE TABLE IF NOT EXISTS `%s` ', $table_name )
. $table_sql . ' '
. self::get_charset_collate()
) !== false );
}
private static function create_flags() {
$sql = "(`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`lang_code` VARCHAR( 10 ) NOT NULL ,
`flag` VARCHAR( 32 ) NOT NULL ,
`from_template` TINYINT NOT NULL DEFAULT '0',
UNIQUE (`lang_code`)
)";
return self::create_table( 'icl_flags', $sql );
}
public static function fill_flags() {
global $wpdb;
if ( self::create_flags() === false ) {
return;
}
$codes = $wpdb->get_col( "SELECT code FROM {$wpdb->prefix}icl_languages" );
foreach ( $codes as $code ) {
if ( ! $code || $wpdb->get_var(
$wpdb->prepare(
"SELECT lang_code
FROM {$wpdb->prefix}icl_flags
WHERE lang_code = %s ",
$code
)
)
) {
continue;
}
$code_parts = explode( '-', $code );
$file = 'nil.png';
if ( file_exists( WPML_PLUGIN_PATH . '/res/flags/' . $code . '.png' ) ) {
$file = $code . '.png';
} elseif ( file_exists( WPML_PLUGIN_PATH . '/res/flags/' . $code_parts[0] . '.png' ) ) {
$file = $code_parts[0] . '.png';
}
$wpdb->insert(
$wpdb->prefix . 'icl_flags',
array(
'lang_code' => $code,
'flag' => $file,
'from_template' => 0,
)
);
}
}
public static function insert_default_category( $lang_code ) {
global $sitepress;
$default_language = $sitepress->get_default_language();
if ( $lang_code === $default_language ) {
return;
}
// Get default categories.
$default_categories = $sitepress->get_setting( 'default_categories', array() );
if ( isset( $default_categories[ $lang_code ] ) ) {
return;
}
$sitepress->switch_locale( $lang_code );
$tr_cat = __( 'Uncategorized', 'sitepress' );
$tr_cat = $tr_cat === 'Uncategorized' && $lang_code !== 'en' ? 'Uncategorized @' . $lang_code : $tr_cat;
$tr_term = term_exists( $tr_cat, 'category' );
$sitepress->switch_locale();
// check if the term already exists
if ( $tr_term !== 0 && $tr_term !== null ) {
$tmp = get_term( $tr_term['term_taxonomy_id'], 'category', ARRAY_A );
} else {
$tmp = wp_insert_term( $tr_cat, 'category' );
}
// add it to settings['default_categories']
$default_categories[ $lang_code ] = $tmp['term_taxonomy_id'];
$sitepress->set_default_categories( $default_categories );
// update translations table
$default_category_trid = $sitepress->get_element_trid(
get_option( 'default_category' ),
'tax_category'
);
$sitepress->set_element_language_details(
$tmp['term_taxonomy_id'],
'tax_category',
$default_category_trid,
$lang_code,
$default_language
);
}
}

View File

@@ -0,0 +1,462 @@
<?php
class WPML_Installation extends WPML_WPDB_And_SP_User {
const WPML_START_VERSION_KEY = 'wpml_start_version';
public static function getStartVersion() {
return get_option( self::WPML_START_VERSION_KEY, '0.0.0' );
}
function go_to_setup1() {
// Reverse $this->prepopulate_translations()
$this->wpdb->query( "TRUNCATE TABLE {$this->wpdb->prefix}icl_translations" );
// Unset or reset sitepress settings
$settings = $this->sitepress->get_settings();
unset(
$settings['default_categories'],
$settings['default_language'],
$settings['setup_wizard_step']
);
$settings['existing_content_language_verified'] = 0;
$settings['active_languages'] = array();
$GLOBALS['sitepress_settings']['existing_content_language_verified'] = $settings['existing_content_language_verified'];
update_option( 'icl_sitepress_settings', $settings );
// Reverse $this->maybe_set_locale()
$this->wpdb->query( "TRUNCATE TABLE {$this->wpdb->prefix}icl_locale_map" );
// Make sure no language is active
$this->wpdb->update( $this->wpdb->prefix . 'icl_languages', array( 'active' => 0 ), array( 'active' => 1 ) );
}
/**
* Sets the locale in the icl_locale_map if it has not yet been set
*
* @param string $initial_language_code
*/
private function maybe_set_locale( $initial_language_code ) {
$q = "SELECT code FROM {$this->wpdb->prefix}icl_locale_map WHERE code=%s";
$q_prepared = $this->wpdb->prepare( $q, $initial_language_code );
if ( ! $this->wpdb->get_var( $q_prepared ) ) {
$q = "SELECT default_locale FROM {$this->wpdb->prefix}icl_languages WHERE code=%s";
$q_prepared = $this->wpdb->prepare( $q, $initial_language_code );
$default_locale = $this->wpdb->get_var( $q_prepared );
if ( $default_locale ) {
$this->wpdb->insert(
$this->wpdb->prefix . 'icl_locale_map',
array( 'code' => $initial_language_code, 'locale' => $default_locale )
);
}
}
}
public function finish_step2( $active_languages ) {
return $this->set_active_languages( $active_languages );
}
public function set_active_languages( $arr ) {
$tmp = $this->sanitize_language_input( $arr );
if ( (bool) $tmp === false ) {
return false;
}
foreach ( $tmp as $code ) {
$default_locale_prepared = $this->wpdb->prepare(
"SELECT default_locale FROM {$this->wpdb->prefix}icl_languages WHERE code= %s LIMIT 1",
$code
);
$default_locale = $this->wpdb->get_var( $default_locale_prepared );
if ( $default_locale ) {
$code_exists_prepared = $this->wpdb->prepare(
"SELECT code FROM {$this->wpdb->prefix}icl_locale_map WHERE code = %s LIMIT 1",
$code
);
$code_exists = $this->wpdb->get_var( $code_exists_prepared );
if ( $code_exists ) {
$this->wpdb->update(
$this->wpdb->prefix . 'icl_locale_map',
array( 'locale' => $default_locale ),
array( 'code' => $code )
);
} else {
$this->wpdb->insert(
$this->wpdb->prefix . 'icl_locale_map',
array( 'code' => $code, 'locale' => $default_locale )
);
}
}
SitePress_Setup::insert_default_category( $code );
}
$this->wpdb->query(
"UPDATE {$this->wpdb->prefix}icl_languages SET active = 1 WHERE code IN (" . wpml_prepare_in( $tmp ) . " ) "
);
$this->wpdb->query(
"UPDATE {$this->wpdb->prefix}icl_languages SET active = 0 WHERE code NOT IN (" . wpml_prepare_in( $tmp ) . " ) "
);
$this->updated_active_languages();
return true;
}
private function sanitize_language_input( $lang_codes ) {
$languages = $this->sitepress->get_languages( false, false, true );
$sanitized_codes = array();
$lang_codes = array_filter( array_unique( $lang_codes ) );
foreach ( $lang_codes as $code ) {
$code = esc_sql( trim( $code ) );
if ( isset( $languages[ $code ] ) ) {
$sanitized_codes[] = $code;
}
}
return $sanitized_codes;
}
public function finish_installation( ) {
icl_set_setting( 'store_frontend_cookie', 1 );
icl_set_setting( 'setup_complete', 1, true );
update_option( self::WPML_START_VERSION_KEY, ICL_SITEPRESS_VERSION );
do_action( 'wpml_setup_completed' );
}
public function store_site_key( $site_key = false ) {
if ( $site_key ) {
icl_set_setting( 'site_key', $site_key, true );
}
}
public function finish_step3() {
$this->maybe_move_setup( 4 );
}
private function maybe_move_setup( $step ) {
$setup_complete = icl_get_setting( 'setup_complete' );
if ( empty( $setup_complete ) ) {
icl_set_setting( 'setup_wizard_step', $step, true );
}
}
private function updated_active_languages() {
wp_cache_init();
icl_cache_clear();
$this->refresh_active_lang_cache( wpml_get_setting_filter( false, 'default_language' ), true );
$this->update_languages_order();
wpml_reload_active_languages_setting( true );
$active_langs = $this->sitepress->get_active_languages( true );
$this->maybe_move_setup( 3 );
if ( count( $active_langs ) > 1 ) {
icl_set_setting( 'dont_show_help_admin_notice', true );
}
}
public function finish_step1( $initial_language_code ) {
$this->set_initial_default_category( $initial_language_code );
$this->prepopulate_translations( $initial_language_code );
$this->update_active_language( $initial_language_code );
$admin_language = $this->get_admin_language( $initial_language_code );
$this->maybe_set_locale( $admin_language );
icl_set_setting( 'existing_content_language_verified', 1 );
icl_set_setting( 'default_language', $initial_language_code );
icl_set_setting( 'setup_wizard_step', 2 );
icl_save_settings();
wp_cache_flush();
$this->refresh_active_lang_cache( $initial_language_code );
add_filter( 'locale', array( $this->sitepress, 'locale_filter' ), 10, 1 );
if ( ! array_key_exists( 'wp_styles', $GLOBALS ) || ! $GLOBALS['wp_styles'] ) {
wp_styles();
}
if ( $this->sitepress->is_rtl( $admin_language ) ) {
$GLOBALS['text_direction'] = 'rtl';
$GLOBALS['wp_styles']->text_direction = 'rtl';
} else {
$GLOBALS['text_direction'] = 'ltr';
$GLOBALS['wp_styles']->text_direction = 'ltr';
}
$GLOBALS['wp_locale'] = new WP_Locale();
$GLOBALS['locale'] = $this->sitepress->get_locale( $admin_language );
do_action( 'icl_initial_language_set' );
}
/**
* @param string $initial_language_code
*
* @return string
*/
private function get_admin_language( $initial_language_code ) {
$user_locale = get_user_meta( get_current_user_id(), 'locale', true );
if ( $user_locale ) {
$lang = $this->sitepress->get_language_code_from_locale( $user_locale );
if ( $lang ) {
return $lang;
}
}
return $initial_language_code;
}
private function set_initial_default_category( $initial_lang ) {
$blog_default_cat = get_option( 'default_category' );
$blog_default_cat_tax_id = $this->wpdb->get_var(
$this->wpdb->prepare(
" SELECT term_taxonomy_id
FROM {$this->wpdb->term_taxonomy}
WHERE term_id=%d
AND taxonomy='category'",
$blog_default_cat
)
);
if ($initial_lang !== 'en') {
$this->rename_default_category_of_initial_language( $initial_lang, $blog_default_cat );
}
icl_set_setting( 'default_categories', array( $initial_lang => $blog_default_cat_tax_id ), true );
}
private function rename_default_category_of_initial_language( $initial_lang, $category_id ) {
global $sitepress;
$sitepress->switch_locale( $initial_lang );
$tr_cat = __( 'Uncategorized', 'sitepress' );
$tr_cat = $tr_cat === 'Uncategorized' ? 'Uncategorized @' . $initial_lang : $tr_cat;
$sitepress->switch_locale();
wp_update_term( $category_id, 'category', array(
'name' => $tr_cat,
'slug' => sanitize_title( $tr_cat ),
) );
}
/**
* @param string $display_language
* @param bool $active_only
* @param bool $major_first
* @param string $order_by
*
* @return array<string,\stdClass>
*/
public function refresh_active_lang_cache( $display_language, $active_only = false, $major_first = false, $order_by = 'english_name' ) {
$active_snippet = $active_only ? " l.active = 1 AND " : "";
$res_query
= "
SELECT
l.code,
l.id,
english_name,
nt.name AS native_name,
major,
active,
default_locale,
encode_url,
tag,
lt.name AS display_name
FROM {$this->wpdb->prefix}icl_languages l
JOIN {$this->wpdb->prefix}icl_languages_translations nt
ON ( nt.language_code = l.code AND nt.display_language_code = l.code )
LEFT OUTER JOIN {$this->wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
WHERE {$active_snippet}
( lt.display_language_code = %s
OR (lt.display_language_code = 'en'
AND NOT EXISTS ( SELECT *
FROM {$this->wpdb->prefix}icl_languages_translations ls
WHERE ls.language_code = l.code
AND ls.display_language_code = %s ) ) )
GROUP BY l.code";
$order_by_fields = array();
if ( $major_first ) {
$order_by_fields[] = 'major DESC';
}
$order_by_fields[] = ( $order_by ? $order_by : 'english_name' ) . ' ASC';
$res_query .= PHP_EOL . 'ORDER BY ' . implode( ', ', $order_by_fields );
$res_query_prepared = $this->wpdb->prepare( $res_query, $display_language, $display_language );
$res = $this->wpdb->get_results( $res_query_prepared, ARRAY_A );
$languages = array();
$icl_cache = $this->sitepress->get_language_name_cache();
foreach ( (array) $res as $r ) {
$languages[ $r[ 'code' ] ] = $r;
$icl_cache->set( 'language_details_' . $r['code'] . $display_language, $r );
}
if ( $active_only ) {
$icl_cache->set( 'in_language_' . $display_language . '_' . $major_first . '_' . $order_by, $languages );
} else {
$icl_cache->set( 'all_language_' . $display_language . '_' . $major_first . '_' . $order_by, $languages );
}
$icl_cache->save_cache_if_required();
return $languages;
}
private function update_languages_order() {
$needs_update = false;
$current_order = $this->sitepress->get_setting( 'languages_order', array() );
if ( ! is_array( $current_order ) ) {
$current_order = array();
}
$languages = $this->sitepress->get_languages( false, false, true );
$new_order = $current_order;
foreach ( $languages as $language_code => $language ) {
if ( ! in_array( $language_code, $new_order ) && '1' === $language['active'] ) {
$new_order[] = $language_code;
$needs_update = true;
}
if ( in_array( $language_code, $new_order ) && '1' !== $language['active'] ) {
$new_order = array_diff( $new_order, array( $language_code ) );
$needs_update = true;
}
}
if ( $needs_update ) {
$new_order = array_values( $new_order );
$this->sitepress->set_setting( 'languages_order', $new_order, true );
}
}
private function prepopulate_translations( $lang ) {
$existing_lang_verified = icl_get_setting( 'existing_content_language_verified' );
if ( ! empty( $existing_lang_verified ) ) {
return;
}
icl_cache_clear();
// case of icl_sitepress_settings accidentally lost
// if there's at least one translation do not initialize the languages for elements
$one_translation = $this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT translation_id FROM {$this->wpdb->prefix}icl_translations WHERE language_code<>%s",
$lang
)
);
if ( $one_translation ) {
return;
}
$this->wpdb->query( "TRUNCATE TABLE {$this->wpdb->prefix}icl_translations" );
$this->wpdb->query(
$this->wpdb->prepare(
"
INSERT INTO {$this->wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
SELECT CONCAT('post_',post_type), ID, ID, %s, NULL FROM {$this->wpdb->posts} WHERE post_status IN ('draft', 'publish','schedule','future','private', 'pending')
",
$lang
)
);
$maxtrid = 1 + (int) $this->wpdb->get_var( "SELECT MAX(trid) FROM {$this->wpdb->prefix}icl_translations" );
global $wp_taxonomies;
$taxonomies = array_keys( (array) $wp_taxonomies );
foreach ( $taxonomies as $tax ) {
$element_type = 'tax_' . $tax;
$insert_query
= "
INSERT INTO {$this->wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
SELECT %s, term_taxonomy_id, %d+term_taxonomy_id, %s, NULL FROM {$this->wpdb->term_taxonomy} WHERE taxonomy = %s
";
$insert_prepare = $this->wpdb->prepare( $insert_query, array( $element_type, $maxtrid, $lang, $tax ) );
$this->wpdb->query( $insert_prepare );
$maxtrid = 1 + (int) $this->wpdb->get_var( "SELECT MAX(trid) FROM {$this->wpdb->prefix}icl_translations" );
}
$this->wpdb->query(
$this->wpdb->prepare(
"
INSERT INTO {$this->wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
SELECT 'comment', comment_ID, {$maxtrid}+comment_ID, %s, NULL FROM {$this->wpdb->comments}
",
$lang
)
);
}
public function update_active_language( $lang ) {
$this->wpdb->update( $this->wpdb->prefix . 'icl_languages', array( 'active' => '1' ), array( 'code' => $lang ) );
}
function reset_language_data() {
global $sitepress, $wpdb;
$active = $this->wpdb->get_col( "SELECT code FROM {$this->wpdb->prefix}icl_languages WHERE active = 1" );
$this->wpdb->query( "TRUNCATE TABLE `{$this->wpdb->prefix}icl_languages`" );
SitePress_Setup::fill_languages();
$this->wpdb->query( "TRUNCATE TABLE `{$this->wpdb->prefix}icl_languages_translations`" );
SitePress_Setup::fill_languages_translations();
$this->wpdb->query( "TRUNCATE TABLE `{$this->wpdb->prefix}icl_flags`" );
SitePress_Setup::fill_flags();
//restore active
$this->wpdb->query(
"UPDATE {$this->wpdb->prefix}icl_languages SET active=1 WHERE code IN(" . wpml_prepare_in( $active ) . ")"
);
$this->wpdb->update( $this->wpdb->prefix . 'icl_flags', array( 'from_template' => 0 ), null );
$codes = $this->wpdb->get_col( "SELECT code FROM {$this->wpdb->prefix}icl_languages" );
foreach ( $codes as $code ) {
if ( ! $code || $this->wpdb->get_var(
$this->wpdb->prepare( "SELECT lang_code FROM {$this->wpdb->prefix}icl_flags WHERE lang_code = %s", $code )
)
) {
continue;
}
if ( ! file_exists( WPML_PLUGIN_PATH . '/res/flags/' . $code . '.png' ) ) {
$file = 'nil.png';
} else {
$file = $code . '.png';
}
$this->wpdb->insert(
$this->wpdb->prefix . 'icl_flags',
array( 'lang_code' => $code, 'flag' => $file, 'from_template' => 0 )
);
}
$last_default_language = $this->sitepress !== null ? $this->sitepress->get_default_language() : 'en';
if ( ! in_array( $last_default_language, $codes ) ) {
$last_active_languages = $this->sitepress->get_active_languages();
foreach ( $last_active_languages as $code => $last_active_language ) {
if ( in_array( $code, $codes ) ) {
$this->sitepress->set_default_language( $code );
break;
}
}
}
$language_pair_records = new WPML_Language_Pair_Records( $wpdb, new WPML_Language_Records( $wpdb ) );
$users = get_users( [ 'fields' => [ 'ID' ] ] );
foreach ( $users as $user ) {
$language_pair_records->remove_invalid_language_pairs( $user->ID );
}
icl_cache_clear();
$sitepress->get_translations_cache()->clear();
$sitepress->clear_flags_cache();
$sitepress->get_language_name_cache()->clear();
}
}

View File

@@ -0,0 +1,25 @@
<?php
class WPML_Media_Duplication_Setup {
const MEDIA_SETTINGS_OPTION_KEY = '_wpml_media';
public static function initialize_settings() {
if ( ! get_option( self::MEDIA_SETTINGS_OPTION_KEY, [] ) ) {
$settings = [
'new_content_settings' => [
'always_translate_media' => true,
'duplicate_media' => true,
'duplicate_featured' => true,
],
'translate_media_library_texts' => false,
];
update_option( self::MEDIA_SETTINGS_OPTION_KEY, $settings );
}
}
public static function isTranslateMediaLibraryTextsEnabled() {
$settings = get_option( self::MEDIA_SETTINGS_OPTION_KEY, [] );
return \WPML\FP\Obj::propOr(false, 'translate_media_library_texts', $settings);
}
}

View File

@@ -0,0 +1,315 @@
<?php
class WPML_Settings_Helper {
const KEY_CPT_UNLOCK_OPTION = 'custom_posts_unlocked_option';
const KEY_TAXONOMY_UNLOCK_OPTION = 'taxonomies_unlocked_option';
/** @var SitePress */
protected $sitepress;
/** @var WPML_Post_Translation */
protected $post_translation;
/**
* @var WPML_Settings_Filters
*/
private $filters;
/**
* @param WPML_Post_Translation $post_translation
* @param SitePress $sitepress
*/
public function __construct( WPML_Post_Translation $post_translation, SitePress $sitepress ) {
$this->sitepress = $sitepress;
$this->post_translation = $post_translation;
}
/**
* @return WPML_Settings_Filters
*/
private function get_filters() {
if ( ! $this->filters ) {
$this->filters = new WPML_Settings_Filters();
}
return $this->filters;
}
function set_post_type_translatable( $post_type ) {
$this->set_post_type_translate_mode( $post_type, WPML_CONTENT_TYPE_TRANSLATE );
}
function set_post_type_display_as_translated( $post_type ) {
$this->set_post_type_translate_mode( $post_type, WPML_CONTENT_TYPE_DISPLAY_AS_IF_TRANSLATED );
}
function set_post_type_not_translatable( $post_type ) {
$sync_settings = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
if ( isset( $sync_settings[ $post_type ] ) ) {
unset( $sync_settings[ $post_type ] );
}
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'custom_posts_sync_option', $sync_settings, true );
}
private function set_post_type_translate_mode( $post_type, $mode ) {
$sync_settings = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
$sync_settings[ $post_type ] = $mode;
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'custom_posts_sync_option', $sync_settings, true );
$this->sitepress->verify_post_translations( $post_type );
$this->post_translation->reload();
}
function set_taxonomy_translatable( $taxonomy ) {
$this->set_taxonomy_translatable_mode( $taxonomy, WPML_CONTENT_TYPE_TRANSLATE );
}
function set_taxonomy_display_as_translated( $taxonomy ) {
$this->set_taxonomy_translatable_mode( $taxonomy, WPML_CONTENT_TYPE_DISPLAY_AS_IF_TRANSLATED );
}
function set_taxonomy_translatable_mode( $taxonomy, $mode ) {
$sync_settings = $this->sitepress->get_setting( 'taxonomies_sync_option', array() );
$sync_settings[ $taxonomy ] = $mode;
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'taxonomies_sync_option', $sync_settings, true );
$this->sitepress->verify_taxonomy_translations( $taxonomy );
}
function set_taxonomy_not_translatable( $taxonomy ) {
$sync_settings = $this->sitepress->get_setting( 'taxonomies_sync_option', array() );
if ( isset( $sync_settings[ $taxonomy ] ) ) {
unset( $sync_settings[ $taxonomy ] );
}
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'taxonomies_sync_option', $sync_settings, true );
}
function set_post_type_translation_unlocked_option( $post_type, $unlocked = true ) {
$unlocked_settings = $this->sitepress->get_setting( 'custom_posts_unlocked_option', array() );
$unlocked_settings[ $post_type ] = $unlocked ? 1 : 0;
$this->sitepress->set_setting( 'custom_posts_unlocked_option', $unlocked_settings, true );
}
function set_taxonomy_translation_unlocked_option( $taxonomy, $unlocked = true ) {
$unlocked_settings = $this->sitepress->get_setting( 'taxonomies_unlocked_option', array() );
$unlocked_settings[ $taxonomy ] = $unlocked ? 1 : 0;
$this->sitepress->set_setting( 'taxonomies_unlocked_option', $unlocked_settings, true );
}
/**
* @deprecated use the action `wpml_activate_slug_translation` instead
* or `WPML_ST_Post_Slug_Translation_Settings` instead (on ST side)
*
* @param string $post_type
*/
function activate_slug_translation( $post_type ) {
$slug_settings = $this->sitepress->get_setting( 'posts_slug_translation', array() );
$slug_settings['types'] = isset( $slug_settings['types'] )
? $slug_settings['types'] : array();
$slug_settings['types'][ $post_type ] = 1;
/** @deprected key `on`, use option `wpml_base_slug_translation` instead */
$slug_settings['on'] = 1;
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'posts_slug_translation', $slug_settings, true );
update_option( 'wpml_base_slug_translation', 1 );
}
/**
* @deprecated use `WPML_ST_Post_Slug_Translation_Settings` instead (on ST side)
*
* @param string $post_type
*/
function deactivate_slug_translation( $post_type ) {
$slug_settings = $this->sitepress->get_setting( 'posts_slug_translation', array() );
if ( isset( $slug_settings['types'][ $post_type ] ) ) {
unset( $slug_settings['types'][ $post_type ] );
}
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'posts_slug_translation', $slug_settings, true );
}
/**
* @param array[] $taxs_obj_type
*
* @see \WPML_Config::maybe_add_filter
*
* @return array
*/
function _override_get_translatable_taxonomies( $taxs_obj_type ) {
global $wp_taxonomies;
$taxs = $taxs_obj_type['taxs'];
$object_type = $taxs_obj_type['object_type'];
foreach ( $taxs as $k => $tax ) {
if ( ! $this->sitepress->is_translated_taxonomy( $tax ) ) {
unset( $taxs[ $k ] );
}
}
$tm_settings = $this->sitepress->get_setting( 'translation-management', array() );
foreach ( $tm_settings['taxonomies_readonly_config'] as $tx => $translate ) {
if ( $translate
&& ! in_array( $tx, $taxs )
&& isset( $wp_taxonomies[ $tx ] )
&& in_array( $object_type, $wp_taxonomies[ $tx ]->object_type )
) {
$taxs[] = $tx;
}
}
$ret = array(
'taxs' => $taxs,
'object_type' => $taxs_obj_type['object_type'],
);
return $ret;
}
/**
* @param array[] $types
*
* @see \WPML_Config::maybe_add_filter
*
* @return array
*/
function _override_get_translatable_documents( $types ) {
$tm_settings = $this->sitepress->get_setting( 'translation-management', array() );
$cpt_unlocked_options = $this->sitepress->get_setting( 'custom_posts_unlocked_option', array() );
foreach ( $types as $k => $type ) {
if ( isset( $tm_settings['custom-types_readonly_config'][ $k ] )
&& ! $tm_settings['custom-types_readonly_config'][ $k ]
) {
unset( $types[ $k ] );
}
}
$types = $this->get_filters()->get_translatable_documents( $types, $tm_settings['custom-types_readonly_config'], $cpt_unlocked_options );
return $types;
}
/**
* Updates the custom post type translation settings with new settings.
*
* @param array $new_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new custom post type settings after the update
*/
function update_cpt_sync_settings( array $new_options ) {
$cpt_sync_options = $this->sitepress->get_setting( WPML_Element_Sync_Settings_Factory::KEY_POST_SYNC_OPTION, array() );
$cpt_sync_options = array_merge( $cpt_sync_options, $new_options );
$new_options = array_filter( $new_options );
$this->clear_ls_languages_cache();
do_action( 'wpml_verify_post_translations', $new_options );
do_action( 'wpml_save_cpt_sync_settings' );
$this->sitepress->set_setting( WPML_Element_Sync_Settings_Factory::KEY_POST_SYNC_OPTION, $cpt_sync_options, true );
return $cpt_sync_options;
}
/**
* Updates the taxonomy type translation settings with new settings.
*
* @param array $new_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new taxonomy type settings after the update
*/
function update_taxonomy_sync_settings( array $new_options ) {
$taxonomy_sync_options = $this->sitepress->get_setting( WPML_Element_Sync_Settings_Factory::KEY_TAX_SYNC_OPTION, [] );
$changed_sync_options = array_diff_assoc( $new_options, $taxonomy_sync_options );
$taxonomy_sync_options = array_merge( $taxonomy_sync_options, $new_options );
foreach ( $changed_sync_options as $taxonomy_name => $taxonomy_sync_option ) {
$this->sitepress->verify_taxonomy_translations( $taxonomy_name );
}
$this->clear_ls_languages_cache();
do_action( 'wpml_save_taxonomy_sync_settings' );
$this->sitepress->set_setting( WPML_Element_Sync_Settings_Factory::KEY_TAX_SYNC_OPTION, $taxonomy_sync_options, true );
return $taxonomy_sync_options;
}
/**
* Updates the custom post type unlocked settings with new settings.
*
* @param array $unlock_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new custom post type unlocked settings after the update
*/
function update_cpt_unlocked_settings( array $unlock_options ) {
return $this->update_unlocked_settings( $unlock_options, self::KEY_CPT_UNLOCK_OPTION );
}
/**
* Updates the taxonomy type unlocked settings with new settings.
*
* @param array $unlock_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new taxonomy type unlocked settings after the update
*/
function update_taxonomy_unlocked_settings( array $unlock_options ) {
return $this->update_unlocked_settings( $unlock_options, self::KEY_TAXONOMY_UNLOCK_OPTION );
}
/**
* @param array $unlock_options
* @param string $setting_key
*
* @return array
*/
private function update_unlocked_settings( array $unlock_options, $setting_key ) {
$cpt_unlock_options = $this->sitepress->get_setting( $setting_key, array() );
$cpt_unlock_options = array_merge( $cpt_unlock_options, $unlock_options );
$this->sitepress->set_setting( $setting_key, $cpt_unlock_options, true );
return $cpt_unlock_options;
}
/**
* @param string $config_type
*/
function maybe_add_filter( $config_type ) {
if ( $config_type === 'taxonomies' ) {
add_filter(
'get_translatable_taxonomies',
array( $this, '_override_get_translatable_taxonomies' )
);
} elseif ( $config_type === 'custom-types' ) {
add_filter(
'get_translatable_documents',
array( $this, '_override_get_translatable_documents' )
);
}
}
private function clear_ls_languages_cache() {
$cache = new WPML_WP_Cache( 'ls_languages' );
$cache->flush_group_cache();
}
}