public static function method_get_layout_blocks_in_json() { $layout = headway_post('layout', false); $layout_status = HeadwayLayout::get_status($layout); if ($layout_status['customized'] != true) { return false; } self::json_encode(array('blocks' => HeadwayBlocksData::get_blocks_by_layout($layout, true), 'wrappers' => HeadwayWrappers::get_layout_wrappers($layout, true))); }
/** * Traverses up the hierarchy tree to figure out which layout is being used. * * @return mixed **/ public static function get_current_in_use() { //If the user is viewing the site through the iframe and the mode is set to Layout, then display that exact layout. if (headway_get('ve-layout') && (HeadwayRoute::is_visual_editor_iframe() || HeadwayRoute::is_visual_editor())) { return headway_get('ve-layout'); } //Get hierarchy $hierarchy = array_reverse(self::get_current_hierarchy()); //Loop through entire hierarchy to find which one is customized or has a template foreach ($hierarchy as $layout) { $status = self::get_status($layout); //If the layout isn't customized or using a template, skip to next, otherwise we return the current layout in the next line. if ($status['customized'] === false && $status['template'] === false) { continue; } //If the layout has a template assigned to it, use the template. Templates will take precedence over customized status. if ($status['template']) { return 'template-' . $status['template']; } //If it's a customized layout, then use the layout itself after making sure there are blocks on the layout if ($status['customized'] && count(HeadwayBlocksData::get_blocks_by_layout($layout)) > 0) { return $layout; } } //If there's still not a customized layout, loop through the top-level layouts and find the first one that's customized. $top_level_layouts = array('index', 'single', 'archive', 'four04'); if (get_option('show_on_front') == 'page') { $top_level_layouts[] = 'front_page'; } foreach ($top_level_layouts as $top_level_layout) { $status = self::get_status($top_level_layout); if ($status['customized'] === false && $status['template'] === false) { continue; } //If the layout has a template assigned to it, use the template. Templates will take precedence over customized status. if ($status['template']) { return 'template-' . $status['template']; } //If it's a customized layout and the layout has blocks, then use the layout itself if ($status['customized'] && count(HeadwayBlocksData::get_blocks_by_layout($top_level_layout)) > 0) { return $top_level_layout; } } //If there STILL isn't a customized layout, just return the top level of the current layout. return end($hierarchy); }
public function __construct() { $this->id = HeadwayLayout::get_current_in_use(); $this->blocks = HeadwayBlocksData::get_blocks_by_layout($this->id); $this->wrappers = HeadwayWrappers::get_layout_wrappers($this->id); }
public static function register_block_element_instances() { if ((headway_get('layout-in-use') || headway_post('layout')) && !headway_get('compiler-cache')) { $layout_id = headway_get('layout-in-use') ? headway_get('layout-in-use') : headway_post('layout'); $register_block_mirrors = true; if (!($blocks = HeadwayBlocksData::get_blocks_by_layout($layout_id))) { return false; } } else { $register_block_mirrors = false; if (!($blocks = HeadwayBlocksData::get_all_blocks())) { return false; } } foreach ($blocks as $block) { /* Do not register instance for block that's in a mirrored wrapper */ if (HeadwayWrappers::get_wrapper_mirror(HeadwayWrappers::get_wrapper(headway_get('wrapper', $block)))) { continue; } /* If it's a mirrored block then we'll register it as long as $register_block_mirrors is true */ if ($block_mirror = HeadwayBlocksData::is_block_mirrored($block)) { if (!$register_block_mirrors) { continue; } $block = $block_mirror; } $default_name = self::block_type_nice($block['type']) . ' #' . $block['id']; $name = headway_get('alias', $block['settings'], $default_name); HeadwayElementAPI::register_element_instance(array('group' => 'blocks', 'element' => 'block-' . $block['type'], 'id' => $block['type'] . '-block-' . $block['id'], 'name' => $name, 'selector' => '#block-' . $block['id'], 'layout' => $block['layout'])); /* Register sub elements */ $block_element = HeadwayElementAPI::get_element('block-' . $block['type']); foreach (headway_get('children', $block_element, array()) as $block_element_sub_element) { /* Make sure that the element supports instances */ if (!headway_get('supports-instances', $block_element_sub_element)) { continue; } /* Register instance */ $instance_selector = str_replace('.block-type-' . $block['type'], '#block-' . $block['id'], $block_element_sub_element['selector']); HeadwayElementAPI::register_element_instance(array('group' => 'blocks', 'grandparent' => 'block-' . $block['type'], 'element' => $block_element_sub_element['id'], 'id' => $block_element_sub_element['id'] . '-block-' . $block['id'], 'name' => $name . ' - ' . $block_element_sub_element['name'], 'selector' => $instance_selector, 'layout' => $block['layout'])); /* Register instance states as instances */ if (!empty($block_element_sub_element['states']) && is_array($block_element_sub_element['states'])) { foreach ($block_element_sub_element['states'] as $instance_state_id => $instance_state_info) { HeadwayElementAPI::register_element_instance(array('group' => 'blocks', 'grandparent' => 'block-' . $block['type'], 'element' => $block_element_sub_element['id'], 'id' => $block_element_sub_element['id'] . '-block-' . $block['id'] . '-state-' . $instance_state_id, 'name' => $name . ' - ' . $block_element_sub_element['name'] . ' (State: ' . $instance_state_info['name'] . ')', 'selector' => str_replace('.block-type-' . $block['type'], '#block-' . $block['id'], $instance_state_info['selector']), 'layout' => $block['layout'], 'state-of' => $block_element_sub_element['id'] . '-block-' . $block['id'], 'state-name' => $instance_state_info['name'])); } } } /* /foreach */ } }
public static function export_layout($layout_id) { /* Set up variables */ if (!($layout_name = HeadwayLayout::get_name($layout_id))) { die('Error: Invalid layout.'); } $layout = array('name' => $layout_name, 'blocks' => HeadwayBlocksData::get_blocks_by_layout($layout_id)); /* Convert all mirrored blocks into original blocks by pulling their mirror target's settings */ /* Loop through each block in the template and check if it's mirrored. If it is, replace it with the block that it's mirroring */ foreach ($layout['blocks'] as $layout_block_index => $layout_block) { if (!($mirrored_block = HeadwayBlocksData::is_block_mirrored($layout_block))) { continue; } $layout['blocks'][$layout_block_index] = $mirrored_block; } /* Spit the file out */ return self::to_json('Headway Layout - ' . $layout_name, 'layout', $layout); }
public static function save($options, $current_layout = false, $mode = false) { if (!$current_layout) { $current_layout = headway_post('layout'); } if (!$mode) { $mode = headway_post('mode'); } //Handle triple slash bullshit if (get_magic_quotes_gpc() === 1) { $options = array_map('stripslashes_deep', $options); } $blocks = isset($options['blocks']) ? $options['blocks'] : null; $wrappers = isset($options['wrappers']) ? $options['wrappers'] : null; $layout_options = isset($options['layout-options']) ? $options['layout-options'] : null; $options_inputs = isset($options['options']) ? $options['options'] : null; $design_editor_inputs = isset($options['design-editor']) ? $options['design-editor'] : null; //Set the current layout to customized if it's the grid mode if ($mode == 'grid') { HeadwayLayoutOption::set($current_layout, 'customized', true); } /* Blocks */ if ($blocks) { foreach ($blocks as $id => $methods) { foreach ($methods as $method => $value) { switch ($method) { case 'new': if (HeadwayBlocksData::get_block($id)) { continue; } $dimensions = explode(',', $blocks[$id]['dimensions']); $position = explode(',', $blocks[$id]['position']); $settings = isset($blocks[$id]['settings']) ? $blocks[$id]['settings'] : array(); $args = array('id' => $id, 'type' => $value, 'position' => array('left' => $position[0], 'top' => $position[1]), 'dimensions' => array('width' => $dimensions[0], 'height' => $dimensions[1]), 'settings' => $settings); HeadwayBlocksData::add_block($current_layout, $args); break; case 'delete': HeadwayBlocksData::delete_block($current_layout, $id); break; case 'dimensions': $dimensions = explode(',', $value); $args = array('dimensions' => array('width' => $dimensions[0], 'height' => $dimensions[1])); HeadwayBlocksData::update_block($current_layout, $id, $args); break; case 'position': $position = explode(',', $value); $args = array('position' => array('left' => $position[0], 'top' => $position[1])); HeadwayBlocksData::update_block($current_layout, $id, $args); break; case 'wrapper': $args = array('wrapper' => $value); HeadwayBlocksData::update_block($current_layout, $id, $args); break; case 'settings': //Retrieve all blocks from layout $layout_blocks = HeadwayBlocksData::get_blocks_by_layout($current_layout); //Get the block from the layout $block = headway_get($id, $layout_blocks); //If block doesn't exist, we can't do anything. if (!$block) { continue; } //If there aren't any options, then don't do anything either if (!is_array($value) || count($value) === 0) { continue; } $block['settings'] = array_merge($block['settings'], $value); HeadwayBlocksData::update_block($current_layout, $id, $block); break; } } } } /* End Blocks */ /* Wrappers */ if ($wrappers) { /* Pluck last-id out of wrappers and send it to DB */ if (headway_get('last-id', $wrappers)) { $last_id = $wrappers['last-id']; unset($wrappers['last-id']); HeadwayOption::set('last-id', $last_id, 'wrappers'); } /* Save layout wrappers to dB */ HeadwayOption::set($current_layout, $wrappers, 'wrappers'); } /* End Wrappers */ /* Layout Options */ if ($layout_options) { foreach ($layout_options as $group => $options) { foreach ($options as $option => $value) { HeadwayLayoutOption::set($current_layout, $option, $value, $group); } } } /* End Layout Options */ /* Options */ if ($options_inputs) { foreach ($options_inputs as $group => $options) { foreach ($options as $option => $value) { HeadwayOption::set($option, $value, $group); } } } /* End Options */ /* Design Editor Inputs */ if ($design_editor_inputs) { /* If skin import is set to true then nuke all design settings to prevent overlapping settings */ if (headway_get('skin-import', $design_editor_inputs)) { HeadwayElementsData::delete_all(); } /* End skin import nuke */ /* Handle skin templates */ $skin_templates = headway_get('skin-import-templates', $design_editor_inputs); if (is_array($skin_templates) && count($skin_templates)) { $skin_template_block_id_translations = array(); $skin_template_wrapper_id_translations = array(); foreach ($skin_templates as $skin_template_name => $skin_template_blocks) { /* Pluck wrappers array out of blocks array */ $skin_template_wrappers = $skin_template_blocks['wrappers']; unset($skin_template_blocks['wrappers']); $template = HeadwayLayout::add_template($skin_template_name, $skin_template_blocks, $skin_template_wrappers); /* Use + rather than array_merge because + preserves numeric keys */ $skin_template_block_id_translations = $skin_template_block_id_translations + $template['block-id-translations']; $skin_template_wrapper_id_translations = $skin_template_wrapper_id_translations + $template['wrapper-id-translations']; } /* Re-map block IDs in instances according to block ID translations */ foreach ($design_editor_inputs as $element_id => $element_data) { if (!is_array($element_data) || !isset($element_data['special-element-instance'])) { continue; } foreach ($element_data['special-element-instance'] as $instance_id => $instance_properties) { $instance_id_fragments = explode('-', $instance_id); $instance_potential_block_id_search = preg_match('/\\bblock\\b\\-[0-9]+/', $instance_id, $instance_potential_block_id_search_results); $instance_potential_block_id = str_replace('block-', '', end($instance_potential_block_id_search_results)); $instance_potential_wrapper_id = $instance_id_fragments[1]; /* Wrapper instance conditional. Modify new instance ID accordingly */ if (strpos($instance_id, 'wrapper-') === 0 && isset($skin_template_wrapper_id_translations[intval($instance_potential_wrapper_id)])) { $new_wrapper_id = $skin_template_wrapper_id_translations[intval($instance_potential_wrapper_id)]['id']; $new_wrapper_layout = $skin_template_wrapper_id_translations[intval($instance_potential_wrapper_id)]['layout']; $new_instance_id = 'wrapper-' . $new_wrapper_id . '-layout-' . $new_wrapper_layout; /* Block instance conditional. Modify new instance ID accordingly */ } else { if (strpos($instance_id, 'block-') !== false && is_numeric($instance_potential_block_id) && isset($skin_template_block_id_translations[intval($instance_potential_block_id)])) { $new_block_id = $skin_template_block_id_translations[intval($instance_potential_block_id)]; $new_instance_id = str_replace('block-' . $instance_potential_block_id, 'block-' . $new_block_id, $instance_id); /* Not a proper block or wrapper instance, just skip it */ } else { continue; } } /* Remove existing instance key/value pair */ unset($design_editor_inputs[$element_id]['special-element-instance'][$instance_id]); /* Add new instance key/value pair with new instance ID */ $design_editor_inputs[$element_id]['special-element-instance'][$new_instance_id] = $instance_properties; } } } /* End skin template handling */ /* Loop through to get every element and its properties */ foreach ($design_editor_inputs as $element_id => $element_data) { if (!is_array($element_data) || !isset($element_data['group'])) { continue; } $element_group = $element_data['group']; //Dispatch depending on type of element data foreach ($element_data as $element_data_node => $element_data_node_data) { //Handle different nodes depending on what they are if ($element_data_node == 'properties') { //Set each property for the regular element foreach ($element_data_node_data as $property_id => $property_value) { HeadwayElementsData::set_property($element_group, $element_id, $property_id, $property_value); } //Handle instances, states, etc. } else { if (strpos($element_data_node, 'special-element-') === 0) { $special_element_type = str_replace('special-element-', '', $element_data_node); //Loop through the special elements foreach ($element_data_node_data as $special_element => $special_element_properties) { //Set the special element properties now foreach ($special_element_properties as $special_element_property => $special_element_property_value) { HeadwayElementsData::set_special_element_property($element_group, $element_id, $special_element_type, $special_element, $special_element_property, $special_element_property_value); } } } } } } /* End loop */ } /* End Design Editor Inputs */ //This hook is used by cache flushing, plugins, etc. Do not fire on preview save because it'll flush preview options if (!headway_get('ve-preview')) { do_action('headway_visual_editor_save'); } return true; }
public static function list_pages($pages = null) { //Since this function is recursive, we must designate the default like this if ($pages === null) { $pages = HeadwayLayout::get_pages(); $root_pages = true; } $return = ''; $return .= '<ul>' . "\n"; /** * Only show the message the function is being called for the first time (not showing children) and that the * mode is NOT the grid and the grid is supported still. **/ if (isset($root_pages) && HeadwayVisualEditor::get_current_mode() !== 'grid' && current_theme_supports('headway-grid')) { $return .= '<li class="layout-item info-layout-item"><span class="layout"><strong>To add blocks to a new layout, please switch to the Grid mode.</strong></span></li>'; } foreach ($pages as $id => $children) { $layout_id_fragments = explode('-', $id); $status = HeadwayLayout::get_status($id, true); $class = array('layout-item'); if (is_array($children) && count($children) !== 0 && headway_post('mode') === 'grid') { $class[] = 'has-children'; } if ($status['customized'] === true && !$status['template'] && count(HeadwayBlocksData::get_blocks_by_layout($id)) > 0) { $class[] = 'layout-item-customized'; } if ($status['template']) { $class[] = 'layout-item-template-used'; } if ($id === headway_post('currentLayout')) { $class[] = 'layout-selected'; } $template_id = $status['template'] ? 'template-' . $status['template'] : 'none'; $template_name = $status['template'] ? HeadwayLayout::get_name('template-' . $status['template']) : null; /* Take care of layouts that are the front page or blog index */ if (get_option('show_on_front') === 'page' && (isset($layout_id_fragments[1]) && $layout_id_fragments[1] == 'page') || apply_filters('headway_layout_selector_no_edit_item_' . $id, false)) { /* If the page is set as the static homepage or blog page, hide it if they don't have children. The Blog Index and Front Page layouts will override them. */ if (end($layout_id_fragments) == get_option('page_on_front') || end($layout_id_fragments) == get_option('page_for_posts') || apply_filters('headway_layout_selector_no_edit_item_' . $id, false)) { /* Layout has children--add the no edit class and has children class. */ if (is_array($children) && count($children) !== 0) { $class[] = 'layout-item-no-edit'; /* If the layout doesn't have children, then just hide it. */ } else { continue; } } } /* Set layouts that aren't customized in the DE to no edit */ if (headway_post('mode') === 'design') { /* Handle layouts that aren't customized or have a template */ if (headway_get('customized', $status, false) === false || headway_get('template', $status, false) !== false) { /* If there ARE customized children, add the no-edit class */ if (is_array($children) && count($children) !== 0) { $show_node = false; //Get the variable ready /* Check if the children are customized. */ if (self::is_any_layout_child_customized($children)) { $class[] = 'layout-item-no-edit'; $class[] = 'has-children'; $class[] = 'has-customized-children'; $show_node = true; } /* If the children aren't customized, then don't display it at all */ if (!isset($show_node) || !$show_node) { continue; } /* If there aren't any children, do not display the node at all */ } else { continue; } /* Handle layouts that are customized */ } } /* Add other classes */ if (is_array($children) && count($children) && self::is_any_layout_child_customized($children)) { $class[] = 'has-children'; $class[] = 'has-customized-children'; } /* Get post status */ $post_status = headway_get('post_status', $status); $formatted_post_status = $post_status && $post_status != 'Published' ? '<span class="status status-post-status">(' . $post_status . ')</span>' : null; /* Output Stuff */ $return .= '<li class="' . implode(' ', array_filter($class)) . '">'; $return .= '<span class="layout-has-customized-children tooltip" title="This layout has customized children.">°</span>'; $return .= '<span data-layout-id="' . $id . '" data-layout-url="' . HeadwayLayout::get_url($id) . '" class="layout layout-page">'; $return .= '<strong>' . htmlspecialchars(HeadwayLayout::get_name($id)) . '</strong>'; $return .= $formatted_post_status; $return .= '<span class="status status-template" data-template-id="' . $template_id . '">' . $template_name . '</span>'; $return .= '<span class="status status-customized">Customized</span>'; $return .= '<span class="status status-currently-editing">Currently Editing</span>'; $return .= '<span class="remove-template layout-selector-button">Remove Template</span>'; if (HeadwayVisualEditor::get_current_mode() !== 'design') { $return .= '<span class="edit layout-selector-button">Edit</span>'; } else { $return .= '<span class="edit layout-selector-button">View</span>'; } $return .= '<span class="revert layout-selector-button tooltip" title="Resetting a layout will remove all of its blocks<br />so it inherits the blocks of a parent layout.">Reset</span>'; $return .= '</span>' . "\n"; if (is_array($children) && count($children) !== 0) { $return .= self::list_pages($children); } $return .= '</li>' . "\n"; } $return .= '</ul>' . "\n"; return $return; }
public static function display_canvas() { echo '<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="' . get_bloginfo('charset') . '" /> <link rel="profile" href="http://gmpg.org/xfn/11" /> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <meta http-equiv="cache-control" content="no-cache" /> <title>Visual Editor Grid: ' . wp_title(false, false) . '</title>'; do_action('headway_grid_iframe_head'); echo '</head><!-- /head --> <body class="visual-editor-iframe-grid ' . join(' ', get_body_class()) . '">'; $wrappers = HeadwayWrappers::get_layout_wrappers(HeadwayLayout::get_current_in_use()); $blocks = HeadwayBlocksData::get_blocks_by_layout(HeadwayLayout::get_current_in_use()); echo '<div id="whitewrap" data-wrappers="' . htmlspecialchars(json_encode($wrappers)) . '">'; foreach ($wrappers as $wrapper_id => $wrapper_settings) { /* Setup wrapper classes */ $wrapper_classes = array('wrapper'); $wrapper_classes[] = headway_get('use-independent-grid', $wrapper_settings) ? 'independent-grid' : null; $wrapper_classes[] = $wrapper_settings['fluid'] ? 'wrapper-fluid' : 'wrapper-fixed'; $wrapper_classes[] = $wrapper_settings['fluid'] && $wrapper_settings['fluid-grid'] ? 'wrapper-fluid-grid' : 'wrapper-fixed-grid'; if (HeadwayWrappers::get_wrapper_mirror($wrapper_settings)) { $wrapper_classes[] = 'wrapper-mirrored'; } /* Populate wrapper with its blocks */ $wrapper_blocks = array(); foreach ($blocks as $block_id => $block) { /* Grab blocks belonging to this wrapper */ if (headway_get('wrapper', $block, HeadwayWrappers::$default_wrapper_id) === $wrapper_id) { $wrapper_blocks[$block_id] = $block; } /* If last wrapper, grab all blocks on this layout with invalid wrapper IDs to make sure they're editable somewhere */ $last_wrapper_id = array_slice(array_keys($wrappers), -1, 1); $last_wrapper_id = $last_wrapper_id[0]; if ($last_wrapper_id == $wrapper_id && !headway_get(headway_get('wrapper', $block, HeadwayWrappers::$default_wrapper_id), $wrappers)) { $wrapper_blocks[$block_id] = $block; } } /* Output the wrapper */ echo '<div id="' . $wrapper_id . '" class="' . implode(' ', array_filter($wrapper_classes)) . '">'; echo '<div class="wrapper-mirror-overlay"></div><!-- .wrapper-mirror-overlay -->'; self::display_grid_blocks($wrapper_blocks, $wrapper_settings); echo '</div><!-- #wrapper-' . $wrapper_id . ' -->'; } echo '<div id="wrapper-buttons-template">'; echo '<div class="wrapper-top-margin-handle wrapper-handle wrapper-margin-handle" title="Drag to change wrapper top margin"><span></span><span></span><span></span></div>'; echo '<div class="wrapper-drag-handle wrapper-handle tooltip tooltip-right" title="Drag to change wrapper order"><span></span><span></span><span></span></div>'; echo '<div class="wrapper-bottom-margin-handle wrapper-handle wrapper-margin-handle" title="Drag to change wrapper bottom margin"><span></span><span></span><span></span></div>'; echo '<div class="wrapper-options tooltip tooltip-right" title="Click to open wrapper options"><span></span></div>'; echo '</div><!-- .wrapper-buttons -->'; do_action('headway_grid_iframe_footer'); echo '</div><!-- #whitewrap --> </body> </html>'; }