<div class="theme-offer">
	<?php 
        // Check if the demo import has been completed
        $video_podcasting_demo_import_completed = get_option('video_podcasting_demo_import_completed', false);

        // If the demo import is completed, display the "View Site" button
        if ($video_podcasting_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'video-podcasting') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'video-podcasting') . '</a></span>';
        }

		//POST and update the customizer and other related data
        if (isset($_POST['submit'])) {

            if (!is_plugin_active('audioigniter/audioigniter.php')) {
                // Plugin details
                $video_podcasting_audioigniter_plugin_slug = 'audioigniter';
                $video_podcasting_audioigniter_plugin_file = 'audioigniter/audioigniter.php';
            
                // Check if plugin is installed
                $video_podcasting_installed_plugins = get_plugins();
                if (!isset($video_podcasting_installed_plugins[$video_podcasting_audioigniter_plugin_file])) {
                    // Include necessary WordPress files for plugin installation
                    include_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
                    include_once(ABSPATH . 'wp-admin/includes/file.php');
                    include_once(ABSPATH . 'wp-admin/includes/misc.php');
                    include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
            
                    // Install the plugin
                    $video_podcasting_plugin_upgrader = new Plugin_Upgrader();
                    $video_podcasting_plugin_upgrader->install('https://downloads.wordpress.org/plugin/audioigniter.latest-stable.zip');
                }
            
                // Activate the plugin
                activate_plugin($video_podcasting_audioigniter_plugin_file);
            } 

            // ------- Create Nav Menu --------
            $video_podcasting_menuname = 'Main Menus';
            $video_podcasting_bpmenulocation = 'primary';
            $video_podcasting_menu_exists = wp_get_nav_menu_object($video_podcasting_menuname);

            if (!$video_podcasting_menu_exists) {
                $video_podcasting_menu_id = wp_create_nav_menu($video_podcasting_menuname);

                // Create Home Page
                $video_podcasting_home_title = 'Home';
                $video_podcasting_home = array(
                    'post_type' => 'page',
                    'post_title' => $video_podcasting_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $video_podcasting_home_id = wp_insert_post($video_podcasting_home);
                // Assign Home Page Template
                add_post_meta($video_podcasting_home_id, '_wp_page_template', 'page-template/custom-home-page.php');
                // Update options to set Home Page as the front page
                update_option('page_on_front', $video_podcasting_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($video_podcasting_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'video-podcasting'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $video_podcasting_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $video_podcasting_pages_title = 'Pages';
                $video_podcasting_pages_content = '
                <p>Explore all the pages we have on our website. Find information about our services, company, and more.</p>

                 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br> 

                  All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $video_podcasting_pages = array(
                    'post_type' => 'page',
                    'post_title' => $video_podcasting_pages_title,
                    'post_content' => $video_podcasting_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $video_podcasting_pages_id = wp_insert_post($video_podcasting_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($video_podcasting_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'video-podcasting'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $video_podcasting_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $video_podcasting_about_title = 'About Us';
                $video_podcasting_about_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>

                         Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br> 

                            There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br> 

                            All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $video_podcasting_about = array(
                    'post_type' => 'page',
                    'post_title' => $video_podcasting_about_title,
                    'post_content' => $video_podcasting_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $video_podcasting_about_id = wp_insert_post($video_podcasting_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($video_podcasting_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'video-podcasting'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $video_podcasting_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Trending Page with Dummy Content
                    $video_podcasting_trending_title = 'Trending';
                    $video_podcasting_trending_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><br>

                    Discover trending video podcasts, popular creators, and most-watched episodes updated regularly.';

                    $video_podcasting_trending = array(
                        'post_type'    => 'page',
                        'post_title'   => $video_podcasting_trending_title,
                        'post_content' => $video_podcasting_trending_content,
                        'post_status'  => 'publish',
                        'post_author'  => 1,
                        'post_slug'    => 'trending'
                    );

                    $video_podcasting_trending_id = wp_insert_post($video_podcasting_trending);

                    // Add Trending Page to Menu
                    wp_update_nav_menu_item($video_podcasting_menu_id, 0, array(
                        'menu-item-title'     => __('Trending', 'video-podcasting'),
                        'menu-item-classes'  => 'trending',
                        'menu-item-url'      => home_url('/trending/'),
                        'menu-item-status'   => 'publish',
                        'menu-item-object-id'=> $video_podcasting_trending_id,
                        'menu-item-object'   => 'page',
                        'menu-item-type'     => 'post_type'
                    ));

                    // Create Blog Page 
                    $video_podcasting_blog_page_title = 'Blog';

                    $video_podcasting_blog_page_query = new WP_Query(array(
                        'post_type'      => 'page',
                        'name'           => sanitize_title($video_podcasting_blog_page_title),
                        'post_status'    => 'publish',
                        'posts_per_page' => 1
                    ));
                    if (!$video_podcasting_blog_page_query->have_posts()) {
                        $video_podcasting_blog_page = array(
                            'post_type'   => 'page',
                            'post_title'  => $video_podcasting_blog_page_title,
                            'post_status' => 'publish',
                            'post_author' => 1,
                        );
                        $video_podcasting_blog_page_id = wp_insert_post($video_podcasting_blog_page);
                        update_option('page_for_posts', $video_podcasting_blog_page_id);

                        wp_update_nav_menu_item($video_podcasting_menu_id, 0, array(
                            'menu-item-title'      => __('Blog', 'video-podcasting'),
                            'menu-item-url'        => get_permalink($video_podcasting_blog_page_id),
                            'menu-item-status'     => 'publish',
                            'menu-item-object-id'  => $video_podcasting_blog_page_id,
                            'menu-item-object'     => 'page',
                            'menu-item-type'       => 'post_type',
                        ));
                    }
                    
                    // Create Upcoming Page with Dummy Content
                    $video_podcasting_upcoming_title = 'Upcoming';
                    $video_podcasting_upcoming_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><br>

                    Stay tuned for upcoming video podcast episodes, live streams, and exclusive creator content.';

                    $video_podcasting_upcoming = array(
                        'post_type'    => 'page',
                        'post_title'   => $video_podcasting_upcoming_title,
                        'post_content' => $video_podcasting_upcoming_content,
                        'post_status'  => 'publish',
                        'post_author'  => 1,
                        'post_slug'    => 'upcoming'
                    );

                    $video_podcasting_upcoming_id = wp_insert_post($video_podcasting_upcoming);

                    // Add Upcoming Page to Menu
                    wp_update_nav_menu_item($video_podcasting_menu_id, 0, array(
                        'menu-item-title'     => __('Upcoming', 'video-podcasting'),
                        'menu-item-classes'  => 'upcoming',
                        'menu-item-url'      => home_url('/upcoming/'),
                        'menu-item-status'   => 'publish',
                        'menu-item-object-id'=> $video_podcasting_upcoming_id,
                        'menu-item-object'   => 'page',
                        'menu-item-type'     => 'post_type'
                    ));

                    // Create Contact Page with Dummy Content
                    $video_podcasting_contact_title = 'Contact';
                    $video_podcasting_contact_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><br>

                    Get in touch with us for collaborations, sponsorships, or podcast submissions.';

                    $video_podcasting_contact = array(
                        'post_type'    => 'page',
                        'post_title'   => $video_podcasting_contact_title,
                        'post_content' => $video_podcasting_contact_content,
                        'post_status'  => 'publish',
                        'post_author'  => 1,
                        'post_slug'    => 'contact'
                    );

                    $video_podcasting_contact_id = wp_insert_post($video_podcasting_contact);

                    // Add Contact Page to Menu
                    wp_update_nav_menu_item($video_podcasting_menu_id, 0, array(
                        'menu-item-title'     => __('Contact', 'video-podcasting'),
                        'menu-item-classes'  => 'contact',
                        'menu-item-url'      => home_url('/contact/'),
                        'menu-item-status'   => 'publish',
                        'menu-item-object-id'=> $video_podcasting_contact_id,
                        'menu-item-object'   => 'page',
                        'menu-item-type'     => 'post_type'
                    ));

                // Set the menu location if it's not already set
                if (!has_nav_menu($video_podcasting_bpmenulocation)) {
                    $locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($locations)) {
                        $locations = array();
                    }
                    $locations[$video_podcasting_bpmenulocation] = $video_podcasting_menu_id;
                    set_theme_mod('nav_menu_locations', $locations);
                }               
        }

         
            // Set the demo import completion flag
    		update_option('video_podcasting_demo_import_completed', true);
    		// Display success message and "View Site" button
    		echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'video-podcasting') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'video-podcasting') . '</a></span>';
            //end 


            // Top Bar //
            set_theme_mod( 'video_podcasting_donate_btn_text', 'Donate' );  
            set_theme_mod( 'video_podcasting_donate_btn_url', '#' );
            set_theme_mod( 'video_podcasting_donate_icon', 'fas fa-dollar-sign' );

            // slider section start //  
            set_theme_mod( 'video_podcasting_slider_small_text', 'Trending Episode' );   

            
            // select track pages
            for($video_podcasting_i=1;$video_podcasting_i<=3;$video_podcasting_i++){             

                // Create post object
                $video_podcasting_my_post = array(
                    'post_title'   => 'Featured-album-' . $video_podcasting_i,
                    'post_content' => "Lorem Ipsum is simply dummy text ...",
                    'post_status'  => 'publish',
                    'post_type'    => 'ai_playlist',
                );

                // Insert playlist post
                $video_podcasting_bwt_post_id = wp_insert_post($video_podcasting_my_post);
                if (is_wp_error($video_podcasting_bwt_post_id)) continue;

                $video_podcasting_upload_dir = wp_upload_dir();
                $video_podcasting_player_url = get_template_directory_uri() . '/assets/images/audio/sample.mp3';

                // Playlist tracks
                $video_podcasting_featured_album_tracks = array();

                // Define artist names in a simple array
                // $video_podcasting_artists = array(
                //     'John Doe',
                //     'Sarah Lee',
                //     'Mike Hunt'
                // );
                $video_podcasting_artists = array("John Doe", "Sarah Lee", "Mike Hunt");

                // Loop 3 tracks per playlist
                for ($video_podcasting_s = 1; $video_podcasting_s <= 3; $video_podcasting_s++) {

                    // Different image per track
                    $video_podcasting_image_url  = get_template_directory_uri() . '/assets/images/track' . $video_podcasting_s . '.png';
                    $video_podcasting_image_name = 'track' . $video_podcasting_s . '.png';

                    // Get image data
                    $video_podcasting_image_data = @file_get_contents($video_podcasting_image_url);
                    if ($video_podcasting_image_data === false) continue;

                    // Prepare file path
                    $video_podcasting_unique_file_name = wp_unique_filename($video_podcasting_upload_dir['path'], $video_podcasting_image_name);
                    $video_podcasting_file = $video_podcasting_upload_dir['path'] . '/' . $video_podcasting_unique_file_name;

                    // ✅ Safe file writing using WP_Filesystem
                    if ( ! function_exists('WP_Filesystem') ) {
                        require_once(ABSPATH . 'wp-admin/includes/file.php');
                    }

                    global $wp_filesystem;
                    if ( WP_Filesystem() ) {
                        $wp_filesystem->put_contents(
                            $video_podcasting_file,
                            $video_podcasting_image_data,
                            FS_CHMOD_FILE
                        );
                    } else {
                        continue;
                    }

                    // Attach image
                    $video_podcasting_wp_filetype = wp_check_filetype($video_podcasting_unique_file_name, null);
                    $video_podcasting_attachment = array(
                        'post_mime_type' => $video_podcasting_wp_filetype['type'],
                        'post_title'     => sanitize_file_name($video_podcasting_unique_file_name),
                        'post_status'    => 'inherit',
                    );
                    $video_podcasting_attach_id = wp_insert_attachment($video_podcasting_attachment, $video_podcasting_file, $video_podcasting_bwt_post_id);

                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    $video_podcasting_attach_data = wp_generate_attachment_metadata($video_podcasting_attach_id, $video_podcasting_file);
                    wp_update_attachment_metadata($video_podcasting_attach_id, $video_podcasting_attach_data);

                    // Add track with different artist
                    $video_podcasting_featured_album_tracks[] = array(
                        'cover_id'                => $video_podcasting_attach_id,
                        'title'                   => 'Lorem Ipsum',
                        'artist'                  => $video_podcasting_artists[$video_podcasting_s - 1],
                        'track_url'               => $video_podcasting_player_url,
                        'buy_link'                => '#',
                        'download_url'            => $video_podcasting_player_url,
                        'download_uses_track_url' => '0',
                    );
                }

                // Save all tracks to playlist
                update_post_meta($video_podcasting_bwt_post_id, '_audioigniter_tracks', $video_podcasting_featured_album_tracks);    
                 


                $video_podcasting_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'title'          => 'Page1',
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));

                if (!$video_podcasting_page_query->have_posts()) {
                    $video_podcasting_page_title = 'Dream as if you live forever';
                    $productpage = '[ai_playlist id="' . $video_podcasting_bwt_post_id . '"]';

                    // Append the WooCommerce products shortcode to the content
                    $video_podcasting_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam';
                    $video_podcasting_content .= do_shortcode($productpage);

                    // Create the new page
                    $video_podcasting_page = array(
                        'post_type'    => 'page',
                        'post_title'   => $video_podcasting_page_title,
                        'post_content' => $video_podcasting_content,
                        'post_status'  => 'publish',
                        'post_author'  => 1,
                        'post_slug'    => 'products'
                    );

                    // Insert the page and get its ID
                    $video_podcasting_page_id = wp_insert_post($video_podcasting_page);

                    // Store the page ID in theme mod
                    if (!is_wp_error($video_podcasting_page_id)) {
                        // set_theme_mod('video_podcasting_slider_page', $video_podcasting_page_id);

                        set_theme_mod('video_podcasting_slider_page' . $video_podcasting_i, $video_podcasting_page_id);

                    }
                }
            }  

            // About Section
            set_theme_mod('video_podcasting_section_title', 'About');

            // Define single post data

            $video_podcasting_title = "Best Podcast For Curious Mind";
            $video_podcasting_content = 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s when an unknown printer took it to make a type specimen book';
             for($video_podcasting_i=1; $video_podcasting_i<=4; $video_podcasting_i++) {
                set_theme_mod( 'video_podcasting_about_page_list'.$video_podcasting_i, 'Top Level Help' );
            }


            $video_podcasting_my_post = array(
                 'post_title'   => wp_strip_all_tags($video_podcasting_title),
                 'post_content' => $video_podcasting_content,
                 'post_status'  => 'publish',
                 'post_type'    => 'post',
            );
            $video_podcasting_postData1 = wp_insert_post($video_podcasting_my_post);

             // Set theme mod for the post
             set_theme_mod('video_podcasting_about_post', $video_podcasting_postData1);

             // Get image URL
             $video_podcasting_image_url = get_template_directory_uri() . '/inc/block-patterns/images/about.png';
             $video_podcasting_image_data = wp_remote_get($video_podcasting_image_url);
             if (!is_wp_error($video_podcasting_image_data)) {
                 $video_podcasting_image_body = wp_remote_retrieve_body($video_podcasting_image_data);
                 $video_podcasting_upload_dir = wp_upload_dir();
                 $video_podcasting_image_name = 'service-image.png';
                 $uploaded_file = wp_upload_bits($video_podcasting_image_name, null, $video_podcasting_image_body);
                 if (!$uploaded_file['error']) {
                     // Set attachment data
                     $attachment = array(
                         'post_mime_type' => $uploaded_file['type'],
                         'post_title'     => sanitize_file_name($video_podcasting_image_name),
                         'post_content'   => '',
                         'post_status'    => 'inherit',
                     );
                     $video_podcasting_attach_id = wp_insert_attachment($attachment, $uploaded_file['file'], $video_podcasting_postData1);
                     require_once(ABSPATH . 'wp-admin/includes/image.php');
                     $video_podcasting_attach_data = wp_generate_attachment_metadata($video_podcasting_attach_id, $uploaded_file['file']);
                     wp_update_attachment_metadata($video_podcasting_attach_id, $video_podcasting_attach_data);
                     set_post_thumbnail($video_podcasting_postData1, $video_podcasting_attach_id);
                 }
            }
            
        }
        
    ?>


    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=video_podcasting_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('video_podcasting_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Please back up your website if it’s already live with data. This importer will overwrite your existing settings with the new customizer values for Video Podcasting','video-podcasting'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','video-podcasting'); ?>" class="button button-primary button-large">
        </form>
    <?php endif; ?>
    </form>
    <script type="text/javascript">
        function validate(valid) {
             if(confirm("Do you really want to import the theme demo content?")){
                document.forms[0].submit();
            }
            else {
                return false;
            }
        }
    </script>
</div>

