Example #1
0
  public function import() {
    global $wf;

    $base_dir = MASTERPRESS_TMP_DIR;
    $base_url = MASTERPRESS_TMP_URL;
    
    $restore = false;
    
    if (isset($_POST["import_masterplan"])) {
      $masterplan = $_POST["import_masterplan"];
    } else {
      if (isset($_POST["restore_masterplan"])) {
        $masterplan = $_POST["restore_masterplan"];
        $base_dir = MASTERPRESS_CONTENT_MASTERPLANS_DIR;
        $base_url = MASTERPRESS_CONTENT_MASTERPLANS_URL;
        $restore = true;
      }
    }
    
    
    $view = MasterPress::$view;
    
    if (isset($masterplan)) {
      
      extract(self::open_masterplan($masterplan, true, $base_dir, $base_url));
    
      if ($rep) {



        $icon_dest = MASTERPRESS_CONTENT_MENU_ICONS_DIR;
        $icon_src = $dir.WOOF_DIR_SEP."icons".WOOF_DIR_SEP;

        $field_type_dest = MASTERPRESS_EXTENSIONS_FIELD_TYPES_DIR;
        $field_type_src = $dir.WOOF_DIR_SEP."field-types".WOOF_DIR_SEP;
        
                   
        // install field type extensions
        
        foreach ($field_types as $field_type) {
          $dest = $field_type_dest.$field_type["name"];
          $src = $field_type_src.$field_type["name"];
          
          if (!(file_exists($dest) && !isset($_POST["import_types_overwrite"]))) {
            MPU::copyr($src, $dest);
          }
        
        }
        
        $info = array("log" => array());
          
        $mode = $_REQUEST["import_mode"];
        
        if ($restore) {
          $mode = "replace";
        }
        
        $log = &$info["log"];
        
        $backup = isset($_REQUEST["import_backup"]);

        $view->rep = $rep;
        $view->backup = $backup;
        $view->mode = $mode;
        
        // first obtain the current representation
        
        $current = self::get_current_rep();
        $view->current = $current;
      
        if ($backup) {
          
          // build the backup (a rollback point)
      
          $backup_filename = "_backup.pre-import.".$wf->format_date("[date-time-sortable]").".masterplan.zip";
          $backup_readme = "# Backup Masterplan for ".$wf->sites()->first()->name." #

+ Created: ".$wf->format_date("[date-time-long]");
        
          $backup_rep = $this->export_rep($current);
          $package = $this->build_package($backup_rep, $backup_filename, $backup_readme);
        
          if ($package) {
            $info["backup_package"] = $package;
            $info["backup_url"] = MASTERPRESS_CONTENT_MASTERPLANS_URL.$backup_filename;
            $info["backup_filename"] = $backup_filename;
          }
        
        }
        
        // process the representation

        // store all objects by key to look them up for potential removal (if mode is replace)
        
        $import = array(
          "post_types" => array(),
          "taxonomies" => array(),
          "shared_field_sets" => array(),
          "site_field_sets" => array(),
          "templates" => array(),
          "roles" => array()
        );

        $taxonomies_by_key = array();
        $shared_field_sets_by_key = array();
        $shared_field_sets_by_key = array();
        
        if (isset($rep["post_types"])) {
        
          foreach ($rep["post_types"] as $post_type) {
          
            $data = $post_type;
            unset($data["field_sets"], $data["id"], $data["sites"]);
          
            $name = $post_type["name"];
          
            $insert = true;
          
            if ($model = MPM_PostType::find_by_name($name)) {
              $insert = false;

            } else {
              $model = new MPM_PostType();
            }
            
            $model->set($data, true);
          
            if ($insert) {
              $model->insert();
            } else {
              $model->update();
            }
        
            // install icons
        
            if ($post_type["_builtin"] == "0" && !is_null($post_type["menu_icon"]) && $post_type["menu_icon"] != "") {
              copy($icon_src.$post_type["menu_icon"], $icon_dest.$post_type["menu_icon"]);
            }

            $import["post_types"][$name] = $post_type;
          
            // now run through the field sets, taking care to preserve 
            $import["post_types"][$name]["field_sets"] = self::import_field_sets($post_type["field_sets"], " AND `type` = 'p' AND ".MPM::visibility_rlike("post_types", $name), $dir);


          }
        
        }
        
        if (isset($rep["taxonomies"])) {
        
          foreach ($rep["taxonomies"] as $tax) {
          
            $data = $tax;
            unset($data["field_sets"], $data["id"], $data["sites"]);
          
            $name = $tax["name"];
            
            $insert = true;
          
            if ($model = MPM_Taxonomy::find_by_name($name)) {
              $insert = false;
            } else {
              $model = new MPM_Taxonomy();
            }
            
            $model->set($data, true);
          
            if ($insert) {
              $model->insert();
            } else {
              $model->update();
            }
      
            // install icons
        
            if ($tax["_builtin"] == "0" && !is_null($tax["title_icon"]) && $tax["title_icon"] != "") {
              copy($icon_src.$tax["title_icon"], $icon_dest.$tax["title_icon"]);
            }
          
            $import["taxonomies"][$name] = $tax;
          
            $import["taxonomies"][$name]["field_sets"] = self::import_field_sets($tax["field_sets"], " AND `type` = 'x' AND ".MPM::visibility_rlike("taxonomies", $name), $dir);
          
          }
          
        }

        if (isset($rep["shared_field_sets"])) {
          $import["shared_field_sets"] = self::import_field_sets($rep["shared_field_sets"], " AND `type` = 's' ", $dir);
        }

        if (isset($rep["site_field_sets"])) {
          $import["site_field_sets"] = self::import_field_sets($rep["site_field_sets"], " AND `type` = 'w' ", $dir);
        }
        
        if (isset($rep["templates"])) {
          foreach ($rep["templates"] as $template) {
            
            $id = $template["id"];
            
            // template models are self-creating, so no need for special handling here
            $model = MPM_Template::find_by_id($id);
            
            $data = $template;
            unset($data["field_sets"]);
            
            $model->set($data, true);
            $model->update();

            $import["templates"][$id] = $template;
            
            if (isset($template["field_sets"])) {
              $import["templates"][$id]["field_sets"] = self::import_field_sets($template["field_sets"], " AND `type` = 't' AND ".MPM::visibility_rlike("templates", $id), $dir);
            }

          
          }
        }

        if (isset($rep["roles"])) {
          foreach ($rep["roles"] as $role) {
            if (isset($role["field_sets"])) {
              $name = $role["name"];
              
              $import["roles"][$name] = $role;
              $import["roles"][$name]["field_sets"] = self::import_field_sets($role["field_sets"], " AND `type` = 'r' AND ".MPM::visibility_rlike("roles", $name), $dir);

            }
            
          }
        }
        
        
        if ($mode == "replace") {
          
          // we now need to remove anything in the current setup that ISN'T in the imported masterplan

          if (isset($current["post_types"])) {
            foreach ($current["post_types"] as $post_type) {
              
              $name = $post_type["name"];
              $builtin = $post_type["_builtin"] == "1";
              
              if (!isset($import["post_types"][$name]) && !$builtin) {
                
                $model = MPM_PostType::find_by_name($name);
    
                if ($model) {
                  $model->delete( array(
                    "posts" => "leave",
                    "posts_reassign_type" => "",
                    "field_sets" => "delete",
                    "field_data" => "keep"
                  ));
                }
                
              } else {
                
                if (isset($post_type["field_sets"])) {
                  self::replace_field_sets($post_type["field_sets"], $import["post_types"][$name]["field_sets"]);
                }
                
              }
              
            }
          }
          
          

          if (isset($current["taxonomies"])) {
            foreach ($current["taxonomies"] as $tax) {

              $name = $tax["name"];
              $builtin = $tax["_builtin"] == "1";
              
              if (!isset($import["taxonomies"][$name]) && !$builtin) {
                
                $model = MPM_Taxonomy::find_by_name($name);
                
                if ($model) {
                  $model->delete( array(
                    "existing_terms" => "leave",
                    "existing_terms_reassign_taxonomy" => "",
                    "field_sets" => "delete",
                    "field_data" => "leave"
                  ));
                }
                
              } else {
                if (isset($tax["field_sets"])) {
                  self::replace_field_sets($tax["field_sets"], $import["taxonomies"][$name]["field_sets"]);
                }
              
              }
              
            }
          }

          if (isset($current["shared_field_sets"])) {
            self::replace_field_sets($current["shared_field_sets"], $import["shared_field_sets"]);
          }

          if (isset($current["site_field_sets"])) {
            self::replace_field_sets($current["site_field_sets"], $import["site_field_sets"]);
          }

          if (isset($current["templates"])) {

            foreach ($current["templates"] as $template) {

              $id = $template["id"];

              if (isset($import["templates"][$id])) {
                if (isset($template["field_sets"])) {
                  self::replace_field_sets($template["field_sets"], $import["templates"][$id]["field_sets"]);
                }
              }
              
            }

          }


          if (isset($current["roles"])) {

            foreach ($current["roles"] as $name => $role) {

              if (isset($import["roles"][$name])) {
                  
                if (isset($role["field_sets"])) {
                  self::replace_field_sets($role["field_sets"], $import["roles"][$name]["field_sets"]);
                }
              }
              
            }
          }
          
        } // mode = replace
        
        
        // cleanup the unzipped folder
        
        MPU::rmdir_r($dir);
        
        if ($restore) {
          wp_redirect( MasterPress::admin_url( $this->key(), "manage", array("restore-complete" => "1"), false ) );
        } else {
          wp_redirect( MasterPress::admin_url( $this->key(), "manage", array("import-complete" => "1"), false ) );
        }
      
        
        $view->info = $info;
        
      } else {
        self::ajax_error(__("The Masterplan is no longer valid. Please try another file", MASTERPRESS_DOMAIN));
      }
      
    } else {
      self::ajax_error(__("The Masterplan is no longer valid. Please try another file", MASTERPRESS_DOMAIN));
    }

    
    
  }
