PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/sitepress-multilingual-cms/inc/upgrade-functions/upgrade-2.0.0.php

https://gitlab.com/woxiprogrammers/infinia-wordpress
PHP | 279 lines | 218 code | 44 blank | 17 comment | 33 complexity | fb5f5e997e871029606106a5d5ba34a4 MD5 | raw file
  1. <?php
  2. function icl_upgrade_2_0_0_steps($step, $stepper){
  3. global $wpdb, $sitepress, $wp_post_types, $sitepress_settings;
  4. if(!isset($sitepress)) $sitepress = new SitePress;
  5. $TranslationManagement = new TranslationManagement;
  6. $default_language = $sitepress->get_default_language();
  7. define('ICL_TM_DISABLE_ALL_NOTIFICATIONS', true); // make sure no notifications are being sent
  8. //if(defined('icl_upgrade_2_0_0_runonce')){
  9. // return;
  10. //}
  11. //define('icl_upgrade_2_0_0_runonce', true);
  12. // fix source_language_code
  13. // assume that the lowest element_id is the source language
  14. ini_set('max_execution_time', 300);
  15. $post_types = array_keys($wp_post_types);
  16. foreach($post_types as $pt){
  17. $types[] = 'post_' . $pt;
  18. }
  19. $temp_upgrade_data = get_option('icl_temp_upgrade_data',
  20. array('step' => 0, 'offset' => 0));
  21. switch($step) {
  22. case 1:
  23. // if the tables are missing, call the plugin activation routine
  24. $table_name = $wpdb->prefix.'icl_translation_status';
  25. if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name){
  26. icl_sitepress_activate();
  27. }
  28. $wpdb->query("ALTER TABLE `{$wpdb->prefix}icl_translations` CHANGE `element_type` `element_type` VARCHAR( 32 ) NOT NULL DEFAULT 'post_post'");
  29. $wpdb->query("ALTER TABLE `{$wpdb->prefix}icl_translations` CHANGE `element_id` `element_id` BIGINT( 20 ) NULL DEFAULT NULL ");
  30. // fix source_language_code
  31. // all source documents must have null
  32. if ( isset( $types ) ) {
  33. $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}icl_translations SET source_language_code = NULL
  34. WHERE element_type IN('".join("','", $types)."') AND source_language_code = '' AND language_code=%s", $default_language ));
  35. // get translated documents with missing source language
  36. $res = $wpdb->get_results($wpdb->prepare("
  37. SELECT translation_id, trid, language_code
  38. FROM {$wpdb->prefix}icl_translations
  39. WHERE (source_language_code = '' OR source_language_code IS NULL)
  40. AND element_type IN('".join("','", $types)."')
  41. AND language_code <> %s
  42. ", $default_language
  43. ));
  44. foreach($res as $row){
  45. $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}icl_translations SET source_language_code = %s WHERE translation_id=%d", $default_language, $row->translation_id));
  46. }
  47. }
  48. $temp_upgrade_data['step'] = 2;
  49. update_option('icl_temp_upgrade_data', $temp_upgrade_data);
  50. return array('message' => __('Processing translations...', 'sitepress'));
  51. break;
  52. case 2:
  53. $limit = 100;
  54. $offset = $temp_upgrade_data['offset'];
  55. $processing = FALSE;
  56. //loop existing translations
  57. if ( isset( $types ) ) {
  58. $res = $wpdb->get_results( $wpdb->prepare(
  59. "SELECT * FROM {$wpdb->prefix}icl_translations
  60. WHERE element_type IN(" . wpml_prepare_in( $types ) . " )
  61. AND source_language_code IS NULL LIMIT %d OFFSET %d", array($limit, $offset)
  62. ));
  63. foreach( $res as $row){
  64. $processing = TRUE;
  65. // grab translations
  66. $translations = $sitepress->get_element_translations($row->trid, $row->element_type);
  67. $md5 = 0;
  68. $table_name = $wpdb->prefix.'icl_node';
  69. if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") == $table_name){
  70. list($md5, $links_fixed) = $wpdb->get_row($wpdb->prepare("
  71. SELECT md5, links_fixed FROM {$wpdb->prefix}icl_node
  72. WHERE nid = %d
  73. ", $row->element_id), ARRAY_N);
  74. }
  75. if(!$md5){
  76. $md5 = $TranslationManagement->post_md5($row->element_id);
  77. }
  78. $translation_package = $TranslationManagement->create_translation_package($row->element_id);
  79. foreach($translations as $lang => $t){
  80. if(!$t->original){
  81. // determine service and status
  82. $service = 'local';
  83. $needs_update = 0;
  84. list($rid, $status, $current_md5) = $wpdb->get_row($wpdb->prepare("
  85. SELECT c.rid, n.status , c.md5
  86. FROM {$wpdb->prefix}icl_content_status c
  87. JOIN {$wpdb->prefix}icl_core_status n ON c.rid = n.rid
  88. WHERE c.nid = %d AND target = %s
  89. ORDER BY rid DESC
  90. LIMIT 1
  91. ", $row->element_id, $lang), ARRAY_N);
  92. $translator_id = false;
  93. if($rid){
  94. if($current_md5 != $md5){
  95. $needs_update = 1;
  96. }
  97. if($status == 3){
  98. $status = 10;
  99. }else{
  100. $status = 2;
  101. }
  102. $service = 'icanlocalize';
  103. foreach($sitepress_settings['icl_lang_status'] as $lpair){
  104. if($lpair['from'] == $row->language_code && $lpair['to'] == $lang && isset($lpair['translators'][0]['id'])){
  105. $translator_id = $lpair['translators'][0]['id'];
  106. break;
  107. }
  108. }
  109. }else{
  110. $status = 10;
  111. $translator_id = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM {$wpdb->posts} WHERE ID=%d", $t->element_id));
  112. $tlp = get_user_meta($translator_id, $wpdb->prefix.'language_pairs', true);
  113. $tlp[$row->language_code][$lang] = 1;
  114. $TranslationManagement->edit_translator($translator_id, $tlp);
  115. }
  116. // add translation_status record
  117. list($newrid) = $TranslationManagement->update_translation_status(array(
  118. 'translation_id' => $t->translation_id,
  119. 'status' => $status,
  120. 'translator_id' => $translator_id,
  121. 'needs_update' => $needs_update,
  122. 'md5' => $md5,
  123. 'translation_service' => $service,
  124. 'translation_package' => serialize($translation_package),
  125. 'links_fixed' => intval(isset($links_fixed)?$links_fixed:0)
  126. ));
  127. $job_id = $TranslationManagement->add_translation_job( $newrid, $translator_id, $translation_package );
  128. if ( $job_id && $status == 10 ) {
  129. do_action( 'wpml_save_job_fields_from_post', $job_id );
  130. }
  131. }
  132. }
  133. }
  134. }
  135. if ($processing) {
  136. update_option('icl_temp_upgrade_data', array('step' => 2, 'offset' => intval($offset+100)));
  137. $stepper->setNextStep(2);
  138. } else {
  139. update_option('icl_temp_upgrade_data', array('step' => 3, 'offset' => 99999999999999999999));
  140. }
  141. $message = $processing ? __('Processing translations...', 'sitepress') : __('Finalizing upgrade...', 'sitepress');
  142. return array('message' => $message);
  143. break;
  144. case 3:
  145. // removing the plugins text table; importing data into a Sitepress setting
  146. $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}icl_plugins_texts");
  147. if(!empty($results)){
  148. foreach($results as $row){
  149. $cft[$row->attribute_name] = $row->translate + 1;
  150. }
  151. if ( isset( $cft ) ) {
  152. $iclsettings['translation-management']['custom_fields_translation'] = $cft;
  153. $sitepress->save_settings($iclsettings);
  154. }
  155. $wpdb->query("DROP TABLE {$wpdb->prefix}icl_plugins_texts");
  156. }
  157. $iclsettings['language_selector_initialized'] = 1;
  158. if(get_option('_force_mp_post_http')){
  159. $iclsettings['troubleshooting_options']['http_communication'] = intval(get_option('_force_mp_post_http'));
  160. delete_option('_force_mp_post_http');
  161. }
  162. // set default translators
  163. if (isset($sitepress_settings['icl_lang_status'])) {
  164. foreach($sitepress_settings['icl_lang_status'] as $lpair){
  165. if(!empty($lpair['translators'])){
  166. $iclsettings['default_translators'][$lpair['from']][$lpair['to']] = array('id'=>$lpair['translators'][0]['id'], 'type'=>'icanlocalize');
  167. }
  168. }
  169. }
  170. $sitepress->save_settings($iclsettings);
  171. $iclsettings['migrated_2_0_0'] = 1;
  172. $sitepress->save_settings($iclsettings);
  173. delete_option('icl_temp_upgrade_data');
  174. return array('message' => __('Done', 'sitepress'), 'completed' => 1);
  175. break;
  176. default:
  177. return array('error' => __('Missing step', 'sitepress'), 'stop' => 1);
  178. }
  179. }
  180. // $iclsettings defined in upgrade.php
  181. if(empty($iclsettings['migrated_2_0_0'])){
  182. wp_enqueue_script('icl-stepper', ICL_PLUGIN_URL . '/inc/upgrade-functions/2.0.0/stepper.js', array('jquery'));
  183. add_filter('admin_notices', 'icl_migrate_2_0_0');
  184. add_action('icl_ajx_custom_call', 'icl_ajx_upgrade_2_0_0', 1, 2);
  185. }
  186. function icl_migrate_2_0_0() {
  187. $ajax_action = 'wpml_upgrade_2_0_0';
  188. $ajax_action_none = wp_create_nonce($ajax_action);
  189. $link = 'index.php?icl_ajx_action=' . $ajax_action . '&nonce=' . $ajax_action_none;
  190. $txt = get_option('icl_temp_upgrade_data', FALSE) ? __('Resume Upgrade Process', 'sitepress') : __('Run Upgrade Process', 'sitepress');
  191. echo '<div class="message error" id="icl-migrate"><p><strong>'.__('WPML requires database upgrade', 'sitepress').'</strong></p>'
  192. .'<p>' . __('This normally takes a few seconds, but may last up to several minutes of very large databases.', 'sitepress') . '</p>'
  193. . '<p><a href="' . $link . '" style="" id="icl-migrate-start">' . $txt . '</a></p>'
  194. . '<div id="icl-migrate-progress" style="display:none; margin: 10px 0 20px 0;">'
  195. . '</div></div>';
  196. }
  197. function icl_ajx_upgrade_2_0_0($call, $request){
  198. if($call == 'wpml_upgrade_2_0_0'){
  199. $error = 0;
  200. $completed = 0;
  201. $stop = 0;
  202. $message = __('Starting the upgrade process...', 'sitepress');
  203. include_once ICL_PLUGIN_PATH . '/inc/upgrade-functions/2.0.0/stepper.php';
  204. include_once ICL_PLUGIN_PATH . '/inc/upgrade-functions/upgrade-2.0.0.php';
  205. $temp_upgrade_data = get_option('icl_temp_upgrade_data',
  206. array('step' => 0, 'offset' => 0));
  207. $step = isset($request['step']) ? $request['step'] : $temp_upgrade_data['step'];
  208. $migration = new Icl_Stepper($step);
  209. $migration->registerSteps(
  210. 'icl_upgrade_2_0_0_steps',
  211. 'icl_upgrade_2_0_0_steps',
  212. 'icl_upgrade_2_0_0_steps');
  213. if (isset($request['init'])) {
  214. echo json_encode(array(
  215. 'error' => $error,
  216. 'output' => $migration->render(),
  217. 'step' => $migration->getNextStep(),
  218. 'message' => __('Creating new tables...', 'sitepress'),
  219. 'stop' => $stop,
  220. ));
  221. exit;
  222. }
  223. $data = $migration->init();
  224. @extract($data, EXTR_OVERWRITE);
  225. echo json_encode(array(
  226. 'error' => $error,
  227. 'completed' => $completed,
  228. 'message' => $message,
  229. 'step' => $migration->getNextStep(),
  230. 'barWidth' => $migration->barWidth(),
  231. 'stop' => $stop,
  232. ));
  233. }
  234. }
  235. ?>