<?php
#[AllowDynamicProperties]
abstract class viewGmp extends baseObjectGmp
{
  /*
   * @deprecated
   */
  protected $_tpl = GMP_DEFAULT;
  protected $_nonceIsInited = false;
  /*
   * @var string name of theme to load from templates, if empty - default values will be used
   */
  protected $_theme = '';
  /*
   * @var string module code for this view
   */
  protected $_code = '';
  protected static $_allowedHtml;
  public static function supStrRgbToHex($color)
  {
    preg_match_all('/\((.+?)\)/', $color, $matches);
    if (!empty($matches[1][0])) {
      $rgb = explode(',', $matches[1][0]);
      $size = count($rgb);
      if ($size == 3 || $size == 4) {
        if ($size == 4) {
          $alpha = array_pop($rgb);
          $alpha = floatval(trim($alpha));
          $alpha = ceil(($alpha * (255 * 100)) / 100);
          array_push($rgb, $alpha);
        }
        $result = '#';
        foreach ($rgb as $row) {
          $result .= str_pad(dechex(trim($row)), 2, '0', STR_PAD_LEFT);
        }
        return $result;
      }
    }
    return false;
  }
  public static function ksesString($str)
  {
    $allowedHtml = self::getAllowedHtml();
    if (!empty($str) && is_string($str)) {
      $str = htmlspecialchars_decode($str);
      $re = '/rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/';
      $str = preg_replace_callback(
        $re,
        function ($m) {
          return self::supStrRgbToHex($m[0]);
        },
        $str,
      );
      $re = '/rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\d*(?:\.\d+)?\)/';
      $str = preg_replace_callback(
        $re,
        function ($m) {
          return self::supStrRgbToHex($m[0]);
        },
        $str,
      );
      $str = wp_kses($str, $allowedHtml);
    }
    return $str;
  }
  public function addGmpNonce()
  {
    if (!$this->_nonceIsInited) {
      $this->_nonceIsInited = true;
      if (is_admin()) {
        $gmp_nonce = wp_create_nonce('gmp_nonce');
        $jsData = [
          'gmp_nonce' => $gmp_nonce,
        ];
        frameGmp::_()->addScript('nonceGmp', GMP_JS_PATH . 'nonce.js');
        frameGmp::_()->addJSVar('nonceGmp', 'GMP_NONCE', $jsData);
      } else {
        $gmp_nonce = wp_create_nonce('gmp_nonce_frontend');
        $jsData = [
          'gmp_nonce_frontend' => $gmp_nonce,
        ];
        frameGmp::_()->addScript('nonceGmp', GMP_JS_PATH . 'nonce.js');
        frameGmp::_()->addJSVar('nonceGmp', 'GMP_NONCE_FRONTEND', $jsData);
      }
    }
  }
  public static function getAllowedHtml()
  {
    if (empty(self::$_allowedHtml)) {
      $allowedHtml = wp_kses_allowed_html();
      $allowedDiv = [
        'style' => [],
        'div' => [
          'hidden' => 1,
          'data-show' => 1,
          'data-section' => 1,
          'data-slider-type' => 1,
          'data-unit' => 1,
          'data-unit' => 1,
          'data-mapid' => 1,
          'data-viewid' => 1,
          'onclick' => 1,
          'data-is-mobile' => 1,
          'data-tab-link' => 1,
          'data-tab-item' => 1,
          'data-today' => 1,
          'data-tabs-for' => 1,
          'data-type' => 1,
          'style' => 1,
          'title' => 1,
          'align' => 1,
          'class' => 1,
          'width' => 1,
          'height' => 1,
          'title' => 1,
          'id' => 1,
          'data-tooltip-content' => 1,
        ],
        'section' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'title' => 1, 'id' => 1],
        'nav' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'title' => 1, 'id' => 1],
        'small' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'span' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'pre' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'p' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'br' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'hr' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'hgroup' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'h1' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'h2' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'h3' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'h4' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'h5' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'h6' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'ul' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'ol' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'li' => ['data-section' => 1, 'data-key' => 1, 'data-id' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'dl' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'dt' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'dd' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'strong' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'em' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'b' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'i' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1, 'data-tooltip-content' => 1],
        'u' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'img' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'a' => ['data-nonce' => 1, 'data-url' => 1, 'onclick' => 1, 'data-active-label' => 1, 'data-apply-label' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1, 'link' => 1, 'rel' => 1, 'href' => 1, 'target' => 1],
        'abbr' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'address' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'blockquote' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'area' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'form' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1, 'action' => 1, 'target' => 1, 'method' => 1],
        'fieldset' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'label' => ['for' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'input' => [
          'onclick' => 1,
          'data-type' => 1,
          'data-hideid' => 1,
          'placeholder' => 1,
          'readonly' => 1,
          'checked' => 1,
          'disabled' => 1,
          'selected' => 1,
          'style' => 1,
          'title' => 1,
          'align' => 1,
          'class' => 1,
          'width' => 1,
          'height' => 1,
          'value' => 1,
          'type' => 1,
          'id' => 1,
          'name' => 1,
          'src' => 1,
          'border' => 1,
          'alt' => 1,
          'name' => 1,
          'maxlength' => 1,
        ],
        'textarea' => ['rows' => 1, 'cols' => 1, 'name' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'caption' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'table' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'tbody' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'td' => ['colspan' => 1, 'rowspan' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'tfoot' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'th' => ['colspan' => 1, 'rowspan' => 1, 'scope' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'thead' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'tr' => ['style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'select' => ['name' => 1, 'checked' => 1, 'disabled' => 1, 'selected' => 1, 'multiple' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1],
        'option' => ['name' => 1, 'checked' => 1, 'disabled' => 1, 'selected' => 1, 'style' => 1, 'title' => 1, 'align' => 1, 'class' => 1, 'width' => 1, 'height' => 1, 'id' => 1, 'value' => 1],
        'sup' => [],
        'sub' => [],
        'button' => ['data-wp-editor-id' => 1, 'style' => 1, 'class' => 1, 'id' => 1, 'data-mapid' => 1, 'data-viewid' => 1, 'onclick' => 1],
        'img' => ['src' => 1, 'style' => 1, 'width' => 1, 'height' => 1, 'id' => 1, 'class' => 1, 'alt' => 1, 'border' => 1],
        'track' => ['src' => 1, 'kind' => 1, 'label' => 1, 'srclang' => 1],
        'source' => ['src' => 1, 'type' => 1],
        'audio' => ['src' => 1, 'style' => 1, 'width' => 1, 'height' => 1, 'id' => 1, 'class' => 1, 'autoplay' => 1, 'controls' => 1, 'crossorigin' => 1, 'loop' => 1, 'muted' => 1, 'preload' => 1],
        'iframe' => ['src' => 1, 'style' => 1, 'width' => 1, 'height' => 1, 'id' => 1, 'class' => 1, 'title' => 1, 'allow' => 1, 'allowfullscreen' => 1, 'allowpaymentrequest' => 1, 'csp' => 1, 'height' => 1, 'loading' => 1, 'name' => 1, 'referrerpolicy' => 1, 'sandbox' => 1, 'srcdoc' => 1],
      ];
      self::$_allowedHtml = array_merge($allowedHtml, $allowedDiv);
    }
    return self::$_allowedHtml;
  }
  public function display($tpl = '')
  {
    $tpl = empty($tpl) ? $this->_tpl : $tpl;
    if (($content = $this->getContent($tpl)) !== false) {
      $this->addGmpNonce();
      echo self::ksesString($content);
    }
  }
  public function getPath($tpl)
  {
    $path = '';
    $code = $this->_code;
    $parentModule = frameGmp::_()->getModule($this->_code);
    $plTemplate = frameGmp::_()->getModule('options')->get('template'); // Current plugin template
    if (empty($plTemplate) || !frameGmp::_()->getModule($plTemplate)) {
      $plTemplate = '';
    }
    if (file_exists(utilsGmp::getCurrentWPThemeDir() . 'gmp' . DS . $code . DS . $tpl . '.php')) {
      $path = utilsGmp::getCurrentWPThemeDir() . 'gmp' . DS . $code . DS . $tpl . '.php';
    } elseif ($plTemplate && file_exists(frameGmp::_()->getModule($plTemplate)->getModDir() . 'templates' . DS . $code . DS . $tpl . '.php')) {
      $path = frameGmp::_()->getModule($plTemplate)->getModDir() . 'templates' . DS . $code . DS . $tpl . '.php';
    } elseif (file_exists($parentModule->getModDir() . 'views' . DS . 'tpl' . DS . $tpl . '.php')) {
      //Then try to find it in module directory
      $path = $parentModule->getModDir() . DS . 'views' . DS . 'tpl' . DS . $tpl . '.php';
    }
    return $path;
  }
  public function getModule()
  {
    return frameGmp::_()->getModule($this->_code);
  }
  public function getModel($code = '')
  {
    return frameGmp::_()->getModule($this->_code)->getController()->getModel($code);
  }
  public function getContent($tpl = '')
  {
    $tpl = empty($tpl) ? $this->_tpl : $tpl;
    $path = $this->getPath($tpl);
    if ($path) {
      $content = '';
      ob_start();
      require $path;
      $content = ob_get_contents();
      ob_end_clean();
      return $content;
    }
    return false;
  }
  public function setTheme($theme)
  {
    $this->_theme = $theme;
  }
  public function getTheme()
  {
    return $this->_theme;
  }
  public function setTpl($tpl)
  {
    $this->_tpl = $tpl;
  }
  public function getTpl()
  {
    return $this->_tpl;
  }
  public function init() {}
  public function assign($name, $value)
  {
    $this->$name = $value;
  }
  public function setCode($code)
  {
    $this->_code = $code;
  }
  public function getCode()
  {
    return $this->_code;
  }

  /**
   * This will display form for our widgets
   */
  public function displayWidgetForm($data = [], $widget = [], $formTpl = 'form')
  {
    $this->assign('data', $data);
    $this->assign('widget', $widget);
    if (frameGmp::_()->isTplEditor()) {
      if ($this->getPath($formTpl . '_ext')) {
        $formTpl .= '_ext';
      }
    }
    self::display($formTpl);
  }
  public function sizeToPxPt($size)
  {
    if (!strpos($size, 'px') && !strpos($size, '%')) {
      $size .= 'px';
    }
    return $size;
  }
  public function getInlineContent($tpl = '')
  {
    return preg_replace('/\s+/', ' ', $this->getContent($tpl));
  }
}
