<?php
class Whizzie {

    public function __construct() {
        $this->init();
    }

    public function init() {
        $this->software_it_company_setup_widgets();
        self::software_it_company_setup_forms();
    }

    public function software_it_company_setup_widgets() {

        require_once ABSPATH . 'wp-admin/includes/media.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/image.php';
        
        $software_it_company_options = get_theme_mod('theme_options', []);
        $software_it_company_options['header_top_phone_text'] = 'Need help! Talk to an expert!';
        $software_it_company_options['header_top_phone_number'] = '+1234567890';
		$software_it_company_options['header_top_hiring_head'] = 'We are hiring!';
		$software_it_company_options['header_top_hiring'] = 'Are you a driven and motivated IT support Engineers?';
        $software_it_company_options['header_top_hiring_url'] = '#';
		$software_it_company_options['header_top_hiring_text'] = 'Register Now';
		$software_it_company_options['header_top_location'] = '1290 Mignal Hill Road California, NA 20110.';

		$software_it_company_options['social_media_facebook'] = '#';
		$software_it_company_options['social_media_twitter'] = '#';
		$software_it_company_options['social_media_instagram'] = '#';
        $software_it_company_options['social_media_linkdin'] = '#';
		$software_it_company_options['social_media_youtube'] = '#';
        set_theme_mod('theme_options', $software_it_company_options);

        $post_titles = array(
            "Analytic Solutions",
            "IT Consultancy",
            "Cloud Computing",
            "Cyber Security"
        );

        for ($i = 1; $i <= 4; $i++) {

            $title   = $post_titles[$i - 1];
            $content = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.";

            $existing_post_query = new WP_Query( array(
                'post_type'      => 'post',
                'title'          => $title,
                'post_status'    => 'publish',
                'posts_per_page' => 1,
                'fields'         => 'ids',
                'no_found_rows'  => true,
            ) );
            if ( $existing_post_query->have_posts() ) {
                continue;
            }

            $post_id = wp_insert_post(array(
                'post_title'   => wp_strip_all_tags($title),
                'post_content' => $content,
                'post_status'  => 'publish',
                'post_type'    => 'post',
            ));

            if (is_wp_error($post_id)) {
                continue;
            }

            $image_url = get_template_directory_uri() . '/images/post/image' . $i . '.png';
            $attach_id = media_sideload_image($image_url, $post_id, null, 'id');

            if (!is_wp_error($attach_id)) {
                set_post_thumbnail($post_id, $attach_id);
            }
        }
    }

    // FORM DEMO IMPORT
    public static function software_it_company_setup_forms() {

        $existing_form_query = new WP_Query( array(
            'post_type'      => 'wpcf7_contact_form',
            'title'          => 'Appointment Form',
            'post_status'    => 'publish',
            'posts_per_page' => 1,
            'fields'         => 'ids',
            'no_found_rows'  => true,
        ) );
        if ( $existing_form_query->have_posts() ) {
            return;
        }

        $contact_form_content = <<<'EOT'
        [text* EnterFirstName placeholder "Enter Name Here..."][text* your-subject placeholder "Enter Subject Here..."][textarea your-message placeholder "Enter Message Here..."][email EnterEmail placeholder "Enter Email Here..."][tel* ContactNumber placeholder "Enter Phone Here..."][submit " Submit Now"]
                     
EOT;

        $contact_post_id = wp_insert_post(array(
            'post_title'  => 'Appointment Form',
            'post_type'   => 'wpcf7_contact_form',
            'post_status' => 'publish',
        ));

        if (!is_wp_error($contact_post_id)) {

            update_post_meta($contact_post_id, '_form', $contact_form_content);

            update_post_meta($contact_post_id, '_mail', array(
                'subject'   => 'New Appointment Request',
                'sender'    => '[first-name] <[your-email]>',
                'recipient' => get_option('admin_email'),
                'body'      => "Name: [first-name]\nEmail: [your-email]\nPhone: [contact-number]\nMessage: [your-message]",
                'use_html'  => false,
            ));
        }

        /* Create Menu */
            $software_it_company_menuname  = 'Primary Menu';
            $software_it_company_location  = 'primary-menu';

            $software_it_company_menu = wp_get_nav_menu_object( $software_it_company_menuname );

            if ( ! $software_it_company_menu ) {
            $software_it_company_menu_id = wp_create_nav_menu( $software_it_company_menuname );

           // Home Page 
			wp_update_nav_menu_item( $software_it_company_menu_id, 0, array(
				'menu-item-title'     => __( 'Home', 'software-it-company' ),
				'menu-item-url'       => home_url( '/' ),
				'menu-item-type'      => 'custom',
				'menu-item-status'    => 'publish',
			) );

			// About Us Page 
			$software_it_company_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'About Us',
			'post_status' => 'publish',
			) );
 
			if ( $software_it_company_about_id ) {
			wp_update_nav_menu_item( $software_it_company_menu_id, 0, array(
			'menu-item-title'     => 'About Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $software_it_company_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Pages Page 
			$software_it_company_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Pages',
			'post_status' => 'publish',
			) );

			if ( $software_it_company_about_id ) {
			wp_update_nav_menu_item( $software_it_company_menu_id, 0, array(
			'menu-item-title'     => 'Pages',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $software_it_company_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

            // Portfolio Page 
			$software_it_company_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Portfolio',
			'post_status' => 'publish',
			) );

			if ( $software_it_company_about_id ) {
			wp_update_nav_menu_item( $software_it_company_menu_id, 0, array(
			'menu-item-title'     => 'Portfolio',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $software_it_company_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

			// Contact Us Page 
			$software_it_company_about_id = wp_insert_post( array(
			'post_type'   => 'page',
			'post_content' => 'We are committed to providing reliable, professional, and result-oriented solutions tailored to meet individual needs. Our goal is to empower people with the right guidance, knowledge, and support to help them make informed decisions for a better future. <br><br> Our mission is to deliver high-quality services with honesty, transparency, and dedication. We focus on understanding client requirements and offering practical solutions that create long-term value. <br><br> With a client-centric approach, experienced professionals, and a commitment to excellence, we ensure every individual receives the attention and guidance they deserve. We believe in building trust through quality service and consistent results.',
			'post_title'  => 'Contact Us',
			'post_status' => 'publish',
			) );

			if ( $software_it_company_about_id ) {
			wp_update_nav_menu_item( $software_it_company_menu_id, 0, array(
			'menu-item-title'     => 'Contact Us',
			'menu-item-object'    => 'page',
			'menu-item-object-id' => $software_it_company_about_id,
			'menu-item-type'      => 'post_type',
			'menu-item-status'    => 'publish',
			) );
			}

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

                $software_it_company_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($software_it_company_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$software_it_company_blog_page_query->hae_posts()) {
                    $software_it_company_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $software_it_company_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $software_it_company_blog_page_id = wp_insert_post($software_it_company_blog_page);
                    update_option('page_for_posts', $software_it_company_blog_page_id);

                    wp_update_nav_menu_item($software_it_company_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'software-it-company'),
                        'menu-item-url'        => get_permalink($software_it_company_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $software_it_company_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }
          
			/* Assign Menu Location */
			$software_it_company_locations = get_theme_mod( 'nav_menu_locations', array() );
			$software_it_company_locations[ $software_it_company_location ] = $software_it_company_menu_id;
			set_theme_mod( 'nav_menu_locations', $software_it_company_locations );
		}
    }
}
