コード例 #1
0
 function init()
 {
     do_action('wpml_before_init');
     global $wpdb;
     $this->get_user_preferences();
     $this->set_admin_language();
     $default_language = $this->get_default_language();
     // default value for theme_localization_type OR
     // reset theme_localization_type if string translation was on (theme_localization_type was set to 2) and then it was deactivated
     if (!isset($this->settings['theme_localization_type']) || $this->settings['theme_localization_type'] == 1 && !defined('WPML_ST_VERSION') && !defined('WPML_DOING_UPGRADE')) {
         global $sitepress_settings;
         $this->settings['theme_localization_type'] = $sitepress_settings['theme_localization_type'] = 2;
     }
     //configure callbacks for plugin menu pages
     if (defined('WP_ADMIN') && isset($_GET['page']) && 0 === strpos($_GET['page'], basename(ICL_PLUGIN_PATH) . '/')) {
         add_action('icl_menu_footer', array($this, 'menu_footer'));
     }
     //Run only if existing content language has been verified, and is front-end or settings are not corrupted
     if (!empty($this->settings['existing_content_language_verified']) && (!is_admin() || SitePress::check_settings_integrity())) {
         if ($this->settings['language_negotiation_type'] == 1 && $this->settings['urls']['directory_for_default_language'] && $this->settings['urls']['show_on_root'] == 'page') {
             include ICL_PLUGIN_PATH . '/inc/home-url-functions.php';
         }
         if ($this->settings['language_negotiation_type'] == 2) {
             add_filter('allowed_redirect_hosts', array($this, 'allowed_redirect_hosts'));
         }
         if (defined('WP_ADMIN')) {
             if ($this->settings['language_negotiation_type'] == 2) {
                 //Login and Logout
                 add_filter('login_url', array($this, 'convert_url'));
                 add_filter('logout_url', array($this, 'convert_url'));
                 add_filter('site_url', array($this, 'convert_url'));
             }
             if (isset($_GET['post']) && is_numeric($_GET['post'])) {
                 $post_type = get_post_type($_GET['post']);
             }
             $current_user = wp_get_current_user();
             if (isset($_GET['lang'])) {
                 $this->this_lang = rtrim(strip_tags($_GET['lang']), '/');
                 $al = $this->get_active_languages();
                 $al['all'] = true;
                 if (empty($al[$this->this_lang])) {
                     $this->this_lang = $default_language;
                 }
                 // force default language for string translation
                 // we also make sure it's not saved in the cookie
             } elseif (isset($_GET['page']) && (defined('WPML_ST_FOLDER') && $_GET['page'] == WPML_ST_FOLDER . '/menu/string-translation.php' || defined('WPML_TM_FOLDER') && $_GET['page'] == WPML_TM_FOLDER . '/menu/translations-queue.php')) {
                 $this->this_lang = $default_language;
             } elseif (wpml_is_ajax()) {
                 $al = $this->get_active_languages();
                 if (isset($_POST['lang']) && isset($al[$_POST['lang']])) {
                     $this->this_lang = $_POST['lang'];
                 } else {
                     if ($this->check_if_admin_action_from_referer()) {
                         $this->this_lang = $this->get_admin_language_cookie();
                     } elseif (isset($_SERVER['HTTP_REFERER'])) {
                         $this->this_lang = $this->get_language_from_url($_SERVER['HTTP_REFERER']);
                     } elseif ($this->get_language_cookie()) {
                         $this->this_lang = $this->get_language_cookie();
                     } else {
                         $this->this_lang = $this->get_default_language();
                     }
                 }
             } elseif ($lang = $this->get_admin_language_cookie()) {
                 $this->this_lang = $lang;
             } elseif (isset($_POST['action']) && $_POST['action'] == 'editpost' && isset($_POST['icl_post_language'])) {
                 $this->this_lang = $_POST['icl_post_language'];
             } elseif (isset($_GET['post']) && is_numeric($_GET['post']) && isset($post_type) && $this->is_translated_post_type($post_type)) {
                 $this->this_lang = $this->get_language_for_element($_GET['post'], 'post_' . $post_type);
             } elseif (isset($current_user->data)) {
                 $lang = $this->get_user_admin_language($current_user->data->ID);
                 $this->this_lang = $lang ? $lang : $default_language;
             }
             $update_admin_language_cookie = false;
             $update_admin_language_cookie |= (is_admin() && !wpml_is_ajax() || wpml_is_ajax() && $this->check_if_admin_action_from_referer()) && isset($_GET['lang']) && $this->get_current_language() != $this->get_admin_language_cookie();
             $update_admin_language_cookie |= isset($_GET['admin_bar']) && $_GET['admin_bar'] == 1 && !isset($_GET['page']);
             $update_admin_language_cookie |= !defined('WPML_ST_FOLDER') || isset($_GET['page']) && $_GET['page'] != WPML_ST_FOLDER . '/menu/string-translation.php';
             $update_admin_language_cookie |= !$this->get_admin_language_cookie();
             if ($update_admin_language_cookie) {
                 $this->set_admin_language_cookie();
             }
         } else {
             $al = $this->get_active_languages();
             foreach ($al as $l) {
                 $active_languages[] = $l['code'];
             }
             $active_languages[] = 'all';
             $s = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '';
             $home = get_home_url();
             if ($s) {
                 $home = preg_replace('#^http://#', 'https://', $home);
             }
             $url_parts = parse_url($home);
             $non_default_port = isset($url_parts['port']) && $url_parts['port'] != 80 ? ':' . $url_parts['port'] : '';
             $request = 'http' . $s . '://' . $this->get_server_host_name() . $_SERVER['REQUEST_URI'];
             $blog_path = !empty($url_parts['path']) ? $url_parts['path'] : '';
             switch ($this->settings['language_negotiation_type']) {
                 case 1:
                     $path = str_replace($home, '', $request);
                     $parts = explode('?', $path);
                     $path = $parts[0];
                     $exp = explode('/', trim($path, '/'));
                     $language_part = $exp[0];
                     if (in_array($language_part, $active_languages)) {
                         $this->this_lang = $exp[0];
                         // before hijacking the SERVER[REQUEST_URI]
                         // override the canonical_redirect action
                         // keep a copy of the original request uri
                         remove_action('template_redirect', 'redirect_canonical');
                         global $_icl_server_request_uri;
                         $_icl_server_request_uri = $_SERVER['REQUEST_URI'];
                         add_action('template_redirect', array($this, 'icl_redirect_canonical_wrapper'), 0);
                         //deal with situations when template files need to be called directly
                         add_action('template_redirect', array($this, '_allow_calling_template_file_directly'));
                         //$_SERVER['REQUEST_URI'] = preg_replace('@^'. $blog_path . '/' . $this->this_lang.'@i', $blog_path ,$_SERVER['REQUEST_URI']);
                         // Check for special case of www.example.com/fr where the / is missing on the end
                         $parts = parse_url($_SERVER['REQUEST_URI']);
                         if (strlen($parts['path']) == 0) {
                             $_SERVER['REQUEST_URI'] = '/' . $_SERVER['REQUEST_URI'];
                         }
                     } else {
                         $this->this_lang = $default_language;
                     }
                     /* If we access the root url via a non root slug/parameter combination,
                      * we redirect accordingly.
                      */
                     if ($this->settings['language_negotiation_type'] == 1 && $this->settings['urls']['directory_for_default_language'] && $this->settings['urls']['show_on_root'] == 'page') {
                         $filtered_root_url = WPML_Root_Page::filter_root_permalink(wpml_strip_subdir_from_url(site_url()) . $_SERVER['REQUEST_URI']);
                         if ($filtered_root_url != wpml_strip_subdir_from_url(site_url()) . $_SERVER['REQUEST_URI']) {
                             wp_redirect($filtered_root_url, 302);
                             exit;
                         }
                     }
                     if (WPML_Root_Page::is_current_request_root()) {
                         if ($this->settings['urls']['show_on_root'] == 'html_file') {
                             // html file
                             if (false === strpos($this->settings['urls']['root_html_file_path'], '/')) {
                                 $html_file = ABSPATH . $this->settings['urls']['root_html_file_path'];
                             } else {
                                 $html_file = $this->settings['urls']['root_html_file_path'];
                             }
                             /** @noinspection PhpIncludeInspection */
                             include $html_file;
                             exit;
                         } else {
                             wpml_home_url_setup_root_page();
                         }
                     }
                     break;
                 case 2:
                     $this->this_lang = $default_language;
                     foreach ($this->settings['language_domains'] as $language_code => $domain) {
                         if (rtrim($this->get_server_host_name() . $blog_path, '/') == rtrim(preg_replace('@^https?://@', '', $domain), '/')) {
                             $this->this_lang = $language_code;
                             break;
                         }
                     }
                     if (defined('ICL_USE_MULTIPLE_DOMAIN_LOGIN') && ICL_USE_MULTIPLE_DOMAIN_LOGIN) {
                         include ICL_PLUGIN_PATH . '/modules/multiple-domains-login.php';
                     }
                     add_filter('site_url', array($this, 'convert_url'));
                     break;
                 case 3:
                 default:
                     if (isset($_GET['lang'])) {
                         $this->this_lang = preg_replace("/[^0-9a-zA-Z-]/i", '', strip_tags($_GET['lang']));
                         // set the language based on the content id - for short links
                     } elseif (isset($_GET['page_id'])) {
                         $language_code_prepared = $wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type='post_page' AND element_id=%d", $_GET['page_id']);
                         $this->this_lang = $wpdb->get_var($language_code_prepared);
                     } elseif (isset($_GET['p'])) {
                         $post_type_prepared = $wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $_GET['p']);
                         $post_type = $wpdb->get_var($post_type_prepared);
                         $language_code_prepared = $wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", 'post_' . $post_type, $_GET['p']);
                         $this->this_lang = $wpdb->get_var($language_code_prepared);
                     } elseif (isset($_GET['cat_ID'])) {
                         $cat_tax_id_prepared = $wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", $_GET['cat_ID'], 'category');
                         $cat_tax_id = $wpdb->get_var($cat_tax_id_prepared);
                         $language_code_prepared = $wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_category' AND element_id=%d", $cat_tax_id);
                         $this->this_lang = $wpdb->get_var($language_code_prepared);
                     } elseif (isset($_GET['tag'])) {
                         $tag_tax_id_prepared = $wpdb->prepare("\r\n\t\t\t\t\t\t\t\t\t SELECT x.term_taxonomy_id FROM {$wpdb->term_taxonomy} x JOIN {$wpdb->terms} t ON t.term_id = x.term_id\r\n\t\t\t\t\t\t\t\t\t WHERE t.slug=%s AND x.taxonomy='post_tag'", $_GET['tag']);
                         $tag_tax_id = $wpdb->get_var($tag_tax_id_prepared);
                         $language_code_prepared = $wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations\r\n\t\t\t\t\t\t\t\t\t WHERE element_type='tax_post_tag' AND element_id=%d", $tag_tax_id);
                         $this->this_lang = $wpdb->get_var($language_code_prepared);
                     }
                     //
                     if (!isset($_GET['lang']) && ($this->this_lang && $this->this_lang != $default_language)) {
                         if (!isset($GLOBALS['wp_rewrite'])) {
                             require_once ABSPATH . WPINC . '/rewrite.php';
                             $GLOBALS['wp_rewrite'] = new WP_Rewrite();
                         }
                         define('ICL_DOING_REDIRECT', true);
                         if (isset($_GET['page_id'])) {
                             wp_redirect(get_page_link($_GET['page_id']), '301');
                             exit;
                         } elseif (isset($_GET['p'])) {
                             wp_redirect(get_permalink($_GET['p']), '301');
                             exit;
                         } elseif (isset($_GET['cat_ID'])) {
                             wp_redirect(get_term_link(intval($_GET['cat_ID']), 'category'));
                             exit;
                         } elseif (isset($_GET['tag'])) {
                             wp_redirect(get_term_link($_GET['tag'], 'post_tag'));
                             exit;
                         } else {
                             if (isset($this->settings['taxonomies_sync_option'])) {
                                 $taxs = array_keys((array) $this->settings['taxonomies_sync_option']);
                                 foreach ($taxs as $t) {
                                     if (isset($_GET[$t])) {
                                         $term_obj = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE t.slug=%s AND x.taxonomy=%s", $_GET[$t], $t));
                                         $term_link = get_term_link($term_obj, $t);
                                         $term_link = str_replace('&', '&', $term_link);
                                         // fix
                                         if ($term_link && !is_wp_error($term_link)) {
                                             wp_redirect($term_link);
                                             exit;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     if (empty($this->this_lang)) {
                         $this->this_lang = $default_language;
                     }
             }
             // allow forcing the current language when it can't be decoded from the URL
             $this->this_lang = apply_filters('icl_set_current_language', $this->this_lang);
         }
         //reorder active language to put 'this_lang' in front
         foreach ($this->active_languages as $k => $al) {
             if ($al['code'] == $this->this_lang) {
                 unset($this->active_languages[$k]);
                 $this->active_languages = array_merge(array($k => $al), $this->active_languages);
             }
         }
         WPML_Troubleshooting_Terms_Menu::display_terms_with_suffix_admin_notice();
         // filter some queries
         add_filter('query', array($this, 'filter_queries'));
         add_filter('option_rewrite_rules', array($this, 'rewrite_rules_filter'));
         $this->set_language_cookie();
         if (is_admin() && (!isset($_GET['page']) || !defined('WPML_ST_FOLDER') || $_GET['page'] != WPML_ST_FOLDER . '/menu/string-translation.php') && (!isset($_GET['page']) || !defined('WPML_TM_FOLDER') || $_GET['page'] != WPML_TM_FOLDER . '/menu/translations-queue.php')) {
             if (version_compare($GLOBALS['wp_version'], '3.3', '<')) {
                 // Legacy code for admin language switcher
                 if (!$this->is_rtl() && version_compare($GLOBALS['wp_version'], '3.3', '>')) {
                     add_action('admin_notices', 'wpml_set_admin_language_switcher_place', 100);
                     add_action('network_admin_notices', 'wpml_set_admin_language_switcher_place', 100);
                     add_action('user_admin_notices', 'wpml_set_admin_language_switcher_place', 100);
                     function wpml_set_admin_language_switcher_place()
                     {
                         echo '<br clear="all" />';
                     }
                 }
                 add_action('in_admin_header', array($this, 'admin_language_switcher_legacy'));
             } else {
                 // Admin language switcher goes to the WP admin bar
                 add_action('wp_before_admin_bar_render', array($this, 'admin_language_switcher'));
             }
         }
         if (!is_admin() && defined('DISQUS_VERSION')) {
             include ICL_PLUGIN_PATH . '/modules/disqus.php';
         }
     }
     /*
      * If user perform bulk taxonomy deletion when displaying non-default
      * language taxonomies, after deletion should stay with same language
      */
     if (is_admin() && isset($_POST['_wp_http_referer']) && false !== strpos($_POST['_wp_http_referer'], 'edit-tags.php') && !empty($_POST['delete_tags']) && is_array($_POST['delete_tags']) && !empty($_GET['lang']) && ($_POST['action'] == 'delete' || $_POST['action2'] == 'delete')) {
         add_filter('wp_redirect', array($this, 'preserve_lang_param_after_bulk_category_delete'));
     }
     if ($this->is_rtl()) {
         $GLOBALS['text_direction'] = 'rtl';
     }
     if (!wpml_is_ajax() && is_admin() && empty($this->settings['dont_show_help_admin_notice'])) {
         if (!$this->get_setting('setup_wizard_step')) {
             if (SitePress::check_settings_integrity()) {
                 add_action('admin_notices', array($this, 'help_admin_notice'));
             }
         }
     }
     $short_v = implode('.', array_slice(explode('.', ICL_SITEPRESS_VERSION), 0, 3));
     if (is_admin() && (!isset($this->settings['hide_upgrade_notice']) || $this->settings['hide_upgrade_notice'] != $short_v)) {
         add_action('admin_notices', array($this, 'upgrade_notice'));
     }
     require ICL_PLUGIN_PATH . '/inc/template-constants.php';
     if (defined('WPML_LOAD_API_SUPPORT')) {
         require ICL_PLUGIN_PATH . '/inc/wpml-api.php';
     }
     add_action('wp_footer', array($this, 'display_wpml_footer'), 20);
     if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) {
         add_action('xmlrpc_call', array($this, 'xmlrpc_call_actions'));
         add_filter('xmlrpc_methods', array($this, 'xmlrpc_methods'));
     }
     if (defined('WPML_TM_VERSION') && is_admin()) {
         require ICL_PLUGIN_PATH . '/inc/quote.php';
     }
     add_action('init', array($this, 'set_up_language_selector'));
     global $pagenow;
     // set language to default and remove language switcher when in Taxonomy Translation page
     // If the page uses AJAX and the language must be forced to default, please use the
     // if ( $pagenow == 'admin-ajax.php' )above
     if (is_admin() && (isset($_GET['page']) && ($_GET['page'] == ICL_PLUGIN_FOLDER . '/menu/taxonomy-translation.php' || $_GET['page'] == ICL_PLUGIN_FOLDER . '/menu/menus-sync.php' || $_GET['page'] == ICL_PLUGIN_FOLDER . '/menu/taxonomy-translation-display.class.php') || $pagenow == 'admin-ajax.php' && isset($_POST['action']) && $_POST['action'] == 'wpml_tt_save_labels_translation')) {
         $default_language = $this->get_admin_language();
         $this->switch_lang($default_language, true);
         add_action("admin_init", array("WPML_Taxonomy_Translation_Table_Display", "enqueue_taxonomy_table_js"));
         add_action('init', array($this, 'remove_admin_language_switcher'));
     }
     /* Posts and new inline created terms, can only be saved in an active language.
      * Also the content of the post-new.php should always be filtered for one specific
      * active language, so to display the correct taxonomy terms for selection.
      */
     if ($pagenow == 'post-new.php') {
         if (!$this->is_active_language($this->get_current_language())) {
             $default_language = $this->get_admin_language();
             $this->switch_lang($default_language, true);
         }
     }
     //Code to run when reactivating the plugin
     $recently_activated = $this->get_setting('just_reactivated');
     if ($recently_activated) {
         add_action('init', array($this, 'rebuild_language_information'), 1000);
     }
     do_action('wpml_after_init');
 }