Beispiel #1
0
 /**
  * Gather the data that will be deployed
  *
  * @param string $raw - doesn't optimize the deploy data for transit
  * @return array
  */
 public function get_deploy_data($raw = false, $preflight = false)
 {
     $this->_deploy_data = array('taxonomies' => array(), 'users' => array(), 'post_types' => array(), 'menus' => array(), 'bookmarks' => array());
     foreach ($this->data as $key => $group) {
         switch ($key) {
             case 'post_types':
                 foreach ($group as $post_type => $post_ids) {
                     if (!empty($post_ids)) {
                         // start all our post objects
                         foreach ($post_ids as $post_id) {
                             try {
                                 $posts[$post_id] = new cfd_post($post_id);
                             } catch (Exception $e) {
                                 // no real error handling yet
                                 error_log('cfd_post object error: ' . $e->getMessage() . ' - ' . __FILE__ . '::' . __LINE__);
                             }
                         }
                         // sort the post objects
                         $post_type_object = get_post_type_object($post_type);
                         if ($post_type_object->hierarchical) {
                             $posts = $this->sort_posts_by_parent($posts);
                         }
                         //  process the post objects
                         foreach ($posts as $post) {
                             // pull post and distribute parts across the deploy data
                             $this->parse_post_for_deploy($post, $raw);
                         }
                         // cleanup
                         unset($posts);
                     }
                 }
                 break;
             case 'users':
                 if (!empty($group)) {
                     $this->_deploy_data['users'] = array_merge($this->_deploy_data['users'], $this->get_users_by_ids($group));
                 }
                 break;
             case 'taxonomies':
                 if (!empty($group)) {
                     foreach ($group as $tax_type => $term_ids) {
                         if (!empty($term_ids)) {
                             foreach ($this->get_terms($tax_type, $term_ids) as $slug => $term) {
                                 $this->_deploy_data['taxonomies'][$tax_type][$slug] = $term;
                             }
                         }
                     }
                 }
                 break;
             case 'bookmarks':
                 if (!empty($group)) {
                     foreach ($group as $bookmark_id) {
                         $bookmark = $this->get_bookmark($bookmark_id);
                         if (!empty($bookmark->link_category)) {
                             $link_cats = $bookmark->link_category;
                             $bookmark->link_category = array();
                             foreach ($link_cats as $key => $term) {
                                 $bookmark->link_category[] = $term->slug;
                                 $this->_deploy_data['taxonomies']['link_category'][$term->slug] = $term;
                             }
                         }
                         $this->_deploy_data['bookmarks'][$bookmark->link_url] = $bookmark;
                     }
                 }
                 break;
             case 'menus':
                 if (!empty($group)) {
                     foreach ($group as $menu_id) {
                         try {
                             $menu = new cfd_menu($menu_id);
                             if ($raw) {
                                 $this->_deploy_data['menus'][$menu->guid()] = $menu;
                             } else {
                                 $this->_deploy_data['menus'][$menu->guid()] = $menu->get_data_for_transit();
                             }
                         } catch (Exception $e) {
                             // no real error handling yet
                             error_log('cfd_menu object error: ' . $e->getMessage . ' - ' . __FILE__ . '::' . __LINE__);
                         }
                     }
                 }
                 break;
         }
     }
     // we have to wait to reorder taxonomies since post_types & bookmarks can add to the list of terms
     foreach ($this->_deploy_data['taxonomies'] as $tax_type => &$terms) {
         $taxonomy = get_taxonomy($tax_type);
         if ($taxonomy->hierarchical) {
             $terms = $this->sort_terms_by_parent($terms);
         }
     }
     // raw returns objects, not-raw returns data ready for transit
     if (!$raw) {
         array_walk_recursive($this->_deploy_data, array($this, 'object_to_array'));
         #array_walk_recursive($this->_deploy_data, array($this, 'trim_scalar'));
     }
     if ($preflight) {
         foreach ($this->_deploy_data['post_types'] as $post_type => $objects) {
             if (!empty($objects)) {
                 foreach ($objects as $key => $post_obj) {
                     $this->_deploy_data['post_types'][$post_type][$key]['post']['post_content'] = md5($post_obj['post']['post_content']);
                     if (!empty($post_obj['post']['post_excerpt'])) {
                         $this->_deploy_data['post_types'][$post_type][$key]['post']['post_excerpt'] = md5($post_obj['post']['post_excerpt']);
                     }
                 }
             }
         }
     }
     // make absolutely sure that attachments are last in the post_types array
     if (!empty($this->_deploy_data['post_types']['attachment'])) {
         $a = $this->_deploy_data['post_types']['attachment'];
         unset($this->_deploy_data['post_types']['attachment']);
         $this->_deploy_data['post_types']['attachment'] = $a;
     }
     return $this->_deploy_data;
 }
 protected function import_menu($menu)
 {
     $_menu = $menu['menu'];
     if (is_nav_menu($menu['menu']['slug'])) {
         $local_menu = wp_get_nav_menu_object($menu['menu']['slug']);
         $menu_id = $local_menu->term_id;
         $update = true;
         if (!empty($local_menu)) {
             $local_menu = new cfd_menu($menu['menu']['slug']);
         }
     } else {
         $menu_id = 0;
         $update = false;
     }
     $insert_menu_id = wp_update_nav_menu_object($menu_id, array('menu-name' => $menu['menu']['name'], 'description' => $menu['menu']['description'], 'slug' => $menu['menu']['slug']));
     if (is_wp_error($insert_menu_id)) {
         $this->add_import_message('menus', '__error__', sprintf(__('Menu import failed for menu "%s". Error: ', 'cf-deploy'), $menu['menu']['name']) . $insert_menu_id->get_error_message());
         return false;
     }
     // nuke existing menu items, trust me, its easier this way
     if ($update) {
         // Taken directly from wp_delete_nav_menu, wp-includes/nav-menu.php
         $menu_objects = get_objects_in_term($insert_menu_id, 'nav_menu');
         if (!empty($menu_objects)) {
             foreach ($menu_objects as $item) {
                 wp_delete_post($item);
             }
         }
     }
     // handle menu items
     if (!empty($menu['items'])) {
         $this->menu_item_parent_map = array();
         $processed_items = 0;
         foreach ($menu['items'] as $item) {
             if ($this->import_menu_item($item, $insert_menu_id)) {
                 $processed_items++;
             }
         }
     }
     // log rollback state
     $item_change['menus'][$menu['menu']['slug']] = 'new';
     if (!empty($local_menu)) {
         $item_change['menus'][$menu['menu']['slug']] = $local_menu->get_data_for_transit();
     }
     $this->log_item_change($item_change);
     return $processed_items == count($menu['items']);
 }