<?php
/**
 * Callback for Social Links  
 */
function spirallite_social_cb() {
    $facebook    = get_theme_mod( 'spirallite_facebook', '#' );
    $twitter     = get_theme_mod( 'spirallite_twitter' );
    $pinterest   = get_theme_mod( 'spirallite_pinterest', '#' );
    $linkedin    = get_theme_mod( 'spirallite_linkedin' );
    $instagram   = get_theme_mod( 'spirallite_instagram', '#' );
    $youtube     = get_theme_mod( 'spirallite_youtube' );
    $tiktok      = get_theme_mod( 'spirallite_tiktok' );

    if ( $facebook || $twitter || $pinterest || $linkedin || $instagram || $youtube || $tiktok ) { ?>
        <div class="icons">
            <?php 
                if ( $facebook ) echo '<a href="' . esc_url( $facebook ) . '" target="_blank" title="' . esc_attr__( 'Facebook', 'spiral-lite-one-page' ) . '"><i class="fa fa-facebook"></i></a>';
                if ( $twitter ) echo '<a href="' . esc_url( $twitter ) . '" target="_blank" title="' . esc_attr__( 'Twitter', 'spiral-lite-one-page' ) . '"><i class="fa fa-twitter"></i></a>'; 
                if ( $pinterest ) echo '<a href="' . esc_url( $pinterest ) . '" target="_blank" title="' . esc_attr__( 'Pinterest', 'spiral-lite-one-page' ) . '"><i class="fa fa-pinterest-p"></i></a>';
                if ( $linkedin ) echo '<a href="' . esc_url( $linkedin ) . '" target="_blank" title="' . esc_attr__( 'LinkedIn', 'spiral-lite-one-page' ) . '"><i class="fa fa-linkedin"></i></a>';
                if ( $instagram ) echo '<a href="' . esc_url( $instagram ) . '" target="_blank" title="' . esc_attr__( 'Instagram', 'spiral-lite-one-page' ) . '"><i class="fa fa-instagram"></i></a>';
                if ( $youtube ) echo '<a href="' . esc_url( $youtube ) . '" target="_blank" title="' . esc_attr__( 'YouTube', 'spiral-lite-one-page' ) . '"><i class="fa fa-youtube"></i></a>';
                if ( $tiktok ) echo '<a href="' . esc_url( $tiktok ) . '" target="_blank" title="' . esc_attr__( 'TikTok', 'spiral-lite-one-page' ) . '"><i class="fab fa-tiktok"></i></a>';
            ?>
        </div>
    <?php }
}
add_action( 'spirallite_social', 'spirallite_social_cb' );

/**
 * For Header 
*/
function spirallite_social__header(){
   $facebook    = get_theme_mod( 'spirallite_facebook', '#' );
    $twitter     = get_theme_mod( 'spirallite_twitter' );
    $pinterest   = get_theme_mod( 'spirallite_pinterest', '#' );
    $linkedin    = get_theme_mod( 'spirallite_linkedin' );
    $instagram   = get_theme_mod( 'spirallite_instagram', '#' );
    $youtube     = get_theme_mod( 'spirallite_youtube' );
    $tiktok      = get_theme_mod( 'spirallite_tiktok' );
    
    if( $facebook || $twitter || $pinterest || $linkedin || $instagram || $youtube || $tiktok ){ ?>
    <div class="social-icon fa-social-hover">
		<?php 
            if( $facebook ) echo '<a href="'. esc_url( $facebook ) .'" target="_blank" title="'. esc_attr__( 'Facebook', 'spiral-lite-one-page' ) .'"><i class="fa fa-facebook"></i></a>';
            if( $twitter ) echo '<a href="'. esc_url( $twitter ) .'" target="_blank" title="'. esc_attr__( 'Twitter', 'spiral-lite-one-page' ) .'"><i class="fa fa-twitter"></i></a>'; 
            if( $pinterest ) echo '<a href="'. esc_url( $pinterest ) .'" target="_blank" title="'. esc_attr__( 'Pinterest', 'spiral-lite-one-page' ) .'"><i class="fa fa-pinterest-p"></i></a>';
            if( $linkedin ) echo '<a href="'. esc_url( $linkedin ) .'" target="_blank" title="'. esc_attr__( 'LinkedIn', 'spiral-lite-one-page' ) .'"><i class="fa fa-linkedin"></i></a>';
            if( $instagram ) echo '<a href="'. esc_url( $instagram ) .'" target="_blank" title="'. esc_attr__( 'Instagram', 'spiral-lite-one-page' ) .'"><i class="fa fa-instagram"></i></a>';
            if( $youtube ) echo '<a href="'. esc_url( $youtube ) .'" target="_blank" title="'. esc_attr__( 'YouTube', 'spiral-lite-one-page' ) .'"><i class="fa fa-youtube"></i></a>';
            if( $tiktok ) echo '<a href="'. esc_url( $tiktok ) .'" target="_blank" title="'. esc_attr__( 'Tiktok', 'spiral-lite-one-page' ) .'"><i class="fab fa-tiktok"></i></a>';
        ?>
	</div>
    <?php }
}
add_action( 'spirallite_social_top', 'spirallite_social__header' );
/**
 * Function to get Sections 
 */

