value array so we can check dependencies
static $elementHidden = array(); //all elements that didnt pass the dependency test and are hidden
static $imageCount = 0; //count all image elements to assign the right attachment id
static $cache = array(); //cache for entries that are already fetched
static function render_metabox($element)
{
//query the metadata of the current post and check if a key is set, if not set the default value to the standard value, otherwise to the key value
if(!isset(self::$metaData[$element['current_post']]))
{
self::$metaData[$element['current_post']] = get_post_custom($element['current_post']);
}
if(isset(self::$metaData[$element['current_post']][$element['id']]))
{
$element['std'] = self::$metaData[$element['current_post']][$element['id']][0];
}
return self::render_element($element);
}
static function render_multiple_elements($elements, $parent_class = false)
{
$output = "";
foreach ($elements as $element)
{
$output .= self::render_element($element, $parent_class);
}
return $output;
}
static function ajax_modify_id($element)
{
// check if its an ajax request. if so prepend a string to ensure that the ids are unique.
// If there are multiple modal windows called prepend the string multiple times
if(isset($_POST['ajax_fetch']))
{
$prepends = isset($_POST['instance']) ? $_POST['instance'] : 0;
$element['ajax'] = true;
for($i = 0; $i < $prepends; $i++)
{
$element['id'] = "aviaTB" . $element['id'];
}
}
return $element;
}
static function render_element($element, $parent_class = false)
{
$defaults = array('id'=>'', 'name'=>'', 'label' => '', 'std' => '', 'class' =>'', 'container_class'=>'', 'desc' =>'', 'required'=>array(), 'target'=>array(), 'shortcode_data'=>array(), 'builder_active' => '');
$element = array_merge($defaults, $element);
$output = "";
if( $element['builder_active'] )
{
$output .= "
";
}
self::$elementValues[$element['id']] = $element['std']; //save the values into a unique array in case we need it for dependencies
//create default data und class string and checks the dependencies of an object
extract(self::check_dependencies($element));
// check if its an ajax request. if so prepend a string to ensure that the ids are unique.
// If there are multiple modal windows called prepend the string multiple times
$element = self::ajax_modify_id($element);
$id_string = empty($element['id']) ? "" : "id='".$element['id']."-form-container'";
$class_string .= empty($element['container_class']) ? "" : $element['container_class'];
$description_class = empty($element['description_class']) ? "" : $element['description_class'];
$target_string = "";
if(!empty($element['target']))
{
$data['target-element'] = $element['target'][0];
$data['target-property'] = $element['target'][1];
$target_string = AviaHelper::create_data_string($data);
$class_string .= " avia-attach-targeting ";
}
if(!empty($element['fetchTMPL'])) $class_string .= " avia-attach-templating ";
if(empty($element['nodescription']) )
{
$output .= "
";
$output .= __('This element only works with activated advanced layout builder','avia_framework')."
";
$output .= "
";
}
return $output;
}
/*
* Helper function that checks dependencies between objects based on the $element['required'] array
*
* If the array is set it needs to have exactly 3 entries.
* The first entry describes which element should be monitored by the current element. eg: "content"
* The second entry describes the comparison parameter. eg: "equals, not, is_larger, is_smaller ,contains"
* The third entry describes the value that we are comparing against.
*
* Example: if the required array is set to array('content','equals','Hello World'); then the current
* element will only be displayed if the element with id "content" has exactly the value "Hello World"
*
*/
static function check_dependencies($element)
{
$params = array('data_string' => "", 'class_string' => "");
if(!empty($element['required']))
{
$data['check-element'] = $element['required'][0];
$data['check-comparison'] = $element['required'][1];
$data['check-value'] = $element['required'][2];
$params['data_string'] = AviaHelper::create_data_string($data);
$return = false;
//required element must not be hidden. otherwise hide this one by default
if(!isset(self::$elementHidden[$data['check-element']]))
{
if(isset(self::$elementValues[$data['check-element']]))
{
$value1 = self::$elementValues[$data['check-element']];
$value2 = $data['check-value'];
switch($data['check-comparison'])
{
case 'equals': if($value1 == $value2) $return = true; break;
case 'not': if($value1 != $value2) $return = true; break;
case 'is_larger': if($value1 > $value2) $return = true; break;
case 'is_smaller': if($value1 < $value2) $return = true; break;
case 'contains': if(strpos($value1,$value2) !== false) $return = true; break;
case 'doesnt_contain': if(strpos($value1,$value2) === false) $return = true; break;
case 'is_empty_or': if(empty($value1) || $value1 == $value2) $return = true; break;
case 'not_empty_and': if(!empty($value1) && $value1 != $value2) $return = true; break;
case 'parent_in_array': // $value1 = "value,id" or "value"
$sep = strpos( $value1, ',' );
$val = ( false !== $sep ) ? substr( $value1, 0, $sep ) : $value1;
if( ! empty( $val ) )
{
$return = in_array( $val, explode( ' ', $value2 ) );
}
break;
case 'parent_not_in_array'; // $value1 = "value,id" or "value"
$sep = strpos( $value1, ',' );
$val = ( false !== $sep ) ? substr( $value1, 0, $sep ) : $value1;
if( ! empty( $val ) )
{
$return = ! in_array( $val, explode( ' ', $value2 ) );
}
break;
}
}
}
if(!$return)
{
$params['class_string'] = ' avia-hidden ';
self::$elementHidden[$element['id']] = true;
}
}
return $params;
}
/**
* Creates a wrapper around a set of elements. This set can be cloned with javascript
* @param array $element the array holds data like id, class and some js settings
* @return string $output the string returned contains the html code generated within the method
*/
static function modal_group($element, $parent_class)
{
$iterations = count($element['std']);
$class = isset($element['class']) ? $element['class'] : "";
$output = "";
$output .= "
";
return $output;
}
/**
*
* Empty Element - The heading method renders a text and description only that might allow to describe some following functionallity
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function heading( $element )
{
return;
}
/**
* Tab Container Elements
*/
static function tab_container( $element )
{
$output = "
";
return $output;
}
/**
*
* Empty Element - The heading method renders a text and description only that might allow to describe some following functionallity
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function hr( $element )
{
$output = "";
return $output;
}
/**
*
* Checks if a certain option has a required value and if not shows a message and makes options within unclickable
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function condition( $element )
{
//array('option' => 'header_position', 'compare' => "equal_or_empty", "value"=>"header_top")
if(isset( $element['condition']['option'] ))
{
$option = avia_get_option($element['condition']['option']);
}
else
{
$key = $element['condition']['element'];
$option = isset($element['shortcode_data'][$key]) ? $element['shortcode_data'][$key] : "";
}
$value = $element['condition']['value'];
$result = false;
$class = "av_option_hidden";
$overlay = "
";
return $output;
}
/**
*
* The tiny_mce method renders a tiny mce text field, also known as the wordpress visual editor
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function tiny_mce($element)
{
//tinymce only allows ids in the range of [a-z] so we need to filter them.
$element['id'] = preg_replace('![^a-zA-Z_]!', "", $element['id']);
/* monitor this: seems only ajax elements need the replacement */
$user_ID = get_current_user_id();
if(get_user_meta($user_ID, 'rich_editing', true) == "true" && isset($element['ajax']))
{
global $wp_version;
//replace new lines with brs, otherwise the editor will mess up. this was fixed with wp 4.3 so only do that in old versions
if ( version_compare( $wp_version, "4.3", '<' ) )
{
$element['std'] = str_replace("\n",' ',$element['std']);
}
}
ob_start();
wp_editor( $element['std'], $element['id'] , array('editor_class' => 'avia_advanced_textarea avia_tinymce', 'media_buttons' => true ) );
$output = ob_get_clean();
return $output;
}
/**
*
* The input method renders one or more input type:text elements, based on the definition of the $elements array
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function input($element)
{
$output = '';
return $output;
}
/**
*
* The input method renders one or more input type:text elements, based on the definition of the $elements array
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function multi_input($element)
{
$output = "";
$count = 0;
$element['std'] = explode(",", $element['std'] );
$value = "";
$checked = count(array_unique($element['std'], SORT_STRING));
$output .= "
";
foreach($element['multi'] as $multi => $label)
{
$output .= "
";
if(isset($element['sync']))
{
$checked = $checked === 1 ? "checked='checked'" : "";
$label = __('Apply the same value to all?','avia_framework');
$output .= "";
}
return $output;
}
/**
*
* The hidden method renders a single input type:hidden element
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function hidden( $element )
{
$output = '';
return $output;
}
/**
*
* The checkbox method renders a single input type:checkbox element
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
* @todo: fix: checkboxes at metaboxes currently dont work
*/
static function checkbox( $element )
{
$checked = "";
if( $element['std'] != "" ) { $checked = 'checked = "checked"'; }
$output = '';
return $output;
}
/**
*
* The textarea method renders a single textarea element
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function textarea( $element )
{
$output = '';
return $output;
}
/**
*
* The iconfont method renders a single icon-select element based on a font
* @param array $element the array holds data like type, value, id, class, description which are necessary to render the whole option-section
* @return string $output the string returned contains the html code generated within the method
*/
static function iconfont( $element )
{
$output = "";
if(!empty($element['chars']) && is_array($element['chars']))
{
$chars = $element['chars'];
}
else
{
$chars = avia_font_manager::load_charlist();
}
//get either the passed font or the default font
$std_font = isset($element['shortcode_data']['font']) ? $element['shortcode_data']['font'] : key(AviaBuilder::$default_iconfont);
$output .= "
";
$run = 0;
$active_font = "";
foreach($chars as $font => $charset)
{
$run ++;
asort($charset);
if($run === 1)
{
//if the el value is empty set it to the first char
if(empty($element['std'])) $element['std'] = key($charset);
$standard = avia_font_manager::get_display_char($element['std'], $std_font);
}
$output .= "