<?php

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

    public $title = '';

    public $short_description = '';

    public $gallery_items = array();

    public $main_item = null;

    public $main_image_id = 0;

    public $main_image_alt = '';

    public $main_poster_url = '';

    public $main_video_src = '';

    public $categories = array();

    public $primary_category = null;

    public $product = null;

    public static function from_current_product()
    {
        if (! function_exists('wc_get_product')) {
            return null;
        }

return self::from_id(get_the_ID());
    }

    public static function from_id($product_id)
    {
        if (! function_exists('wc_get_product')) {
            return null;
        } $product = wc_get_product($product_id);

        if (! $product) {
            return null;
        }

return new self($product);
    }

    public function __construct($product)
    {
        $this->product = $product;
        $this->id = $product->get_id();
        $this->title = $product->get_name();
        $this->short_description = $product->get_short_description();
        $this->load_categories();
        $this->load_gallery();
    }

    private function load_categories()
    {
        $terms = get_the_terms($this->id, 'product_cat');

        if (! empty($terms) && ! is_wp_error($terms)) {
            $this->categories = $terms;
            $this->primary_category = $terms[0];
        }
    }

    private function load_gallery()
    {
        $image_ids = array();
        $featured_id = $this->product->get_image_id();

        if ($featured_id) {
            $image_ids[] = (int) $featured_id;
        }

foreach ((array) $this->product->get_gallery_image_ids() as $gid) {
            $gid = (int) $gid;

            if ($gid && ! in_array($gid, $image_ids, true)) {
                $image_ids[] = $gid;
            }
        } $this->gallery_items = array();

        foreach ($image_ids as $img_id) {
            $this->gallery_items[] = array('type' => 'image', 'id' => $img_id, 'is_video' => false, 'video_src' => '', 'poster_url' => '', 'thumb_id' => $img_id, );
        } $this->main_item = ! empty($this->gallery_items) ? $this->gallery_items[0] : null;
        $this->main_image_id = $this->main_item ? (int) $this->main_item['id'] : 0;

        if ($this->main_image_id) {
            $this->main_image_alt = (string) get_post_meta($this->main_image_id, '_wp_attachment_image_alt', true);
        }
    }

    public function get_price_html()
    {
        return $this->product ? (string) $this->product->get_price_html() : '';
    }

    private static function price_html_to_text($price_html)
    {
        if ('' === (string) $price_html) {
            return '';
        } $html = preg_replace('#<span class="screen-reader-text">.*?</span>#s', '', $price_html);
        $html = str_replace('</span><', '</span> <', $html);
        $text = wp_strip_all_tags($html);
        $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');

        return trim(preg_replace('/\s+/u', ' ', $text));
    }

    public function get_breadcrumb_items()
    {
        $items = array();
        $shop_url = function_exists('wc_get_page_permalink') ? wc_get_page_permalink('shop') : '';
        $items[] = array('url' => $shop_url ?: null, 'label' => __('Products', 'puffergo'), );

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

        return $items;
    }

    public function get_related_products($limit = 4)
    {
        if (! function_exists('wc_get_related_products')) {
            return array();
        } $cat_ids = array();

        if (! empty($this->categories) && ! is_wp_error($this->categories)) {
            foreach ($this->categories as $term) {
                $cat_ids[] = (int) $term->term_id;
            }
        } $related_ids = wc_get_related_products($this->id, $limit, array());
        $cards = array();

        foreach ($related_ids as $pid) {
            $rel = wc_get_product($pid);

            if (! $rel) {
                continue;
            } $cards[] = array('id' => (int) $pid, 'title' => $rel->get_name(), 'url' => get_permalink($pid), 'image_id' => (int) $rel->get_image_id(), 'is_video' => false, 'price' => self::price_html_to_text($rel->get_price_html()), );
        }

return $cards;
    }
}
