<?php

    namespace Pixebuilder\Element;
    
    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    class Query_Var extends Item{

        static function get_type(){
            return 'post';
        }

        static function get_name(){
            return 'query-var';
        }

        static function get_title(){
            return esc_html__('Query Var', 'pixebuilder');
        }

        static function get_icon(){
            return 'content';
        }
        
        static function render_content($settings){
            
            // get variables
            $tag = empty($settings['text-tag'])? 'div': $settings['text-tag'];

            $atts = array();
            if( !empty($settings['text-typo']['preset']) ){
                $atts['typo-preset'] = $settings['text-typo']['preset'];
            }

            // print
            $ret = '';
            $query_var = '';

            if( is_search() ){
                $query_var = get_query_var('s');
            }else if( is_post_type_archive() ){
                $query_var = get_query_var('post_type');
            }else if( is_tax() || is_category() || is_tag() ){
                $term = get_queried_object();
                $query_var = $term->name;
            }

            if( !empty($query_var) ){
                $ret .= '<' . $tag . ' class="pixe-text" ';
                $ret .= pixe_get_data_atts($atts);
                $ret .= '>' . $query_var . '</' . $tag . '>';
            }

            return $ret;
        }

    }

    \Pixe_Builder::register_element('Pixebuilder\Element\Query_Var');
    