3 class WPML_ST_Upgrade_Migrate_Originals implements IWPML_St_Upgrade_Command {
8 /** @var SitePress sitepress */
11 private $translations = array();
12 private $not_translated = array();
13 private $active_languages;
15 public function __construct( wpdb $wpdb, SitePress $sitepress ) {
17 $this->sitepress = $sitepress;
18 $active_languages = $this->sitepress->get_active_languages();
19 foreach ( $active_languages as $lang ) {
20 $this->active_languages[] = $lang['code'];
24 public static function get_command_id() {
28 public function run() {
29 if ( $this->is_migration_required() ) {
30 if ( current_user_can( 'manage_options' ) ) {
31 $this->sitepress->get_wp_api()->add_action( 'admin_notices', array( $this, 'update_message' ) );
39 function update_message() {
41 <div id="wpml-st-upgrade-migrate-originals" class="update-nag" style="display:block">
43 <?php esc_html_e( "WPML needs to update the database. This update will help improve WPML's performance when fetching translated strings.", 'wpml-string-translation' ); ?>
45 <button class="wpml-st-upgrade-migrate-originals"><?php esc_html_e( 'Update Now', 'wpml-string-translation' ); ?></button> <span class="spinner" style="float: none"></span>
47 <?php wp_nonce_field( 'wpml-st-upgrade-migrate-originals-nonce', 'wpml-st-upgrade-migrate-originals-nonce' ); ?>
49 <div id="wpml-st-upgrade-migrate-originals-complete" class="update-nag" style="display:none">
51 <?php esc_html_e( 'The database has been updated.', 'wpml-string-translation' ); ?>
53 <button class="wpml-st-upgrade-migrate-originals-close"><?php esc_html_e( 'Close', 'wpml-string-translation' ); ?></button>
55 <?php wp_nonce_field( 'wpml-st-upgrade-migrate-originals-nonce', 'wpml-st-upgrade-migrate-originals-nonce' ); ?>
57 <script type="text/javascript">
58 jQuery( function( $ ) {
59 jQuery( '.wpml-st-upgrade-migrate-originals' ).click( function() {
60 jQuery( this ).prop( 'disabled', true );
61 jQuery( this ).parent().find( '.spinner' ).css( 'visibility', 'visible' );
66 action: 'wpml-st-upgrade-migrate-originals',
67 nonce: jQuery( '#wpml-st-upgrade-migrate-originals-nonce' ).val()
69 success: function ( response ) {
70 jQuery( '#wpml-st-upgrade-migrate-originals' ).hide();
71 jQuery( '#wpml-st-upgrade-migrate-originals-complete' ).css( 'display', 'block' );
75 jQuery( '.wpml-st-upgrade-migrate-originals-close' ).click( function() {
76 jQuery( '#wpml-st-upgrade-migrate-originals-complete' ).hide();
83 public function run_ajax() {
85 if ( $this->is_migration_required() ) {
86 $this->get_strings_without_translations();
87 $this->get_originals_with_translations();
88 $this->migrate_translations();
94 public function run_frontend() {}
97 private function is_migration_required() {
100 FROM {$this->wpdb->prefix}icl_strings
101 WHERE context LIKE 'plugin %' OR context LIKE 'theme %'
103 $found = $this->wpdb->get_var( $query );
107 private function get_strings_without_translations() {
109 foreach ( $this->active_languages as $lang ) {
110 $res_args = array( $lang, $lang );
116 FROM {$this->wpdb->prefix}icl_strings s
118 SELECT st.string_id FROM {$this->wpdb->prefix}icl_string_translations st
123 $res_prepare = $this->wpdb->prepare( $res_query, $res_args );
124 $this->not_translated[ $lang ] = $this->wpdb->get_results( $res_prepare, ARRAY_A );
129 private function get_originals_with_translations() {
131 foreach ( $this->active_languages as $lang ) {
132 $res_args = array( ICL_TM_COMPLETE, $lang );
138 FROM {$this->wpdb->prefix}icl_strings s
139 LEFT JOIN {$this->wpdb->prefix}icl_string_translations st
141 WHERE st.status=%d AND st.language=%s
143 $res_prepare = $this->wpdb->prepare( $res_query, $res_args );
144 $result = $this->wpdb->get_results( $res_prepare, ARRAY_A );
146 foreach ( $result as $string ) {
147 $strings[ $string['org'] ] = $string['tra'];
149 $this->translations[ $lang ] = $strings;
153 private function migrate_translations() {
155 foreach ( $this->active_languages as $lang ) {
156 foreach ( $this->not_translated[ $lang ] as $not_translated ) {
158 if ( isset( $this->translations[ $lang ][ $not_translated['value'] ] ) ) {
159 icl_add_string_translation( $not_translated['id'], $lang, $this->translations[ $lang ][ $not_translated['value'] ], ICL_TM_COMPLETE );