/** * Creates the custom rewrite rules. * @return array $rules. */ function uls_create_custom_rewrite_rules() { global $wp_rewrite; //get available languages // $languages = '(en|es|en_US|es_ES)'; $languages = "(" . implode("|", array_values(uls_get_available_languages())) . ")"; $wp_rewrite->add_rewrite_tag('%lang%', $languages, 'lang='); //create prefixed rules $new_rules = array($languages . '/?$' => 'index.php?lang=$matches[1]'); foreach ($wp_rewrite->rules as $left => $right) { //ignore attachment rules //if(false === strpos($left, '/attachment/')){ $new_rules[$languages . '/' . $left] = preg_replace_callback('/matches\\[(\\d{1,2})\\]/', 'uls_replace_matched_rule', $right) . '&lang=$matches[1]'; //} } //add new rules //_db2($wp_rewrite->rules); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; //_db2($wp_rewrite->rules); return $wp_rewrite->rules; }
static function create_table_available_language($options) { $languages = uls_get_available_languages(false); // get the all languages available in the wp $options = get_option('uls_settings'); // get information from DB $available_language = isset($options['available_language']) ? $options['available_language'] : uls_get_available_languages(false); // get the information that actually is in the DB wp_enqueue_script('uls_languages_js', plugins_url('js/uls-languages-tap.js', __FILE__), array('jquery')); ?> <table id="menu-locations-table" class=""> <thead> <tr> <th>Enable</th> <th>Language</th> <th>Frontend flag optional</th> <th>Optional flag</th> <th>Remove</th> </tr> </thead> <tbody> <?php foreach ($languages as $lang_name => $lang_code) { ?> <tr> <?php $checked = isset($available_language[$lang_name]) ? 'checked' : ''; ?> <td> <input type="checkbox" name="uls_available_language[<?php echo $lang_name; ?> ]" value="<?php echo $lang_code; ?> " <?php echo $checked; ?> /> </td> <td> <!--img src="<?php echo plugins_url("css/blank.gif", __FILE__); ?> " style="margin-right:5px;" class="flag_16x11 flag-<?php echo strtolower(substr($lang_code, -2)); ?> " alt="<?php echo $lang_name; ?> " title="<?php echo $lang_name; ?> " /--> <img src="<?php echo plugins_url("css/blank.gif", __FILE__); ?> " style="margin-right:5px;" class="flag_16x11 flag-<?php echo Codes::languageCode2CountryCode($lang_code); ?> " alt="<?php echo $lang_name; ?> " title="<?php echo $lang_name; ?> " /> <span><?php echo $lang_name; ?> </span> </td> <td> <input type="file" name="uls_available_language_new_flags[<?php echo $lang_name; ?> ]" value="" title="<?php echo __("the default dimension is 32x32 px, it is neccessary to keep the aesthetics"); ?> " /> </td> <td> <?php if (isset($options['uls_available_language_new_flags']) && isset($options['uls_available_language_new_flags'][$lang_name])) { ?> <img src="<?php echo $options['uls_available_language_new_flags'][$lang_name]['url']; ?> " class="optional_flag" alt="<?php echo $lang_name; ?> " title="<?php echo $lang_name; ?> " ></img> <?php } ?> </td> <td> <?php if (isset($options['uls_available_language_new_flags']) && isset($options['uls_available_language_new_flags'][$lang_name])) { ?> <input type="checkbox" name="uls_available_language_del_flags[<?php echo $lang_name; ?> ]" value="" /> <?php } ?> </td> </tr> <?php } ?> </tbody> </table> <input type="hidden" name="available_languages" value="available_languages" > <br/> <table id="menu-locations-table" class=""> <thead> <tr> <th>Install Additional Languages</th> </tr> </thead> <tbody> <tr> <?php require_once ABSPATH . '/wp-admin/includes/translation-install.php'; $translations = wp_get_available_translations(); uasort($translations, array(__CLASS__, 'sort_translations_callback')); // check the requirement to can download language $execute_languages = true; $zip_message = ''; // meessage information $ssl_message = ''; // meessage information if (!class_exists('ZipArchive')) { $zip_message = '<p class="bg-warning">'; $zip_message .= __("Missing class ZipArchive. Please install and retry later."); $zip_message .= '</p>'; $execute_languages = false; } if (!extension_loaded('openssl')) { $ssl_message = '<p class="bg-warning">'; $ssl_message .= __("Missing extension openssl. Please enable openss extension in your php.ini and retry later."); $ssl_message .= '</p>'; $execute_languages = false; } echo "<td>" . __('Select a language') . ": </td><td><select id='tblang'>"; if ($execute_languages) { foreach ($translations as $language) { echo "<option value='" . $language['language'] . ";" . $language['package'] . ";" . $language['english_name'] . "'>"; echo $language['english_name'] . " - " . $language['native_name'] . "</option>"; } } echo "</select>"; if ($execute_languages) { ?> <input type="button" class="button-primary" id="button-download-language" value="<?php echo __('Download', 'user-language-switch'); ?> " /> <?php } ?> </td> </tr> </tbody> </table> <div id="div_message_download" class="div_message_download"> <?php if (isset($_GET['success'])) { if ($_GET['success'] == 1) { $ok_message = '<p class="bg-success">'; $ok_message .= __("Language successfully downloaded!!!"); $ok_message .= '</p>'; echo $ok_message; } else { if ($_GET['success'] == 0) { $error_message = '<p class="bg-warning">'; $error_message .= __("File writing permission denied. Please fix permissions to directory wp-content/languages."); $error_message .= '</p>'; echo $error_message; } } } echo $zip_message; echo $ssl_message; ?> </div> <?php }
/** * Redirect user after successful login. It removes languages in the URL if it is an admin URL. * * @param string $redirect_to URL to redirect to. * @param string $request URL the user is coming from. * @param object $user Logged user's data. * @return string */ function uls_login_redirect($redirect_to, $request, $user) { //check if the URL is an admin URL if (false !== strpos($redirect_to, '/wp-admin')) { //get languages available $languages = uls_get_available_languages(); //add slash to languages to remove it in the URL $aux = array(); foreach ($languages as $key => $item) { $aux[$key] = "/{$item}"; } //remove languages in the URL $redirect_to = str_replace($aux, '', $redirect_to); } return $redirect_to; }
<?php global $wp_query; $shortcode_class = isset($shortcode_class) ? $shortcode_class : 'tab_background_color_picker'; $options = get_option('uls_settings'); // get information from DB $new_img_file = isset($options['uls_available_language_new_flags']) ? $options['uls_available_language_new_flags'] : ''; $languages = uls_get_available_languages(); $postId = null; if (!is_home() && !is_archive() && !is_search() && !is_404()) { $postId = $wp_query->post->ID; } ?> <div id="<?php echo $shortcode_class; ?> "> <?php foreach ($languages as $key => $value) { ?> <?php $content_style = ''; $tagHtml = ' <img src="' . plugins_url('css/blank.gif', __FILE__) . '" style="margin-right:5px;" class="flag_32x32 flag-' . Codes::languageCode2CountryCode($value) . '" alt="' . $value . '" title="' . $key . '" /> '; $grayClass = ''; if (!is_null($postId)) { $hasTranslation = uls_get_post_translation_id($postId, $value) !== false; $grayClass = $hasTranslation ? '' : 'uls-grayscale'; } // if the user load a specific img for a language load it and add its configuration if (isset($new_img_file[$key]) && !empty($new_img_file[$key])) { // get width and height from image loaded
function uls_register_sidebar_laguages() { global $wp_registered_sidebars; $languages = uls_get_available_languages(); // get the all languages available in the wp $options = get_option('uls_settings'); // delete language that is actually in the side, from available languages $lang_code = uls_get_site_language(); foreach ($languages as $lang_name) { if (($lang_name = array_search($lang_code, $languages)) !== false) { unset($languages[$lang_name]); } } // create the N_sidebar X available_languages, but fir ask if the enable checkbox is true if (!isset($options['enable_translation_sidebars_language_switch']) || $options['enable_translation_sidebars_language_switch']) { if (function_exists('register_sidebar')) { $temporal_sidebars = $wp_registered_sidebars; foreach ($temporal_sidebars as $sidebar => $items) { foreach ($languages as $lang_name => $lang_code) { register_sidebar(array('name' => $items['name'] . ' / ' . $lang_name, 'id' => strtolower("uls_" . $items['id'] . '_' . $lang_code), 'description' => __($items['description'] . ' / ' . $lang_name, 'user-language-switch'), 'before_widget' => $items['before_widget'], 'after_widget' => $items['after_widget'], 'before_title' => $items['before_title'], 'after_title' => $items['after_title'])); } } } } }
/** * This function creates a form to update language selection of an user to display in the front end side. If user isn't logged in or can't change the language, then it returns null. * * @param $default_language string language code used as default value of the input selector. If it is null, then the language saved by the user is selected. * @param $label string label to use for the language field. * @param $submit_label string label to use in the button to submit the form. * @param $usccess_message string Message to display if language is saved successfully. * @param $error_message string Message to display if language isn't saved. * * @return mixed HTML code of the form as a string. If user isn't logged in or user can't choose a language(settings of the plugin) then null is returned. */ function uls_create_user_language_switch_form($default_language = null, $label = null, $submit_label = null, $success_message = null, $error_message = null) { //check if user is logged in if (!is_user_logged_in()) { return null; } //check if the user can change the language $options = get_option('uls_settings'); $type = 'frontend'; if (!$options["user_{$type}_configuration"]) { return null; } //get default values $label = empty($label) ? __('Language', 'user-language-switch') : $label; $submit_label = empty($submit_label) ? __('Save', 'user-language-switch') : $submit_label; $success_message = empty($success_message) ? __('Language saved', 'user-language-switch') : $success_message; $error_message = empty($error_message) ? __('Error saving language', 'user-language-switch') : $error_message; //get user's language $language = get_user_meta(get_current_user_id(), "uls_{$type}_language", true); //set the default language if the user doesn't have a preference if (empty($language)) { $language = $options["default_{$type}_language"]; } //available languages $available_languages = uls_get_available_languages(); ob_start(); //include some JS libraries wp_enqueue_script('jquery-form'); ?> <div class="uls-user-language-form-div"> <form id="uls_user_language_form" action="<?php echo admin_url('admin-ajax.php'); ?> " method="POST"> <input type="hidden" name="action" value="uls_user_language_switch" /> <?php if (function_exists("wp_nonce_field")) { ?> <?php wp_nonce_field('uls_user_language_switch', 'uls_wpnonce'); ?> <?php } ?> <label for="uls_language"><?php echo $label; ?> </label> <select id="uls_language" name="<?php echo $options['frontend_language_field_name']; ?> "> <?php foreach ($available_languages as $langName => $langCode) { ?> <?php if ($langCode == $language) { ?> <option value="<?php echo $langCode; ?> " selected="selected"><?php echo $langName; ?> </option> <?php } else { ?> <option value="<?php echo $langCode; ?> "><?php echo $langName; ?> </option> <?php } ?> <?php } ?> </select> <div class="uls_submit_div"> <input type="submit" class="btn" value="<?php echo $submit_label; ?> " /> </div> </form> <div id="uls_user_language_message" class="uls_user_language_message"></div> <script> jQuery(document).ready(function(){ jQuery("form#uls_user_language_form").ajaxForm({ beforeSubmit: function(arr, $form, options) { jQuery("div#uls_user_language_message").html(""); }, success: function (responseText, statusText, xhr, $form){ var $response = jQuery.parseJSON(responseText); if($response.success) jQuery("div#uls_user_language_message").html("<p class='success'><?php echo $success_message; ?> </p>"); else jQuery("div#uls_user_language_message").html("<p class='error'><?php echo $error_message; ?> </p>"); }, error: function(){ jQuery("div#uls_user_language_message").html("<p class='error'><?php echo $error_message; ?> </p>"); } }); }); </script> </div> <?php $res = ob_get_contents(); ob_end_clean(); return $res; }
function head_reference_translation() { //get the id of the current page $url = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://"; $url .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; $post_id = url_to_postid($url); // get all available languages $languages = uls_get_available_languages(); $curren_code = uls_get_user_language(); // get current language // delete the current language site $code_value = array_search($curren_code, $languages); unset($languages[$code_value]); // build the url to be tranlation $url = ''; // get url from where it's using if (is_home()) { $url = get_home_url(); } else { if (is_archive() || is_search() || is_author() || is_category() || is_tag() || is_date()) { $url = uls_get_browser_url(); } } // get browser url // if exits the url so, translate this if (!empty($url)) { // use all available languages and get the url translation foreach ($languages as $language => $code) { $translation_url = uls_get_url_translated($url, $code); echo '<link rel="alternate" hreflang="' . substr($code, 0, 2) . '" href="' . $translation_url . '" />'; } } // build url to the home if (!empty($post_id) && empty($url)) { // change the filter global $uls_permalink_convertion; $uls_permalink_convertion = false; // use all available languages and get the url translation foreach ($languages as $language => $code) { // get the post_id translation if the current page has translation $translation_id = uls_get_post_translation_id($post_id, $code); if (!empty($translation_id)) { $translation_url = uls_get_url_translated(get_permalink($translation_id), $code); echo '<link rel="alternate" hreflang="' . substr($code, 0, 2) . '" href="' . $translation_url . '" />'; } } // leave the global car like it was before $uls_permalink_convertion = true; } }
/** * Save language associations */ function uls_save_association($post_id) { //verify post is a revision $parent_id = wp_is_post_revision($post_id); if ($parent_id === false) { $parent_id = $post_id; } $languages = uls_get_available_languages(); $selected_language = isset($_POST['uls_language']) ? $_POST['uls_language'] : null; if (!empty($selected_language)) { foreach ($languages as $lang) { $related_post = isset($_POST['uls_translation_' . strtolower($lang)]) ? $_POST['uls_translation_' . strtolower($lang)] : null; if (!empty($related_post)) { update_post_meta($related_post, 'uls_translation_' . strtolower($selected_language), $parent_id); } } } }