<?php

namespace WPIS;

class EstateMetaLabelShortcode {

    private static $shortcode = 'wpis-label';

    static public function init() {

        $class = new self();

        add_shortcode( self::$shortcode, [$class, 'generateShortcode'] );
    }

    public function generateShortcode($atts = false, $content = null) {

        if( !isset($atts['meta']) ){
            return $this->generateError('No meta provided');
        }
        if( isset($atts['wrap']) && !strstr($atts['wrap'], '%s') ){
            return $this->generateError('No string replacement key found, you must add %s in the wrapper');
        };
        if( isset($atts['wrap']) && $content ){
            return $this->generateError('wrap option is not supported if content is used');
        };
        if( $content && !strstr($content, '%s')  ){
            return $this->generateError('No string replacement key found, you must add %s in the wrapper');
        };

        $label = EstateProperties::getPropertyLabel($atts['meta']);

        if( isset($atts['wrap']) ){
            $buffer = sprintf($atts['wrap'], $label);
        }
        elseif( $content ){
            $buffer = sprintf($content, $label);
        }
        else {
            $buffer = $label;
        }
        return $buffer;
    }

    public function generateError( $message ){
        return sprintf('[ %s ]', self::$shortcode) . ' : ' .  $message;
    }
}