Beispiel #1
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');
     // Define layout parameters
     $layout['type'] = 'fluid';
     // layout_type
     $layout['cssframework'] = $wpddlayout->get_css_framework();
     $layout['template'] = '';
     $layout['parent'] = '';
     $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 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);
 }
 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;
 }
 public function create_layout_auto()
 {
     // verify permissions
     if (!current_user_can('manage_options') && WPDD_Layouts_Users_Profiles::user_can_create() && WPDD_Layouts_Users_Profiles::user_can_assign()) {
         die(__('Untrusted user', 'ddl-layouts'));
     }
     // verify nonce
     check_admin_referer('create_auto');
     // validate parameters
     $b_type = isset($_GET['type']) && preg_match('/^([-a-z0-9_]+)$/', $_GET['type']);
     $b_class = isset($_GET['class']) && preg_match('/^(archive|page)$/', $_GET['class']);
     $b_post_id = isset($_GET['post']) && (int) $_GET['post'] >= 0;
     // validate request
     if (!($b_type && $b_class && $b_post_id)) {
         die(__('Invalid parameters', 'ddl-layouts'));
     }
     // get parameters
     $type = $_GET['type'];
     $class = $_GET['class'];
     $post_id = (int) $_GET['post'];
     // enforce rules
     $b_page_archive = 'page' === $type && 'archive' === $class;
     if ($b_page_archive) {
         die(__('Not allowed', 'ddl-layouts'));
     }
     // prepare processing
     if ($post_id === 0) {
         $post_id = null;
     }
     $layout = null;
     $layout_id = 0;
     global $toolset_admin_bar_menu;
     $post_title = $toolset_admin_bar_menu->get_name_auto('layouts', $type, $class, $post_id);
     $title = sanitize_text_field(stripslashes_deep($post_title));
     $taxonomy = get_taxonomy($type);
     $is_tax = $taxonomy !== false;
     $post_type_object = get_post_type_object($type);
     $is_cpt = $post_type_object != null;
     /* Create a new Layout */
     global $wpddlayout;
     // Is there another Layout with the same name?
     $already_exists = $wpddlayout->does_layout_with_this_name_exist($title);
     if ($already_exists) {
         die(__('A layout with this name already exists. Please use a different name.', 'ddl-layouts'));
     }
     // Create a empty layout. No preset.
     // TODO: Pick the preset best suited (and check if Views is installed)
     $layout = $wpddlayout->create_layout(12, 'fluid');
     // Define layout parameters
     $layout['type'] = 'fluid';
     // layout_type
     $layout['cssframework'] = $wpddlayout->get_css_framework();
     $layout['template'] = '';
     $layout['parent'] = '';
     $layout['name'] = $title;
     $args = array('post_title' => $title, '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;
     // assign layout
     if ('archive' === $class) {
         if (preg_match('/^(home-blog|search|author|year|month|day)$/', $type)) {
             // Create a new Layout for X archives
             /* assign Layout to X archives */
             $layouts_wordpress_loop = sprintf('layouts_%s-page', $type);
             $wordpress_archive_loops = array($layouts_wordpress_loop);
             $wpddlayout->layout_post_loop_cell_manager->handle_archives_data_save($wordpress_archive_loops, $layout_id);
         } else {
             if ($is_tax) {
                 // Create a new Layout for Y archives
                 /* assign Layout to Y archives */
                 $layouts_taxonomy_loop = sprintf('layouts_taxonomy_loop_%s', $type);
                 $wordpress_archive_loops = array($layouts_taxonomy_loop);
                 $wpddlayout->layout_post_loop_cell_manager->handle_archives_data_save($wordpress_archive_loops, $layout_id);
             } else {
                 if ($is_cpt) {
                     // Create a new Layout for Z archives
                     /* assign Layout to Z archives */
                     $layouts_cpt = sprintf('layouts_cpt_%s', $type);
                     $wordpress_archive_loops = array($layouts_cpt);
                     $wpddlayout->layout_post_loop_cell_manager->handle_archives_data_save($wordpress_archive_loops, $layout_id);
                 } else {
                     die(__('An unexpected error happened.', 'ddl-layouts'));
                 }
             }
         }
     } else {
         if ('page' === $class) {
             if ('404' === $type) {
                 // Create a new Layout for Error 404 page
                 /* assign Layout to 404 page */
                 $wordpress_others_section = array('layouts_404_page');
                 $wpddlayout->layout_post_loop_cell_manager->handle_others_data_save($wordpress_others_section, $layout_id);
             } else {
                 if ('page' === $type) {
                     // Create a new Layout for 'Page Title'
                     /* assign Layout to Page */
                     $posts = array($post_id);
                     $wpddlayout->post_types_manager->update_post_meta_for_post_type($posts, $layout_id);
                 } else {
                     if ($is_cpt) {
                         // Create a new Layout for Ys
                         /* assign Layout to Y */
                         $post_types = array($type);
                         $wpddlayout->post_types_manager->handle_post_type_data_save($layout_id, $post_types, $post_types);
                         //$wpddlayout->post_types_manager->handle_set_option_and_bulk_at_once( $layout_id, $post_types, $post_types );
                     } else {
                         die(__('An unexpected error happened.', 'ddl-layouts'));
                     }
                 }
             }
         }
     }
     // update changes
     WPDD_Layouts::save_layout_settings($layout_id, $layout);
     // redirect to editor (headers already sent)
     $edit_link = $toolset_admin_bar_menu->get_edit_link('layouts', false, $type, $class, $layout_id);
     $exit_string = '<script type="text/javascript">' . 'window.location = "' . $edit_link . '";' . '</script>';
     exit($exit_string);
 }
 public function remove_orphaned_ct_cells($cell_type, $property)
 {
     $this->remapped = false;
     $this->cell_type = $cell_type;
     $this->property = $property;
     $rows = $this->get_rows();
     $rows = $this->remap_rows($rows);
     if (null !== $rows) {
         $this->layout->Rows = $rows;
         WPDD_Layouts::save_layout_settings($this->layout_id, $this->layout);
     }
     return $this->removed;
 }
 function create_layout_callback()
 {
     $nonce = $_POST["wpnonce"];
     if (!wp_verify_nonce($nonce, 'wp_nonce_create_layout')) {
         $result = array('error' => 'error', 'error_message' => __('Security check failed', 'ddl-layouts'));
     } else {
         // Check for duplicate layout name.
         $layout_name = str_replace('\\\\', '##DDL-SLASH##', $_POST['title']);
         $layout_name = stripslashes_deep($layout_name);
         $layout_name = str_replace('##DDL-SLASH##', '\\\\', $layout_name);
         if ($this->does_layout_with_this_name_exist($layout_name)) {
             $result = array('error' => 'error', 'error_message' => __('A layout with this name already exists. Please use a different name.', 'ddl-layouts'));
         } else {
             if (isset($_POST['extras'])) {
                 $extras = $_POST['extras'];
                 $extras_action = $extras['action'];
                 if ($extras_action === 'one') {
                     $assign_post = $extras['post_id'];
                 } elseif ($extras_action === 'all' && isset($extras['post_types'])) {
                     $types_to_batch = $extras['post_types'];
                 }
             }
             $parent_post_name = '';
             if ($_POST['layout_parent']) {
                 global $wpdb;
                 $parent_post_name = WPDD_Layouts_Cache_Singleton::get_name_by_id($_POST['layout_parent']);
             }
             if (isset($_POST['layout_preset']) && $_POST['layout_preset']) {
                 $layout = $this->load_layout($_POST['layout_preset'], $_POST['layout_type']);
             } else {
                 if ($_POST['layout_parent'] > 0) {
                     if ($this->get_css_framework() == 'bootstrap-3' || $this->get_css_framework() == 'bootstrap-2' && $_POST['layout_type'] == 'fluid') {
                         $_POST['columns'] = 12;
                     }
                 }
                 $layout = $this->create_layout($_POST['columns'], $_POST['layout_type']);
             }
             //When create child layout, get width from parent cell
             if ($_POST['layout_parent'] > 0 && isset($_POST['layout_preset']) && $_POST['layout_preset']) {
                 $parent_layout = $this->get_layout_from_id($_POST['layout_parent']);
                 $parent_layout_settings = $this->get_layout_settings($_POST['layout_parent'], true);
                 $width = $parent_layout->get_width_of_child_layout_cell();
                 if ($this->get_css_framework() == 'bootstrap-3' || $this->get_css_framework() == 'bootstrap-2' && $_POST['layout_type'] == 'fluid') {
                     $width = 12;
                 }
                 if (isset($_POST['layout_preset']) && $_POST['layout_preset']) {
                     $layout = $this->load_layout($_POST['layout_preset'], $_POST['layout_type']);
                 } else {
                     $layout = $this->create_layout($width, $_POST['layout_type']);
                 }
                 if ($this->get_css_framework() == 'bootstrap-2' && $_POST['layout_type'] == 'fluid') {
                     $layout['width'] = 12;
                 }
             }
             $layout['type'] = $_POST['layout_type'];
             $layout['cssframework'] = $this->get_css_framework();
             $layout['template'] = '';
             $layout['parent'] = $parent_post_name;
             $layout['name'] = $layout_name;
             $postarr = array('post_title' => $layout_name, 'post_content' => '', 'post_status' => 'publish', 'post_type' => WPDDL_LAYOUTS_POST_TYPE);
             $post_id = wp_insert_post($postarr);
             // force layout object to take right ID
             $layout_post = get_post($post_id);
             $layout['id'] = $post_id;
             $layout['slug'] = $layout_post->post_name;
             if (isset($extras_action) && $extras_action === 'all' && isset($types_to_batch) === false) {
                 if (isset($_POST['post_types'])) {
                     $post_types = count($_POST['post_types']) === 0 ? array() : array_unique($_POST['post_types']);
                 } else {
                     $post_types = array();
                 }
                 $this->post_types_manager->handle_post_type_data_save($post_id, $post_types);
             } else {
                 if (isset($extras_action) && $extras_action === 'all' && isset($types_to_batch)) {
                     $post_types = count($_POST['post_types']) === 0 ? array() : array_unique($_POST['post_types']);
                     $this->post_types_manager->handle_set_option_and_bulk_at_once($post_id, $post_types, $types_to_batch);
                 } else {
                     if (isset($extras_action) && $extras_action === 'one' && isset($assign_post)) {
                         $this->post_types_manager->update_post_meta_for_post_type(array($assign_post), $post_id);
                     }
                 }
             }
             //Set default parent
             if (isset($_POST['save_parent']) && $_POST['save_parent'] == 1) {
                 $this->parents_options->update_options(self::PARENTS_OPTIONS, $_POST['layout_parent']);
             }
             WPDD_Layouts::save_layout_settings($post_id, $layout);
             $result['id'] = $post_id;
         }
     }
     die(wp_json_encode($result));
 }
 private function purge_layout_children($children, $action)
 {
     global $wpddlayout;
     if (!is_array($children)) {
         return false;
     }
     $ret = array();
     foreach ($children as $child) {
         $id = intval($child);
         $layout = WPDD_Layouts::get_layout_settings($id, true);
         $layout->parent = '';
         WPDD_Layouts::save_layout_settings($id, $layout);
         if ($action === 'delete') {
             // We also need to delete grandchildren
             $layout = $wpddlayout->get_layout_from_id($id);
             $grand_children = $layout->get_children();
             $this->purge_layout_children($grand_children, $action);
             $wpddlayout->post_types_manager->purge_layout_post_type_data($id);
             $ret[] = wp_trash_post($id);
         }
     }
     return true;
 }