/**
  * Saves the layout and returns the layout ID key.
  * Layout ID key is *not* the same as layout ID,
  * it's a hash used to resolve this particular layout.
  * @param Upfront_Layout $layout layout to store
  * @return mixed (bool)false on failure, (string)layout ID key on success
  */
 public function save_revision($layout)
 {
     $cascade = $layout->get_cascade();
     $store = $layout->to_php();
     $layout_id_key = self::to_hash($store);
     $existing_revision = $this->get_revision($layout_id_key);
     if (!empty($existing_revision)) {
         return $layout_id_key;
     }
     $post_id = wp_insert_post(array("post_content" => base64_encode(serialize($store)), "post_title" => self::to_string($cascade), "post_name" => $layout_id_key, "post_type" => self::REVISION_TYPE, "post_status" => self::REVISION_STATUS, "post_author" => get_current_user_id()));
     return !empty($post_id) && !is_wp_error($post_id) ? $layout_id_key : false;
 }
Exemplo n.º 2
0
function upfront_is_page_template($page_template, $layout_ids = null)
{
    if (defined("DOING_AJAX") && DOING_AJAX) {
        if (is_array($layout_ids)) {
            $layout = $layout_ids;
        } else {
            if (isset($_POST['layout'])) {
                $layout = $_POST['layout'];
            } else {
                if (isset($_GET['layout'])) {
                    $layout = $_GET['layout'];
                } else {
                    if (Upfront_Layout::get_instance()) {
                        $layout = Upfront_Layout::get_instance()->get('layout');
                    }
                }
            }
        }
        if (!$layout) {
            return false;
        }
        return $page_template == upfront_get_page_template_slug($layout);
    } else {
        return is_page_template($page_template);
    }
}
Exemplo n.º 3
0
 public static function get_layout($layout_ids, $apply = false)
 {
     $layout = Upfront_Layout::from_entity_ids($layout_ids);
     if ($layout->is_empty()) {
         $layout = Upfront_Layout::create_layout($layout_ids);
     }
     $post_id = is_singular() ? get_the_ID() : '';
     $post = get_post($post_id);
     self::$_instance = new self($layout, $post);
     // Add actions
     add_action('wp_enqueue_scripts', array(self::$_instance, 'add_styles'));
     add_action('wp_enqueue_scripts', array(self::$_instance, 'add_scripts'), 2);
     // Do the template...
     if ($apply) {
         return self::$_instance->apply_layout();
     }
     return self::$_instance;
 }
