PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/sitepress-multilingual-cms/inc/setup/wpml-installation.class.php

https://gitlab.com/woxiprogrammers/infinia-wordpress
PHP | 325 lines | 272 code | 39 blank | 14 comment | 21 complexity | 41d9ab1614e5ef67c2df8c9b4f30328b MD5 | raw file
  1. <?php
  2. require 'wpml-language-switcher-settings.class.php';
  3. class WPML_Installation extends WPML_WPDB_And_SP_User {
  4. function go_to_setup1() {
  5. icl_set_setting( 'existing_content_language_verified', 0 );
  6. icl_set_setting( 'setup_wizard_step', 1 );
  7. $this->wpdb->query( "TRUNCATE TABLE {$this->wpdb->prefix}icl_translations" );
  8. $this->wpdb->update( $this->wpdb->prefix . 'icl_languages', array( 'active' => 0 ), array( 'active' => 1 ) );
  9. icl_save_settings();
  10. }
  11. /**
  12. * Sets the locale in the icl_locale_map if it has not yet been set
  13. *
  14. * @param $initial_language_code
  15. */
  16. private function maybe_set_locale( $initial_language_code ) {
  17. $q = "SELECT code FROM {$this->wpdb->prefix}icl_locale_map WHERE code=%s";
  18. $q_prepared = $this->wpdb->prepare( $q, $initial_language_code );
  19. if ( ! $this->wpdb->get_var( $q_prepared ) ) {
  20. $q = "SELECT default_locale FROM {$this->wpdb->prefix}icl_languages WHERE code=%s";
  21. $q_prepared = $this->wpdb->prepare( $q, $initial_language_code );
  22. $default_locale = $this->wpdb->get_var( $q_prepared );
  23. if ( $default_locale ) {
  24. $this->wpdb->insert(
  25. $this->wpdb->prefix . 'icl_locale_map',
  26. array( 'code' => $initial_language_code, 'locale' => $default_locale )
  27. );
  28. }
  29. }
  30. }
  31. public function finish_step2( $active_languages ) {
  32. return $this->set_active_languages( $active_languages );
  33. }
  34. public function set_active_languages( $arr ) {
  35. $tmp = $this->sanitize_language_input( $arr );
  36. if ( (bool) $tmp === false ) {
  37. return false;
  38. }
  39. foreach ( $tmp as $code ) {
  40. $default_locale_prepared = $this->wpdb->prepare(
  41. "SELECT default_locale FROM {$this->wpdb->prefix}icl_languages WHERE code= %s LIMIT 1",
  42. $code
  43. );
  44. $default_locale = $this->wpdb->get_var( $default_locale_prepared );
  45. if ( $default_locale ) {
  46. $code_exists_prepared = $this->wpdb->prepare(
  47. "SELECT code FROM {$this->wpdb->prefix}icl_locale_map WHERE code = %s LIMIT 1",
  48. $code
  49. );
  50. $code_exists = $this->wpdb->get_var( $code_exists_prepared );
  51. if ( $code_exists ) {
  52. $this->wpdb->update(
  53. $this->wpdb->prefix . 'icl_locale_map',
  54. array( 'locale' => $default_locale ),
  55. array( 'code' => $code )
  56. );
  57. } else {
  58. $this->wpdb->insert(
  59. $this->wpdb->prefix . 'icl_locale_map',
  60. array( 'code' => $code, 'locale' => $default_locale )
  61. );
  62. }
  63. }
  64. SitePress_Setup::insert_default_category( $code );
  65. }
  66. $this->wpdb->query(
  67. "UPDATE {$this->wpdb->prefix}icl_languages SET active = 1 WHERE code IN (" . wpml_prepare_in( $tmp ) . " ) "
  68. );
  69. $this->wpdb->query(
  70. "UPDATE {$this->wpdb->prefix}icl_languages SET active = 0 WHERE code NOT IN (" . wpml_prepare_in( $tmp ) . " ) "
  71. );
  72. $this->updated_active_languages();
  73. return true;
  74. }
  75. private function sanitize_language_input( $lang_codes ) {
  76. $languages = $this->sitepress->get_languages( false, false, true );
  77. $sanitized_codes = array();
  78. $lang_codes = array_filter( array_unique( $lang_codes ) );
  79. foreach ( $lang_codes as $code ) {
  80. $code = esc_sql( trim( $code ) );
  81. if ( isset( $languages[ $code ] ) ) {
  82. $sanitized_codes[] = $code;
  83. }
  84. }
  85. return $sanitized_codes;
  86. }
  87. public function finish_installation( $site_key = false ) {
  88. icl_set_setting( 'setup_complete', 1, true );
  89. if ( $site_key ) {
  90. icl_set_setting( 'site_key', $site_key, true );
  91. }
  92. }
  93. public function finish_step3( $ls_sidebars, $ls_options ) {
  94. $ls_setup = new WPML_Language_Switcher_Settings( $ls_sidebars, $ls_options );
  95. if ( $ls_sidebars ) {
  96. $ls_setup->set_ls_sidebar();
  97. }
  98. $ls_setup->set_ls_options();
  99. $this->maybe_move_setup( 4 );
  100. }
  101. private function maybe_move_setup( $step ) {
  102. $setup_complete = icl_get_setting( 'setup_complete' );
  103. if ( empty( $setup_complete ) ) {
  104. icl_set_setting( 'setup_wizard_step', $step, true );
  105. }
  106. }
  107. private function updated_active_languages() {
  108. wp_cache_init();
  109. icl_cache_clear();
  110. $this->refresh_active_lang_cache( wpml_get_setting_filter( false, 'default_language' ) );
  111. wpml_reload_active_languages_setting( true );
  112. $active_langs = $this->sitepress->get_active_languages( true );
  113. $this->maybe_move_setup( 3 );
  114. if ( count( $active_langs ) > 1 ) {
  115. icl_set_setting( 'dont_show_help_admin_notice', true );
  116. }
  117. }
  118. public function finish_step1( $initial_language_code ) {
  119. $this->set_initial_default_category( $initial_language_code );
  120. $this->prepopulate_translations( $initial_language_code );
  121. icl_set_setting( 'existing_content_language_verified', 1 );
  122. icl_set_setting( 'default_language', $initial_language_code );
  123. icl_set_setting( 'admin_default_language', $initial_language_code );
  124. $this->maybe_set_locale( $initial_language_code );
  125. icl_set_setting( 'setup_wizard_step', 2 );
  126. icl_save_settings();
  127. $this->refresh_active_lang_cache( $initial_language_code );
  128. do_action( 'icl_initial_language_set' );
  129. }
  130. private function set_initial_default_category( $initial_lang ) {
  131. $blog_default_cat = get_option( 'default_category' );
  132. $blog_default_cat_tax_id = $this->wpdb->get_var(
  133. $this->wpdb->prepare(
  134. " SELECT term_taxonomy_id
  135. FROM {$this->wpdb->term_taxonomy}
  136. WHERE term_id=%d
  137. AND taxonomy='category'",
  138. $blog_default_cat
  139. )
  140. );
  141. icl_set_setting( 'default_categories', array( $initial_lang => $blog_default_cat_tax_id ), true );
  142. }
  143. /**
  144. * @param string $display_language
  145. * @param bool $active_only
  146. *
  147. * @return array
  148. */
  149. public function refresh_active_lang_cache( $display_language, $active_only = false ) {
  150. $active_snippet = $active_only ? " l.active = 1 AND " : "";
  151. $res_query
  152. = "
  153. SELECT
  154. l.code,
  155. l.id, english_name,
  156. nt.name AS native_name,
  157. major,
  158. active,
  159. default_locale,
  160. encode_url,
  161. tag,
  162. lt.name AS display_name
  163. FROM {$this->wpdb->prefix}icl_languages l
  164. JOIN {$this->wpdb->prefix}icl_languages_translations nt
  165. ON ( nt.language_code = l.code AND nt.display_language_code = l.code )
  166. LEFT OUTER JOIN {$this->wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
  167. WHERE {$active_snippet}
  168. ( lt.display_language_code = %s
  169. OR (lt.display_language_code = 'en'
  170. AND NOT EXISTS ( SELECT *
  171. FROM {$this->wpdb->prefix}icl_languages_translations ls
  172. WHERE ls.language_code = l.code
  173. AND ls.display_language_code = %s ) ) )
  174. GROUP BY l.code
  175. ORDER BY major DESC, english_name ASC";
  176. $res_query_prepared = $this->wpdb->prepare( $res_query, $display_language, $display_language );
  177. $res = $this->wpdb->get_results( $res_query_prepared, ARRAY_A );
  178. $languages = array();
  179. foreach ( (array) $res as $r ) {
  180. $languages[ $r[ 'code' ] ] = $r;
  181. $this->sitepress->icl_language_name_cache->set( 'language_details_' . $r[ 'code' ] . $display_language, $r );
  182. }
  183. if ( $active_only ) {
  184. $this->sitepress->icl_language_name_cache->set( 'in_language_' . $display_language, $languages );
  185. } else {
  186. $this->sitepress->icl_language_name_cache->set( 'all_language_' . $display_language, $languages );
  187. }
  188. $this->sitepress->icl_language_name_cache->save_cache_if_requred( );
  189. return $languages;
  190. }
  191. private function prepopulate_translations( $lang ) {
  192. $existing_lang_verified = icl_get_setting( 'existing_content_language_verified' );
  193. if ( ! empty( $existing_lang_verified ) ) {
  194. return;
  195. }
  196. icl_cache_clear();
  197. // case of icl_sitepress_settings accidentally lost
  198. // if there's at least one translation do not initialize the languages for elements
  199. $one_translation = $this->wpdb->get_var(
  200. $this->wpdb->prepare(
  201. "SELECT translation_id FROM {$this->wpdb->prefix}icl_translations WHERE language_code<>%s",
  202. $lang
  203. )
  204. );
  205. if ( $one_translation ) {
  206. return;
  207. }
  208. $this->wpdb->query( "TRUNCATE TABLE {$this->wpdb->prefix}icl_translations" );
  209. $this->wpdb->query(
  210. $this->wpdb->prepare(
  211. "
  212. INSERT INTO {$this->wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
  213. SELECT CONCAT('post_',post_type), ID, ID, %s, NULL FROM {$this->wpdb->posts} WHERE post_status IN ('draft', 'publish','schedule','future','private', 'pending')
  214. ",
  215. $lang
  216. )
  217. );
  218. $maxtrid = 1 + $this->wpdb->get_var( "SELECT MAX(trid) FROM {$this->wpdb->prefix}icl_translations" );
  219. global $wp_taxonomies;
  220. $taxonomies = array_keys( (array) $wp_taxonomies );
  221. foreach ( $taxonomies as $tax ) {
  222. $element_type = 'tax_' . $tax;
  223. $insert_query
  224. = "
  225. INSERT INTO {$this->wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
  226. SELECT %s, term_taxonomy_id, %d+term_taxonomy_id, %s, NULL FROM {$this->wpdb->term_taxonomy} WHERE taxonomy = %s
  227. ";
  228. $insert_prepare = $this->wpdb->prepare( $insert_query, array( $element_type, $maxtrid, $lang, $tax ) );
  229. $this->wpdb->query( $insert_prepare );
  230. $maxtrid = 1 + $this->wpdb->get_var( "SELECT MAX(trid) FROM {$this->wpdb->prefix}icl_translations" );
  231. }
  232. $this->wpdb->query(
  233. $this->wpdb->prepare(
  234. "
  235. INSERT INTO {$this->wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
  236. SELECT 'comment', comment_ID, {$maxtrid}+comment_ID, %s, NULL FROM {$this->wpdb->comments}
  237. ",
  238. $lang
  239. )
  240. );
  241. $this->wpdb->update( $this->wpdb->prefix . 'icl_languages', array( 'active' => '1' ), array( 'code' => $lang ) );
  242. }
  243. function reset_language_data() {
  244. $active = $this->wpdb->get_col( "SELECT code FROM {$this->wpdb->prefix}icl_languages WHERE active = 1" );
  245. $this->wpdb->query( "TRUNCATE TABLE `{$this->wpdb->prefix}icl_languages`" );
  246. SitePress_Setup::fill_languages();
  247. $this->wpdb->query( "TRUNCATE TABLE `{$this->wpdb->prefix}icl_languages_translations`" );
  248. SitePress_Setup::fill_languages_translations();
  249. $this->wpdb->query( "TRUNCATE TABLE `{$this->wpdb->prefix}icl_flags`" );
  250. SitePress_Setup::fill_flags();
  251. //restore active
  252. $this->wpdb->query(
  253. "UPDATE {$this->wpdb->prefix}icl_languages SET active=1 WHERE code IN(" . wpml_prepare_in( $active ) . ")"
  254. );
  255. $this->wpdb->update( $this->wpdb->prefix . 'icl_flags', array( 'from_template' => 0 ), null );
  256. $codes = $this->wpdb->get_col( "SELECT code FROM {$this->wpdb->prefix}icl_languages" );
  257. foreach ( $codes as $code ) {
  258. if ( ! $code || $this->wpdb->get_var(
  259. $this->wpdb->prepare( "SELECT lang_code FROM {$this->wpdb->prefix}icl_flags WHERE lang_code = %s", $code )
  260. )
  261. ) {
  262. continue;
  263. }
  264. if ( ! file_exists( ICL_PLUGIN_PATH . '/res/flags/' . $code . '.png' ) ) {
  265. $file = 'nil.png';
  266. } else {
  267. $file = $code . '.png';
  268. }
  269. $this->wpdb->insert(
  270. $this->wpdb->prefix . 'icl_flags',
  271. array( 'lang_code' => $code, 'flag' => $file, 'from_template' => 0 )
  272. );
  273. }
  274. $last_default_language = $this->sitepress !== null ? $this->sitepress->get_default_language() : 'en';
  275. if ( ! in_array( $last_default_language, $codes ) ) {
  276. $last_active_languages = $this->sitepress->get_active_languages();
  277. foreach ( $last_active_languages as $code => $last_active_language ) {
  278. if ( in_array( $code, $codes ) ) {
  279. $this->sitepress->set_default_language( $code );
  280. break;
  281. }
  282. }
  283. }
  284. icl_cache_clear();
  285. }
  286. }