/*
function spiral_lite_one_page_get_sections() {
    $sections = array('about', 'calltoaction', 'services', 'cta2', 'blog', 'contact');
    $enabled_sections = array();
    $default_titles = array(
        'about' => __('About Us', 'spiral-lite-one-page'),
        'calltoaction' => __('', 'spiral-lite-one-page'),
        'services' => __('Services', 'spiral-lite-one-page'),
        'cta2' => __('', 'spiral-lite-one-page'),
        'blog' => __('Blog', 'spiral-lite-one-page'),
        'contact' => __('Contact Us', 'spiral-lite-one-page'),
    );

    foreach ($sections as $section) {
        if ( get_theme_mod( 'spiral-lite-one-page_ed_' . $section . '_section' ) == 1 ) {
            $menu_text = get_theme_mod("spiral_lite_one_page_{$section}_section_menu_title", $default_titles[$section]);
            $enabled_sections[] = array(
                'id' => $section,
                'menu_text' => $menu_text,
            );
        }
    }
    return $enabled_sections;
}
*/

function spiral_lite_one_page_get_sections() {
    // List of all section slugs
    $section_ids = array( 'about', 'calltoaction', 'services', 'cta2', 'blog', 'contact' );

    // Custom default menu titles for each section
    $default_titles = array(
        'about'        => __( 'About Us', 'spiral-lite-one-page' ),
        'services'     => __( 'Services', 'spiral-lite-one-page' ),
        'blog'         => __( 'Blog', 'spiral-lite-one-page' ),
        'contact'      => __( 'Contact Us', 'spiral-lite-one-page' ),
        'calltoaction' => '', // No menu title for this section
        'cta2'         => '', // No menu title for this section
    );

    $enabled_sections = array();

    foreach ( $section_ids as $id ) {
        // Check if the section is enabled
        $enabled = get_theme_mod( "spiral-lite-one-page_ed_{$id}_section", true );

        if ( $enabled ) {
            // Use specific default titles if provided
            $default_title = isset( $default_titles[ $id ] ) ? $default_titles[ $id ] : '';

            // Get menu text, fallback to default title
            $menu_text = get_theme_mod( "spiral_lite_one_page_{$id}_section_menu_title", $default_title );

            $enabled_sections[] = array(
                'id'        => $id,
                'menu_text' => $menu_text,
            );
        }
    }

    return $enabled_sections;
}


