<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_CPT_Single_Data
{
    public $id = 0;

    public $post_type = '';

    public $title = '';

    public $raw_content = '';

    public $quote_text = '';

    public $hero_image_url = '';

    public $hero_image_alt = '';

    public $primary_taxonomy = '';

    public $primary_term = null;

    public $primary_term_name = '';

    public $prev_post = null;

    public $next_post = null;

    public $related_posts = array();

    public $archive_url = '';

    public $archive_label = '';

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

    public function __construct($post_id)
    {
        $this->id = (int) $post_id;
        $this->post_type = get_post_type($this->id);
        $this->load_basic_fields();
        $this->discover_primary_taxonomy();
        $this->load_primary_term();
        $this->load_hero_image();
        $this->load_quote_text();
        $this->load_adjacent_posts();
        $this->load_related_posts();
        $this->resolve_archive();
    }

    private function load_basic_fields()
    {
        $this->title = get_the_title($this->id);
        $this->raw_content = get_post_field('post_content', $this->id);
    }

    private function discover_primary_taxonomy()
    {
        $taxonomies = get_object_taxonomies($this->post_type, 'objects');

        foreach ($taxonomies as $tax) {
            if (! empty($tax->hierarchical)) {
                $this->primary_taxonomy = $tax->name;

                return;
            }
        }
    }

    private function load_primary_term()
    {
        if ('' === $this->primary_taxonomy) {
            return;
        } $terms = get_the_terms($this->id, $this->primary_taxonomy);

        if (empty($terms) || is_wp_error($terms)) {
            return;
        } $term = $terms[0];
        $current_lang = PufferGo_Polylang::current_language();

        if ('' !== $current_lang) {
            $translated_id = PufferGo_Polylang::get_term_translation($term->term_id, $current_lang);

            if ($translated_id > 0 && $translated_id !== $term->term_id) {
                $translated_term = get_term($translated_id, $this->primary_taxonomy);

                if ($translated_term && ! is_wp_error($translated_term)) {
                    $term = $translated_term;
                }
            }
        } $this->primary_term = $term;
        $this->primary_term_name = $term->name;
    }

    private function load_hero_image()
    {
        $thumb_id = get_post_thumbnail_id($this->id);

        if ($thumb_id) {
            $url = wp_get_attachment_image_url($thumb_id, 'large');
            $this->hero_image_url = $url ? $url : '';
            $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
            $this->hero_image_alt = $alt ? $alt : $this->title;

            return;
        }

if ('puffergo_testimonial' === $this->post_type) {
            $photo_id = (int) get_post_meta($this->id, '_puffergo_testimony_photo', true);

            if ($photo_id) {
                $url = wp_get_attachment_image_url($photo_id, 'large');
                $this->hero_image_url = $url ? $url : '';
                $alt = get_post_meta($photo_id, '_wp_attachment_image_alt', true);
                $this->hero_image_alt = $alt ? $alt : $this->title;
            }
        }
    }

    private function load_quote_text()
    {
        if ('puffergo_testimonial' !== $this->post_type) {
            return;
        } $quote = get_post_meta($this->id, '_puffergo_testimony_quote', true);

        if (is_string($quote)) {
            $this->quote_text = $quote;
        }
    }

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

        if ($prev instanceof WP_Post) {
            $this->prev_post = $this->build_adjacent_dict($prev);
        } $next = get_next_post();

        if ($next instanceof WP_Post) {
            $this->next_post = $this->build_adjacent_dict($next);
        }
    }

    private function build_adjacent_dict(WP_Post $post)
    {
        $term_name = '';

        if ('' !== $this->primary_taxonomy) {
            $terms = get_the_terms($post->ID, $this->primary_taxonomy);

            if (! empty($terms) && ! is_wp_error($terms)) {
                $term = $terms[0];
                $lang = PufferGo_Polylang::current_language();

                if ('' !== $lang) {
                    $translated_id = PufferGo_Polylang::get_term_translation($term->term_id, $lang);

                    if ($translated_id > 0 && $translated_id !== $term->term_id) {
                        $translated_term = get_term($translated_id, $this->primary_taxonomy);

                        if ($translated_term && ! is_wp_error($translated_term)) {
                            $term = $translated_term;
                        }
                    }
                } $term_name = $term->name;
            }
        }

return array('id' => $post->ID, 'title' => get_the_title($post->ID), 'url' => get_permalink($post->ID), 'term_name' => $term_name, );
    }

    private function load_related_posts()
    {
        if (! $this->primary_term) {
            return;
        } $query = new WP_Query(array('post_type' => $this->post_type, 'post_status' => 'publish', 'posts_per_page' => 3, 'post__not_in' => array($this->id), 'tax_query' => array(array('taxonomy' => $this->primary_taxonomy, 'field' => 'term_id', 'terms' => array($this->primary_term->term_id), ), ), 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, ));

        while ($query->have_posts()) {
            $query->the_post();
            $pid = get_the_ID();
            $thumb_id = get_post_thumbnail_id($pid);
            $image_url = $thumb_id ? wp_get_attachment_image_url($thumb_id, 'medium') : '';
            $image_alt = $thumb_id ? get_post_meta($thumb_id, '_wp_attachment_image_alt', true) : '';
            $this->related_posts[] = array('id' => $pid, 'title' => get_the_title($pid), 'url' => get_permalink($pid), 'image_url' => $image_url ? $image_url : '', 'image_alt' => $image_alt ? $image_alt : get_the_title($pid), 'term_name' => $this->primary_term_name, );
        } wp_reset_postdata();
    }

    private function resolve_archive()
    {
        $archive_link = get_post_type_archive_link($this->post_type);
        $this->archive_url = $archive_link ? $archive_link : '';
        $this->archive_label = class_exists('PufferGo_Frontend_I18n') ? PufferGo_Frontend_I18n::cpt_archive_label($this->post_type) : '';
    }

    public function get_breadcrumb_items()
    {
        $items = array();

        if ('' !== $this->archive_url) {
            $items[] = array('url' => $this->archive_url, 'label' => $this->archive_label, );
        }

if ($this->primary_term) {
            $term_link = get_term_link($this->primary_term);
            $items[] = array('url' => is_wp_error($term_link) ? null : $term_link, 'label' => $this->primary_term_name, );
        } $items[] = array('url' => null, 'label' => $this->title, );

        return $items;
    }
}
