/**
  * Checks if template has BP cell and add filters.
  * 
  * @param type $layout_id
  * @param type $args
  * @return type
  */
 function has_cell($layout_id, $args)
 {
     $layout_settings = WPDD_Layouts::get_layout_settings($layout_id, true);
     $layout_instance = new WPDD_json2layout();
     $layout = $layout_instance->json_decode(wp_json_encode($layout_settings));
     if ($layout->has_cell_of_type('buddypress') && is_buddypress()) {
         // Do something
     }
     return $layout_id;
 }
 public function duplicate_layout_callback()
 {
     // Clear any errors that may have been rendered that we don't have control of.
     if (ob_get_length()) {
         ob_clean();
     }
     if (user_can_create_layouts() === false) {
         die(WPDD_Utils::ajax_caps_fail(__METHOD__));
     }
     if ($_POST && wp_verify_nonce($_POST['layout-duplicate-layout-nonce'], 'layout-duplicate-layout-nonce')) {
         global $wpdb, $wpddlayout;
         $result = $wpdb->get_row($wpdb->prepare("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_type=%s AND ID=%d AND post_status = 'publish'", WPDDL_LAYOUTS_POST_TYPE, $_POST['layout_id']));
         if ($result) {
             $layout_json = WPDD_Layouts::get_layout_settings($result->ID);
             $layout_array = json_decode($layout_json, true);
             $layout_name_base = __('Copy of ', 'ddl-layouts') . str_replace('\\', '\\\\', $layout_array['name']);
             $layout_name = $layout_name_base;
             $count = 1;
             while ($wpddlayout->does_layout_with_this_name_exist($layout_name)) {
                 $layout_name = $layout_name_base . ' - ' . $count;
                 $count++;
             }
             $postarr = array('post_title' => $layout_name, 'post_content' => '', 'post_status' => 'publish', 'post_type' => WPDDL_LAYOUTS_POST_TYPE);
             $post_id = wp_insert_post($postarr);
             $post_slug = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE post_type=%s AND ID=%d", WPDDL_LAYOUTS_POST_TYPE, $post_id));
             $layout_array['name'] = $layout_name;
             $layout_array['slug'] = $post_slug;
             WPDD_Layouts::save_layout_settings($post_id, $layout_array);
             $wpddlayout->register_strings_for_translation($post_id);
         }
         $send = $wpddlayout->listing_page->get_send(isset($_GET['status']) && $_GET['status'] === 'trash' ? $_GET['status'] : 'publish', false, $post_id, $post_id, '', $_POST);
     } else {
         $send = wp_json_encode(array('error' => __(sprintf('Nonce problem: apparently we do not know where the request comes from. %s', __METHOD__), 'ddl-layouts')));
     }
     die($send);
 }
 public function build_export_data_from_post($layout, $args = array(post_types, archives, posts, null))
 {
     global $wpddlayout;
     $result = null;
     $post_types = null;
     $archives = null;
     $posts = null;
     $layout_array = WPDD_Layouts::get_layout_settings($layout->ID, true);
     if (is_null($layout_array) === false) {
         do_action('ddl-layout-export-processing-start', $layout_array);
         /**
          * there is a replace do it when it's lighter
          */
         if (is_array($args) && in_array(attachments, $args)) {
             $media = new WPDDL_LayoutsResources($layout_array);
         }
         try {
             $layout_array = self::fix_toolset_association_on_import_export($layout_array, 'export');
         } catch (Exception $e) {
             printf("Error: %s in %s at %d", $e->getMessage(), $e->getFile(), $e->getLine());
         }
         if (is_array($args) && in_array(post_types, $args)) {
             $post_types = $this->set_post_types_export_data($layout->ID);
             $layout_array->post_types = apply_filters('ddl-export-assigned-post-types', $post_types, $layout->ID);
         }
         if (is_array($args) && in_array(archives, $args)) {
             $archives = $wpddlayout->layout_post_loop_cell_manager->get_layout_loops($layout->ID);
             $layout_array->archives = apply_filters('ddl-export-assigned-archives', $archives, $layout->ID);
         }
         $posts = $this->set_layouts_associated_posts_for_export($layout->post_name, $post_types);
         if (is_array($args) && in_array(posts, $args)) {
             $layout_array->posts = apply_filters('ddl-export-assigned-posts', $posts, $layout->post_name, $post_types);
         }
         $translations = apply_filters('wpml_get_translated_strings', array(), array('kind' => 'Layout', 'name' => $layout->ID));
         if (!empty($translations)) {
             $layout_array->translations = $translations;
         }
         /**
          * then add media at the end since they're very heavy
          */
         if (is_array($args) && in_array(attachments, $args)) {
             $layout_array->media = apply_filters('ddl-get-layout-images-array', null);
         }
         /**
          * remove the id for comparison, we rely on post_name/slug properties
          */
         do_action('ddl-layout-export-processing-end', $layout_array);
         unset($layout_array->id);
         $layout_json = apply_filters('ddl-get-exported-layout', wp_json_encode($layout_array), $layout_array);
         $file_name = $layout->post_name . '.ddl';
         $result = array('file_data' => $layout_json, 'file_name' => $file_name, 'title' => $layout->post_title, 'has_posts' => is_null($posts) || count($posts) === 0 ? null : $posts);
     }
     return $result;
 }
 public function __construct($layout_id)
 {
     $this->remapped = false;
     $this->removed = array();
     $this->layout_id = $layout_id;
     $this->layout = WPDD_Layouts::get_layout_settings($this->layout_id, true);
 }
 public static function save_layout_settings($post_id, $settings)
 {
     if (is_string($settings)) {
         $settings = wp_json_encode(json_decode($settings, true));
     } else {
         if (is_array($settings) || is_object($settings)) {
             $settings = wp_json_encode((array) $settings);
         }
     }
     $layouts_raw = WPDD_Layouts::get_layout_settings($post_id, false);
     $result = update_post_meta($post_id, WPDDL_LAYOUTS_SETTINGS, addslashes($settings), $layouts_raw);
     // clear the cache.
     $layouts_raw = WPDD_Layouts::get_layout_settings($post_id, false, true);
     return $result;
 }
 public static function get_children($layout, $layouts_list, $previous_slug = null)
 {
     $ret = array();
     if (isset($layout->has_child) && ($layout->has_child === 'true' || $layout->has_child === true)) {
         $layout_slug = $previous_slug === null ? $layout->slug : $previous_slug;
         foreach ($layouts_list as $post) {
             $child = WPDD_Layouts::get_layout_settings($post->ID, true);
             if ($child) {
                 if (property_exists($child, 'parent') && $child->parent == $layout_slug && $layout->id != $post->ID) {
                     $ret[] = $post->ID;
                 }
             }
         }
         return $ret;
     }
     return $ret;
 }
