/** * add noredirect query var to the links * * @wp-hook mlp_linked_element_link * @param string $link * @param int $site_id * @return string */ public function add_noredirect_parameter($link, $site_id) { $link = (string) $link; if (empty($link)) { return $link; } $languages = mlp_get_available_languages(); if (empty($languages[$site_id])) { return $link; } return add_query_arg('noredirect', $languages[$site_id], $link); }
/** * Adds the noredirect query var to the given URL. * * @wp-hook mlp_linked_element_link * * @param string $url URL. * @param int $site_id Site ID. * * @return string */ public function add_noredirect_parameter($url, $site_id) { $url = (string) $url; if (!$url) { return $url; } $languages = mlp_get_available_languages(); if (empty($languages[$site_id])) { return $url; } $url = add_query_arg('noredirect', $languages[$site_id], $url); return $url; }
/** * show the metabox * * @access public * @since 0.1 * @uses get_site_option, get_current_blog_id * @param $post post object * @return void */ public function display_cpt_meta_box($post) { $linked = mlp_get_linked_elements($post->ID); if (0 < count($linked)) { // post is a linked post $languages = mlp_get_available_languages(); if (0 < count($languages)) { ?> <select name="multilingual_press" id="multilingual_press"> <option><?php _e('choose preview language', 'multilingualpress'); ?> </option> <?php foreach ($languages as $language_blogid => $language_name) { if ($language_blogid != get_current_blog_id()) { ?> <option value="<?php echo $language_blogid; ?> "><?php echo $language_name; ?> </option> <?php } } ?> </select> <div id="multilingual_press_content"></div> <script type="text/javascript"> //<![CDATA[ jQuery( document ).ready( function( $ ) { $( '#multilingual_press' ).change( function() { blogid = ''; $( '#multilingual_press option:selected' ).each( function () { blogid += $( this ).attr( 'value' ); } ); $.post( ajaxurl, { action: 'get_metabox_content', blogid: blogid, post: <?php echo $post->ID; ?> }, function( returned_data ) { if ( '' != returned_data ) { $( '#multilingual_press_content' ).html( returned_data ); $( '#multilingual_press_content #headline' ).remove(); $( '#multilingual_press_content #content' ).remove(); } } ); } ); } ); //]]> </script> <?php } } }
/** * function for custom plugins to get activated on all language blogs * * @param int $element_id ID of the selected element * @param string $type type of the selected element * @param int $blog_id ID of the selected blog * @param string $hook * @param mixed $param * @return WP_Error|NULL */ public static function run_custom_plugin($element_id, $type, $blog_id, $hook, $param) { if (empty($element_id)) { return new WP_Error('mlp_empty_custom_element', __('Empty Element', 'multilingual-press')); } if (empty($type)) { return new WP_Error('mlp_empty_custom_type', __('Empty Type', 'multilingual-press')); } if (empty($hook) || !is_callable($hook)) { return new WP_Error('mlp_empty_custom_hook', __('Invalid Hook', 'multilingual-press')); } // set the current element in the mlp class $languages = mlp_get_available_languages(); $current_blog = get_current_blog_id(); if (0 == count($languages)) { return NULL; } foreach ($languages as $language_id => $language_name) { if ($current_blog == $language_id) { continue; } switch_to_blog($language_id); /** * custom hook * @param mixed $param */ do_action($hook, $param); restore_current_blog(); } return NULL; }
/** * add noredirect query var to the links * * @since 0.1 * @param string $link | the current link to the blog * @param string $blog_id | Blog ID of the linked blog */ public function add_noredirect_links($link, $blog_id) { $languages = mlp_get_available_languages(); // if language is available add to link if (isset($languages[$blog_id])) { $link = add_query_arg('noredirect', $languages[$blog_id], $link); } return $link; }
/** * Get the linked elements and display them as a list * flag from a blogid * * @since 0.1 * @access public * @param int $blog_id ID of a blog * @uses mlp_get_available_languages, mlp_get_available_languages_titles, is_single, * is_page, mlp_get_linked_elements, mlp_get_language_flag, get_current_blog_id, * get_blog_post, get_site_url * @return string output of the bloglist */ public static function show_linked_elements($args) { global $wp_query; $output = ''; $languages = mlp_get_available_languages(); $language_titles = mlp_get_available_languages_titles(); if (!(0 < count($languages))) { return $output; } // returns NULL if there is no post, get_the_ID() throws a notice, // if we don' check this before. $default_post = get_post(); if ($default_post) { $current_element_id = get_the_ID(); } elseif (!empty($wp_query->queried_object) && !empty($wp_query->queried_object->ID)) { $current_element_id = $wp_query->queried_object->ID; } else { $current_element_id = 0; } $linked_elements = array(); // double check to avoid issues with a static front page. if (!is_front_page() && !is_home() && is_singular()) { $linked_elements = mlp_get_linked_elements($current_element_id); } $defaults = array('link_text' => 'text', 'echo' => TRUE, 'sort' => 'blogid', 'show_current_blog' => FALSE); $params = wp_parse_args($args, $defaults); if ('blogid' == $params['sort']) { ksort($languages); } else { asort($languages); } $output .= '<div class="mlp_language_box"><ul>'; foreach ($languages as $language_blog => $language_string) { $current_language = mlp_get_current_blog_language(2); if ($current_language == $language_string && $params['show_current_blog'] == FALSE) { continue; } // Get params $flag = mlp_get_language_flag($language_blog); $title = mlp_get_available_languages_titles(TRUE); // Display type if ('flag' == $params['link_text'] && '' != $flag) { $display = '<img src="' . $flag . '" alt="' . $languages[$language_blog] . '" title="' . $title[$language_blog] . '" />'; } else { if ('text' == $params['link_text'] && !empty($language_titles[$language_blog])) { $display = $language_titles[$language_blog]; } else { if ('text_flag' == $params['link_text']) { $display = '<img src="' . $flag . '" alt="' . $languages[$language_blog] . '" title="' . $title[$language_blog] . '" />'; if (!empty($language_titles[$language_blog])) { $display .= ' ' . $language_titles[$language_blog]; } } else { $display = $languages[$language_blog]; } } } $class = get_current_blog_id() == $language_blog ? 'id="mlp_current_locale"' : ''; // set element to 0 to avoid empty element if (!isset($linked_elements[$language_blog])) { $linked_elements[$language_blog] = 0; } // Check post status $post = $linked_elements[$language_blog] > 0 ? get_blog_post($language_blog, $linked_elements[$language_blog]) : ''; do_action('mlp_before_link'); $link = (is_single() || is_page() || is_home()) && isset($post->post_status) && ('publish' === $post->post_status || 'private' === $post->post_status && is_super_admin()) ? get_blog_permalink($language_blog, $linked_elements[$language_blog]) : get_site_url($language_blog); // apply filter to help others to change the link $link = apply_filters('mlp_linked_element_link', $link, $language_blog, $linked_elements[$language_blog]); do_action('mlp_after_link'); // Output link elements $output .= '<li ' . ($current_language == $language_string ? 'class="current"' : '') . '><a rel="alternate" hreflang="' . self::get_blog_language($language_blog) . '" ' . $class . ' href="' . $link . '">' . $display . '</a></li>'; } $output .= '</ul></div>'; return $output; }
/** * Checks if given language already exists on WP. * * @param $language * * @return bool */ private function language_exists($language) { $all_lngs = mlp_get_available_languages(); $short_code = Wpml2mlp_Helper::get_short_language($language['language_code']); $short_locale = Wpml2mlp_Helper::get_short_language($language['default_locale']); foreach ($all_lngs as $lng) { $short_lng = Wpml2mlp_Helper::get_short_language($lng); if ($short_code == $short_lng || $short_locale == $short_lng) { return TRUE; } } return FALSE; }
/** * Create the tab and prepend it to the body-tag * * @since 0.1 * @access public * @param string $content * @uses is_single, is_page, get_site_option, mlp_get_linked_elements, mlp_get_available_languages * __, plugins_url, esc_html * @return string $content */ public function frontend_tab($content) { if (!is_single() && !is_page()) { return $content; } // Get post link option $option = get_site_option('inpsyde_multilingual_quicklink_options'); $available_blogs = get_site_option('inpsyde_multilingual'); if (!$option) { $option = array('mlp_quicklink_position' => 'tr'); } // Get the linked elements for the current post // array: blog_id => post_id $linked = mlp_get_linked_elements(get_the_ID()); if (array() == $linked) { return $content; } // Get the blogs that are related to the current blog $languages = mlp_get_available_languages(); if (array() == $languages) { return $content; } $translated = array(); $titles = mlp_get_available_languages_titles(); foreach ($languages as $language_blogid => $language_name) { if ($language_blogid == get_current_blog_id()) { continue; } $url = $this->get_element_permalink($language_blogid, get_the_ID()); if ($url) { $translated[$url] = esc_html($titles[$language_blogid]); } } if (empty($translated)) { return $content; } $position = $option['mlp_quicklink_position']; $switcher = $this->to_html($translated, $position); // position at the top if ('t' === $position[0]) { return $switcher . $content; } return $content . $switcher; }
/** * Display the default form fields * * @param int $current_blog_id | The ID of the current blog * @since 0.5.5b * @uses get_site_option, _e, plugins_url, selected, plugin_dir_path, * esc_html, mlp_get_available_languages, switch_to_blog, get_bloginfo, * restore_current_blog, get_blog_option * @return void */ public function draw_blog_settings_form_fields($current_blog_id) { $lang_codes = $this->lang_codes; // get registered blogs $siteoption = get_site_option('inpsyde_multilingual', array()); // Get values to display // in form fields $lang_title = isset($siteoption[$current_blog_id]['text']) ? stripslashes($siteoption[$current_blog_id]['text']) : ''; $selected = isset($siteoption[$current_blog_id]['lang']) ? $siteoption[$current_blog_id]['lang'] : ''; $blogoption_flag = esc_url(get_blog_option($current_blog_id, 'inpsyde_multilingual_flag_url')); ?> <!-- Language select, alt. language title and flag image url --> <div class="postbox"> <div title="Click to toggle" class="handlediv"><br></div> <h3 class="hndle"><?php _e('Language', 'multilingualpress'); ?> </h3> <div class="inside"> <table class="form-table" id="mlp_blog_language"> <tr class="form-field"> <th><?php _e('Choose blog language', 'multilingualpress'); ?> </th> <td> <?php if (!empty($lang_codes)) { ?> <select name="inpsyde_multilingual_lang" id="inpsyde_multilingual_lang"> <option value="-1"><?php _e('choose language', 'multilingualpress'); ?> </option> <option value="en_US" <?php echo selected('en_US', $selected); ?> ><?php _e('English (US)', 'multilingualpress'); ?> </option> <?php foreach ($lang_codes as $language_code => $language_name) { if (5 == strlen($language_code)) { $language_code_flag = strtolower(substr($language_code, 3, 2)); } else { $language_code_flag = $language_code; } if (file_exists(plugin_dir_path(dirname(__FILE__)) . '/flags/' . $language_code_flag . '.gif')) { ?> <option value="<?php echo $language_code; ?> "<?php echo selected($selected, $language_code, false); ?> ><?php echo esc_html($language_name); ?> </option> <?php } } ?> </select> <br /> <span class="description"><?php _e('Determine blog language and flag. This will be used in the frontend widget.', 'multilingualpress'); ?> </span> <?php } ?> </td> </tr> <tr id="mlp_check_language"><th></th><td></td></tr> <tr> <th> <?php _e('Alternative language title', 'multilingualpress'); ?> </th> <td> <input class="regular-text" type="text" id="inpsyde_multilingual_text" name="inpsyde_multilingual_text" value="<?php echo $lang_title; ?> " /> <br /> <span class="description"><?php _e('Enter a title here that you want to be displayed in the frontend instead of the default one (i.e. "My English Site")', 'multilingualpress'); ?> </span> </td> </tr> <tr> <th> <?php _e('Blog flag image URL', 'multilingualpress'); ?> </th> <td> <input class="regular-text" type="text" id="inpsyde_multilingual_flag_url" name="inpsyde_multilingual_flag_url" value="<?php echo $blogoption_flag; ?> " /> <br /> <span class="description"><?php _e('(Optional, must begin with http://)', 'multilingualpress'); ?> </span> </td> </tr> </table> </div> </div> <?php // Only display this part if there are blogs to interlink $a_lang = mlp_get_available_languages(TRUE); ?> <!-- Blog relationships --> <?php // only display relationship box if an other blog is available if ($siteoption && count($siteoption) > 1 || count($siteoption) == 1 && empty($siteoption[$current_blog_id])) { ?> <div class="postbox"> <div title="Click to toggle" class="handlediv"><br></div> <h3 class="hndle"><?php _e('Blog Relationships', 'multilingualpress'); ?> </h3> <div class="inside"> <table class="form-table"> <tr> <th><?php _e('Multilingual blog relationships', 'multilingualpress'); ?> </th> <td> <?php foreach ($siteoption as $blog_id => $meta) { // Filter out current blog if ($current_blog_id === $blog_id) { continue; } // Get blog display name switch_to_blog($blog_id); $blog_name = get_bloginfo('Name'); restore_current_blog(); // Get current settings $related_blogs = get_blog_option($current_blog_id, 'inpsyde_multilingual_blog_relationship'); if (is_array($related_blogs) && in_array($blog_id, $related_blogs)) { $selected = 'checked="checked"'; } else { $selected = ''; } if (!isset($lang_codes[$meta['lang']])) { $lang_codes[$meta['lang']] = __('English (US)', 'multilingualpress'); } ?> <input id="related_blog_<?php echo $blog_id; ?> " <?php echo $selected; ?> type="checkbox" name="related_blogs[]" value="<?php echo $blog_id; ?> " /> <?php echo $blog_name; ?> - <?php echo $lang_codes[$meta['lang']]; ?> <br /> <?php } ?> </td> </tr> </table> </div> </div> <?php } }