TranslatePlus API codes. */ const CODE_MAP = array( 'zh-hans' => 'zh-CN', 'zh-hant' => 'zh-TW', 'zh' => 'zh-CN', 'zh-cn' => 'zh-CN', 'zh-tw' => 'zh-TW', 'pt-br' => 'pt', 'pt-pt' => 'pt', 'pb' => 'pt', ); /** @var Settings */ private $settings; public function __construct( Settings $settings ) { $this->settings = $settings; } public static function code( string $code ): string { $c = strtolower( str_replace( '_', '-', $code ) ); if ( isset( self::CODE_MAP[ $c ] ) ) { return self::CODE_MAP[ $c ]; } return strtok( $c, '-' ); } /** * Which sources have data on this site. * * @return array */ public function detect(): array { global $wpdb; $wpml = (bool) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'icl_translations' ) ); // phpcs:ignore WordPress.DB $pll = taxonomy_exists( 'post_translations' ) || (bool) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE taxonomy = %s', $wpdb->term_taxonomy, 'post_translations' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $trp = (array) $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->prefix . 'trp_dictionary_' ) . '%' ) ); // phpcs:ignore WordPress.DB return array( 'wpml' => array( 'available' => $wpml, 'label' => 'WPML', 'details' => $wpml ? __( 'Links existing translated posts and terms.', 'translateplus' ) : '', ), 'polylang' => array( 'available' => $pll, 'label' => 'Polylang', 'details' => $pll ? __( 'Links existing translated posts and terms.', 'translateplus' ) : '', ), 'translatepress' => array( 'available' => (bool) $trp, 'label' => 'TranslatePress', /* translators: %d: number of dictionaries */ 'details' => $trp ? sprintf( _n( 'Imports %d dictionary into translation memory.', 'Imports %d dictionaries into translation memory.', count( $trp ), 'translateplus' ), count( $trp ) ) : '', ), ); } /** * @return array{posts:int, terms:int, memory:int, languages:string[]} */ public function run( string $source ): array { switch ( $source ) { case 'wpml': $result = $this->wpml(); break; case 'polylang': $result = $this->polylang(); break; case 'translatepress': $result = $this->translatepress(); break; default: throw new \InvalidArgumentException( esc_html__( 'Unknown migration source.', 'translateplus' ) ); } if ( $result['languages'] ) { $this->settings->update( array( 'languages' => array_values( array_unique( array_merge( Languages::targets(), array_diff( $result['languages'], array( Languages::source() ) ) ) ) ) ) ); } Logger::info( sprintf( 'Migration from %s: %d posts, %d terms, %d memory entries.', $source, $result['posts'], $result['terms'], $result['memory'] ) ); return $result; } private function wpml(): array { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT trid, element_type, element_id, language_code, source_language_code FROM %i WHERE element_id IS NOT NULL', $wpdb->prefix . 'icl_translations' ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $trids = array(); foreach ( $rows as $r ) { $trids[ $r['element_type'] . '|' . $r['trid'] ][] = $r; } $posts = 0; $terms = 0; $langs = array(); foreach ( $trids as $key => $members ) { $type = 0 === strpos( $key, 'post_' ) ? 'post' : ( 0 === strpos( $key, 'tax_' ) ? 'term' : '' ); $source = current( wp_list_filter( $members, array( 'source_language_code' => null ) ) ) ?: $members[0]; if ( ! $type || count( $members ) < 2 ) { continue; } $source_id = (int) $source['element_id']; if ( 'term' === $type ) { $source_id = $this->term_id_from_tt( $source_id ); } foreach ( $members as $m ) { $lang = self::code( $m['language_code'] ); $langs[] = $lang; if ( $m === $source ) { continue; } $id = 'term' === $type ? $this->term_id_from_tt( (int) $m['element_id'] ) : (int) $m['element_id']; if ( $id && $source_id ) { Groups::link( $type, $source_id, $id, $lang, Groups::STATUS_TRANSLATED ); 'post' === $type ? $posts++ : $terms++; } } } $settings = get_option( 'icl_sitepress_settings' ); if ( is_array( $settings ) && isset( $settings['language_negotiation_type'] ) ) { $mode = array( 1 => 'directory', 2 => 'domain', 3 => 'query' )[ (int) $settings['language_negotiation_type'] ] ?? 'directory'; $this->settings->update( array( 'url_mode' => $mode ) ); } return array( 'posts' => $posts, 'terms' => $terms, 'memory' => 0, 'languages' => array_values( array_unique( $langs ) ) ); } private function term_id_from_tt( int $tt_id ): int { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM %i WHERE term_taxonomy_id = %d", $wpdb->term_taxonomy, $tt_id ) ); // phpcs:ignore WordPress.DB } private function polylang(): array { global $wpdb; $posts = 0; $terms = 0; $langs = array(); $default = ''; $pll_opts = get_option( 'polylang' ); if ( is_array( $pll_opts ) && ! empty( $pll_opts['default_lang'] ) ) { $default = (string) $pll_opts['default_lang']; } foreach ( array( 'post_translations' => 'post', 'term_translations' => 'term' ) as $taxonomy => $type ) { $descs = $wpdb->get_col( $wpdb->prepare( "SELECT description FROM %i WHERE taxonomy = %s", $wpdb->term_taxonomy, $taxonomy ) ); // phpcs:ignore WordPress.DB foreach ( $descs as $desc ) { $map = maybe_unserialize( $desc ); if ( ! is_array( $map ) || count( $map ) < 2 ) { continue; } $source_slug = ( $default && isset( $map[ $default ] ) ) ? $default : array_key_first( $map ); $source_id = (int) $map[ $source_slug ]; foreach ( $map as $slug => $id ) { $lang = self::code( (string) $slug ); $langs[] = $lang; if ( $slug === $source_slug ) { continue; } Groups::link( $type, $source_id, (int) $id, $lang, Groups::STATUS_TRANSLATED ); 'post' === $type ? $posts++ : $terms++; } } } return array( 'posts' => $posts, 'terms' => $terms, 'memory' => 0, 'languages' => array_values( array_unique( $langs ) ) ); } private function translatepress(): array { global $wpdb; $tables = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->prefix . 'trp_dictionary_' ) . '%' ) ); // phpcs:ignore WordPress.DB $n = 0; $langs = array(); foreach ( $tables as $table ) { // trp_dictionary_{source_locale}_{target_locale}, e.g. trp_dictionary_en_us_fr_fr. if ( ! preg_match( '/trp_dictionary_([a-z]{2,3}(?:_[a-z0-9]+)?)_([a-z]{2,3}(?:_[a-z0-9]+)?)$/i', $table, $m ) ) { continue; } $target = self::code( $m[2] ); $langs[] = $target; $offset = 0; do { $rows = $wpdb->get_results( $wpdb->prepare( "SELECT original, translated FROM %i WHERE status <> 0 AND translated <> '' LIMIT 1000 OFFSET %d", $table, $offset ), ARRAY_A ); // phpcs:ignore WordPress.DB $pairs = array(); foreach ( $rows as $r ) { $pairs[ $r['original'] ] = $r['translated']; } Memory::save( $pairs, Languages::source(), $target ); $n += count( $pairs ); $offset += 1000; } while ( count( $rows ) === 1000 ); } return array( 'posts' => 0, 'terms' => 0, 'memory' => $n, 'languages' => array_values( array_unique( $langs ) ) ); } }