<?php

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

    class Post_Featured_Image extends Item{
            
        static function get_type(){
            return 'post';
        }
        
        static function get_name(){
            return 'post-featured-image';
        }

        static function get_title(){
            return esc_html__('Featured Image', 'pixebuilder');
        }

        static function get_icon(){
            return 'image';
        }
        
        static function render_content($settings){
            if( !isset($settings['overlay']) ) $settings['overlay'] = '';
            if( !isset($settings['overlay-atts']) ) $settings['overlay-atts'] = '';

            $ret = '';

            // find post id
            $post_id = get_the_ID();
            if( empty($post_id) && pixe_is_admin() ){
                global $pixe_request_type, $pixe_request_id;
                if( $pixe_request_type == 'post' && !empty($pixe_request_id) ){
                    $post_id = $pixe_request_id;
                }
            }
            
            // return images
            if( !empty($post_id) ){
                $thumbnail_id = get_post_thumbnail_id($post_id);
                if( !empty($thumbnail_id) ){
                    $image_size = empty($settings['layout']['image-size'])? 'full': $settings['layout']['image-size'];
                    $image = empty($settings['link'])? array(): $settings['link'];
                    $image['id'] = $thumbnail_id;
                    if( empty($image['link-to']) ){ 
                        $image['link-to'] = 'post'; 
                        $image['post-id'] = $post_id; 
                    }

                    $link_tag = pixe_get_link_tag($image);

                    $ret  = '<div class="pixe-image" >';
                    $ret .= $link_tag;
                    $ret .= wp_get_attachment_image($thumbnail_id, $image_size);
                    $ret .= pixe_get_image_overlay_content($thumbnail_id, $settings['overlay-atts'], $settings['overlay']);
                    $ret .= empty($link_tag)? '': '</a>';
                    $ret .= '</div>';
                }
            }

            return $ret;
        }

    }

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