Exemplo n.º 4
0
 function update_layout_element()
 {
     if (!Upfront_Permissions::current(Upfront_Permissions::SAVE)) {
         $this->_reject();
     }
     $data = !empty($_POST) ? stripslashes_deep($_POST) : false;
     if (!$data) {
         return $this->_out(new Upfront_JsonResponse_Error("No data"));
     }
     if (empty($data['layout'])) {
         return $this->_out(new Upfront_JsonResponse_Error("No layout id given"));
     }
     if (empty($data['element'])) {
         return $this->_out(new Upfront_JsonResponse_Error("No element data given"));
     }
     $element = json_decode($data['element'], true);
     $layout = Upfront_Layout::from_entity_ids($data['layout'], $data['storage_key']);
     if (empty($layout)) {
         return $this->_out(new Upfront_JsonResponse_Error("Unkown layout"));
     }
     $updated = $layout->set_element_data($element);
     if (!$updated) {
         return $this->_out(new Upfront_JsonResponse_Error("Error updating the layout"));
     }
     $layout->save();
     $this->_out(new Upfront_JsonResponse_Success("Layout updated"));
 }
 public function getThemeStylesAsCss()
 {
     //$layout = Upfront_Layout::get_cascade();
     $layout = Upfront_Layout::get_parsed_cascade();
     // Use pure static method instead
     $layout_id = !empty($layout['specificity']) ? $layout['specificity'] : (!empty($layout['item']) ? $layout['item'] : $layout['type']);
     $out = '';
     // See if there are styles in theme files
     $styles_root = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'element-styles';
     // List subdirectories as element types
     $element_types = is_dir($styles_root) ? array_diff(scandir($styles_root), Upfront::$Excluded_Files) : array();
     $alternate_layout_id = false;
     if (!empty($layout['item']) && 'single-page' == $layout['item'] && !empty($layout['specificity'])) {
         $page_id = preg_replace('/.*-([0-9]+)$/', '$1', $layout['specificity']);
         if (is_numeric($page_id)) {
             foreach ($this->get_required_pages() as $page) {
                 if ((int) $page->get_id() !== (int) $page_id) {
                     continue;
                 }
                 $alternate_layout_id = $page->get_layout_name();
                 break;
             }
         }
     }
     foreach ($element_types as $type) {
         $style_files = array_diff(scandir($styles_root . DIRECTORY_SEPARATOR . $type), Upfront::$Excluded_Files);
         foreach ($style_files as $style) {
             // If region CSS, only load the one saved matched the layout_id
             $style_rx = '/^(' . preg_quote("{$layout_id}", '/') . '|' . preg_quote("{$type}", '/') . (!empty($alternate_layout_id) ? '|' . preg_quote($alternate_layout_id, '/') : '') . ')/';
             if (preg_match('/^region(-container|)$/', $type) && !preg_match($style_rx, $style)) {
                 continue;
             }
             $style_content = file_get_contents($styles_root . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $style);
             $style_content = $this->_expand_passive_relative_url($style_content);
             $out .= $style_content;
         }
     }
     // Add icon font style if there is active icon font other than UpFont
     $font = $this->getActiveIconFont();
     if ($font) {
         //$out .= "\nin font \n";
         $longSrc = '';
         foreach ($font['files'] as $type => $file) {
             $longSrc .= "url('" . self::THEME_BASE_URL_MACRO . '/icon-fonts/' . $file . "') format('";
             switch ($type) {
                 case 'eot':
                     $longSrc .= 'embedded-opentype';
                     break;
                 case 'woff':
                     $longSrc .= 'woff';
                     break;
                 case 'ttf':
                     $longSrc .= 'truetype';
                     break;
                 case 'svg':
                     $longSrc .= 'svg';
                     break;
             }
             $longSrc .= "'),";
         }
         $icon_font_style = "@font-face {" . "\tfont-family: '" . $font['family'] . "';";
         if (isset($font['files']['eot'])) {
             $icon_font_style .= "src: url('" . self::THEME_BASE_URL_MACRO . '/icon-fonts/' . $font['files']['eot'] . "');";
         }
         $icon_font_style .= "src:" . substr($longSrc, 0, -1) . ';';
         $icon_font_style .= "\tfont-weight: normal;" . "\tfont-style: normal;" . "}" . ".uf_font_icon {" . "\tfont-family: '" . $font['family'] . "'!important;" . "}";
         $out .= $this->_expand_passive_relative_url($icon_font_style) . "\n";
     }
     $this->_theme_styles_called = true;
     return $out;
 }
Exemplo n.º 6
0
 public static function create_layout($layout_ids = array(), $layout_slug = '')
 {
     self::$layout_slug = $layout_slug;
     self::$cascade = $layout_ids;
     $data = array("name" => "Default Layout", "properties" => self::get_layout_properties(), "regions" => self::get_regions_data(true), "layout_slug" => self::$layout_slug);
     return self::from_php(apply_filters('upfront_create_default_layout', $data, $layout_ids, self::$cascade));
 }
 function prepare_theme_styles()
 {
     // Fix storage key missing _dev in dev mode. This is regular GET request.
     $storage_key = Upfront_Layout::get_storage_key();
     if (isset($_GET['load_dev']) && $_GET['load_dev'] == 1 && strpos($storage_key, '_dev') === false) {
         $storage_key = $storage_key . '_dev';
     }
     $styles = get_option($storage_key . '_' . get_stylesheet() . '_styles', array());
     $out = '';
     //$layout = Upfront_Layout::get_cascade();
     $layout = Upfront_Layout::get_parsed_cascade();
     // Use pure static method instead
     $layout_id = !empty($layout['specificity']) ? $layout['specificity'] : (!empty($layout['item']) ? $layout['item'] : $layout['type']);
     $layout_style_loaded = false;
     // Keep track of global layout CSS, so we sent over to the filter
     if (is_array($styles)) {
         foreach ($styles as $type => $elements) {
             foreach ($elements as $name => $content) {
                 // If region CSS, only load the one saved matched the layout_id
                 $style_rx = '/^(' . preg_quote("{$layout_id}", '/') . '|' . preg_quote("{$type}", '/') . ')/';
                 if (preg_match('/^region(-container|)$/', $type) && !preg_match($style_rx, $name)) {
                     continue;
                 }
                 $out .= $content;
                 if ($type == 'layout' && $name == 'layout-style') {
                     $layout_style_loaded = true;
                 }
             }
         }
     }
     $out = apply_filters('upfront_prepare_theme_styles', $out, $layout_style_loaded);
     return $out;
 }
 /**
  * Builds preview layout model and dispatches save.
  */
 public function build_preview()
 {
     if (!Upfront_Permissions::current(Upfront_Permissions::SAVE) && !Upfront_Permissions::current(Upfront_Permissions::SAVE_REVISION)) {
         $this->_reject();
     }
     global $post;
     $raw_data = stripslashes_deep($_POST);
     $data = !empty($raw_data['data']) ? $raw_data['data'] : '';
     $current_url = !empty($raw_data['current_url']) ? $raw_data['current_url'] : home_url();
     $current_url = wp_validate_redirect(wp_sanitize_redirect($current_url), false);
     $current_url = $current_url ? $current_url : home_url();
     $layout = Upfront_Layout::from_json($data);
     $layout_id_key = $this->_data->save_revision($layout);
     // Check concurrent edits from other users
     $current_user_id = get_current_user_id();
     $current_others_revisions = $this->_data->get_entity_revisions($layout->get_cascade(), array('date_query' => array(array('after' => "-15 minutes")), 'author__not_in' => array($current_user_id)));
     $concurrent_users = array();
     if (!empty($current_others_revisions)) {
         foreach ($current_others_revisions as $rvsn) {
             if (empty($rvsn->post_author)) {
                 continue;
             }
             $user = get_user_by('id', $rvsn->post_author);
             if (empty($user) || empty($user->ID)) {
                 continue;
             }
             $concurrent_users[$user->ID] = $user->display_name;
         }
     }
     $preview_url = remove_query_arg('editmode', add_query_arg(array(self::HOOK => $layout_id_key), $current_url));
     $this->_out(new Upfront_JsonResponse_Success(array('html' => $preview_url, 'concurrent_users' => $concurrent_users)));
 }
