/**
 * Remove language $lang from the database.
 * @since 3.3
 */
function qtranxf_deleteLanguage($lang)
{
    global $q_config;
    if (!qtranxf_language_predefined($lang)) {
        if ($q_config['default_language'] == $lang) {
            //if(!isset($q_config['language_name'][$lang])||strtolower($lang)=='code') $error = __('No such language!', 'qtranslate');
            return __('Cannot delete Default Language!', 'qtranslate');
        }
        qtranxf_disableLanguage($lang);
    }
    $langs = array();
    qtranxf_load_languages($langs);
    qtranxf_unsetLanguage($langs, $lang);
    qtranxf_save_languages($langs);
    if ($q_config['language'] == $lang) {
        qtranxf_setLanguageAdmin($q_config['default_language']);
    }
    return '';
}
function qtranxf_disableLanguage($lang)
{
    global $q_config;
    if (!qtranxf_isEnabled($lang)) {
        return false;
    }
    foreach ($q_config['enabled_languages'] as $k => $l) {
        if ($l != $lang) {
            continue;
        }
        unset($q_config['enabled_languages'][$k]);
        break;
    }
    qtranxf_unsetLanguage($q_config, $lang);
    if ($q_config['language'] == $lang) {
        qtranxf_setLanguageAdmin($q_config['default_language']);
    }
    qtranxf_update_config_header_css();
    return true;
}
function qtranxf_editConfig()
{
    global $q_config;
    if (!qtranxf_verify_nonce('qtranslate-x_configuration_form')) {
        return;
    }
    // init some needed variables
    if (!isset($q_config['url_info']['errors'])) {
        $q_config['url_info']['errors'] = array();
    }
    if (!isset($q_config['url_info']['warnings'])) {
        $q_config['url_info']['warnings'] = array();
    }
    if (!isset($q_config['url_info']['messages'])) {
        $q_config['url_info']['messages'] = array();
    }
    $errors =& $q_config['url_info']['errors'];
    //$warnings = &$q_config['url_info']['warnings'];
    $messages =& $q_config['url_info']['messages'];
    $q_config['posted'] = array();
    $q_config['posted']['lang_props'] = array();
    $q_config['posted']['language_code'] = '';
    $q_config['posted']['original_lang'] = '';
    $language_code =& $q_config['posted']['language_code'];
    $lang_props =& $q_config['posted']['lang_props'];
    $original_lang =& $q_config['posted']['original_lang'];
    // check for action
    if (isset($_POST['qtranslate_reset']) && isset($_POST['qtranslate_reset2'])) {
        $messages[] = __('qTranslate has been reset.', 'qtranslate');
    } elseif (isset($_POST['default_language'])) {
        qtranxf_updateSettings();
        //execute actions
        qtranxf_executeOnUpdate();
    }
    if (isset($_POST['original_lang'])) {
        // validate form input
        $original_lang = sanitize_text_field($_POST['original_lang']);
        $lang = sanitize_text_field($_POST['language_code']);
        $lang_props['language_name'] = sanitize_text_field($_POST['language_name']);
        $lang_props['flag'] = sanitize_text_field($_POST['language_flag']);
        $lang_props['locale'] = sanitize_text_field($_POST['language_locale']);
        if (strlen($lang) != 2) {
            $errors[] = __('Language Code has to be 2 characters long!', 'qtranslate');
        }
        if (empty($lang_props['language_name'])) {
            $errors[] = __('The Language must have a name!', 'qtranslate');
        }
        if (strlen($lang_props['locale']) < 2) {
            $errors[] = __('The Language must have a Locale!', 'qtranslate');
        }
        if ($original_lang) {
            $lang_props['locale_html'] = sanitize_text_field($_POST['language_locale_html']);
            $lang_props['date_format'] = sanitize_text_field(stripslashes($_POST['language_date_format']));
            $lang_props['time_format'] = sanitize_text_field(stripslashes($_POST['language_time_format']));
            $nam = wp_kses_post(stripslashes($_POST['language_na_message']));
            //allow valid HTML
            $nam = htmlentities(html_entity_decode($nam));
            //standardize HTML entities
            $lang_props['not_available'] = $nam;
            if (empty($lang_props['date_format'])) {
                $errors[] = sprintf(__('Option "%s" must not be empty.', 'qtranslate'), qtranxf_translate_wp('Date Format')) . ' ' . sprintf(__('It has now been pre-filled with a default value as entered on page %s, which you may wish to adjust for this language.', 'qtranslate'), '<a href="' . admin_url('options-general.php') . '" target="_blank">' . qtranxf_translate_wp('General Settings') . '</a>');
                $lang_props['date_format'] = qtranxf_translate_dt_format('date_format', $lang);
            }
            if (empty($lang_props['time_format'])) {
                $errors[] = sprintf(__('Option "%s" must not be empty.', 'qtranslate'), qtranxf_translate_wp('Time Format')) . ' ' . sprintf(__('It has now been pre-filled with a default value as entered on page %s, which you may wish to adjust for this language.', 'qtranslate'), '<a href="' . admin_url('options-general.php') . '" target="_blank">' . qtranxf_translate_wp('General Settings') . '</a>');
                $lang_props['time_format'] = qtranxf_translate_dt_format('time_format', $lang);
            }
            if (empty($lang_props['not_available'])) {
                $errors[] = __('The Language must have a Not-Available Message!', 'qtranslate');
            }
        }
        $langs_preset = qtranxf_langs_preset();
        $langs_stored = qtranxf_langs_stored($langs_preset);
        if (empty($errors)) {
            if (empty($original_lang)) {
                // new language
                if (isset($langs_stored[$lang])) {
                    $errors[] = __('There is already a language with the same Language Code!', 'qtranslate');
                }
            } else {
                // language update
                if ($lang != $original_lang && isset($langs_stored[$lang])) {
                    $errors[] = __('There is already a language with the same Language Code!', 'qtranslate');
                } else {
                    if ($lang != $original_lang) {
                        // remove old language
                        //qtranxf_unsetLanguage($langs,$original_lang);
                        unset($langs_stored[$original_lang]);
                        qtranxf_unsetLanguage($q_config, $original_lang);
                        // if was enabled, set modified one to enabled too
                        foreach ($q_config['enabled_languages'] as $k => $lng) {
                            if ($lng != $original_lang) {
                                continue;
                            }
                            $q_config['enabled_languages'][$k] = $lng;
                            break;
                        }
                    }
                    if ($original_lang == $q_config['default_language']) {
                        // was default, so set modified the default
                        $q_config['default_language'] = $lang;
                    }
                    if ($q_config['language'] == $original_lang) {
                        qtranxf_setLanguageAdmin($lang);
                    }
                }
            }
        }
        if (empty($errors)) {
            // everything is fine, insert language
            foreach ($lang_props as $k => $v) {
                $q_config[$k][$lang] = $v;
                if (empty($v) || !empty($langs_preset[$lang][$k]) && $langs_preset[$lang][$k] == $v) {
                    unset($langs_stored[$lang][$k]);
                } else {
                    $langs_stored[$lang][$k] = $v;
                }
            }
            qtranxf_set_date_i18n_formats($q_config, $lang);
            qtranxf_save_languages($langs_stored);
            qtranxf_enableLanguage($lang);
            $s = 'Custom Language Properties Used for "' . $lang . '"';
            $b = 'I use the following language properties for ' . $lang . ':' . PHP_EOL . PHP_EOL;
            $b .= 'Language Code: ' . $lang . PHP_EOL;
            foreach ($lang_props as $k => $v) {
                $b .= $k . ': ' . $v . PHP_EOL;
            }
            $b .= PHP_EOL . 'which should probably be used as a default preset on the plugin.' . PHP_EOL;
            $b .= PHP_EOL . 'Thank you very much!' . PHP_EOL;
            $u = 'qtranslateteam@gmail.com?subject=' . rawurlencode($s) . '&body=' . rawurlencode($b);
            $messages[] = sprintf(__('The new language properties have been saved. If you think these properties should be the preset default, please %ssend email%s to the development team.', 'qtranslate'), '<a href="mailto:' . $u . '"><strong>', '</strong></a>');
        }
        //qtranxf_dbg_log('$lang: ',$lang);
        if (!empty($errors) || isset($_GET['edit'])) {
            if (empty($errors) && $original_lang != $lang) {
                $target = admin_url('options-general.php?page=qtranslate-x&edit=' . $lang);
                wp_redirect($target);
                exit;
            }
            // get old values in the form
            $language_code = $lang;
        } else {
            if (empty($original_lang)) {
                $target = admin_url('options-general.php?page=qtranslate-x&edit=' . $lang . '&msg=new');
                wp_redirect($target);
                exit;
            } else {
                //reset form for new language
                $lang_props = array();
                $original_lang = '';
            }
        }
    } elseif (isset($_GET['convert'])) {
        // update language tags
        global $wpdb;
        $wpdb->show_errors();
        @set_time_limit(0);
        $cnt = 0;
        //this will not work correctly if set of languages is different
        foreach ($q_config['enabled_languages'] as $lang) {
            $cnt += $wpdb->query('UPDATE ' . $wpdb->posts . ' set post_title = REPLACE(post_title, "[lang_' . $lang . ']","[:' . $lang . ']"),  post_content = REPLACE(post_content, "[lang_' . $lang . ']","[:' . $lang . ']")');
            $wpdb->query('UPDATE ' . $wpdb->posts . ' set post_title = REPLACE(post_title, "[/lang_' . $lang . ']","[:]"),  post_content = REPLACE(post_content, "[/lang_' . $lang . ']","[:]")');
        }
        if ($cnt > 0) {
            $messages[] = sprintf(__('%d database entries have been converted.', 'qtranslate'), $cnt);
        } else {
            $messages[] = __('No database entry has been affected while processing the conversion request.', 'qtranslate');
        }
    } elseif (isset($_GET['markdefault'])) {
        // update language tags
        global $wpdb;
        $wpdb->show_errors();
        @set_time_limit(0);
        $result = $wpdb->get_results('SELECT ID, post_content, post_title, post_excerpt, post_type FROM ' . $wpdb->posts . ' WHERE post_status = \'publish\' AND  (post_type = \'post\' OR post_type = \'page\') AND NOT (post_content LIKE \'%<!--:-->%\' OR post_title LIKE \'%<!--:-->%\' OR post_content LIKE \'%![:!]%\' ESCAPE \'!\' OR post_title LIKE \'%![:!]%\' ESCAPE \'!\')');
        if (is_array($result)) {
            $cnt_page = 0;
            $cnt_post = 0;
            foreach ($result as $post) {
                $title = qtranxf_mark_default($post->post_title);
                $content = qtranxf_mark_default($post->post_content);
                $excerpt = qtranxf_mark_default($post->post_excerpt);
                if ($title == $post->post_title && $content == $post->post_content && $excerpt == $post->post_excerpt) {
                    continue;
                }
                switch ($post->post_type) {
                    case 'post':
                        ++$cnt_post;
                        break;
                    case 'page':
                        ++$cnt_page;
                        break;
                }
                //qtranxf_dbg_log('markdefault:'. PHP_EOL .'title old: '.$post->post_title. PHP_EOL .'title new: '.$title. PHP_EOL .'content old: '.$post->post_content. PHP_EOL .'content new: '.$content); continue;
                $wpdb->query($wpdb->prepare('UPDATE ' . $wpdb->posts . ' set post_content = %s, post_title = %s, post_excerpt = %s WHERE ID = %d', $content, $title, $excerpt, $post->ID));
            }
            if ($cnt_page > 0) {
                $messages[] = sprintf(__('%d pages have been processed to set the default language.', 'qtranslate'), $cnt_page);
            } else {
                $messages[] = __('No initially untranslated pages found to set the default language', 'qtranslate');
            }
            if ($cnt_post > 0) {
                $messages[] = sprintf(__('%d posts have been processed to set the default language.', 'qtranslate'), $cnt_post);
            } else {
                $messages[] = __('No initially untranslated posts found to set the default language.', 'qtranslate');
            }
            $messages[] = sprintf(__('Post types other than "post" or "page", as well as unpublished entries, will have to be adjusted manually as needed, since there is no common way to automate setting the default language otherwise. It can be done with a custom script though. You may request a %spaid support%s for this.', 'qtranslate'), '<a href="https://qtranslatexteam.wordpress.com/contact-us/">', '</a>');
        }
    } elseif (isset($_GET['edit'])) {
        $lang = sanitize_text_field($_GET['edit']);
        $lang = preg_replace('/[^a-z]/i', '', $lang);
        if (strlen($lang) != 2) {
            $errors[] = __('Language Code has to be 2 characters long!', 'qtranslate');
        }
        $original_lang = $lang;
        $language_code = $lang;
        //$langs = array(); qtranxf_langs_config($langs);
        //$lang_props['language_name'] = isset($langs['language_name'][$lang])?$langs['language_name'][$lang]:'';
        //$lang_props['locale'] = isset($langs['locale'][$lang])?$langs['locale'][$lang]:'';
        //$lang_props['locale_html'] = isset($langs['locale_html'][$lang])?$langs['locale_html'][$lang]:'';
        //$lang_props['date_format'] = isset($langs['date_format'][$lang])?$langs['date_format'][$lang]:'';
        //$lang_props['time_format'] = isset($langs['time_format'][$lang])?$langs['time_format'][$lang]:'';
        //$lang_props['not_available'] = isset($langs['not_available'][$lang])?$langs['not_available'][$lang]:'';
        //$lang_props['flag'] = isset($langs['flag'][$lang])?$langs['flag'][$lang]:'';
        $langs = qtranxf_langs_config();
        $lang_props = isset($langs[$lang]) ? $langs[$lang] : array();
    } elseif (isset($_GET['delete'])) {
        $lang = sanitize_text_field($_GET['delete']);
        // validate delete (protect code)
        //if($q_config['default_language']==$lang) $errors[] = 'Cannot delete Default Language!';
        //if(!isset($q_config['language_name'][$lang])||strtolower($lang)=='code') $errors[] = __('No such language!', 'qtranslate');
        //if(empty($errors)) {
        //	// everything seems fine, delete language
        $err = qtranxf_deleteLanguage($lang);
        if (!empty($err)) {
            $errors[] = $err;
        }
        //}
    } elseif (isset($_GET['enable'])) {
        $lang = sanitize_text_field($_GET['enable']);
        // enable validate
        if (!qtranxf_enableLanguage($lang)) {
            $errors[] = __('Language is already enabled or invalid!', 'qtranslate');
        }
    } elseif (isset($_GET['disable'])) {
        $lang = sanitize_text_field($_GET['disable']);
        // enable validate
        if ($lang == $q_config['default_language']) {
            $errors[] = __('Cannot disable Default Language!', 'qtranslate');
        }
        if (!qtranxf_isEnabled($lang)) {
            if (!isset($q_config['language_name'][$lang])) {
                $errors[] = __('No such language!', 'qtranslate');
            }
        }
        // everything seems fine, disable language
        if (empty($errors) && !qtranxf_disableLanguage($lang)) {
            $errors[] = __('Language is already disabled!', 'qtranslate');
        }
    } elseif (isset($_GET['moveup'])) {
        $lang = sanitize_text_field($_GET['moveup']);
        $languages = qtranxf_getSortedLanguages();
        $msg = __('No such language!', 'qtranslate');
        foreach ($languages as $key => $language) {
            if ($language != $lang) {
                continue;
            }
            if ($key == 0) {
                $msg = __('Language is already first!', 'qtranslate');
                break;
            }
            $languages[$key] = $languages[$key - 1];
            $languages[$key - 1] = $language;
            $q_config['enabled_languages'] = $languages;
            $msg = __('New order saved.', 'qtranslate');
            qtranxf_update_config_header_css();
            break;
        }
        $messages[] = $msg;
    } elseif (isset($_GET['movedown'])) {
        $lang = sanitize_text_field($_GET['movedown']);
        $languages = qtranxf_getSortedLanguages();
        $msg = __('No such language!', 'qtranslate');
        foreach ($languages as $key => $language) {
            if ($language != $lang) {
                continue;
            }
            if ($key == sizeof($languages) - 1) {
                $msg = __('Language is already last!', 'qtranslate');
                break;
            }
            $languages[$key] = $languages[$key + 1];
            $languages[$key + 1] = $language;
            $q_config['enabled_languages'] = $languages;
            $msg = __('New order saved.', 'qtranslate');
            qtranxf_update_config_header_css();
            break;
        }
        $messages[] = $msg;
    }
    do_action('qtranslate_editConfig');
    $everything_fine = (isset($_POST['submit']) || isset($_GET['delete']) || isset($_GET['enable']) || isset($_GET['disable']) || isset($_GET['moveup']) || isset($_GET['movedown'])) && empty($errors);
    if ($everything_fine) {
        // settings might have changed, so save
        qtranxf_saveConfig();
        if (empty($messages)) {
            $messages[] = __('Options saved.', 'qtranslate');
        }
    }
    if ($q_config['auto_update_mo']) {
        if (!is_dir(WP_LANG_DIR) || !($ll = @fopen(trailingslashit(WP_LANG_DIR) . 'qtranslate.test', 'a'))) {
            $errors[] = sprintf(__('Could not write to "%s", Gettext Databases could not be downloaded!', 'qtranslate'), WP_LANG_DIR);
        } else {
            @fclose($ll);
            @unlink(trailingslashit(WP_LANG_DIR) . 'qtranslate.test');
        }
    }
}
function qtranxf_editConfig()
{
    global $q_config;
    // init some needed variables
    if (!isset($q_config['url_info']['errors'])) {
        $q_config['url_info']['errors'] = array();
    }
    if (!isset($q_config['url_info']['warnings'])) {
        $q_config['url_info']['warnings'] = array();
    }
    if (!isset($q_config['url_info']['messages'])) {
        $q_config['url_info']['messages'] = array();
    }
    $errors =& $q_config['url_info']['errors'];
    //$warnings = &$q_config['url_info']['warnings'];
    $messages =& $q_config['url_info']['messages'];
    $q_config['posted'] = array();
    $q_config['posted']['lang_props'] = array();
    $q_config['posted']['language_code'] = '';
    $q_config['posted']['original_lang'] = '';
    $language_code =& $q_config['posted']['language_code'];
    $lang_props =& $q_config['posted']['lang_props'];
    $original_lang =& $q_config['posted']['original_lang'];
    // check for action
    if (isset($_POST['qtranslate_reset']) && isset($_POST['qtranslate_reset2'])) {
        $messages[] = __('qTranslate has been reset.', 'qtranslate');
    } elseif (isset($_POST['default_language'])) {
        qtranxf_updateSettings();
        //execute actions
        qtranxf_executeOnUpdate();
    }
    if (isset($_POST['original_lang'])) {
        // validate form input
        $original_lang = sanitize_text_field($_POST['original_lang']);
        $lang = sanitize_text_field($_POST['language_code']);
        if ($_POST['language_na_message'] == '') {
            $errors[] = __('The Language must have a Not-Available Message!', 'qtranslate');
        }
        if (strlen($_POST['language_locale']) < 2) {
            $errors[] = __('The Language must have a Locale!', 'qtranslate');
        }
        if ($_POST['language_name'] == '') {
            $errors[] = __('The Language must have a name!', 'qtranslate');
        }
        if (strlen($lang) != 2) {
            $errors[] = __('Language Code has to be 2 characters long!', 'qtranslate');
        }
        $langs = array();
        qtranxf_load_languages($langs);
        $language_names = $langs['language_name'];
        if ($original_lang == '' && empty($errors)) {
            // new language
            if (isset($language_names[$lang])) {
                $errors[] = __('There is already a language with the same Language Code!', 'qtranslate');
            }
        }
        if ($original_lang != '' && empty($errors)) {
            // language update
            if ($lang != $original_lang && isset($language_names[$lang])) {
                $errors[] = __('There is already a language with the same Language Code!', 'qtranslate');
            } else {
                if ($lang != $original_lang) {
                    // remove old language
                    qtranxf_unsetLanguage($langs, $original_lang);
                    qtranxf_unsetLanguage($q_config, $original_lang);
                }
                if (in_array($original_lang, $q_config['enabled_languages'])) {
                    // was enabled, so set modified one to enabled too
                    for ($i = 0; $i < sizeof($q_config['enabled_languages']); $i++) {
                        if ($q_config['enabled_languages'][$i] == $original_lang) {
                            $q_config['enabled_languages'][$i] = $lang;
                        }
                    }
                }
                if ($original_lang == $q_config['default_language']) {
                    // was default, so set modified the default
                    $q_config['default_language'] = $lang;
                }
                if ($q_config['language'] == $original_lang) {
                    qtranxf_setLanguageAdmin($lang);
                }
            }
        }
        $lang_props['language_name'] = sanitize_text_field($_POST['language_name']);
        $lang_props['flag'] = sanitize_text_field($_POST['language_flag']);
        $lang_props['locale'] = sanitize_text_field($_POST['language_locale']);
        $lang_props['locale_html'] = sanitize_text_field($_POST['language_locale_html']);
        $lang_props['date_format'] = sanitize_text_field(stripslashes($_POST['language_date_format']));
        $lang_props['time_format'] = sanitize_text_field(stripslashes($_POST['language_time_format']));
        $lang_props['not_available'] = wp_kses_post(stripslashes($_POST['language_na_message']));
        //allow valid HTML
        if (empty($errors)) {
            // everything is fine, insert language
            foreach ($lang_props as $k => $v) {
                $q_config[$k][$lang] = $v;
            }
            qtranxf_copyLanguage($langs, $q_config, $lang);
            qtranxf_save_languages($langs);
            qtranxf_enableLanguage($lang);
            //qtranxf_update_config_header_css();
        }
        if (!empty($errors) || isset($_GET['edit'])) {
            // get old values in the form
            $language_code = $lang;
        } else {
            $lang_props = array();
            //reset form for new language
        }
    } elseif (isset($_GET['convert'])) {
        // update language tags
        global $wpdb;
        $wpdb->show_errors();
        @set_time_limit(0);
        $cnt = 0;
        //this will not work correctly if set of languages is different
        foreach ($q_config['enabled_languages'] as $lang) {
            $cnt += $wpdb->query('UPDATE ' . $wpdb->posts . ' set post_title = REPLACE(post_title, "[lang_' . $lang . ']","[:' . $lang . ']"),  post_content = REPLACE(post_content, "[lang_' . $lang . ']","[:' . $lang . ']")');
            $wpdb->query('UPDATE ' . $wpdb->posts . ' set post_title = REPLACE(post_title, "[/lang_' . $lang . ']","[:]"),  post_content = REPLACE(post_content, "[/lang_' . $lang . ']","[:]")');
        }
        if ($cnt > 0) {
            $messages[] = sprintf(__('%d database entries have been converted.', 'qtranslate'), $cnt);
        } else {
            $messages[] = __('No database entry has been affected while processing the conversion request.', 'qtranslate');
        }
    } elseif (isset($_GET['markdefault'])) {
        // update language tags
        global $wpdb;
        $wpdb->show_errors();
        @set_time_limit(0);
        $result = $wpdb->get_results('SELECT ID, post_content, post_title, post_excerpt, post_type FROM ' . $wpdb->posts . ' WHERE post_status = \'publish\' AND  (post_type = \'post\' OR post_type = \'page\') AND NOT (post_content LIKE \'%<!--:-->%\' OR post_title LIKE \'%<!--:-->%\' OR post_content LIKE \'%![:!]%\' ESCAPE \'!\' OR post_title LIKE \'%![:!]%\' ESCAPE \'!\')');
        if (is_array($result)) {
            $cnt_page = 0;
            $cnt_post = 0;
            foreach ($result as $post) {
                $title = qtranxf_mark_default($post->post_title);
                $content = qtranxf_mark_default($post->post_content);
                $excerpt = qtranxf_mark_default($post->post_excerpt);
                if ($title == $post->post_title && $content == $post->post_content && $excerpt == $post->post_excerpt) {
                    continue;
                }
                switch ($post->post_type) {
                    case 'post':
                        ++$cnt_post;
                        break;
                    case 'page':
                        ++$cnt_page;
                        break;
                }
                //qtranxf_dbg_log('markdefault:'. PHP_EOL .'title old: '.$post->post_title. PHP_EOL .'title new: '.$title. PHP_EOL .'content old: '.$post->post_content. PHP_EOL .'content new: '.$content); continue;
                $wpdb->query($wpdb->prepare('UPDATE ' . $wpdb->posts . ' set post_content = %s, post_title = %s, post_excerpt = %s WHERE ID = %d', $content, $title, $excerpt, $post->ID));
            }
            if ($cnt_page > 0) {
                $messages[] = sprintf(__('%d pages have been processed to set the default language.', 'qtranslate'), $cnt_page);
            } else {
                $messages[] = __('No initially untranslated pages found to set the default language', 'qtranslate');
            }
            if ($cnt_post > 0) {
                $messages[] = sprintf(__('%d posts have been processed to set the default language.', 'qtranslate'), $cnt_post);
            } else {
                $messages[] = __('No initially untranslated posts found to set the default language.', 'qtranslate');
            }
            $messages[] = sprintf(__('Post types other than "post" or "page", as well as unpublished entries, will have to be adjusted manually as needed, since there is no common way to automate setting the default language otherwise. It can be done with a custom script though. You may request a %spaid support%s for this.', 'qtranslate'), '<a href="https://qtranslatexteam.wordpress.com/contact-us/">', '</a>');
        }
    } elseif (isset($_GET['edit'])) {
        $lang = $_GET['edit'];
        $original_lang = $lang;
        $language_code = $lang;
        //$langs = $q_config;
        $langs = array();
        qtranxf_languages_configured($langs);
        $lang_props['language_name'] = isset($langs['language_name'][$lang]) ? $langs['language_name'][$lang] : '';
        $lang_props['locale'] = isset($langs['locale'][$lang]) ? $langs['locale'][$lang] : '';
        $lang_props['locale_html'] = isset($langs['locale_html'][$lang]) ? $langs['locale_html'][$lang] : '';
        $lang_props['date_format'] = isset($langs['date_format'][$lang]) ? $langs['date_format'][$lang] : '';
        $lang_props['time_format'] = isset($langs['time_format'][$lang]) ? $langs['time_format'][$lang] : '';
        $lang_props['not_available'] = isset($langs['not_available'][$lang]) ? $langs['not_available'][$lang] : '';
        $lang_props['flag'] = isset($langs['flag'][$lang]) ? $langs['flag'][$lang] : '';
    } elseif (isset($_GET['delete'])) {
        $lang = $_GET['delete'];
        // validate delete (protect code)
        //if($q_config['default_language']==$lang) $errors[] = 'Cannot delete Default Language!';
        //if(!isset($q_config['language_name'][$lang])||strtolower($lang)=='code') $errors[] = __('No such language!', 'qtranslate');
        if (empty($errors)) {
            // everything seems fine, delete language
            $err = qtranxf_deleteLanguage($lang);
            if (!empty($err)) {
                $errors[] = $err;
            }
        }
    } elseif (isset($_GET['enable'])) {
        $lang = $_GET['enable'];
        // enable validate
        if (!qtranxf_enableLanguage($lang)) {
            $errors[] = __('Language is already enabled or invalid!', 'qtranslate');
        }
    } elseif (isset($_GET['disable'])) {
        $lang = $_GET['disable'];
        // enable validate
        if ($lang == $q_config['default_language']) {
            $errors[] = __('Cannot disable Default Language!', 'qtranslate');
        }
        if (!qtranxf_isEnabled($lang)) {
            if (!isset($q_config['language_name'][$lang])) {
                $errors[] = __('No such language!', 'qtranslate');
            }
        }
        // everything seems fine, disable language
        if (empty($errors) && !qtranxf_disableLanguage($lang)) {
            $errors[] = __('Language is already disabled!', 'qtranslate');
        }
    } elseif (isset($_GET['moveup'])) {
        $languages = qtranxf_getSortedLanguages();
        $msg = __('No such language!', 'qtranslate');
        foreach ($languages as $key => $language) {
            if ($language != $_GET['moveup']) {
                continue;
            }
            if ($key == 0) {
                $msg = __('Language is already first!', 'qtranslate');
                break;
            }
            $languages[$key] = $languages[$key - 1];
            $languages[$key - 1] = $language;
            $q_config['enabled_languages'] = $languages;
            $msg = __('New order saved.', 'qtranslate');
            break;
        }
        $messages[] = $msg;
    } elseif (isset($_GET['movedown'])) {
        $languages = qtranxf_getSortedLanguages();
        $msg = __('No such language!', 'qtranslate');
        foreach ($languages as $key => $language) {
            if ($language != $_GET['movedown']) {
                continue;
            }
            if ($key == sizeof($languages) - 1) {
                $msg = __('Language is already last!', 'qtranslate');
                break;
            }
            $languages[$key] = $languages[$key + 1];
            $languages[$key + 1] = $language;
            $q_config['enabled_languages'] = $languages;
            $msg = __('New order saved.', 'qtranslate');
            break;
        }
        $messages[] = $msg;
    }
    do_action('qtranslate_editConfig');
    $everything_fine = (isset($_POST['submit']) || isset($_GET['delete']) || isset($_GET['enable']) || isset($_GET['disable']) || isset($_GET['moveup']) || isset($_GET['movedown'])) && empty($errors);
    if ($everything_fine) {
        // settings might have changed, so save
        qtranxf_saveConfig();
        if (empty($messages)) {
            $messages[] = __('Options saved.', 'qtranslate');
        }
    }
    if ($q_config['auto_update_mo']) {
        if (!is_dir(WP_LANG_DIR) || !($ll = @fopen(trailingslashit(WP_LANG_DIR) . 'qtranslate.test', 'a'))) {
            $errors[] = sprintf(__('Could not write to "%s", Gettext Databases could not be downloaded!', 'qtranslate'), WP_LANG_DIR);
        } else {
            @fclose($ll);
            @unlink(trailingslashit(WP_LANG_DIR) . 'qtranslate.test');
        }
    }
}