]> _ Git - physioassist-wordpress.git/blob
1845baf9d54398d67370d69f75ef167cca7a96cb
[physioassist-wordpress.git] /
1 <?php
2
3 class WPML_ST_Upgrade_Migrate_Originals implements IWPML_St_Upgrade_Command {
4
5         /** @var wpdb $wpdb */
6         private $wpdb;
7
8         /** @var SitePress sitepress */
9         private $sitepress;
10
11         private $translations   = array();
12         private $not_translated = array();
13         private $active_languages;
14
15         public function __construct( wpdb $wpdb, SitePress $sitepress ) {
16                 $this->wpdb       = $wpdb;
17                 $this->sitepress  = $sitepress;
18                 $active_languages = $this->sitepress->get_active_languages();
19                 foreach ( $active_languages as $lang ) {
20                         $this->active_languages[] = $lang['code'];
21                 }
22         }
23
24         public static function get_command_id() {
25                 return __CLASS__;
26         }
27
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' ) );
32                         }
33                         return false;
34                 } else {
35                         return true;
36                 }
37         }
38
39         function update_message() {
40                 ?>
41                         <div id="wpml-st-upgrade-migrate-originals" class="update-nag" style="display:block">
42                                 <p>
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' ); ?>
44                                         <br /><br />
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>
46                                 </p>
47                                 <?php wp_nonce_field( 'wpml-st-upgrade-migrate-originals-nonce', 'wpml-st-upgrade-migrate-originals-nonce' ); ?>
48                         </div>
49                         <div id="wpml-st-upgrade-migrate-originals-complete" class="update-nag" style="display:none">
50                                 <p>
51                                         <?php esc_html_e( 'The database has been updated.', 'wpml-string-translation' ); ?>
52                                         <br /><br />
53                                         <button class="wpml-st-upgrade-migrate-originals-close"><?php esc_html_e( 'Close', 'wpml-string-translation' ); ?></button>
54                                 </p>
55                                 <?php wp_nonce_field( 'wpml-st-upgrade-migrate-originals-nonce', 'wpml-st-upgrade-migrate-originals-nonce' ); ?>
56                         </div>
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' );
62                                                 jQuery.ajax({
63                                                         url: ajaxurl,
64                                                         type: "POST",
65                                                         data: {
66                                                                 action: 'wpml-st-upgrade-migrate-originals',
67                                                                 nonce: jQuery( '#wpml-st-upgrade-migrate-originals-nonce' ).val()
68                                                         },
69                                                         success: function ( response ) {
70                                                                 jQuery( '#wpml-st-upgrade-migrate-originals' ).hide();
71                                                                 jQuery( '#wpml-st-upgrade-migrate-originals-complete' ).css( 'display', 'block' );
72                                                         }
73                                                 });
74                                         });
75                                         jQuery( '.wpml-st-upgrade-migrate-originals-close' ).click( function() {
76                                                 jQuery( '#wpml-st-upgrade-migrate-originals-complete' ).hide();
77                                         });
78                                 });
79                         </script>
80                 <?php
81         }
82
83         public function run_ajax() {
84
85                 if ( $this->is_migration_required() ) {
86                         $this->get_strings_without_translations();
87                         $this->get_originals_with_translations();
88                         $this->migrate_translations();
89                 }
90
91                 return true;
92         }
93
94         public function run_frontend() {}
95
96
97         private function is_migration_required() {
98                 $query = "
99                                         SELECT id
100                                         FROM {$this->wpdb->prefix}icl_strings
101                                         WHERE context LIKE 'plugin %' OR context LIKE 'theme %'
102                                         LIMIT 1";
103                 $found = $this->wpdb->get_var( $query );
104                 return $found > 0;
105         }
106
107         private function get_strings_without_translations() {
108
109                 foreach ( $this->active_languages as $lang ) {
110                         $res_args = array( $lang, $lang );
111
112                         $res_query                     = "
113                                                                 SELECT
114                                                                         s.value,
115                                                                         s.id
116                                                                 FROM {$this->wpdb->prefix}icl_strings s
117                                                                 WHERE s.id NOT IN (
118                                                                         SELECT st.string_id FROM {$this->wpdb->prefix}icl_string_translations st
119                                                                         WHERE st.language=%s
120                                                                         )
121                                                                 AND s.language!=%s
122                                                                 ";
123                         $res_prepare                   = $this->wpdb->prepare( $res_query, $res_args );
124                         $this->not_translated[ $lang ] = $this->wpdb->get_results( $res_prepare, ARRAY_A );
125                 }
126
127         }
128
129         private function get_originals_with_translations() {
130
131                 foreach ( $this->active_languages as $lang ) {
132                         $res_args = array( ICL_TM_COMPLETE, $lang );
133
134                         $res_query   = "
135                                                                 SELECT
136                                                                         st.value AS tra,
137                                                                         s.value AS org
138                                                                 FROM {$this->wpdb->prefix}icl_strings s
139                                                                 LEFT JOIN {$this->wpdb->prefix}icl_string_translations st
140                                                                         ON s.id=st.string_id
141                                                                 WHERE st.status=%d AND st.language=%s
142                                                                 ";
143                         $res_prepare = $this->wpdb->prepare( $res_query, $res_args );
144                         $result      = $this->wpdb->get_results( $res_prepare, ARRAY_A );
145                         $strings     = array();
146                         foreach ( $result as $string ) {
147                                 $strings[ $string['org'] ] = $string['tra'];
148                         }
149                         $this->translations[ $lang ] = $strings;
150                 }
151         }
152
153         private function migrate_translations() {
154
155                 foreach ( $this->active_languages as $lang ) {
156                         foreach ( $this->not_translated[ $lang ] as $not_translated ) {
157
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 );
160                                         break;
161                                 }
162                         }
163                 }
164         }
165
166 }
167