/**
  * Return instance of this object
  *
  * @return Singleton
  * @access public
  * @static
  */
 public static function getInstance()
 {
     if (self::$oInstance == false) {
         self::$oInstance = new WPDD_Layouts_Cache_Singleton();
         self::$cache['by_id'] = array();
         self::$cache['by_name'] = array();
         //self::cache_published_layouts();
     }
     return self::$oInstance;
 }
 public function register_modules_layouts_items($arg)
 {
     $ret = array();
     $layouts = WPDD_Layouts_Cache_Singleton::get_published_layouts();
     foreach ($layouts as $layout) {
         $ret[] = array('id' => self::LAYOUTS_MODULE_MANAGER_KEY . $layout->ID, 'title' => $layout->post_title, 'details' => sprintf(__("Fetch layout %s", 'ddl-layouts'), $layout->post_title));
     }
     usort($ret, array(new Toolset_ArrayUtils('title'), 'sort_string_ascendant'));
     $ret[] = array('id' => self::LAYOUTS_MODULE_MANAGER_KEY . self::LAYOUTS_MODULE_MANAGER_CSS_ID, 'title' => __("Layouts CSS", 'ddl-layouts'), 'details' => __("The Layouts CSS you can export as layouts.css file", 'ddl-layouts'));
     return $ret;
 }
 function import_layouts_from_theme($source_dir, $overwrite_assignment = false)
 {
     global $wpddlayout;
     if (is_dir($source_dir)) {
         $layouts = glob($source_dir . '/*.ddl');
         foreach ($layouts as $layout) {
             $file_details = pathinfo($layout);
             $layout_name = $file_details['filename'];
             $layout_json = file_get_contents($layout);
             $layout = json_decode(str_replace('\\\\"', '\\"', $layout_json));
             $layout->file_name = $layout_name;
             $layouts_array[] = $layout;
         }
         usort($layouts_array, array($this, 'sortLayoutsFromFile'));
         $res = array();
         foreach ($layouts_array as $layout) {
             $layout_name = $layout->file_name;
             unset($layout->file_name);
             if (is_null($layout) === false) {
                 $layout_array = WPDD_Layouts_Theme::fix_toolset_association_on_import_export($layout, 'import');
                 // make sure we have the right data type
                 $layout_array = (array) $layout_array;
                 $id = WPDD_Layouts_Cache_Singleton::get_id_by_name($layout_name);
                 if (!$id) {
                     $postarr = array('post_title' => is_object($layout) ? $layout->name : $layout['name'], 'post_name' => $layout_name, 'post_content' => '', 'post_status' => 'publish', 'post_type' => WPDDL_LAYOUTS_POST_TYPE);
                     $post_id = wp_insert_post($postarr);
                     $layout_array['id'] = $post_id;
                     $save = WPDD_Layouts::save_layout_settings($post_id, $layout_array);
                     if ($save) {
                         $res[] = $post_id;
                     }
                     if ($overwrite_assignment) {
                         //Archives
                         if (isset($layout_array['archives']) && count($layout_array['archives']) > 0) {
                             $wpddlayout->layout_post_loop_cell_manager->handle_archives_data_save($layout_array['archives'], $post_id);
                             $wpddlayout->layout_post_loop_cell_manager->handle_archives_data_save($layout_array['archives'], $post_id);
                         }
                         //Post Types
                         if (isset($layout_array['post_types']) && count($layout_array['post_types']) > 0) {
                             $to_set = array();
                             $to_bulk = array();
                             foreach ($layout->post_types as $post_type => $bulk) {
                                 if (post_type_exists($post_type)) {
                                     if ($bulk) {
                                         $to_bulk[] = $post_type;
                                     }
                                     $to_set[] = $post_type;
                                 }
                             }
                             $wpddlayout->post_types_manager->handle_set_option_and_bulk_at_once($post_id, $to_set, $to_bulk, false);
                         }
                         if (isset($layout_array['posts']) && count($layout_array['posts']) > 0) {
                             $this->write_single_posts_assignments($post_id, $layout, $overwrite_assignment);
                         }
                     }
                 }
             }
         }
         $save = $wpddlayout->css_manager->import_css_from_theme($source_dir);
         if ($save) {
             $res[] = 'CSS';
         }
     }
     return $res;
 }
