<?php
/**
 * Blog Over Customizer Edit Shortcuts
 * 
 * Registers selective refresh partials with edit shortcuts (pencil icons)
 * that appear on elements in the Customizer preview and redirect to their settings.
 *
 * @package Blog_Over
 */

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

/**
 * Register selective refresh partials with edit shortcuts
 */
function blog_over_register_edit_shortcuts($wp_customize)
{
    // Make sure selective refresh is available
    if (!isset($wp_customize->selective_refresh)) {
        return;
    }

    // =====================================================
    // TOPBAR EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_topbar_shortcut', array(
        'selector' => '.blog-over-topbar',
        'settings' => array('blog_over_topbar_show'),
        'container_inclusive' => true,
        'render_callback' => 'blog_over_render_topbar_partial',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // DARK MODE TOGGLE EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_dark_mode_shortcut', array(
        'selector' => '.blog-over-theme-toggle',
        'settings' => array('blog_over_dark_mode_toggle'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // SUBSCRIBE BUTTON EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_subscribe_btn_shortcut', array(
        'selector' => '.blog-over-subscribe-btn',
        'settings' => array('blog_over_show_subscribe_btn'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // SOCIAL ICONS EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_social_shortcut', array(
        'selector' => '.blog-over-topbar-social',
        'settings' => array('blog_over_header_social_show'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // SEARCH ICON EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_search_shortcut', array(
        'selector' => '.blog-over-search-wrapper',
        'settings' => array('blog_over_show_search'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // MENU LAYOUT EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_menu_layout_shortcut', array(
        'selector' => '.blog-over-primary-menu-container',
        'settings' => array('blog_over_menu_layout'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // TICKER SECTION EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_ticker_shortcut', array(
        'selector' => '.blog-over-ticker-slider',
        'settings' => array('blog_over_enable_ticker'),
        'container_inclusive' => true,
        'render_callback' => 'blog_over_render_ticker_partial',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // HERO SECTION EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_hero_shortcut', array(
        'selector' => '.blog-over-hero-wrapper',
        'settings' => array('blog_over_enable_hero'),
        'container_inclusive' => true,
        'render_callback' => 'blog_over_render_hero_partial',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // FEATURED LINKS EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_featured_shortcut', array(
        'selector' => '.blog-over-featured-links-wrapper',
        'settings' => array('blog_over_enable_featured_links'),
        'container_inclusive' => true,
        'render_callback' => 'blog_over_render_featured_partial',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // YOU MISSED SECTION EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_missed_shortcut', array(
        'selector' => '.blog-over-missed-wrapper',
        'settings' => array('blog_over_you_missed_show'),
        'container_inclusive' => true,
        'render_callback' => 'blog_over_render_missed_partial',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // FOOTER SECTION EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_footer_shortcut', array(
        'selector' => '.blog-over-footer',
        'settings' => array('blog_over_footer_bg_color'),
        'container_inclusive' => false,
        'render_callback' => 'blog_over_render_footer_partial',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // FOOTER BACKGROUND IMAGE EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_footer_bg_image_shortcut', array(
        'selector' => '.blog-over-footer-widgets',
        'settings' => array('blog_over_footer_bg_image'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // COPYRIGHT SECTION EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_copyright_shortcut', array(
        'selector' => '.blog-over-footer-bottom',
        'settings' => array('blog_over_copyright_text'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // SIDEBAR EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_sidebar_shortcut', array(
        'selector' => '#secondary',
        'settings' => array('blog_over_blog_layout'),
        'container_inclusive' => false,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // BREADCRUMB EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_breadcrumb_shortcut', array(
        'selector' => '.blog-over-breadcrumb',
        'settings' => array('blog_over_breadcrumb_enable'),
        'container_inclusive' => false,
        'render_callback' => 'blog_over_output_breadcrumb_trail',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // PAGE HEADER EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_page_header_shortcut', array(
        'selector' => '.blog-over-page-header',
        'settings' => array('blog_over_page_header_enable'),
        'container_inclusive' => true,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // AUTHOR BOX EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_author_box_shortcut', array(
        'selector' => '.blog-over-author-box',
        'settings' => array('blog_over_author_box_show'),
        'container_inclusive' => true,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // POST NAVIGATION EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_post_nav_shortcut', array(
        'selector' => '.blog-over-post-navigation',
        'settings' => array('blog_over_post_nav_show'),
        'container_inclusive' => true,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // RELATED POSTS EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_related_posts_shortcut', array(
        'selector' => '.blog-over-related-posts',
        'settings' => array('blog_over_related_posts_show'),
        'container_inclusive' => true,
        'render_callback' => '__return_empty_string',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // GO TO TOP (SCROLL TO TOP) EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_scroll_top_shortcut', array(
        'selector' => '.blog-over-scroll-top',
        'settings' => array('blog_over_scroll_top'),
        'container_inclusive' => true,
        'render_callback' => 'blog_over_render_scroll_top_partial',
        'fallback_refresh' => true,
    ));

    // =====================================================
    // ADVERTISING BANNER EDIT SHORTCUT
    // =====================================================
    $wp_customize->selective_refresh->add_partial('blog_over_ad_banner_shortcut', array(
        'selector'            => '.blog-over-ad-banner-wrapper',
        'settings'            => array('blog_over_ad_banner_enable'),
        'container_inclusive'  => true,
        'render_callback'     => 'blog_over_render_ad_banner_partial',
        'fallback_refresh'    => true,
    ));
}
add_action('customize_register', 'blog_over_register_edit_shortcuts', 999);


/**
 * Topbar partial render callback
 */
function blog_over_render_topbar_partial()
{
    if (get_theme_mod('blog_over_topbar_show', true)) {
        get_template_part('template-parts/header/topbar');
    }
}

/**
 * You Missed partial render callback
 */
function blog_over_render_missed_partial()
{
    if (get_theme_mod('blog_over_you_missed_show', false)) {
        get_template_part('template-parts/you-missed');
    }
}

/**
 * Scroll to Top partial render callback
 */
function blog_over_render_scroll_top_partial()
{
    if (get_theme_mod('blog_over_scroll_top', true)) {
        $icon = get_theme_mod('blog_over_scroll_top_icon', 'fas fa-chevron-up');
        $position = get_theme_mod('blog_over_scroll_top_position', 'right');
        ?>
        <button class="blog-over-scroll-top blog-over-scroll-pos-<?php echo esc_attr($position); ?>"
            aria-label="<?php esc_attr_e('Scroll to top', 'blog-over'); ?>">
            <i class="<?php echo esc_attr($icon); ?>"></i>
        </button>
        <?php
    }
}
