function motopressCEAddHeadwayFix($post_id, $tmp_post_id, $post_type) { if (defined('HEADWAY_VERSION')) { global $wpdb; if (version_compare(HEADWAY_VERSION, '3.7.10', '>=') || property_exists($wpdb, 'hw_wrappers')) { global $wp_query; $originalWpQuery = $wp_query; $key = $post_type === 'page' ? 'page_id' : 'p'; $wp_query = new WP_Query($key . '=' . $post_id); if (have_posts()) { while (have_posts()) { the_post(); } } $layoutId = HeadwayLayout::get_current_in_use(); $wrappers = HeadwayWrappersData::get_wrappers_by_layout($layoutId); // if ($wrappers[0]['id'] !== 'default') { if (!array_key_exists('default', $wrappers)) { $sep = preg_quote(HeadwayLayout::$sep); if (preg_match('/^single' . $sep . '[a-z]+' . $sep . '\\d+$/is', $layoutId)) { $layoutTmpId = str_replace($post_id, $tmp_post_id, $layoutId); } else { $layoutTmpId = 'single' . HeadwayLayout::$sep . $post_type . HeadwayLayout::$sep . $tmp_post_id; } update_option('motopress-ce-hw-layout', $layoutTmpId); foreach ($wrappers as $wrapper_id => $wrapper) { $wrapperId = HeadwayWrappersData::add_wrapper($layoutTmpId, $wrapper); $blocks = HeadwayBlocksData::get_blocks_by_wrapper($layoutId, $wrapper_id); foreach ($blocks as $block_id => &$block) { $block['wrapper_id'] = $wrapperId; $block['wrapper'] = $wrapperId; HeadwayBlocksData::add_block($layoutTmpId, $block); } unset($block); } $transient_id_customized_layouts = 'hw_customized_layouts_template_' . HeadwayOption::$current_skin; $customized_layouts = get_transient($transient_id_customized_layouts); if (!$customized_layouts) { $customized_layouts = array_unique($wpdb->get_col($wpdb->prepare("SELECT layout FROM {$wpdb->hw_blocks} WHERE template = '%s'", HeadwayOption::$current_skin))); } if (!in_array($layoutTmpId, $customized_layouts)) { $customized_layouts[] = $layoutTmpId; set_transient($transient_id_customized_layouts, $customized_layouts); } } wp_reset_postdata(); $wp_query = $originalWpQuery; } else { $layout = get_option('headway_layout_options_' . $post_id); if ($layout) { update_option('headway_layout_options_' . $tmp_post_id, $layout); } else { delete_option('headway_layout_options_' . $tmp_post_id); } } } }
public static function add_template($template_name = null, $blocks = null, $wrappers = null) { $templates = HeadwayOption::get('list', 'templates', array()); $last_template_id = HeadwayOption::get('last-id', 'templates', 0); /* These two variables be used for when a blocks/wrappers imported ID is different than the one that it ends up with... i.g. skin importing to line up instances */ $block_id_translations = array(); $wrapper_id_translations = array(); /* Build name */ $id = $last_template_id + 1; $template_name = $template_name ? $template_name : 'Template ' . $id; /* Add template to templates array so it can be sent to DB */ $templates[$id] = $template_name; /* Send array to DB */ HeadwayOption::set('list', $templates, 'templates'); HeadwayOption::set('last-id', $id, 'templates'); /* Add blocks and wrappers */ if ($blocks && $wrappers) { /* Format wrappers */ foreach ($wrappers as $wrapper_id => $wrapper_settings) { $old_wrapper_id = $wrapper_id; $new_wrapper = HeadwayWrappers::add_wrapper('template-' . $id, $wrapper_settings); $wrapper_id_translations[str_replace('wrapper-', '', $old_wrapper_id)] = array('id' => $new_wrapper['id'], 'layout' => 'template-' . $id); } /* Add blocks */ foreach ($blocks as $block) { $old_block_id = $block['id']; unset($block['id']); unset($block['layout']); /* Update block's wrapper ID to match the real ID of the imported wrapper because if you link to the old ID from the export file then it won't match up */ $block['wrapper'] = 'wrapper-' . $wrapper_id_translations[str_replace('wrapper-', '', $block['wrapper'])]['id']; $new_block_id = HeadwayBlocksData::add_block('template-' . $id, $block); $block_id_translations[$old_block_id] = $new_block_id; } } /* End adding wrappers and blocks */ return array('id' => $id, 'name' => $template_name, 'block-id-translations' => $block_id_translations, 'wrapper-id-translations' => $wrapper_id_translations); }
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; }