PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/woxiprogrammers/infinia-wordpress
PHP | 389 lines | 268 code | 69 blank | 52 comment | 42 complexity | b766c99a2b9919e9034c5e67c0ccae6e MD5 | raw file
  1. <?php
  2. /**
  3. * @global WPML_Term_Translation $wpml_term_translations
  4. * @global WPML_Slug_Filter $wpml_slug_filter
  5. */
  6. /**
  7. * Loads global variables providing functionality that is used throughout the plugin.
  8. *
  9. * @global $wpml_language_resolution
  10. * @global $wpml_slug_filter
  11. * @global WPML_Term_Translation $wpml_term_translations
  12. */
  13. function load_essential_globals() {
  14. global $wpml_language_resolution, $wpml_term_translations, $wpdb;
  15. $wpml_languages = array();
  16. $settings = get_option( 'icl_sitepress_settings' );
  17. if ( (bool) $settings === false ) {
  18. icl_sitepress_activate();
  19. } else {
  20. if ( isset( $settings[ 'setup_complete' ] ) && $settings[ 'setup_complete' ] ) {
  21. $active_plugins = get_option( 'active_plugins' );
  22. $wpmu_sitewide_plugins = (array) maybe_unserialize( get_site_option( 'active_sitewide_plugins' ) );
  23. if ( in_array( trailingslashit( ICL_PLUGIN_FOLDER ) . 'sitepress.php',
  24. $active_plugins,
  25. true ) === false &&
  26. in_array( trailingslashit( ICL_PLUGIN_FOLDER ) . 'sitepress.php',
  27. array_keys( $wpmu_sitewide_plugins ),
  28. true ) === false
  29. ) {
  30. // The plugin has just be reactivated.
  31. // reset ajx_health_flag
  32. // set the just_reactivated flag so any posts created while
  33. // WPML was not activate will get the default language
  34. // https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlcore-1924
  35. $settings[ 'ajx_health_checked' ] = 0;
  36. $settings[ 'just_reactivated' ] = 1;
  37. update_option( 'icl_sitepress_settings', $settings );
  38. }
  39. }
  40. }
  41. $active_language_codes = isset( $settings[ 'active_languages' ] ) ? $settings[ 'active_languages' ]
  42. : array();
  43. $active_language_codes = (bool) $active_language_codes === true
  44. ? $active_language_codes : wpml_reload_active_languages_setting ();
  45. $wpml_languages[ 'active_languages' ] = $active_language_codes;
  46. $default_lang_code = isset( $settings[ 'default_language' ] ) ? $settings[ 'default_language' ]
  47. : false;
  48. $wpml_language_resolution = new WPML_Language_Resolution( $active_language_codes, $default_lang_code );
  49. $admin = is_admin ();
  50. wpml_load_post_translation( $admin, $settings, $active_language_codes, $default_lang_code );
  51. $wpml_term_translations = new WPML_Term_Translation( $wpdb );
  52. $domain_validation = filter_input( INPUT_GET, '____icl_validate_domain' ) ? 1 : false;
  53. $domain_validation = filter_input( INPUT_GET, '____icl_validate_directory' ) ? 2 : $domain_validation;
  54. $url_converter = load_wpml_url_converter( $settings, $domain_validation, $default_lang_code );
  55. if ( $domain_validation ) {
  56. echo wpml_validate_host( $_SERVER['REQUEST_URI'], $url_converter, $domain_validation === 2 );
  57. exit;
  58. }
  59. if ( $admin ) {
  60. wpml_load_admin_files();
  61. }
  62. }
  63. function wpml_load_post_translation( $admin, $settings ) {
  64. global $wpml_post_translations, $wpdb;
  65. if ( $admin === true ) {
  66. $wpml_post_translations = new WPML_Admin_Post_Actions( $settings, $wpdb );
  67. } else {
  68. $wpml_post_translations = new WPML_Frontend_Post_Actions( $settings, $wpdb );
  69. wpml_load_frontend_tax_filters ();
  70. }
  71. $wpml_post_translations->init ();
  72. }
  73. function wpml_load_request_handler( $admin, $active_language_codes, $default_language ) {
  74. global $wpml_request_handler, $wpml_url_converter;
  75. if ( ! isset( $wpml_request_handler ) ) {
  76. require ICL_PLUGIN_PATH . '/inc/request-handling/wpml-request.class.php';
  77. require ICL_PLUGIN_PATH . '/inc/request-handling/wpml-backend-request.class.php';
  78. require ICL_PLUGIN_PATH . '/inc/request-handling/wpml-frontend-request.class.php';
  79. }
  80. if ( $admin === true ) {
  81. $wpml_request_handler = new WPML_Backend_Request( $wpml_url_converter,
  82. $active_language_codes,
  83. $default_language );
  84. } else {
  85. $wpml_request_handler = new WPML_Frontend_Request( $wpml_url_converter,
  86. $active_language_codes,
  87. $default_language );
  88. }
  89. return $wpml_request_handler;
  90. }
  91. function wpml_load_query_filter( $installed ) {
  92. global $wpml_query_filter, $sitepress, $wpdb, $wpml_post_translations, $wpml_term_translations;
  93. $wpml_query_filter = $wpml_query_filter ? $wpml_query_filter : new WPML_Query_Filter( $sitepress, $wpdb, $wpml_post_translations, $wpml_term_translations );
  94. if ( $installed ) {
  95. if ( ! has_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ) ) ) {
  96. add_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10, 2 );
  97. add_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10, 2 );
  98. }
  99. }
  100. }
  101. function load_wpml_url_converter($settings, $domain_validation, $default_lang_code){
  102. global $wpml_url_converter;
  103. $url_type = isset( $settings[ 'language_negotiation_type' ] ) ? $settings[ 'language_negotiation_type' ]
  104. : false;
  105. $url_type = $domain_validation ? $domain_validation : $url_type;
  106. $hidden_langs = isset( $settings[ 'hidden_languages' ] ) ? $settings[ 'hidden_languages' ] : array();
  107. if ( $url_type == 1 ) {
  108. require ICL_PLUGIN_PATH . '/inc/url-handling/wpml-lang-subdir-converter.class.php';
  109. $dir_default = isset( $settings[ 'urls' ] ) && isset( $settings[ 'urls' ][ 'directory_for_default_language' ] )
  110. ? $settings[ 'urls' ][ 'directory_for_default_language' ] : false;
  111. $wpml_url_converter = new WPML_Lang_Subdir_Converter( $dir_default, $default_lang_code, $hidden_langs );
  112. } elseif ( $url_type == 2 ) {
  113. require ICL_PLUGIN_PATH . '/inc/url-handling/wpml-lang-domains-converter.class.php';
  114. $domains = isset( $settings[ 'language_domains' ] ) ? $settings[ 'language_domains' ] : array();
  115. $wpml_url_converter = new WPML_Lang_Domains_Converter( $domains, $default_lang_code, $hidden_langs );
  116. } else {
  117. require ICL_PLUGIN_PATH . '/inc/url-handling/wpml-lang-parameter-converter.class.php';
  118. $wpml_url_converter = new WPML_Lang_Parameter_Converter( $default_lang_code, $hidden_langs );
  119. }
  120. return $wpml_url_converter;
  121. }
  122. /**
  123. * @param string $req_uri
  124. * @param WPML_URL_Converter $wpml_url_converter
  125. * @param bool $directory
  126. *
  127. * @return string
  128. */
  129. function wpml_validate_host( $req_uri, $wpml_url_converter, $directory = true ) {
  130. if ( $directory === true ) {
  131. $req_uri_parts = array_filter ( explode ( '/', $req_uri ) );
  132. $lang_slug = array_pop ( $req_uri_parts );
  133. if ( strpos ( $lang_slug, '?' ) === 0 ) {
  134. $lang_slug = array_pop ( $req_uri_parts );
  135. } elseif ( strpos ( $lang_slug, '?' ) !== false ) {
  136. $parts = explode ( '?', $lang_slug );
  137. $lang_slug = array_shift ( $parts );
  138. }
  139. } else {
  140. $lang_slug = '';
  141. }
  142. return '<!--' . untrailingslashit ( trailingslashit ( $wpml_url_converter->get_abs_home () ) . $lang_slug ) . '-->';
  143. }
  144. /**
  145. * Checks if a given taxonomy is currently translated
  146. *
  147. * @param string $taxonomy name/slug of a taxonomy
  148. * @return bool true if the taxonomy is currently set to being translatable in WPML
  149. */
  150. function is_taxonomy_translated( $taxonomy ) {
  151. return in_array( $taxonomy, array( 'category', 'post_tag', 'nav_menu' ), true )
  152. || in_array(
  153. $taxonomy,
  154. array_keys( array_filter( icl_get_setting( 'taxonomies_sync_option', array() ) ) )
  155. );
  156. }
  157. /**
  158. * Checks if a given post_type is currently translated
  159. *
  160. * @param string $post_type name/slug of a post_type
  161. * @return bool true if the post_type is currently set to being translatable in WPML
  162. */
  163. function is_post_type_translated( $post_type ) {
  164. return in_array( $post_type, array( 'post', 'page', 'nav_menu_item' ), true )
  165. || in_array(
  166. $post_type,
  167. array_keys( array_filter( icl_get_setting( 'custom_posts_sync_option', array() ) ) )
  168. );
  169. }
  170. function setup_admin_menus() {
  171. global $pagenow;
  172. if ( $pagenow === 'edit-tags.php' ) {
  173. maybe_load_translated_tax_screen ();
  174. }
  175. }
  176. function maybe_load_translated_tax_screen() {
  177. $taxonomy_get = (string) filter_input( INPUT_GET, 'taxonomy' );
  178. $taxonomy_get = $taxonomy_get ? $taxonomy_get : 'post_tag';
  179. if ( is_taxonomy_translated( $taxonomy_get ) ) {
  180. global $wpdb, $sitepress;
  181. require ICL_PLUGIN_PATH . '/menu/term-taxonomy-menus/wpml-tax-menu-loader.class.php';
  182. new WPML_Tax_Menu_Loader( $wpdb, $sitepress, $taxonomy_get );
  183. }
  184. }
  185. function wpml_reload_active_languages_setting( $override = false ) {
  186. global $wpdb, $sitepress_settings;
  187. if ( (bool) $sitepress_settings === true
  188. && ( $override || wpml_get_setting_filter( false, 'setup_complete' ) )
  189. ) {
  190. $active_languages = $wpdb->get_col( " SELECT code
  191. FROM {$wpdb->prefix}icl_languages
  192. WHERE active = 1" );
  193. $sitepress_settings['active_languages'] = $active_languages;
  194. icl_set_setting( 'active_languages', $active_languages, true );
  195. } else {
  196. $active_languages = array();
  197. }
  198. return (array) $active_languages;
  199. }
  200. /**
  201. * Returns and if necessary instantiates an instance of the WPML_Installation Class
  202. *
  203. * @return \WPML_Installation
  204. */
  205. function wpml_get_setup_instance() {
  206. global $wpml_installation, $wpdb, $sitepress;
  207. if ( ! isset( $wpml_installation ) ) {
  208. require ICL_PLUGIN_PATH . '/inc/setup/wpml-installation.class.php';
  209. $wpml_installation = new WPML_Installation( $wpdb, $sitepress );
  210. }
  211. return $wpml_installation;
  212. }
  213. function wpml_load_admin_files() {
  214. require ICL_PLUGIN_PATH . '/menu/wpml-troubleshooting-terms-menu.class.php';
  215. require ICL_PLUGIN_PATH . '/inc/wpml-post-edit-ajax.class.php';
  216. require ICL_PLUGIN_PATH . '/menu/wpml-post-status-display.class.php';
  217. require ICL_PLUGIN_PATH . '/inc/utilities/wpml-color-picker.class.php';
  218. }
  219. function wpml_get_post_status_helper() {
  220. global $wpml_post_status, $wpdb;
  221. if ( ! isset( $wpml_post_status ) ) {
  222. require ICL_PLUGIN_PATH . '/inc/post-translation/wpml-post-status.class.php';
  223. $wpml_post_status = new WPML_Post_Status( $wpdb );
  224. }
  225. return $wpml_post_status;
  226. }
  227. function wpml_get_create_post_helper() {
  228. global $wpml_create_post_helper, $sitepress;
  229. if ( ! isset( $wpml_create_post_helper ) ) {
  230. require ICL_PLUGIN_PATH . '/inc/post-translation/wpml-create-post-helper.class.php';
  231. $wpml_create_post_helper = new WPML_Create_Post_Helper( $sitepress );
  232. }
  233. return $wpml_create_post_helper;
  234. }
  235. /**
  236. * @return \TranslationManagement
  237. */
  238. function wpml_load_core_tm() {
  239. global $iclTranslationManagement;
  240. if ( !isset( $iclTranslationManagement ) ) {
  241. require_once ICL_PLUGIN_PATH . '/inc/translation-management/translation-management.class.php';
  242. $iclTranslationManagement = new TranslationManagement();
  243. }
  244. return $iclTranslationManagement;
  245. }
  246. function wpml_get_langs_in_dirs_val( $http_client, $wpml_url_converter, $posted_url = false ) {
  247. global $sitepress;
  248. require_once ICL_PLUGIN_PATH . '/inc/url-handling/wpml-lang-url-validator.class.php';
  249. $posted_url = $posted_url ? $posted_url : (string) filter_input ( INPUT_POST, 'url' );
  250. return new WPML_Lang_URL_Validator( $http_client, $wpml_url_converter, $posted_url, $sitepress );
  251. }
  252. function wpml_get_root_page_actions_obj() {
  253. global $wpml_root_page_actions, $sitepress_settings;
  254. if ( !isset( $wpml_root_page_actions ) ) {
  255. require_once ICL_PLUGIN_PATH . '/inc/post-translation/wpml-root-page-actions.class.php';
  256. $wpml_root_page_actions = new WPML_Root_Page_Actions( $sitepress_settings );
  257. }
  258. return $wpml_root_page_actions;
  259. }
  260. function wpml_get_hierarchy_sync_helper( $type = 'post' ) {
  261. global $wpdb;
  262. require_once ICL_PLUGIN_PATH . '/inc/core-abstract-classes/wpml-hierarchy-sync.class.php';
  263. if ( $type === 'post' ) {
  264. require_once ICL_PLUGIN_PATH . '/inc/post-translation/wpml-post-hierarchy-sync.class.php';
  265. $hierarchy_helper = new WPML_Post_Hierarchy_Sync( $wpdb );
  266. } elseif ( $type === 'term' ) {
  267. require_once ICL_PLUGIN_PATH . '/inc/taxonomy-term-translation/wpml-term-hierarchy-sync.class.php';
  268. $hierarchy_helper = new WPML_Term_Hierarchy_Sync( $wpdb );
  269. } else {
  270. $hierarchy_helper = false;
  271. }
  272. return $hierarchy_helper;
  273. }
  274. function wpml_maybe_setup_post_edit() {
  275. global $pagenow, $sitepress, $post_edit_screen;
  276. if ( in_array( $pagenow, array( 'post.php', 'post-new.php', 'edit.php' ), true ) || defined( 'DOING_AJAX' )
  277. ) {
  278. require ICL_PLUGIN_PATH . '/menu/post-menus/post-edit-screen/wpml-post-edit-screen.class.php';
  279. $post_edit_screen = new WPML_Post_Edit_Screen( $sitepress );
  280. add_action( 'admin_head', array( $sitepress, 'post_edit_language_options' ) );
  281. }
  282. }
  283. /**
  284. * @return \WPML_Frontend_Tax_Filters
  285. */
  286. function wpml_load_frontend_tax_filters() {
  287. global $wpml_term_filters;
  288. if ( !isset( $wpml_term_filters ) ) {
  289. require ICL_PLUGIN_PATH . '/inc/taxonomy-term-translation/wpml-frontend-tax-filters.class.php';
  290. $wpml_term_filters = new WPML_Frontend_Tax_Filters();
  291. }
  292. return $wpml_term_filters;
  293. }
  294. /**
  295. * @return \WPML_Settings_Helper
  296. */
  297. function wpml_load_settings_helper() {
  298. global $wpml_settings_helper, $sitepress, $wpml_post_translations;
  299. if ( ! isset( $wpml_settings_helper ) ) {
  300. require_once ICL_PLUGIN_PATH . '/inc/setup/wpml-settings-helper.class.php';
  301. $wpml_settings_helper = new WPML_Settings_Helper( $wpml_post_translations, $sitepress );
  302. }
  303. return $wpml_settings_helper;
  304. }
  305. function wpml_get_term_translation_util() {
  306. require_once ICL_PLUGIN_PATH . '/inc/taxonomy-term-translation/wpml-term-translation-utils.class.php';
  307. return new WPML_Term_Translation_Utils();
  308. }
  309. /**
  310. * @return \WPML_Term_Filters
  311. */
  312. function wpml_load_term_filters() {
  313. global $wpml_term_filters_general, $sitepress, $wpdb;
  314. if ( ! isset( $wpml_term_filters_general ) ) {
  315. require ICL_PLUGIN_PATH . '/inc/taxonomy-term-translation/wpml-term-filters.class.php';
  316. $wpml_term_filters_general = new WPML_Term_Filters( $wpdb, $sitepress );
  317. $wpml_term_filters_general->init();
  318. }
  319. return $wpml_term_filters_general;
  320. }