<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Blog_Single_Data
{
    public $id;

    public $title;

    public $content;

    public $author;

    public $date;

    public $categories = array();

    public $primary_category = null;

    public $category_name = '';

    public $hero_image_url = '';

    public $hero_image_alt = '';

    public $prev_post = null;

    public $next_post = null;

    public $related_posts = array();

    public $comments_open = false;

    public $comment_count = 0;

    public $comments = array();

    public $author_bio = '';

    public $cta = null;

    public $breadcrumb_link;

    public $breadcrumb_text;

    public static function from_current_post()
    {
        return new self(get_the_ID());
    }

    public function __construct($post_id)
    {
        $this->id = $post_id;
        $this->load_post_data();
        $this->load_categories();
        $this->load_adjacent_posts();
        $this->load_related_posts();
        $this->load_comments();
        $this->resolve_breadcrumb();
    }

    private function load_post_data()
    {
        $this->title = get_the_title($this->id);
        $this->author = get_the_author_meta('display_name', get_post_field('post_author', $this->id));
        $this->date = get_the_date(get_option('date_format'), $this->id);
        $raw_content = get_post_field('post_content', $this->id);
        $block_name = 'export-toolkit/blog-detail-page';

        if (strpos($raw_content, '<!-- wp:' . $block_name) !== false) {
            $raw_content = preg_replace('/<!-- wp:' . preg_quote($block_name, '/') . '.*?\/wp:' . preg_quote($block_name, '/') . ' -->/s', '', $raw_content);
        } $this->content = apply_filters('the_content', $raw_content);
        $custom_hero = json_decode(get_post_meta($this->id, '_puffergo_blog_hero_image', true), true);

        if (! empty($custom_hero['url'])) {
            $this->hero_image_url = $custom_hero['url'];
            $this->hero_image_alt = ! empty($custom_hero['alt']) ? $custom_hero['alt'] : $this->title;
        } else {
            $thumbnail_id = get_post_thumbnail_id($this->id);
            $this->hero_image_url = $thumbnail_id ? get_the_post_thumbnail_url($this->id, 'large') : '';
            $this->hero_image_alt = $thumbnail_id ? get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true) : '';

            if (empty($this->hero_image_alt)) {
                $this->hero_image_alt = $this->title;
            }
        } $bio_override = get_post_meta($this->id, '_puffergo_blog_author_bio', true);
        $this->author_bio = $bio_override ?: get_the_author_meta('description', get_post_field('post_author', $this->id));
        $cta_raw = get_post_meta($this->id, '_puffergo_blog_cta', true);
        $this->cta = $cta_raw ? json_decode($cta_raw, true) : null;
    }

    private function load_categories()
    {
        $this->categories = get_the_category($this->id);
        $this->primary_category = ! empty($this->categories) ? $this->categories[0] : null;
        $this->category_name = $this->primary_category ? $this->primary_category->name : '';
    }

    private function load_adjacent_posts()
    {
        $prev = get_previous_post();

        if (! empty($prev)) {
            $cats = get_the_category($prev->ID);
            $this->prev_post = array('id' => $prev->ID, 'title' => get_the_title($prev->ID), 'url' => get_permalink($prev->ID), 'category_name' => ! empty($cats) ? $cats[0]->name : '', );
        } $next = get_next_post();

        if (! empty($next)) {
            $cats = get_the_category($next->ID);
            $this->next_post = array('id' => $next->ID, 'title' => get_the_title($next->ID), 'url' => get_permalink($next->ID), 'category_name' => ! empty($cats) ? $cats[0]->name : '', );
        }
    }

    private function load_related_posts()
    {
        $override_raw = get_post_meta($this->id, '_puffergo_blog_related_posts', true);
        $override_ids = $override_raw ? json_decode($override_raw, true) : array();

        if (! empty($override_ids) && is_array($override_ids)) {
            foreach ($override_ids as $pid) {
                $pid = absint($pid);
                $cats = get_the_category($pid);
                $this->related_posts[] = array('id' => $pid, 'title' => get_the_title($pid), 'url' => get_permalink($pid), 'image_url' => get_the_post_thumbnail_url($pid, 'medium') ?: '', 'category_name' => ! empty($cats) ? $cats[0]->name : '', );
            }

return;
        }

if (! $this->primary_category) {
            return;
        } $query = new WP_Query(array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 3, 'post__not_in' => array($this->id), 'category__in' => array($this->primary_category->term_id), 'orderby' => 'date', 'order' => 'DESC', ));

        while ($query->have_posts()) {
            $query->the_post();
            $cats = get_the_category();
            $this->related_posts[] = array('id' => get_the_ID(), 'title' => get_the_title(), 'url' => get_permalink(), 'image_url' => get_the_post_thumbnail_url(get_the_ID(), 'medium') ?: '', 'category_name' => ! empty($cats) ? $cats[0]->name : '', );
        } wp_reset_postdata();
    }

    private function load_comments()
    {
        $this->comments_open = comments_open($this->id);
        $this->comment_count = get_comments_number($this->id);
        $this->comments = get_comments(array('post_id' => $this->id, 'status' => 'approve', 'order' => 'ASC', ));
    }

    private function resolve_breadcrumb()
    {
        $this->breadcrumb_text = __('Blog', 'puffergo');
        $this->breadcrumb_link = get_post_type_archive_link('post');

        if (empty($this->breadcrumb_link)) {
            $pages = get_posts(array('post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => -1, ));

            foreach ($pages as $page) {
                if (has_block('export-toolkit/blog-archive', $page->ID)) {
                    $this->breadcrumb_link = get_permalink($page->ID);
                    break;
                }
            }
        }

if (empty($this->breadcrumb_link)) {
            $this->breadcrumb_link = home_url('/blog');
        }
    }

    public function get_breadcrumb_items()
    {
        $items = array();
        $items[] = array('url' => $this->breadcrumb_link, 'label' => $this->breadcrumb_text, );

        if ($this->primary_category) {
            $items[] = array('url' => get_category_link($this->primary_category->term_id), 'label' => $this->category_name, );
        } $items[] = array('url' => null, 'label' => $this->title, );

        return $items;
    }
}
