/** * Create a new columns * * @param array $structure Column structure */ public function __construct($structure) { $structure = $this->validate_structure($structure); $this->width = $structure['width']; $this->options = new Layotter_Options('col', $structure['options']); foreach ($structure['elements'] as $element) { $element_object = false; // if a template_id is set, try to create a template if (isset($element['template_id'])) { $element_object = Layotter_Templates::create_element($element); } // if the template doesn't exist anymore, create a regular element if (!$element_object) { $element_object = Layotter::create_element($element); } if ($element_object) { $this->elements[] = $element_object; } } }
/** * Create a new element instance from a saved template * * @param int|array $id_or_structure Template ID or element structure with template_id * @return mixed New element instance, or false on failure */ public static function create_element($id_or_structure, $options = array()) { $element = false; if (is_array($id_or_structure)) { $structure = self::validate_structure($id_or_structure); return self::create_element($structure['template_id'], $structure['options']); } else { if (is_int($id_or_structure)) { $id = $id_or_structure; $templates = get_option('layotter_element_templates'); if (is_array($templates) and isset($templates[$id])) { $template = self::validate_structure($templates[$id]); $element = Layotter::create_element($template['type'], $template['values'], $options); } } } if (!$element) { return false; } if (!isset($template['deleted']) or !$template['deleted']) { $element->set_template_id($id); } return $element; }
function layotter_ajax_update_template() { $post_data = layotter_get_angular_post_data(); // type and field values are required if (isset($post_data['template_id']) and is_int($post_data['template_id'])) { $id = $post_data['template_id']; $template = Layotter_Templates::get($id); if ($template) { $values = Layotter_ACF::unwrap_post_values(); $element = Layotter::create_element($template['type'], $values); if ($element) { $element->set_template_id($id); Layotter_Templates::update($id, $element->get_template_data()); echo json_encode($element->to_array()); } } } die; // required by Wordpress after any AJAX call }
/** * Get element types enabled for a specific post * * @param int $post_id Post ID * @return array Element instances */ public static function get_filtered_element_types($post_id) { $elements = array(); foreach (array_keys(self::$registered_elements) as $element_type) { $element = Layotter::create_element($element_type); if ($element and $element->is_enabled_for($post_id)) { $elements[] = $element; } } usort($elements, array(__CLASS__, 'sort_element_types_helper')); return $elements; }