/*******************************************************************************/
/*  Get Started Notice                                                         */
/*******************************************************************************/
add_action( 'wp_ajax_spiral_lite_one_page_dismissed_notice_handler', 'spiral_lite_one_page_ajax_notice_handler' );

 /**
  * AJAX handler to store the state of dismissible notices.
  */
 function spiral_lite_one_page_ajax_notice_handler() {
     if ( isset( $_POST['type'] ) ) {
         // Pick up the notice "type" - passed via jQuery (the "data-notice" attribute on the notice)
         $type = sanitize_text_field( wp_unslash( $_POST['type'] ) );
         // Store it in the options table
         update_option( 'dismissed-' . $type, TRUE );
     }
 }
 
 function spiral_lite_one_page_deprecated_hook_admin_notice() {
         // Check if it's been dismissed...
         if ( ! get_option('dismissed-get_started', FALSE ) ) {
             // Added the class "notice-get-started-class" so jQuery pick it up and pass via AJAX,
             // and added "data-notice" attribute in order to track multiple / different notices
             // multiple dismissible notice states ?>
             <div class="updated notice notice-get-started-class is-dismissible" data-notice="get_started">
                 <div class="spiral-lite-one-page-getting-started-notice clearfix">
                     <div class="spiral-lite-one-page-theme-screenshot">
                         <img src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/screenshot.png" class="screenshot" alt="<?php esc_attr_e( 'Theme Screenshot', 'spiral-lite-one-page' ); ?>" />
                     </div><!-- /.spiral-lite-one-page-theme-screenshot -->
                     <div class="spiral-lite-one-page-theme-notice-content">
                         <h2 class="spiral-lite-one-page-notice-h2">
                             <?php
                         printf(
                         /* translators: 1: welcome page link starting html tag, 2: welcome page link ending html tag. */
                             esc_html__( 'Welcome! Thank you for choosing %1$s!', 'spiral-lite-one-page' ), '<strong>'. wp_get_theme()->get('Name'). '</strong>' );
                         ?>
                         </h2>
 
                         <p class="plugin-install-notice"><?php echo sprintf(__('Install and activate <strong>Pixelonetry Companion</strong> plugin for taking full advantage of all the features this theme has to offer.', 'spiral-lite-one-page')) ?></p>
 
                         <a class="spiral-lite-one-page-btn-get-started button button-primary button-hero spiral-lite-one-page-button-padding" href="#" data-name="" data-slug=""><?php $theme = wp_get_theme(); printf( esc_html__( 'Get started with %s', 'spiral-lite-one-page' ), esc_html( $theme->get( 'Name' ) ) ); ?></a><span class="spiral-lite-one-page-push-down">
                         <?php
                             /* translators: %1$s: Anchor link start %2$s: Anchor link end */
                             printf(
                                 'or %1$sCustomize theme%2$s</a></span>',
                                 '<a target="_blank" href="' . esc_url( admin_url( 'customize.php' ) ) . '">',
                                 '</a>'
                             );
                         ?>
                     </div><!-- /.spiral-lite-one-page-theme-notice-content -->
                 </div>
             </div>
         <?php }
 }
 
 add_action( 'admin_notices', 'spiral_lite_one_page_deprecated_hook_admin_notice' );

/*******************************************************************************/
/*  Plugin Installer                                                           */
/*******************************************************************************/


 add_action( 'wp_ajax_install_act_plugin', 'spiral_lite_one_page_admin_install_plugin' );
 
 function spiral_lite_one_page_admin_install_plugin() {
     /**
      * Install Plugin.
      */
     include_once ABSPATH . '/wp-admin/includes/file.php';
     include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
 
     if ( ! file_exists( WP_PLUGIN_DIR . '/pixelonetry-companion' ) ) {
         $api = plugins_api( 'plugin_information', array(
             'slug'   => sanitize_key( wp_unslash( 'pixelonetry-companion' ) ),
             'fields' => array(
                 'sections' => false,
             ),
         ) );
 
         $skin     = new WP_Ajax_Upgrader_Skin();
         $upgrader = new Plugin_Upgrader( $skin );
         $result   = $upgrader->install( $api->download_link );
     }
 
     // Activate plugin.
     if ( current_user_can( 'activate_plugin' ) ) {
         $result = activate_plugin( 'pixelonetry-companion/pixelonetry-companion.php' );
     }
 }	
 
 ?>