<?php
/**
 * Hreflang / Multilingual SEO Manager.
 *
 * Two modes of operation:
 * 1. **Auto-detect** – integrates with WPML / Polylang / TranslatePress to
 *    auto-generate hreflang tags from existing translations.
 * 2. **Manual** – per-post UI lets the editor specify alternate-language URLs
 *    for any page (works without a multilingual plugin).
 *
 * @package SEO_Fury
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class SEO_Fury_Hreflang {

    private static $instance = null;

    /** Post meta key storing manual hreflang mappings (JSON array). */
    const META_KEY = '_seo_fury_hreflang';

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        $enabled = get_option( 'seo_fury_hreflang_enabled', 0 );
        if ( ! $enabled ) {
            return;
        }

        // Output hreflang tags.
        add_action( 'wp_head', array( $this, 'output_hreflang_tags' ), 1 );

        // Admin settings.
        if ( is_admin() ) {
            add_action( 'admin_init', array( $this, 'register_settings' ) );
        }

        // Post meta box.
        add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
        add_action( 'save_post', array( $this, 'save_meta_box' ), 10, 2 );

        // AJAX.
        add_action( 'wp_ajax_seo_fury_hreflang_detect', array( $this, 'ajax_detect_translations' ) );
    }

    // =========================================================================
    // Settings
    // =========================================================================

    public function register_settings() {
        $group = 'seo_fury_hreflang_group';

        register_setting( $group, 'seo_fury_hreflang_enabled', array( 'sanitize_callback' => 'absint' ) );
        register_setting( $group, 'seo_fury_hreflang_mode', array(
            'sanitize_callback' => function ( $v ) {
                return in_array( $v, array( 'auto', 'manual', 'both' ), true ) ? $v : 'auto';
            },
        ) );
        register_setting( $group, 'seo_fury_hreflang_default_lang', array( 'sanitize_callback' => 'sanitize_text_field' ) );
        register_setting( $group, 'seo_fury_hreflang_x_default', array( 'sanitize_callback' => 'absint' ) );
        register_setting( $group, 'seo_fury_hreflang_post_types', array(
            'sanitize_callback' => function ( $v ) {
                return is_array( $v ) ? array_map( 'sanitize_text_field', $v ) : array( 'post', 'page' );
            },
        ) );
    }

    // =========================================================================
    // Detection: which multilingual plugin is active
    // =========================================================================

    /**
     * Detect active multilingual plugin.
     *
     * @return string|false  'wpml', 'polylang', 'translatepress', or false
     */
    public function detect_plugin() {
        if ( defined( 'ICL_SITEPRESS_VERSION' ) || class_exists( 'SitePress' ) ) {
            return 'wpml';
        }
        if ( function_exists( 'pll_the_languages' ) || defined( 'POLYLANG_VERSION' ) ) {
            return 'polylang';
        }
        if ( class_exists( 'TRP_Translate_Press' ) ) {
            return 'translatepress';
        }
        return false;
    }

    /**
     * Get all configured languages from the active plugin.
     *
     * @return array [ 'en' => 'English', 'ru' => 'Русский', ... ]
     */
    public function get_languages() {
        $plugin = $this->detect_plugin();

        if ( 'wpml' === $plugin ) {
            return $this->get_wpml_languages();
        } elseif ( 'polylang' === $plugin ) {
            return $this->get_polylang_languages();
        } elseif ( 'translatepress' === $plugin ) {
            return $this->get_translatepress_languages();
        }

        // Manual mode — return from options.
        $default_lang = get_option( 'seo_fury_hreflang_default_lang', substr( get_locale(), 0, 2 ) );
        return array( $default_lang => $this->lang_name( $default_lang ) );
    }

    private function get_wpml_languages() {
        $langs = array();
        if ( function_exists( 'icl_get_languages' ) ) {
            $active = icl_get_languages( 'skip_missing=0' );
            foreach ( $active as $code => $info ) {
                $langs[ $code ] = isset( $info['native_name'] ) ? $info['native_name'] : $code;
            }
        }
        return $langs;
    }

    private function get_polylang_languages() {
        $langs = array();
        if ( function_exists( 'pll_the_languages' ) ) {
            $list = pll_the_languages( array( 'raw' => 1 ) );
            if ( is_array( $list ) ) {
                foreach ( $list as $info ) {
                    $slug = isset( $info['slug'] ) ? $info['slug'] : '';
                    $name = isset( $info['name'] ) ? $info['name'] : $slug;
                    if ( $slug ) {
                        $langs[ $slug ] = $name;
                    }
                }
            }
        }
        return $langs;
    }

    private function get_translatepress_languages() {
        $langs    = array();
        $settings = get_option( 'trp_settings', array() );
        $all      = array();
        if ( ! empty( $settings['default-language'] ) ) {
            $all[] = $settings['default-language'];
        }
        if ( ! empty( $settings['translation-languages'] ) ) {
            $all = array_merge( $all, (array) $settings['translation-languages'] );
        }
        foreach ( array_unique( $all ) as $locale ) {
            $code           = substr( $locale, 0, 2 );
            $langs[ $code ] = $this->lang_name( $code );
        }
        return $langs;
    }

    // =========================================================================
    // Translation URLs retrieval
    // =========================================================================

    /**
     * Get hreflang URLs for a post or the current page.
     *
     * @param int|null $post_id
     * @return array [ 'en' => 'https://…', 'ru' => 'https://…', ... ]
     */
    public function get_hreflang_urls( $post_id = null ) {
        if ( ! $post_id && is_singular() ) {
            $post_id = get_the_ID();
        }

        $mode = get_option( 'seo_fury_hreflang_mode', 'auto' );
        $urls = array();

        // Auto-detect from multilingual plugin.
        if ( in_array( $mode, array( 'auto', 'both' ), true ) ) {
            $urls = $this->get_auto_urls( $post_id );
        }

        // Manual overrides (per-post meta).
        if ( in_array( $mode, array( 'manual', 'both' ), true ) && $post_id ) {
            $manual = $this->get_manual_urls( $post_id );
            if ( ! empty( $manual ) ) {
                // Manual overrides auto for the same language.
                $urls = array_merge( $urls, $manual );
            }
        }

        return $urls;
    }

    /**
     * Retrieve translation URLs from the active multilingual plugin.
     */
    private function get_auto_urls( $post_id ) {
        $plugin = $this->detect_plugin();
        $urls   = array();

        if ( 'wpml' === $plugin && $post_id ) {
            $urls = $this->get_wpml_urls( $post_id );
        } elseif ( 'polylang' === $plugin && $post_id ) {
            $urls = $this->get_polylang_urls( $post_id );
        } elseif ( 'translatepress' === $plugin ) {
            $urls = $this->get_translatepress_urls();
        }

        return $urls;
    }

    private function get_wpml_urls( $post_id ) {
        $urls  = array();
        $type  = get_post_type( $post_id );
        $trid  = apply_filters( 'wpml_element_trid', null, $post_id, 'post_' . $type );
        $trans = apply_filters( 'wpml_get_element_translations', array(), $trid, 'post_' . $type );

        foreach ( $trans as $lang => $info ) {
            if ( ! empty( $info->element_id ) ) {
                $permalink = get_permalink( $info->element_id );
                if ( $permalink ) {
                    $urls[ $lang ] = $permalink;
                }
            }
        }
        return $urls;
    }

    private function get_polylang_urls( $post_id ) {
        $urls  = array();
        $langs = $this->get_polylang_languages();
        foreach ( array_keys( $langs ) as $lang ) {
            if ( function_exists( 'pll_get_post' ) ) {
                $translated_id = pll_get_post( $post_id, $lang );
                if ( $translated_id ) {
                    $permalink = get_permalink( $translated_id );
                    if ( $permalink ) {
                        $urls[ $lang ] = $permalink;
                    }
                }
            }
        }
        return $urls;
    }

    private function get_translatepress_urls() {
        $urls = array();
        // TranslatePress uses URL-based translations.
        $settings = get_option( 'trp_settings', array() );
        $locales  = array();
        if ( ! empty( $settings['default-language'] ) ) {
            $locales[] = $settings['default-language'];
        }
        if ( ! empty( $settings['translation-languages'] ) ) {
            $locales = array_merge( $locales, (array) $settings['translation-languages'] );
        }
        $current_url = $this->get_current_url();
        foreach ( array_unique( $locales ) as $locale ) {
            $code = substr( $locale, 0, 2 );
            $url  = apply_filters( 'trp_get_url_for_language', $current_url, $locale, '' );
            if ( $url ) {
                $urls[ $code ] = $url;
            }
        }
        return $urls;
    }

    /**
     * Get manual hreflang URLs from post meta.
     */
    private function get_manual_urls( $post_id ) {
        $raw = get_post_meta( $post_id, self::META_KEY, true );
        if ( empty( $raw ) ) {
            return array();
        }
        $entries = json_decode( $raw, true );
        if ( ! is_array( $entries ) ) {
            return array();
        }
        $urls = array();
        foreach ( $entries as $entry ) {
            if ( ! empty( $entry['lang'] ) && ! empty( $entry['url'] ) ) {
                $urls[ sanitize_text_field( $entry['lang'] ) ] = esc_url_raw( $entry['url'] );
            }
        }
        return $urls;
    }

    // =========================================================================
    // Output hreflang tags in <head>
    // =========================================================================

    public function output_hreflang_tags() {
        if ( ! is_singular() && ! is_front_page() && ! is_home() ) {
            return;
        }

        // Check post type.
        $allowed_types = (array) get_option( 'seo_fury_hreflang_post_types', array( 'post', 'page' ) );
        if ( is_singular() ) {
            $post_type = get_post_type();
            if ( ! in_array( $post_type, $allowed_types, true ) ) {
                return;
            }
        }

        $urls = $this->get_hreflang_urls();
        if ( count( $urls ) < 2 ) {
            return; // Need at least 2 languages for hreflang to make sense.
        }

        $brand = function_exists( 'seo_fury_get_brand_name' ) ? seo_fury_get_brand_name() : 'SEO Fury';
        echo "\n<!-- " . esc_html( $brand ) . ": hreflang -->\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

        foreach ( $urls as $lang => $url ) {
            printf(
                '<link rel="alternate" hreflang="%s" href="%s" />' . "\n",
                esc_attr( $lang ),
                esc_url( $url )
            );
        }

        // x-default.
        $add_x_default = get_option( 'seo_fury_hreflang_x_default', 1 );
        if ( $add_x_default ) {
            $default_lang = get_option( 'seo_fury_hreflang_default_lang', substr( get_locale(), 0, 2 ) );
            if ( isset( $urls[ $default_lang ] ) ) {
                printf(
                    '<link rel="alternate" hreflang="x-default" href="%s" />' . "\n",
                    esc_url( $urls[ $default_lang ] )
                );
            }
        }

        echo "<!-- /" . esc_html( $brand ) . ": hreflang -->\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    }

    // =========================================================================
    // Post Meta Box (Manual hreflang editor)
    // =========================================================================

    public function add_meta_box() {
        $mode = get_option( 'seo_fury_hreflang_mode', 'auto' );
        if ( 'auto' === $mode ) {
            return; // No need for manual editor in auto mode.
        }

        $post_types = (array) get_option( 'seo_fury_hreflang_post_types', array( 'post', 'page' ) );
        $brand      = function_exists( 'seo_fury_get_brand_name' ) ? seo_fury_get_brand_name() : 'SEO Fury';

        foreach ( $post_types as $pt ) {
            add_meta_box(
                'seo-fury-hreflang',
                $brand . ': ' . __( 'Hreflang', 'seo-fury' ),
                array( $this, 'render_meta_box' ),
                $pt,
                'side',
                'default'
            );
        }
    }

    public function render_meta_box( $post ) {
        wp_nonce_field( 'seo_fury_hreflang_save', '_seo_fury_hreflang_nonce' );

        $raw     = get_post_meta( $post->ID, self::META_KEY, true );
        $entries = $raw ? json_decode( $raw, true ) : array();
        if ( ! is_array( $entries ) ) {
            $entries = array();
        }

        $common_langs = $this->get_common_languages();
        ?>
        <div id="seo-fury-hreflang-editor">
            <p class="description" style="margin-bottom:10px">
                <?php esc_html_e( 'Add alternate language URLs for this page.', 'seo-fury' ); ?>
            </p>
            <div id="seo-fury-hreflang-rows">
                <?php if ( ! empty( $entries ) ) : ?>
                    <?php foreach ( $entries as $i => $entry ) : ?>
                    <div class="seo-fury-hreflang-row" style="display:flex;gap:4px;margin-bottom:6px;align-items:center">
                        <select name="seo_fury_hreflang[<?php echo (int) $i; ?>][lang]" style="width:80px">
                            <?php foreach ( $common_langs as $code => $name ) : ?>
                                <option value="<?php echo esc_attr( $code ); ?>" <?php selected( isset( $entry['lang'] ) ? $entry['lang'] : '', $code ); ?>>
                                    <?php echo esc_html( $code . ' — ' . $name ); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <input type="url" name="seo_fury_hreflang[<?php echo (int) $i; ?>][url]" value="<?php echo esc_url( isset( $entry['url'] ) ? $entry['url'] : '' ); ?>" style="flex:1;min-width:0" placeholder="https://...">
                        <button type="button" class="button seo-fury-hreflang-remove" style="padding:0 6px;color:#b91c1c">&times;</button>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            <button type="button" class="button" id="seo-fury-hreflang-add" style="margin-top:4px">
                + <?php esc_html_e( 'Add Language', 'seo-fury' ); ?>
            </button>
        </div>
        <script>
        jQuery(function($) {
            var idx = <?php echo intval( count( $entries ) ); ?>;
            var langs = <?php echo wp_json_encode( $common_langs ); ?>;

            $('#seo-fury-hreflang-add').on('click', function() {
                var opts = '';
                $.each(langs, function(code, name) {
                    opts += '<option value="' + code + '">' + code + ' — ' + name + '</option>';
                });
                var row = '<div class="seo-fury-hreflang-row" style="display:flex;gap:4px;margin-bottom:6px;align-items:center">' +
                    '<select name="seo_fury_hreflang[' + idx + '][lang]" style="width:80px">' + opts + '</select>' +
                    '<input type="url" name="seo_fury_hreflang[' + idx + '][url]" style="flex:1;min-width:0" placeholder="https://...">' +
                    '<button type="button" class="button seo-fury-hreflang-remove" style="padding:0 6px;color:#b91c1c">&times;</button>' +
                    '</div>';
                $('#seo-fury-hreflang-rows').append(row);
                idx++;
            });

            $(document).on('click', '.seo-fury-hreflang-remove', function() {
                $(this).closest('.seo-fury-hreflang-row').remove();
            });
        });
        </script>
        <?php
    }

    public function save_meta_box( $post_id, $post ) {
        if ( ! isset( $_POST['_seo_fury_hreflang_nonce'] ) ) {
            return;
        }
        if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_seo_fury_hreflang_nonce'] ) ), 'seo_fury_hreflang_save' ) ) {
            return;
        }
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }

        if ( ! isset( $_POST['seo_fury_hreflang'] ) ) {
            delete_post_meta( $post_id, self::META_KEY );
            return;
        }

        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
        $raw_entries = wp_unslash( $_POST['seo_fury_hreflang'] );
        $clean       = array();

        foreach ( (array) $raw_entries as $entry ) {
            $lang = isset( $entry['lang'] ) ? sanitize_text_field( $entry['lang'] ) : '';
            $url  = isset( $entry['url'] ) ? esc_url_raw( $entry['url'] ) : '';
            if ( $lang && $url ) {
                $clean[] = array( 'lang' => $lang, 'url' => $url );
            }
        }

        if ( ! empty( $clean ) ) {
            update_post_meta( $post_id, self::META_KEY, wp_json_encode( $clean ) );
        } else {
            delete_post_meta( $post_id, self::META_KEY );
        }
    }

    // =========================================================================
    // AJAX: detect translations from multilingual plugin
    // =========================================================================

    public function ajax_detect_translations() {
        check_ajax_referer( 'seo_fury_hreflang_nonce', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( __( 'Permission denied.', 'seo-fury' ) );
        }

        $plugin = $this->detect_plugin();
        $langs  = $this->get_languages();

        wp_send_json_success( array(
            'plugin'    => $plugin ? $plugin : 'none',
            'languages' => $langs,
        ) );
    }

    // =========================================================================
    // Admin Tab
    // =========================================================================

    public function render_admin_tab() {
        $enabled       = get_option( 'seo_fury_hreflang_enabled', 0 );
        $mode          = get_option( 'seo_fury_hreflang_mode', 'auto' );
        $default_lang  = get_option( 'seo_fury_hreflang_default_lang', substr( get_locale(), 0, 2 ) );
        $x_default     = get_option( 'seo_fury_hreflang_x_default', 1 );
        $post_types    = (array) get_option( 'seo_fury_hreflang_post_types', array( 'post', 'page' ) );
        $plugin        = $this->detect_plugin();
        $all_pt        = get_post_types( array( 'public' => true ), 'objects' );
        $common_langs  = $this->get_common_languages();
        ?>
        <form method="post" action="options.php">
            <?php settings_fields( 'seo_fury_hreflang_group' ); ?>

            <?php if ( $plugin ) : ?>
            <div style="background:var(--sf-success-light,#f0fdf4);border:1px solid var(--sf-gray-200,#bbf7d0);color:var(--sf-gray-700);border-radius:6px;padding:12px 16px;margin-bottom:20px">
                <strong><?php esc_html_e( 'Detected Multilingual Plugin:', 'seo-fury' ); ?></strong>
                <?php
                $names = array( 'wpml' => 'WPML', 'polylang' => 'Polylang', 'translatepress' => 'TranslatePress' );
                echo esc_html( isset( $names[ $plugin ] ) ? $names[ $plugin ] : $plugin );
                ?>
                — <?php esc_html_e( 'hreflang tags can be generated automatically.', 'seo-fury' ); ?>
            </div>
            <?php else : ?>
            <div style="background:var(--sf-warning-light,#fefce8);border:1px solid var(--sf-gray-200,#fde68a);color:var(--sf-gray-700);border-radius:6px;padding:12px 16px;margin-bottom:20px">
                <strong><?php esc_html_e( 'No multilingual plugin detected.', 'seo-fury' ); ?></strong>
                <?php esc_html_e( 'Use Manual mode to add hreflang URLs per page, or install WPML / Polylang / TranslatePress for auto-detection.', 'seo-fury' ); ?>
            </div>
            <?php endif; ?>

            <table class="form-table">
                <tr>
                    <th scope="row"><?php esc_html_e( 'Enable Hreflang', 'seo-fury' ); ?></th>
                    <td>
                        <label>
                            <input type="checkbox" name="seo_fury_hreflang_enabled" value="1" <?php checked( $enabled ); ?>>
                            <?php esc_html_e( 'Output hreflang tags in page head', 'seo-fury' ); ?>
                        </label>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><?php esc_html_e( 'Mode', 'seo-fury' ); ?></th>
                    <td>
                        <select name="seo_fury_hreflang_mode">
                            <option value="auto" <?php selected( $mode, 'auto' ); ?> <?php disabled( ! $plugin ); ?>>
                                <?php esc_html_e( 'Auto (from multilingual plugin)', 'seo-fury' ); ?>
                            </option>
                            <option value="manual" <?php selected( $mode, 'manual' ); ?>>
                                <?php esc_html_e( 'Manual (per-post URLs)', 'seo-fury' ); ?>
                            </option>
                            <option value="both" <?php selected( $mode, 'both' ); ?> <?php disabled( ! $plugin ); ?>>
                                <?php esc_html_e( 'Both (auto + manual overrides)', 'seo-fury' ); ?>
                            </option>
                        </select>
                        <p class="description">
                            <?php esc_html_e( 'Auto mode requires WPML, Polylang, or TranslatePress.', 'seo-fury' ); ?>
                        </p>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><?php esc_html_e( 'Default Language', 'seo-fury' ); ?></th>
                    <td>
                        <select name="seo_fury_hreflang_default_lang">
                            <?php foreach ( $common_langs as $code => $name ) : ?>
                                <option value="<?php echo esc_attr( $code ); ?>" <?php selected( $default_lang, $code ); ?>>
                                    <?php echo esc_html( $code . ' — ' . $name ); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <p class="description">
                            <?php esc_html_e( 'The primary language of your site. Used for x-default tag.', 'seo-fury' ); ?>
                        </p>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><?php esc_html_e( 'x-default Tag', 'seo-fury' ); ?></th>
                    <td>
                        <label>
                            <input type="checkbox" name="seo_fury_hreflang_x_default" value="1" <?php checked( $x_default ); ?>>
                            <?php esc_html_e( 'Add x-default hreflang for default language', 'seo-fury' ); ?>
                        </label>
                        <p class="description">
                            <?php esc_html_e( 'Recommended. Tells search engines which page to show when no language match.', 'seo-fury' ); ?>
                        </p>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><?php esc_html_e( 'Post Types', 'seo-fury' ); ?></th>
                    <td>
                        <?php foreach ( $all_pt as $pt ) : ?>
                            <label style="display:block;margin-bottom:4px">
                                <input type="checkbox" name="seo_fury_hreflang_post_types[]" value="<?php echo esc_attr( $pt->name ); ?>" <?php checked( in_array( $pt->name, $post_types, true ) ); ?>>
                                <?php echo esc_html( $pt->labels->name ); ?>
                            </label>
                        <?php endforeach; ?>
                    </td>
                </tr>
            </table>

            <?php submit_button(); ?>
        </form>

        <?php if ( $plugin ) : ?>
        <hr>
        <h3><?php esc_html_e( 'Detected Languages', 'seo-fury' ); ?></h3>
        <?php
        $langs = $this->get_languages();
        if ( ! empty( $langs ) ) :
        ?>
        <table class="widefat striped" style="max-width:400px">
            <thead>
                <tr>
                    <th><?php esc_html_e( 'Code', 'seo-fury' ); ?></th>
                    <th><?php esc_html_e( 'Language', 'seo-fury' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ( $langs as $code => $name ) : ?>
                <tr>
                    <td><code><?php echo esc_html( $code ); ?></code></td>
                    <td><?php echo esc_html( $name ); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php else : ?>
            <p><?php esc_html_e( 'No languages detected.', 'seo-fury' ); ?></p>
        <?php endif; ?>
        <?php endif; ?>
        <?php
    }

    // =========================================================================
    // Helpers
    // =========================================================================

    /**
     * Get current URL.
     */
    private function get_current_url() {
        global $wp;
        return home_url( add_query_arg( array(), $wp->request ) );
    }

    /**
     * Common ISO 639-1 languages list.
     */
    private function get_common_languages() {
        return array(
            'en' => 'English',
            'ru' => 'Русский',
            'de' => 'Deutsch',
            'fr' => 'Français',
            'es' => 'Español',
            'it' => 'Italiano',
            'pt' => 'Português',
            'nl' => 'Nederlands',
            'pl' => 'Polski',
            'uk' => 'Українська',
            'tr' => 'Türkçe',
            'ja' => '日本語',
            'ko' => '한국어',
            'zh' => '中文',
            'ar' => 'العربية',
            'hi' => 'हिन्दी',
            'th' => 'ไทย',
            'vi' => 'Tiếng Việt',
            'sv' => 'Svenska',
            'no' => 'Norsk',
            'da' => 'Dansk',
            'fi' => 'Suomi',
            'cs' => 'Čeština',
            'ro' => 'Română',
            'hu' => 'Magyar',
            'bg' => 'Български',
            'el' => 'Ελληνικά',
            'he' => 'עברית',
            'id' => 'Bahasa Indonesia',
            'ms' => 'Bahasa Melayu',
            'ka' => 'ქართული',
            'kk' => 'Қазақша',
        );
    }

    /**
     * Get language name from code.
     */
    private function lang_name( $code ) {
        $all = $this->get_common_languages();
        return isset( $all[ $code ] ) ? $all[ $code ] : $code;
    }
}
