<?php
/**
* Interface Builder module
*
* Version: 1.6.9
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'CX_Interface_Builder' ) ) {
/**
* Class Cherry Interface Builder.
*
* @since 1.0.0
*/
class CX_Interface_Builder {
/**
* Module directory path.
*
* @since 1.5.0
* @access protected
* @var srting.
*/
protected $path;
/**
* Module directory URL.
*
* @since 1.5.0
* @access protected
* @var srting.
*/
protected $url;
/**
* Module version
*
* @var string
*/
protected $version = '1.6.9';
/**
* Conditions
*
* @var array
*/
public static $conditions = array();
/**
* [$conditions description]
* @var array
*/
public static $fields_value = array();
/**
* Module settings.
*
* @since 1.0.0
* @access private
* @var array
*/
private $args = array(
'path' => '',
'url' => '',
'views' => array(
'section' => 'views/section.php',
'component-tab-vertical' => 'views/component-tab-vertical.php',
'component-tab-horizontal' => 'views/component-tab-horizontal.php',
'component-toggle' => 'views/component-toggle.php',
'component-accordion' => 'views/component-accordion.php',
'component-repeater' => 'views/component-repeater.php',
'settings' => 'views/settings.php',
'control' => 'views/control.php',
'settings-children-title' => 'views/settings-children-title.php',
'tab-children-title' => 'views/tab-children-title.php',
'toggle-children-title' => 'views/toggle-children-title.php',
'form' => 'views/form.php',
'html' => 'views/html.php',
),
'views_args' => array(
'parent' => '',
'type' => '',
'view' => '',
'view_wrapping' => true,
'html' => '',
'scroll' => false,
'title' => '',
'description' => '',
'condition' => array(),
),
);
/**
* A reference to an instance of this class.
*
* @since 1.0.0
* @access private
* @var object
*/
private static $instance = null;
/**
* UI element instance.
*
* @since 1.0.0
* @access public
* @var object
*/
public $controls = null;
/**
* The structure of the interface elements.
*
* @since 1.0.0
* @access private
* @var array
*/
private $structure = array();
/**
* Dependencies array
* @var array
*/
private static $deps = array(
'css' => array(),
'js' => array( 'jquery' ),
);
/**
* Cherry_Interface_Builder constructor.
*
* @since 1.0.0
* @access public
* @return void
*/
public function __construct( array $args = array() ) {
$this->path = $args['path'];
$this->url = $args['url'];
$this->args = array_merge(
$this->args,
$args
);
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
require trailingslashit( $this->path ) . 'inc/class-cx-controls-manager.php';
$this->controls = new CX_Controls_Manager( $this->path, $this->url );
}
/**
* Register element type section.
*
* @since 1.0.0
* @access public
* @param array $args Options section.
* @return void
*/
public function register_section( array $args = array() ) {
$this->add_new_element( $args, 'section' );
}
/**
* Register element type component.
*
* @since 1.0.0
* @access public
* @param array $args Options component.
* @return void
*/
public function register_component( array $args = array() ) {
$this->add_new_element( $args, 'component' );
}
/**
* Register element type settings.
*
* @since 1.0.0
* @access public
* @param array $args Options settings.
* @return void
*/
public function register_settings( array $args = array() ) {
$this->add_new_element( $args, 'settings' );
}
/**
* Register element type control.
*
* @since 1.0.0
* @access public
* @param array $args Options control.
* @return void
*/
public function register_control( array $args = array() ) {
$this->add_new_element( $args, 'control' );
}
/**
* Register element type form.
*
* @since 1.0.0
* @access public
* @param array $args Options form.
* @return void
*/
public function register_form( array $args = array() ) {
$this->add_new_element( $args, 'form' );
}
/**
* Register element type html.
*
* @since 1.0.0
* @access public
* @param array $args Options control.
* @return void
*/
public function register_html( array $args = array() ) {
$this->add_new_element( $args, 'html' );
}
/**
* This function adds a new element to the structure.
*
* @since 1.0.0
* @access protected
* @param array $args Options new element.
* @param string $type Type new element.
* @return void
*/
protected function add_new_element( array $args = array(), $type = 'section' ) {
if ( ! isset( $args[0] ) && ! is_array( current( $args ) ) ) {
if ( 'control' !== $type && 'component' !== $type ) {
$args['type'] = $type;
}
if ( ! isset( $args['name'] ) && isset( $args['id'] ) ) {
$args['name'] = $args['id'];
}
if ( 'control' === $type ) {
$instance = $this->controls->register_control( $args['type'], $args );
$args['instance'] = $instance;
$this->add_dependencies( $instance );
}
if ( array_key_exists( 'conditions', $args ) ) {
self::$conditions[ $args['id'] ] = $args['conditions'];
}
if ( array_key_exists( 'value', $args ) ) {
self::$fields_value[ $args['id'] ] = $args['value'];
}
$this->structure[ $args['id'] ] = $args;
} else {
foreach ( $args as $key => $value ) {
if ( 'control' !== $type && 'component' !== $type ) {
$value['type'] = $type;
}
if ( ! isset( $value['id'] ) ) {
$value['id'] = $key;
}
if ( ! isset( $value['name'] ) ) {
$value['name'] = $key;
}
if ( 'control' === $type ) {
$instance = $this->controls->register_control( $value['type'], $value );
$value['instance'] = $instance;
$this->add_dependencies( $instance );
}
if ( array_key_exists( 'conditions', $value ) ) {
self::$conditions[ $key ] = $value['conditions'];
}
if ( array_key_exists( 'value', $value ) ) {
self::$fields_value[ $key ] = $value['value'];
}
$this->structure[ $key ] = $value;
}
}
}
/**
* Add control dependencies to global builder deps
*
* @param [type] $control [description]
*/
protected function add_dependencies( $control ) {
if ( ! $control instanceof CX_Controls_Base ) {
return;
}
self::$deps['js'] = array_merge( self::$deps['js'], $control->get_script_depends() );
self::$deps['css'] = array_merge( self::$deps['css'], $control->get_style_depends() );
$constrol_settings = $control->get_settings();
if ( 'repeater' === $constrol_settings['type'] && ! empty( $constrol_settings['fields'] ) ) {
foreach ( $constrol_settings['fields'] as $field ) {
$child_instance = $this->controls->register_control( $field['type'], $field );
if ( $child_instance ) {
self::$deps['js'] = array_merge( self::$deps['js'], $child_instance->get_script_depends() );
self::$deps['css'] = array_merge( self::$deps['css'], $child_instance->get_style_depends() );
}
}
}
}
/**
* Sorts the elements of the structure, adding child items to the parent.
*
* @since 1.0.0
* @access protected
* @param array $structure The original structure of the elements.
* @param string $parent_key The key of the parent element.
* @return array
*/
protected function sort_structure( array $structure = array(), $parent_key = null ) {
Loading ...