/**
 * CloudPeakWay Child Theme Functions
 * Adds x-default hreflang and custom language switcher
 */

// Add x-default hreflang tag
add_filter("pll_rel_hreflang_attributes", function($hreflangs) {
    if (!isset($hreflangs["x-default"])) {
        $hreflangs["x-default"] = home_url("/");
    }
    return $hreflangs;
});

// Custom language switcher shortcode
add_shortcode("cpw_language_switcher", function() {
    if (!function_exists("pll_the_languages")) return "";
    $languages = pll_the_languages(array("raw" => true));
    if (empty($languages)) return "";
    $current = pll_current_language();
    $output = '<div class="cpw-lang-switcher">';
    $output .= '<button class="cpw-lang-btn current">' . esc_html(strtoupper($current)) . '</button>';
    $output .= '<ul class="cpw-lang-dropdown">';
    foreach ($languages as $lang) {
        if ($lang["slug"] === $current) continue;
        $output .= sprintf(
            '<li><a href="%s" hreflang="%s">%s %s</a></li>',
            esc_url($lang["url"]),
            esc_attr($lang["locale"]),
            $lang["flag"],
            esc_html($lang["name"])
        );
    }
    $output .= '</ul></div>';
    return $output;
});

// Inject CSS
add_action("wp_head", function() {
    echo '<style>
.cpw-lang-switcher {
    position: relative;
    display: inline-block;
    font-family: "Montserrat", sans-serif;
    z-index: 9999;
}
.cpw-lang-btn {
    background: rgba(0,61,107,0.9);
    color: #fff;
    border: 1px solid rgba(255,255,255,0.3);
    border-radius: 4px;
    padding: 6px 14px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.5px;
    transition: all 0.2s;
}
.cpw-lang-btn:hover { background: #003d6b; border-color: #0078d7; }
.cpw-lang-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    min-width: 180px;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.12);
    padding: 6px 0;
    margin: 4px 0 0 0;
    list-style: none;
}
.cpw-lang-switcher:hover .cpw-lang-dropdown,
.cpw-lang-dropdown:hover { display: block; }
.cpw-lang-dropdown li { margin: 0; padding: 0; }
.cpw-lang-dropdown a {
    display: block;
    padding: 8px 16px;
    color: #333;
    text-decoration: none;
    font-size: 13px;
    transition: background 0.15s;
    white-space: nowrap;
}
.cpw-lang-dropdown a:hover { background: #f0f4f8; color: #003d6b; }
.cpw-lang-header { display: flex; align-items: center; margin-left: 16px; }
.cpw-lang-footer { text-align: center; margin-top: 12px; }
@media (max-width: 768px) {
    .cpw-lang-header { margin-left: 0; margin-top: 8px; }
}
</style>';
});

// Add to header
add_action("astra_masthead_content", function() {
    echo '<div class="cpw-lang-header">';
    echo do_shortcode("[cpw_language_switcher]");
    echo '</div>';
}, 99);

// Add to footer
add_action("astra_footer_after", function() {
    echo '<div class="cpw-lang-footer">';
    echo do_shortcode("[cpw_language_switcher]");
    echo '</div>';
}, 99);
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://cloudpeakway.com/wp-sitemap.xsl" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://cloudpeakway.com/zh/author/89f90e/</loc></url></urlset>
