<?php

use \Elementor\Core\DynamicTags\Data_Tag;
use \Elementor\Modules\DynamicTags\Module;
use \Elementor\Core\DynamicTags\Tag;

final class Zbr_Elementor_Subtitle_Tag extends Tag
{

    public function get_name()
    {
        return "zbr-subtitle";
    }

    public function get_title()
    {
        return "Sub-titel";
    }

    public function get_group()
    {
        return "zbr-tags";
    }

    public function get_categories()
    {
        return [Module::TEXT_CATEGORY];
    }

    protected function _register_controls()
    {
    }

    public function render()
    {
        echo get_post_meta(get_the_ID(), "_zbr_sub_title", true);
    }
}

final class Zbr_Elementor_Carousel_Tag extends Data_Tag
{

    public function get_name()
    {
        return "zbr-carousel";
    }

    public function get_title()
    {
        return "Carousel";
    }

    public function get_group()
    {
        return "zbr-tags";
    }

    public function get_categories()
    {
        return [Module::GALLERY_CATEGORY];
    }

    protected function _register_controls()
    {
    }

    protected function get_value(array $options = [])
    {
        $images = [];

        $media = get_post_meta(get_the_ID(), '_zbr_media', true);

        foreach ($media as $item) {
            $image_url = $item->getSource();

            $images[] = [
                'id' => get_the_ID(),
                'url' => $image_url
            ];
        }
        return $images;
    }

    private function get_cached_attachment_url_to_post_id($attachment_url)
    {
        $id = wp_cache_get($attachment_url, __CLASS__);

        if (false === $id) {
            $id = attachment_url_to_postid($attachment_url);

            wp_cache_set($attachment_url, $id, __CLASS__);
        }

        return $id;
    }
}