Exemplo n.º 9
0
/**
 * Run first on each AJAX action registered with upfront_add_ajax
 */
function upfront_ajax_init()
{
    $stylesheet = $layout_ids = $storage_key = $load_dev = false;
    // Automatically instantiate Upfront_Layout object
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $layout_ids = !empty($_POST['layout']) ? $_POST['layout'] : false;
        $storage_key = !empty($_POST['storage_key']) ? $_POST['storage_key'] : false;
        $stylesheet = !empty($_POST['stylesheet']) ? $_POST['stylesheet'] : false;
        $load_dev = !empty($_POST['load_dev']) && $_POST['load_dev'] == 1 ? true : false;
    } else {
        if (isset($_GET['layout'])) {
            $layout_ids = !empty($_GET['layout']) ? $_GET['layout'] : false;
            $storage_key = !empty($_GET['storage_key']) ? $_GET['storage_key'] : false;
            $stylesheet = !empty($_GET['stylesheet']) ? $_GET['stylesheet'] : false;
            $load_dev = !empty($_GET['load_dev']) && $_GET['load_dev'] == 1 ? true : false;
        }
    }
    if ($stylesheet === false) {
        $stylesheet = apply_filters('upfront_get_stylesheet', $stylesheet);
    }
    upfront_switch_stylesheet($stylesheet);
    if (!is_array($layout_ids)) {
        return;
    }
    $layout = Upfront_Layout::from_entity_ids($layout_ids, $storage_key, $load_dev);
    if ($layout->is_empty()) {
        $layout = Upfront_Layout::create_layout($layout_ids);
    }
}
Exemplo n.º 10
0
 function save_resizing()
 {
     $data = stripslashes_deep($_POST);
     $layout = Upfront_Layout::from_entity_ids($data['layout']);
     return $this->_out(new Upfront_JsonResponse_Success($layout->get_element_data('uslider-object-1388746230599-1180')));
 }
