/** * Imports options and fields from the XML file given. * @param $xmlFilename the file to import from. * @return boolean true on success, false on failure */ function importFromXML($xmlFilename) { //parse the xml file $xmlParser = new PhotoQXMLParser($xmlFilename); $xmlParser->parse(); //store the parsed options if they validate if ($xmlParser->validate()) { $optionArray = $xmlParser->getParsedOptions(); $fieldArray = $xmlParser->getParsedFields(); $catsArray = $xmlParser->getParsedCats(); //right now we only support one default category $defaultCat = null; if (!empty($catsArray)) { $defaultCat = $catsArray[0]; } //delete views that are no longer used in the new settings $this->_deleteObsoleteViews($this->getViewNames(), $xmlParser->getViewNames()); //delete image sizes that are no longer used in the new settings $this->_deleteObsoleteImageSizes($this->getImageSizeNames(), $xmlParser->getImageSizeNames()); //add custom fields required by views setting of xml file, //conflicts with fields will not happen as this is already tested in validation. foreach ($xmlParser->getViewNames(true) as $view) { $this->addViewCallback($view, true); } //it may happen that some fields are already there. Do not show an error in this case $this->_errStack->pushCallback(array('PhotoQErrorHandler', 'mutePHOTOQ_FIELD_EXISTS')); //create fields required by the xml file foreach ($fieldArray as $fieldname) { $this->_db->insertField($fieldname, true); } //add default tags to all photoq posts if (isset($optionArray['qPostDefaultTags']['qPostDefaultTags']) && !empty($optionArray['qPostDefaultTags']['qPostDefaultTags'])) { $newTags = preg_split("/[\\s]*,[\\s]*/", $optionArray['qPostDefaultTags']['qPostDefaultTags']); $postIDs = $this->_db->getAllPublishedPhotoIDs(); foreach ($postIDs as $id) { //update the tags in the database wp_set_post_tags($id, add_magic_quotes($newTags), true); } //update all posts in the queue $qEntries = $this->_db->getQueueIDTagPairs(); foreach ($qEntries as $entry) { $oldTags = preg_split("/[\\s]*,[\\s]*/", $entry->q_tags); $tagString = implode(',', array_unique(array_merge($newTags, $oldTags))); $this->_db->updateTags($entry->q_img_id, $tagString); } } //same for default category if ($defaultCat) { //create it if it does not exist if (!category_exists($defaultCat)) { $catID = wp_insert_category(array('cat_name' => $defaultCat)); if (is_wp_error($catID)) { $defaultCat = NULL; } } else { $catID = get_cat_id($defaultCat); } //get all posts and add default category to list of cats $postIDs = $this->_db->getAllPublishedPhotoIDs(); foreach ($postIDs as $id) { //update the tags in the database wp_set_object_terms($id, $defaultCat, 'category', true); } //update all posts in the queue if (!is_wp_error($catID)) { $qIds = $this->_db->getAllQueuedPhotoIDs(); foreach ($qIds as $id) { if (!in_array($catID, $this->_db->getCategoriesByImgId($id))) { $this->_db->insertCategory($id, $catID); } } } } //store the imported options to the database $storedOptions = get_option($this->_optionsDBName); foreach ($optionArray as $key => $val) { if (array_key_exists($key, $storedOptions)) { $storedOptions[$key] = PhotoQHelper::arrayHtmlEntities($val); } } update_option($this->_optionsDBName, $storedOptions); //reload to make the changes active $this->load(); return true; } return false; }