<?php
/**
 * Class for encapsulating visual composer
 * related methods into a single factory class
 *
 * @author jason.xie@victheme.com
 */
class VTCore_Wordpress_Factory_VC
implements VTCore_Wordpress_Interfaces_Factory {

  protected static $maps = array();
  protected $classMaps = array();

  /**
   * Registering visual composer
   */
  public function register() {

    // Hooking VTCore Object to VC
    if (function_exists('vc_add_shortcode_param')) {
      vc_add_shortcode_param('vtcore', array($this, 'VTCoreElementBridge'));
    }
  }

  /**
   * Processing shortcodes and registering
   * them to visual composer
   */
  public function processShortcodes() {

    // Check if we should bypass cache
    $this->maybeByPassCache();

    // Load cache
    $this->loadCache();

    foreach (self::$maps as $class) {

      $name = false;
      $check = strtolower($class);

      if (isset($this->classMaps[$check])) {

        if ($this->classMaps[$check] == true) {
          $name = $class;
        }
        else {
          continue;
        }
      }
      else if (class_exists($class, true)) {
        $name = $class;
        $this->classMaps[$check] = true;
      }
      else {
        $this->classMaps[$check] = false;
      }

      if ($name) {
        $object = new $name(array());
        vc_map($object->connectVC());
      }
    }

    set_transient('vtcore_vc_maps', $this->classMaps, 12 * HOUR_IN_SECONDS);

    return $this;
  }


  /**
   * Method for checking if class should bypass cache
   * @return VTCore_Wordpress_Factory_Layout
   */
  public function maybeByPassCache() {
    // Wordpress on debug mode
    if ((defined('WP_DEBUG') && WP_DEBUG)
        || (defined('VTCORE_CLEAR_CACHE') && VTCORE_CLEAR_CACHE)) {

      $this->clearCache();
    }

    return $this;
  }


  /**
   * Method for loading from cache
   * @return VTCore_Wordpress_Factory_Layout
   */
  public function loadCache() {
    $this->classMaps = get_transient('vtcore_vc_maps');
    return $this;
  }


  /**
   * Clear transient cache
   */
  public function clearCache() {
    delete_transient('vtcore_vc_maps');
    return $this;
  }


  /**
   * Add shortcode form class definition
   * to the centralized mapping system
   *
   * @return VTCore_Wordpress_Factory_VC
   */
  public function mapShortcode($class) {

    if (!is_array($class)) {
      $class = array($class);
    }
    self::$maps = array_merge(self::$maps, $class);

    return $this;
  }

  /**
   * Wrapper for connecting VC into VTCore
   *
   * @see registerVC()
   */
  public function VTCoreElementBridge($settings, $value) {

    // Element that needs default value
    $defaults = array(
      'VTCore_Bootstrap_Form_BsSelect',
    );

    if (!in_array($settings['core_class'], $defaults)
        || (in_array($settings['core_class'], $defaults) && $value != ''))  {

      $settings['core_context']['value'] = $value;
    }

    $element = new $settings['core_class']($settings['core_context']);

    do_action('vtcore_wordpress_alter_visualcomposer_form', $element);

    return $element->__toString();
  }

}