<?php


// Custom dynamic tags
// https://developers.elementor.com/dynamic-tags/

Class WebienTagsSelectedPosts extends \Elementor\Core\DynamicTags\Tag {

    /**
     * Get Name
     *
     * Returns the Name of the tag
     *
     * @since 2.0.0
     * @access public
     *
     * @return string
     */
    public function get_name() {
        return 'webien_tags_selected_posts';
    }

    /**
     * Get Title
     *
     * Returns the title of the Tag
     *
     * @since 2.0.0
     * @access public
     *
     * @return string
     */
    public function get_title() {
        return __( 'Kopplade poster', 'webien' );
    }

    /**
     * Get Group
     *
     * Returns the Group of the tag
     *
     * @since 2.0.0
     * @access public
     *
     * @return string
     */
    public function get_group() {
        return 'webien_tags';
    }

    /**
     * Get Categories
     *
     * Returns an array of tag categories
     *
     * @since 2.0.0
     * @access public
     *
     * @return array
     */
    public function get_categories() {
        return [ \Elementor\Modules\DynamicTags\Module::TEXT_CATEGORY ];
    }

    /**
     * Register Controls
     *
     * Registers the Dynamic tag controls
     *
     * @since 2.0.0
     * @access protected
     *
     * @return void
     */
    protected function register_controls() {

        $this->add_control(
            'source_type',
            [
                'label' => __( 'Välj källa', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'current' => __('Aktuell post', 'webien'),
                    'option' => __('Inställningssida', 'webien'),
                ],
                'default' => 'current',
            ]
        );

        $this->add_control(
            'option_field',
            [
                'label' => __( 'Fält', 'webien' ),
                'description'   => __( 'Sluggen på fältet som finns på en inställningssida', 'webien' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => '',
                'condition' => [
                    'source_type' => 'option',
                ]
            ]
        );

        $this->add_control(
            'post_field',
            [
                'label' => __( 'Fält', 'webien' ),
                'description'   => __( 'Sluggen på fältet som ska användas. Om detta lämnas tomt går det automatiskt mot fält som startar med "kopplade_poster"', 'webien' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => '',
                'condition' => [
                    'source_type' => 'current',
                ]
            ]
        );

    }

    public function render() {

        $source_type = $this->get_settings( 'source_type' );
        $option_field = $this->get_settings( 'option_field' );

        $idArr = [];
        $metaKey = null;

        if($source_type === 'option' && !empty($option_field)){
            $posts = get_option('options_' . $option_field);

            foreach($posts as $id){
                $idArr[] = $id;
            }

        } else {

            $meta = get_post_meta(get_the_ID());

            $post_field = $this->get_settings( 'post_field' );

            // If field is specified
            if(!empty($post_field)){
                $posts = get_field($post_field, get_the_ID());

                foreach($posts as $id){
                    $idArr[] = $id;
                }
            } else {
                // Get ACF fields that starts with our prefix and collect those id:s
                foreach ($meta as $key => $field){
                    if(substr( $key, 0, 15 ) === "kopplade_poster") {
                        $metaKey = $key;
                        $arr = unserialize($field[0]);
                        if($arr){
                            foreach($arr as $id){
                                $idArr[] = $id;
                            }
                        }
                    }
                }
            }


        }

        // Make sure to just include post ID:s even if ACF is set to return object
        $finalArr = [];
        foreach ($idArr as $arr) {
            if(is_object($arr)){
                $finalArr[] = $arr->ID;
            } else {
                $finalArr[] = $arr;
            }
        }


        // make sure only uniques
        $finalArr = array_unique($finalArr);

        // If empty return a dummy id so we don't get all posts
        if(empty($finalArr)){
            $finalArr[] = 0;
        }

        // Make a oppertunity to filter the output
        $finalArr = apply_filters('webien/plugin/kopplade_poster', $finalArr, $metaKey);

        //var_dump($finalArr);
        echo implode(',', $finalArr);
    }
}