Example #2
0
  public function submit() {
    
    global $wpdb;
    
    $action = MasterPress::$action;
    
    if ($action == "create" || $action == "edit") {

      $builtin = $_POST["_builtin"] == "true";
      $external = $_POST["_external"] == "true";
      
      if ($builtin) {

        $post_type = MPM_PostType::find_by_name($_POST["name"]);
        $post_type->supports = implode(",", $_POST["supports"]);
        $post_type->disabled = isset($_POST["disabled"]);
        $post_type->visibility = $this->get_visibility_val("sites");
        $post_type->columns = $_POST["columns"];
        $post_type->labels = $_POST["labels"];
        
        $post_type->menu_icon = self::handle_icon("menu_icon", "menu_icon_select");
        
        if ($_POST["name"] == "page" && $post_type->menu_icon == "") {
          $post_type->menu_icon = "menu-icon-pages.png"; // restore default icon
        }

        if ($_POST["name"] == "post" && $post_type->menu_icon == "") {
          $post_type->menu_icon = "menu-icon-posts.png"; // restore default icon
        }
        
        if ($action == "edit") {
          $post_type->update(MasterPress::$id);
        }
        

      } else if ($external) {

        $post_type = MPM_PostType::find_by_name($_POST["name"]);
        $post_type->columns = $_POST["columns"];
        $post_type->menu_icon = self::handle_icon("menu_icon", "menu_icon_select");
        
        if ($action == "edit") {
          $post_type->update(MasterPress::$id);
        }
        
      } else {


        $post_type = new MPM_PostType(true);
    
        // consume the post data
        $post_type->name = $_POST["name"];
        $post_type->plural_name = $_POST["plural_name"];
        $post_type->disabled = isset($_POST["disabled"]);
        $post_type->labels = $_POST["labels"];
        $post_type->description = $_POST["description"];
        $post_type->publicly_queryable = isset($_POST["publicly_queryable"]);
        $post_type->exclude_from_search = isset($_POST["exclude_from_search"]);
        $post_type->show_ui = isset($_POST["show_ui"]);
        $post_type->show_in_menu = isset($_POST["show_in_menu"]);
        $post_type->hierarchical = isset($_POST["hierarchical"]);
        $post_type->menu_position = $_POST["menu_position"];
        $post_type->menu_sub_position = $_POST["menu_sub_position"];
        
        $post_type->menu_icon = self::handle_icon("menu_icon", "menu_icon_select");
        
        $post_type->manage_sort_order = $_POST["manage_sort_order"];
        
        $cap_type = $_POST["capability_type"];
        
        if ($cap_type == "specific") {
          $post_type->capability_type = $_POST["name"];
        } else if ($cap_type == "custom" && trim($_POST["capability_type_custom_value"]) != "") {
          $post_type->capability_type = $_POST["capability_type_custom_value"];
        } else {
          $post_type->capability_type = $cap_type;
        }
      
        $post_type->capabilities = MPC::post_val("capabilities");
        
        $post_type->map_meta_cap = isset($_POST["map_meta_cap"]);
        $post_type->hierarchical = isset($_POST["hierarchical"]);
        $post_type->supports = MPC::post_implode_val("supports");
        $post_type->permalink_epmask = $_POST["permalink_epmask"];
        $post_type->has_archive = isset($_POST["has_archive"]);
        $post_type->visibility = $this->get_visibility_val("sites,post_types");

        $post_type->show_in_menu = isset($_POST["show_in_menu"]);

        $rewrite = array(
          "slug" => $_POST["rewrite"]["slug"],
          "with_front" => isset($_POST["rewrite"]["with_front"]),
          "feeds" => isset($_POST["rewrite"]["feeds"])
        );
        
        $post_type->rewrite = $rewrite;
      
        $post_type->query_var = $_POST["query_var"];
        $post_type->can_export = isset($_POST["can_export"]);
        $post_type->show_in_nav_menus = isset($_POST["show_in_nav_menus"]);
        $post_type->_builtin = false;
        $post_type->_external = $_POST["_external"] == "true";
        $post_type->columns = $_POST["columns"];

        if ($action == "create") {
          $post_type->insert();
        } else if ($action == "edit") {
          $post_type->update(MasterPress::$id);

          if ($post_type->is_valid()) {
            global $meow_provider;
            $meow_provider->migrate_post_type($post_type, $_POST["name_original"]);
          }

        }
        
        
      }
      
      if ($post_type->is_valid() && !$external) {

        // auto-generate a sprite icon
        MPU::create_icon_sprite($post_type->menu_icon, "", true);

        // attach any taxonomies and shared field sets
        
        $post_type->unlink_taxonomies();
        
        if (MPC::post_val("taxonomies") != "" && count(MPC::post_val("taxonomies"))) {
          $post_type->link_taxonomies( MPM_Taxonomy::find_by_name_in( $_POST["taxonomies"] ) );
        }
        
        // update the menu positions of other post types
        
        $omp = MPC::post_val("other_menu_position");
        $omsp = MPC::post_val("other_menu_sub_position");
        
        if (isset($omp) && is_array($omp)) {
          foreach ($omp as $name => $position) {
            $wpdb->update(MPM::table("post-types"), array( "menu_position" => $position, "menu_sub_position" => $omsp[$name] ), array( "name" => $name ), "%d", "%s" ); 
          }
        }

        MasterPress::flag_for_rewrite();

      } 
    
      return $post_type;
      
      
    } else if ($action == "delete") {

      
      $pt = MPM_PostType::find_by_id(MasterPress::$id);
      $pt->delete(
        array(
          "posts" => $_POST["posts"],
          "posts_reassign_type" => $_POST["posts_reassign_type"],
          "field_sets" => $_POST["field_sets"],
          "field_data" => $_POST["field_data"]
        )
      );
      
      MasterPress::flag_for_rewrite();

      return true;
      
    } else if ($action == "create-field-set" || $action == "edit-field-set") {

      $field_set = new MPM_FieldSet();
      // consume the post data
      
      $field_set->name = $_POST["name"];
      $field_set->singular_name = $_POST["singular_name"];
      $field_set->disabled = isset($_POST["disabled"]);
      $field_set->labels = $_POST["labels"];
      $field_set->allow_multiple = isset($_POST["allow_multiple"]);
      $field_set->visibility = $_POST["visibility"];
      $field_set->capabilities = self::handle_capabilities();
      $field_set->type = "p"; // p = shared
      $field_set->position = $_POST["position"];
      $field_set->icon = self::handle_icon("icon", "icon_select");
      $field_set->expanded = isset($_POST["expanded"]);
      $field_set->sidebar = isset($_POST["sidebar"]);
      $field_set->versions = $_POST["versions"];
      $field_set->visibility = $this->get_visibility_val("sites,templates,post_types");
      
      $post_type = MPM_PostType::find_by_id(MasterPress::$parent);
      
      // inform the validation of the post type
      $field_set->meta("post_type", $post_type);
      
      if (MPC::is_create()) {
        $field_set->insert();
      } else if (MPC::is_edit()) {
        $field_set->update(MasterPress::$id);
      }

      if ($field_set->is_valid()) {

        if (MPC::is_edit()) {
          global $meow_provider;
          $meow_provider->migrate_field_set_meta($field_set, $_POST["name_original"]);
        }

        // update other menu positions
        
				if (isset($_POST["other_position"])) {
	        $op = $_POST["other_position"];
        
	        if (isset($op) && is_array($op)) {
	          foreach ($op as $id => $position) {
	            $wpdb->update(MPM::table("field-sets"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); 
	          }
          
	        }
        }
        
      }        
      
      return $field_set;
      

    } else if ($action == "delete-field-set") {
      
      $fg = MPM_FieldSet::find_by_id(MasterPress::$id);
      
      if ($fg) {
        $field_data_action = $_POST["field_data"];
        if ($field_data_action == "delete") {
          $fg->delete_meta();
        }
      }
      
      $fg->delete();
      
      return true;
      
    } else if ($action == "delete-field") {
      
      $f = MPM_Field::find_by_id(MasterPress::$id);
      
      if ($f) {

        $field_data_action = $_POST["field_data"];
        
        if ($field_data_action == "delete") {
          $f->delete_meta();
        }

      }
      
      $f->delete();
      
      return true;
      
    } else if ($action == "create-field" || $action == "edit-field") {
      
      // FIELD OPERATIONS (NOT FIELD GROUPS!)
       
      $field = new MPM_Field();
      // consume the post data
      
      $field->field_set_id = $_POST["parent"];
      $field->name = $_POST["name"];
      $field->disabled = isset($_POST["disabled"]);
      $field->summary_options = $_POST["summary_options"];
      $field->required = isset($_POST["required"]);
      $field->labels = $_POST["labels"];
      $field->type = $_POST["type"]; 
      $field->icon = self::handle_icon("icon", "icon_select");
      
      if (isset($_POST["type_options"])) {
        $field->type_options = $_POST["type_options"];
      }
    
      $field->position = $_POST["position"];
      $field->visibility = $this->get_visibility_val("sites,templates,post_types");
      $field->capabilities = self::handle_capabilities();

      $fg = MPM_FieldSet::find_by_id($_POST["parent"]);
        
        
      if (MPC::is_create()) {
        $field->insert();
      } else if (MPC::is_edit()) {
        $field->update(MasterPress::$id);
      }

      if ($field->is_valid()) {

        // update other menu positions

        if (MPC::is_edit()) {
          global $meow_provider;
          $meow_provider->migrate_field_meta($field, $_POST["name_original"]);
        }
        
        if (isset($_POST["other_position"])) {
          $op = $_POST["other_position"];
        
          if (isset($op) && is_array($op)) {
            foreach ($op as $id => $position) {
              $wpdb->update(MPM::table("fields"), array( "position" => $position ), array( "id" => $id ), "%d", "%d" ); 
            }
          
          }
        }
        
        
      }     
      
      
      
      return $field;
      
      
    } 
    
    return false;
  }