Exemplo n.º 11
0
 private function get_settings_from_ajax()
 {
     $entity_ids = array();
     try {
         $entity_ids = (array) json_decode(base64_decode($_POST['entity_ids']));
     } catch (Exception $e) {
         return false;
     }
     $storage_key = false;
     if (isset($entity_ids['storage_key'])) {
         $storage_key = $entity_ids['storage_key'];
         unset($entity_ids['storage_key']);
     }
     $layout = Upfront_Layout::from_entity_ids($entity_ids, $storage_key);
     if ($layout instanceof Upfront_Layout) {
         $layout = $layout->to_php();
     } else {
         return false;
     }
     $settings = array();
     if (is_array($layout['regions'])) {
         foreach ($layout['regions'] as $region) {
             if (sizeof($region['modules'])) {
                 foreach ($region['modules'] as $module) {
                     if (sizeof($module['objects'])) {
                         foreach ($module['objects'] as $object) {
                             if (sizeof($object['properties'])) {
                                 foreach ($object['properties'] as $prop) {
                                     if ($prop['name'] == 'element_id' && $prop['value'] == $_POST['contactformid']) {
                                         return $object;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return false;
 }
    function upfront_template_helper_admin_page()
    {
        $specificity = $_POST["template"];
        $json_data = array();
        $codeOutput = "";
        $storageKey = "";
        if ($specificity != "") {
            $layout_ids = array('specificity' => $specificity);
            $layout = Upfront_Layout::from_entity_ids($layout_ids);
            $storageKey = $layout->get_storage_key();
            //		$layout_data = $layout->to_php();
            //		$properties = $layout->get_layout_properties();
            $regionNames = array();
            $regionForm = "";
            // EDIT:: I have changed it to load directly from the database because the Upfront classes merge data with global settings and it causes trouble
            $json_data = json_decode(get_option($storageKey . '-' . $specificity, json_encode(array())), true);
            foreach ($json_data["regions"] as $region) {
                $regionNames[$region["name"]] = $region["title"];
                $regionForm .= '<div class="region-check"><label for="' . $region["name"] . '">' . $region["title"] . '</label><input type="checkbox" id="' . $region["name"] . '" name="' . $region["name"] . '"> </div>
			';
            }
            $exportRegionNames = array();
            foreach ($_POST as $key => $value) {
                if ($value == 'on') {
                    $exportRegionNames[] = $key;
                }
            }
            $exportRegions = array();
            foreach ($json_data["regions"] as $region) {
                if (in_array($region["name"], $exportRegionNames)) {
                    $exportRegions[] = $region;
                }
            }
            $codeOutput = "";
            // create php output
            // create region args & properties
            //
            foreach ($exportRegions as $region) {
                $properties = $region["properties"];
                $modules = $region["modules"];
                $wrappers = $region["wrappers"];
                //unset($region["properties"]);
                //unset($region["modules"]);
                //unset($region["wrappers"]);
                $codeOutput .= '$' . str_replace("-", "_", $region["name"]) . ' = new Upfront_Virtual_Region_From_Existing(' . var_export($region, true) . ');
';
                // this doesn't work becuase it is expecting values to build its own wrappers, not pre-exisiting wrappers
                //				foreach($modules as $module)
                //				{
                //					$codeOutput .= '$'.str_replace("-","_",$region["name"]).'->add_element('.var_export($module,true).');
                //';
                //				}
                $codeOutput .= '$regions->add($' . str_replace("-", "_", $region["name"]) . ');
';
            }
            $codeOutput = highlight_string($codeOutput, true);
        }
        ?>
		<script>hljs.initHighlightingOnLoad();</script>
		<h2>Regions in this menu</h2>
		<form method="post" action="admin.php?page=upfront-template-helper">

			<table class="form-table">
				<tr>
					<td><label for="template-specificity">Template:</label></td>
					<td><input type="text" id="template-specificity" name="template" class="regular-text"  value="<?php 
        echo $specificity;
        ?>
"></td>
				</tr>
				<tr>
					<td>
						<p class="submit">
							<input type="submit" id="submit" class="button button-primary" value="Load Data">
						</p>
					</td>
				</tr>
			</table>
			<label for="template-specificity">

			</label>
		</form>
		<table class="widefat">
			<thead>
			<tr>
				<th class="row-title">Attribute</th>
				<th>Value</th>
			</tr>
			</thead>
			<tbody>
			<tr>
				<td class="row-title"><label for="tablecell">Template: </label></td>
				<td><?php 
        echo $specificity;
        ?>
 </td>
			</tr>
			<tr>
				<td class="row-title"><label for="tablecell">Storage Key: </label></td>
				<td><?php 
        echo $storageKey;
        ?>
 </td>
			</tr>
			<tr>
				<td class="row-title"><label for="tablecell">Regions: </label></td>
				<td><?php 
        echo count($json_data["regions"]);
        ?>
 </td>
			</tr>
			<tr>
				<td class="row-title"><label for="tablecell">Properties: </label></td>
				<td><?php 
        echo count($json_data["properties"]);
        ?>
 </td>
			</tr>
			<tr>
				<td class="row-title"><label for="tablecell">Wrappers: </label></td>
				<td><?php 
        echo count($json_data["wrappers"]);
        ?>
 </td>
			</tr>
			</tbody>
		</table>
		<style>
			.region-check{
				display: inline-block;
				margin: 5px;
				padding: 5px;
				background-color: #fff;
			}
			.upfront-regions > div > input[type="checkbox"]{
				margin-left: 5px;
				vertical-align: middle;
			}
			.upfront-regions > div > label{
				margin: 5px;
				padding:2px;
			}
			.upfront-regions{

			}
		</style>
		<form class="upfront-regions" action="admin.php?page=upfront-template-helper" method="post">
			<input type="hidden" name="template" value="<?php 
        echo $specificity;
        ?>
">
			<?php 
        echo $regionForm;
        ?>
			<div><input class="button button-secondary" type="submit" id="submit" value="Export"></div>
		</form>
		<div>
			<pre class="php">
			<?php 
        echo $codeOutput;
        ?>
			</pre>
		</div>
		<?php 
    }