Ejemplo n.º 4
0
 /**
  * Create a layout with given name
  *
  * @param $name
  *
  * @return bool|int|WP_Error
  * @since 2.0
  */
 private function create_layout($name)
 {
     // todo check with Ric to get a more handy class to create a new layout.
     // currently there is only (which I found)
     // - create_layout_auto(), which has a redirect
     // - create_layout_callback() for ajax only -> uses die()
     global $wpddlayout;
     if (!$this->needed_components_loaded()) {
         return false;
     }
     // permissions
     if (!current_user_can('manage_options') && WPDD_Layouts_Users_Profiles::user_can_create() && WPDD_Layouts_Users_Profiles::user_can_assign()) {
         return false;
     }
     $layout = WPDD_Layouts::create_layout(12, 'fluid');
     $parent_post_name = '';
     $parent_ID = apply_filters('ddl-get-default-' . WPDDL_Options::PARENTS_OPTIONS, WPDDL_Options::PARENTS_OPTIONS);
     if ($parent_ID) {
         $parent_post_name = WPDD_Layouts_Cache_Singleton::get_name_by_id($parent_ID);
     }
     // Define layout parameters
     $layout['type'] = 'fluid';
     // layout_type
     $layout['cssframework'] = $wpddlayout->get_css_framework();
     $layout['template'] = '';
     $layout['parent'] = $parent_post_name;
     $layout['name'] = $name;
     $args = array('post_title' => $name, 'post_content' => '', 'post_status' => 'publish', 'post_type' => WPDDL_LAYOUTS_POST_TYPE);
     $layout_id = wp_insert_post($args);
     // force layout object to take right ID
     // @see WPDD_Layouts::create_layout_callback() @ wpddl.class.php
     $layout_post = get_post($layout_id);
     $layout['id'] = $layout_id;
     $layout['slug'] = $layout_post->post_name;
     // update changes
     WPDD_Layouts::save_layout_settings($layout_id, $layout);
     return $layout_id;
 }
 public static function get_post_ID_by_slug($slug, $post_type = 'post')
 {
     global $wpdb;
     if (!$slug) {
         return null;
     }
     if (WPDDL_LAYOUTS_POST_TYPE == $post_type) {
         return WPDD_Layouts_Cache_Singleton::get_id_by_name($slug);
     }
     return $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type=%s AND post_name=%s", $post_type, $slug));
 }
    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 get_layout_id_for_render($layout, $args = null)
 {
     $options = is_null($args) === false && is_array($args) === true ? (object) $args : false;
     $allow_overrides = $options && property_exists($options, 'allow_overrides') ? $options->allow_overrides : true;
     $id = 0;
     if ($layout) {
         $id = WPDD_Layouts_Cache_Singleton::get_id_by_name($layout);
     }
     if ($allow_overrides === true) {
         // when blog is front
         if (is_front_page() && is_home() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_BLOG)) {
             $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_BLOG);
             // when blog is not front
         } elseif (is_home() && !is_front_page() && !is_page() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_BLOG)) {
             $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_BLOG);
         } elseif (is_post_type_archive()) {
             $post_type_object = $this->get_queried_object();
             if ($post_type_object && property_exists($post_type_object, 'public') && $post_type_object->public && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_TYPES_PREFIX . $post_type_object->name)) {
                 $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_TYPES_PREFIX . $post_type_object->name);
             }
         } elseif (is_archive() && (is_tax() || is_category() || is_tag())) {
             $term = $this->get_queried_object();
             if ($term && property_exists($term, 'taxonomy') && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_TAXONOMY_PREFIX . $term->taxonomy)) {
                 $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_TAXONOMY_PREFIX . $term->taxonomy);
             }
         } elseif (is_search() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_SEARCH)) {
             $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_SEARCH);
         } elseif (is_author() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_AUTHOR)) {
             $author = WPDD_layout_post_loop_cell_manager::OPTION_AUTHOR;
             $id = $this->main->layout_post_loop_cell_manager->get_option($author);
         } elseif (is_year() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_YEAR)) {
             $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_YEAR);
         } elseif (is_month() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_MONTH)) {
             $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_MONTH);
         } elseif (is_day() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_DAY)) {
             $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_DAY);
         } elseif (is_404() && $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_404)) {
             $id = $this->main->layout_post_loop_cell_manager->get_option(WPDD_layout_post_loop_cell_manager::OPTION_404);
         } else {
             global $post;
             if ($post !== null) {
                 $post_id = $post->ID;
                 $layout_selected = get_post_meta($post_id, WPDDL_LAYOUTS_META_KEY, true);
                 if ($layout_selected) {
                     $id = WPDD_Layouts_Cache_Singleton::get_id_by_name($layout_selected);
                     $option = $this->main->post_types_manager->get_layout_to_type_object($post->post_type);
                     if (is_object($option) && property_exists($option, 'layout_id') && (int) $option->layout_id === (int) $id) {
                         $id = $option->layout_id;
                     }
                 }
             }
         }
     }
     return apply_filters('get_layout_id_for_render', (int) $id, $layout);
 }