function display_breadcrumb_tree($parent_id, $available_parents)
{
    global $wpddlayout;
    if ($parent_id == 0) {
        if (isset($available_parents[$parent_id]) && sizeof($available_parents[$parent_id]) > 0) {
            // Add No Parent to the start.
            ?>
			<li class="js-tree-category-item">
				<p class="item-name-wrap js-item-name-wrap">
					<a href="#">
						<span class="js-item-name"
							  data-layout-slug=""
							  data-layout-id=""><?php 
            _e('(no parent)', 'ddl-layouts');
            ?>
</span>
					</a>
				</p>
			<?php 
            foreach ($available_parents[$parent_id] as $parent) {
                display_breadcrumb_tree($parent, $available_parents);
            }
        }
    } else {
        $layout = WPDD_Layouts::get_layout_settings($parent_id, true);
        ?>
			<li class="js-tree-category-item <?php 
        if (isset($available_parents[$parent_id])) {
            ?>
js-tree-category<?php 
        }
        ?>
">
				<p class="item-name-wrap js-item-name-wrap">
					<?php 
        if (isset($available_parents[$parent_id])) {
            ?>
					<i class="js-tree-toggle icon-collapse-alt" data-expanded="true" data-text-expanded="Collapse" data-text-collapsed="Expand" title="Collapse"></i>
					<?php 
        }
        ?>
					<a href="#">
						<span class="js-item-name"
							  data-layout-slug="<?php 
        echo $layout->slug;
        ?>
"
							  data-layout-id="<?php 
        echo $layout->id;
        ?>
"><?php 
        echo esc_html($layout->name);
        ?>
</span>
					</a>
				</p>

				<?php 
        if (isset($available_parents[$parent_id]) && sizeof($available_parents[$parent_id]) > 0) {
            ?>
					<ul class="js-tree-category-items">
						<?php 
            foreach ($available_parents[$parent_id] as $parent) {
                display_breadcrumb_tree($parent, $available_parents);
            }
            ?>
					</ul> <!-- .js-tree-category-items -->
				<?php 
        }
        ?>
			</li> <!-- .js-tree-category-item -->

		<?php 
    }
}
    function meta_box($post)
    {
        global $wpddlayout;
        if ($wpddlayout->theme_has_page_templates() === false) {
            ?>
            <p class="toolset-alert toolset-alert-warning js-layout-support-warning line-height-16">
                <?php 
            echo sprintf(__("A template file that supports layouts is not available.", 'ddl-layouts'), '');
            ?>
<br>
                <?php 
            printf(__('Please check %shere%s to see how to set one up.', 'ddl-layouts'), '<a href="' . WPDLL_LEARN_ABOUT_SETTING_UP_TEMPLATE . '" target="_blank">', '</a>');
            ?>
            </p>
            <?php 
            return;
        }
        global $wpddl_features;
        $layout_tempates_available = WPDD_Layouts_Cache_Singleton::get_published_layouts();
        if (isset($_GET['post'])) {
            $template_selected = get_post_meta($_GET['post'], WPDDL_LAYOUTS_META_KEY, true);
        } else {
            $template_selected = '';
        }
        if ($post->post_type != 'page') {
            $none = new stdClass();
            $none->ID = 0;
            $none->post_name = 0;
            $none->post_title = __('None', 'ddl-layouts');
            array_unshift($layout_tempates_available, $none);
        }
        $post_type_obj = get_post_type_object($post->post_type);
        ?>

        <div class="js-dd-layout-selector">

            <script type="text/javascript">
                var ddl_old_template_text = "<?php 
        _e('Template', 'ddl-layouts');
        ?>
";
            </script>


            <?php 
        if ($post->post_type == 'page') {
            ?>
                <p>
                    <i class="icon-layouts-logo ont-icon-22 ont-color-orange"></i> <strong><?php 
            _e('Template and Layout', 'ddl-layouts');
            ?>
</strong>
                </p>
            <?php 
        }
        ?>
            <p>


                <?php 
        $template_selected = $this->wpml_layout_for_post_edit($template_selected);
        $template_option = $wpddlayout->get_option('templates');
        $post_type_theme = $wpddlayout->post_types_manager->get_layout_template_for_post_type($post->post_type);
        $theme_template = $post_type_theme == 'default' ? basename(get_page_template()) : $post_type_theme;
        $post_type_layout = $wpddlayout->post_types_manager->get_layout_to_type_object($post->post_type);
        ?>

                <input type="hidden" name="ddl-namespace-post-type-tpl" value="<?php 
        echo $post_type_theme == 'default' ? 'default' : $theme_template;
        ?>
" class="js-ddl-namespace-post-type-tpl" />
                <select name="layouts_template" id="js-layout-template-name" <?php 
        disabled($post->post_type == 'attachment');
        /* cannot assign layouts to attachment post type posts individually */
        ?>
 >
                    <?php 
        if (isset($template_option[$theme_template])) {
            $theme_default_layout = $template_option[$theme_template];
        } else {
            $theme_default_layout = '';
        }
        foreach ($layout_tempates_available as $template) {
            $layout = WPDD_Layouts::get_layout_settings($template->ID, true);
            $has_loop = is_object($layout) && property_exists($layout, 'has_loop') ? $layout->has_loop : false;
            $supported = true;
            $warning = '';
            if ($layout && property_exists($layout, 'type') && $layout->type == 'fixed' && !$wpddl_features->is_feature('fixed-layout')) {
                $warning = __("This layout is a fixed layout. The current theme doesn't support fixed layouts and might not display correctly", 'ddl-layouts');
            }
            if ($layout && property_exists($layout, 'type') && $layout->type == 'fluid' && !$wpddl_features->is_feature('fluid-layout')) {
                $warning = __("This layout is a fluid layout. The current theme doesn't support fluid layouts and might not display correctly", 'ddl-layouts');
            }
            if ($layout && $template_selected != $layout->slug && isset($layout->has_child) && ($layout->has_child === 'true' || $layout->has_child === true)) {
                $supported = false;
            }
            if ($supported) {
                $force_layout = ' data-force-layout="false"';
                if ($template_selected == $template->post_name) {
                    $selected = ' selected="selected"';
                } elseif (!isset($_GET['post']) && is_object($post_type_layout) && property_exists($post_type_layout, 'layout_id') && (int) $template->ID === (int) $post_type_layout->layout_id) {
                    $selected = ' selected="selected"';
                    $force_layout = ' data-force-layout="true"';
                } else {
                    $selected = '';
                }
                $title = $template->post_title;
                if ($title == '') {
                    $title = $template->post_name;
                }
                if ($template->post_name == $theme_default_layout && $post->post_type == 'page') {
                    $title .= __(' - Template default', 'ddl-layouts');
                }
                $data_object = array('layout_has_loop' => $has_loop, 'post_type' => $post->post_type, 'is_really_selected' => $selected !== '');
                $data_object = $data_object;
                ?>
                            <option data-object="<?php 
                echo htmlspecialchars(wp_json_encode($data_object));
                ?>
" value="<?php 
                echo $template->post_name;
                ?>
"<?php 
                echo $selected . $force_layout;
                ?>
 data-id="<?php 
                echo $template->ID;
                ?>
" data-ddl-warning="<?php 
                echo $warning;
                ?>
"><?php 
                echo $title;
                ?>
</option>
                        <?php 
            }
        }
        ?>

                </select>

                <input type="hidden" class="js-wpddl-default-template-message" value="<?php 
        echo $this->main_template;
        ?>
" data-message="<?php 
        _e('Show all templates', 'ddl-layouts');
        ?>
" />
                <?php 
        if ($post->post_type == 'page') {
            ?>
                    <select name="combined_layouts_template" id="js-combined-layout-template-name">

                    </select>


                <?php 
        }
        ?>
            <p>
                <a data-href="<?php 
        echo admin_url() . 'admin.php?page=dd_layouts_edit&amp;action=edit&layout_id=';
        ?>
" class="edit-layout-template js-edit-layout-template"><?php 
        _e('Edit this layout', 'ddl-layouts');
        ?>
</a>
            </p>
            </p>

            <?php 
        do_action('ddl_add_create_layout_button');
        ?>

            <div class="display_errors js-display-errors"></div>

            <p class="toolset-alert toolset-alert-warning js-layout-support-warning" style="display:none">
            </p>


            <?php 
        wp_nonce_field('wp_nonce_ddl_dismiss', 'wp_nonce_ddl_dismiss');
        ?>
        </div>



        <?php 
        $woocommerce_support_message = $wpddlayout->post_types_manager->check_layout_template_for_woocommerce($post_type_obj);
        ?>
        <?php 
        if ($woocommerce_support_message) {
            ?>

            <p class=" toolset-alert toolset-alert-warning js-layout-support-missing">
                <?php 
            echo $woocommerce_support_message;
            ?>
            </p>
        <?php 
        } elseif ($wpddlayout->post_types_manager->check_layout_template_page_exists($post_type_obj) === false) {
            ?>

            <p class=" toolset-alert toolset-alert-warning js-layout-support-missing">
                <?php 
            echo sprintf(__("A template file that supports layouts is not available.", 'ddl-layouts'), '<strong>"' . $post_type_obj->labels->singular_name . '"</strong>');
            ?>
<br>
                <?php 
            printf(__('Please check %shere%s to see how to set one up.', 'ddl-layouts'), '<a href="' . WPDLL_LEARN_ABOUT_SETTING_UP_TEMPLATE . '" target="_blank">', '</a>');
            ?>
            </p>
        <?php 
        }
        ?>


    <?php 
    }
 function before_header_hook()
 {
     if (isset($_GET['layout_id'])) {
         $layout_selected = $_GET['layout_id'];
     } else {
         $post_id = get_the_ID();
         $layout_selected = WPDD_Layouts::get_layout_settings($post_id, false);
     }
     if ($layout_selected > 0) {
         //$layout_content = get_post_meta($layout_selected, WPDDL_LAYOUTS_SETTINGS);
         $layout_content = WPDD_Layouts::get_layout_settings_raw_not_cached($layout_selected, false);
         if (sizeof($layout_content) > 0) {
             $test = new WPDD_json2layout();
             $layout = $test->json_decode($layout_content[0]);
             $manager = new WPDD_layout_render_manager($layout);
             $renderer = $manager->get_renderer();
             $html = $renderer->render_to_html();
             echo $html;
         }
     }
 }
 public function view_layout_from_editor_callback()
 {
     global $wpddlayout;
     if (user_can_assign_layouts() === false) {
         die(WPDD_Utils::ajax_caps_fail(__METHOD__));
     }
     if ($_POST && wp_verify_nonce($_POST['ddl-view-layout-nonce'], 'ddl-view-layout-nonce')) {
         $layout = WPDD_Layouts::get_layout_settings($_POST['layout_id'], true);
         if ($layout && isset($layout->has_child) && ($layout->has_child === 'true' || $layout->has_child === true)) {
             $send = wp_json_encode(array('message' => __("This layout contains a child layout and can't be viewed directly.", 'ddl-layouts') . '<br />' . __("You'll need to switch to one of the child layouts and view it.", 'ddl-layouts')));
         } else {
             $items = $this->get_where_used_x_amount_of_posts($_POST['layout_id'], false, 3);
             $posts = $items->posts;
             $layout_post_types = $wpddlayout->post_types_manager->get_layout_post_types($_POST['layout_id']);
             $loops = $wpddlayout->layout_post_loop_cell_manager->get_layout_loops($_POST['layout_id']);
             if (count($posts) === 0 && count($loops) === 0 && count($layout_post_types) === 0) {
                 $send = wp_json_encode(array('message' => __(sprintf("This layout is not assigned to any content. %sFirst, assign it to content and then you can view it on the site's front-end. %sYou can assign this layout to content at the bottom of the layout editor.", '<br>', '<br>'), 'ddl-layouts')));
             } else {
                 $items = array();
                 foreach ($layout_post_types as $post_type) {
                     $push = $this->get_x_posts_of_type($post_type, $_POST['layout_id'], 1);
                     if (is_array($push)) {
                         $posts = array_merge($posts, $push);
                     }
                 }
                 foreach ($posts as $post) {
                     $post_types = $wpddlayout->post_types_manager->get_post_types_from_wp();
                     $label = $post_types[$post->post_type]->labels->singular_name;
                     $labels = $post_types[$post->post_type]->labels->name;
                     $item = array('href' => get_permalink($post->ID), 'title' => $post->post_title, 'type' => $label, 'types' => $labels);
                     if (in_array($item, $items) === false) {
                         $items[] = $item;
                     }
                 }
                 foreach ($loops as $loop) {
                     $push = $wpddlayout->layout_post_loop_cell_manager->get_loop_display_object($loop);
                     if (null !== $push) {
                         array_push($items, $push);
                     }
                 }
                 $send = wp_json_encode(array('Data' => $items, 'message' => __(sprintf("This layout is not assigned to any content. %sFirst, assign it to content and then you can view it on the site's front-end. %sYou can assign this layout to content at the bottom of the layout editor.", '<br>', '<br>'), 'ddl-layouts'), 'no_preview_message' => __('No previews available', 'ddl-layouts')));
             }
         }
     } else {
         $send = wp_json_encode(array('error' => __(sprintf('Nonce problem: apparently we do not know where the request comes from. %s', __METHOD__), 'ddl-layouts')));
     }
     die($send);
 }