<?php
class Whizzie {

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

    public function init() {
        // Initialize content and settings
        $this->charitable_trust_setup_widgets();
        $this->charitable_trust_setup_theme_mods();
    }

    public function charitable_trust_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';
    
        $charitable_trust_title_array = array(
            "A Share Your Food Blessing With Children",
            "Helping the Homeless During Hopeless",
            "Community Food Drives"
        );
    
        for ($i = 1; $i <= 3; $i++) {
            $title = $charitable_trust_title_array[$i - 1];
            $content = "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.";
    
            // WP_Query replacement for get_page_by_title
            $query = new WP_Query([
                'post_type' => 'post',
                'title'     => $title,
                'post_status' => 'any',
                'posts_per_page' => 1,
            ]);
    
            if ($query->have_posts()) {
                wp_reset_postdata();
                continue;
            }
            wp_reset_postdata();
    
            // Create the post
            $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) || !$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);
            }
        }
    }

    public function charitable_trust_setup_theme_mods() {
        $charitable_trust_options = get_theme_mod('theme_options', []);
        $charitable_trust_options['charitable_trust_header_top_text_heading'] = 'Welcome to Charity';
        $charitable_trust_options['charitable_trust_header_top_text'] = 'Be the reason someone smiles today';
        $charitable_trust_options['charitable_trust_header_top_button'] = 'Donate Now';
        $charitable_trust_options['charitable_trust_header_top_buttonlink'] = '#';
		$charitable_trust_options['charitable_trust_header_social_facebook_link'] = 'www.facebook.com';
		$charitable_trust_options['charitable_trust_header_social_twitter_link'] = 'www.twitter.com';
		$charitable_trust_options['charitable_trust_header_social_instagram_link'] = 'www.instagram.com';
		$charitable_trust_options['charitable_trust_header_social_youtube_link'] = 'www.youtube.com';
        set_theme_mod('theme_options', $charitable_trust_options);
    }
}