/** * @param array $current_import * * @return bool */ function import(array $current_import) { try { $parser = new Parser(); $xml = $parser->parse($current_import['file']); } catch (\Exception $e) { return false; } $this->pbCheck($xml); if ($this->isPbWxr) { $xml['posts'] = $this->customNestedSort($xml['posts']); } $match_ids = array_flip(array_keys($current_import['chapters'])); $chapter_parent = $this->getChapterParent(); $total = 0; libxml_use_internal_errors(true); foreach ($xml['posts'] as $p) { // Skip if (!$this->flaggedForImport($p['post_id'])) { continue; } if (!isset($match_ids[$p['post_id']])) { continue; } // Insert $post_type = $this->determinePostType($p['post_id']); // Load HTMl snippet into DOMDocument using UTF-8 hack $utf8_hack = '<?xml version="1.0" encoding="UTF-8"?>'; $doc = new \DOMDocument(); $doc->loadHTML($utf8_hack . $this->tidy($p['post_content'])); // Download images, change image paths $doc = $this->scrapeAndKneadImages($doc); $html = $doc->saveXML($doc->documentElement); // Remove auto-created <html> <body> and <!DOCTYPE> tags. $html = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace(array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $html)); if ('metadata' == $post_type) { $pid = $this->bookInfoPid(); } else { $pid = $this->insertNewPost($post_type, $p, $html, $chapter_parent); if ('part' == $post_type) { $chapter_parent = $pid; } } if (isset($p['postmeta']) && is_array($p['postmeta'])) { $this->importPbPostMeta($pid, $post_type, $p); } Book::consolidatePost($pid, get_post($pid)); // Reorder ++$total; } $errors = libxml_get_errors(); // TODO: Handle errors gracefully libxml_clear_errors(); // Done $_SESSION['pb_notices'][] = sprintf(__('Imported %s chapters.', 'pressbooks'), $total); return $this->revokeCurrentImport(); }
/** * @param array $current_import * * @return bool */ function import(array $current_import) { try { $parser = new Parser(); $xml = $parser->parse($current_import['file']); } catch (\Exception $e) { return false; } $this->pbCheck($xml); if ($this->isPbWxr) { $xml['posts'] = $this->customNestedSort($xml['posts']); } $match_ids = array_flip(array_keys($current_import['chapters'])); $chapter_parent = $this->getChapterParent(); $total = 0; libxml_use_internal_errors(true); foreach ($xml['posts'] as $p) { // Skip if (!$this->flaggedForImport($p['post_id'])) { continue; } if (!isset($match_ids[$p['post_id']])) { continue; } // Insert $post_type = $this->determinePostType($p['post_id']); // Load HTMl snippet into DOMDocument using UTF-8 hack $utf8_hack = '<?xml version="1.0" encoding="UTF-8"?>'; $doc = new \DOMDocument(); $doc->loadHTML($utf8_hack . $this->tidy($p['post_content'])); // Download images, change image paths $doc = $this->scrapeAndKneadImages($doc); $html = $doc->saveXML($doc->documentElement); // Remove auto-created <html> <body> and <!DOCTYPE> tags. $html = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace(array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $html)); $new_post = array('post_title' => wp_strip_all_tags($p['post_title']), 'post_type' => $post_type, 'post_status' => 'part' == $post_type ? 'publish' : 'draft'); if ('part' != $post_type) { $new_post['post_content'] = $html; } if ('chapter' == $post_type) { $new_post['post_parent'] = $chapter_parent; } $pid = wp_insert_post(add_magic_quotes($new_post)); if ('part' == $post_type) { $chapter_parent = $pid; } $meta_to_update = apply_filters('pb_import_metakeys', array('pb_section_author', 'pb_section_license', 'pb_short_title', 'pb_subtitle')); if (isset($p['postmeta']) && is_array($p['postmeta'])) { foreach ($meta_to_update as $meta_key) { $meta_val = $this->searchForMetaValue($meta_key, $p['postmeta']); if (is_serialized($meta_val)) { $meta_val = unserialize($meta_val); } if ($meta_val) { update_post_meta($pid, $meta_key, $meta_val); } } if ('part' == $post_type) { $part_content = $this->searchForPartContent($p['postmeta']); if ($part_content) { update_post_meta($pid, 'pb_part_content', $part_content); } } } update_post_meta($pid, 'pb_show_title', 'on'); update_post_meta($pid, 'pb_export', 'on'); Book::consolidatePost($pid, get_post($pid)); // Reorder ++$total; } // Done $_SESSION['pb_notices'][] = sprintf(__('Imported %s chapters.', 'pressbooks'), $total); return $this->revokeCurrentImport(); }