Ensures this chapter/part/front matter has a "menu_order" when it is saved
static public consolidatePost ( integer $pid, WP_Post $post ) : boolean | ||
$pid | integer | Post ID |
$post | WP_Post | Post |
return | boolean |
/** * Imports user selected chapters from an instance of PB * * @param array $chapters * Array( [5] => Array( [222] => chapter ) [14] => Array( [164] => front-matter ) ) * @return type */ function import(array $chapters) { $this->chapters = $chapters; $chapters_to_import = $this->getChapters(); libxml_use_internal_errors(true); foreach ($chapters_to_import as $new_post) { // 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 . $new_post['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)); $import_post = array('post_title' => $new_post['post_title'], 'post_content' => $html, 'post_type' => $new_post['post_type'], 'post_status' => $new_post['post_status']); // set post parent if ('chapter' == $new_post['post_type']) { $post_parent = $this->getChapterParent(); $import_post['post_parent'] = $post_parent; } // woot, woot! $pid = wp_insert_post($import_post); // check for errors, redirect and record if (is_wp_error($pid)) { error_log('\\PBT\\Import\\PBImport()->import error at `wp_insert_post()`: ' . $pid->get_error_message()); \PBT\Search\ApiSearch::revokeCurrentImport(); \Pressbooks\Redirect\location(get_bloginfo('url') . '/wp-admin/admin.php?page=api_search_import'); } // set post metadata $this->setPostMeta($pid, $new_post); \Pressbooks\Book::consolidatePost($pid, get_post($pid)); } return \PBT\Search\ApiSearch::revokeCurrentImport(); }
/** * @param array $current_import */ function import(array $current_import) { try { $imscc = new IMSCCParser($current_import['file']); } catch (\Exception $e) { return FALSE; } $items = $imscc->manifestGetItems(); $match_ids = array_flip(array_keys($current_import['chapters'])); $total = 0; if (!empty($items)) { $current_post_parent = -1; foreach ($items as $id => $item) { // Skip if (!$this->flaggedForImport($id)) { continue; } if (!isset($match_ids[$id])) { continue; } $post_type = $this->determinePostType($id); $new_post = array('post_title' => wp_strip_all_tags($item['title']), 'post_type' => $post_type, 'post_status' => 'part' == $post_type ? 'publish' : 'draft'); if ('part' != $post_type) { $new_post['post_content'] = $imscc->getContent($id); } if ('chapter' == $post_type) { if ($current_post_parent == -1) { $new_post['post_parent'] = $this->getChapterParent(); } else { $new_post['post_parent'] = $current_post_parent; } } $pid = wp_insert_post(add_magic_quotes($new_post)); //store part post ID to use as parent for subsequent chapters if ('part' == $post_type) { $current_post_parent = $pid; } // @todo postmeta like author update_post_meta($pid, 'pb_show_title', 'on'); update_post_meta($pid, 'pb_export', 'on'); if ('part' == $post_type && $imscc->getContent($id)) { update_post_meta($pid, 'pb_part_content', $imscc->getContent($id)); } Book::consolidatePost($pid, get_post($pid)); ++$total; } } // Done $_SESSION['pb_notices'][] = sprintf(__('Imported %d chapters.', 'pressbooks'), $total); $imscc->cleanUp(); return $this->revokeCurrentImport(); }
/** * Pummel then insert HTML into our database * * @param string $href * @param string $post_type * @param int $chapter_parent * @param string $domain domain name of the webpage */ function kneadandInsert($html, $post_type, $chapter_parent, $domain) { $matches = array(); $meta = $this->getLicenseAttribution($html); $author = isset($meta['authors']) ? $meta['authors'] : $this->getAuthors($html); $license = isset($meta['license']) ? $this->extractCCLicense($meta['license']) : ''; // get the title, preference to title set by PB preg_match('/<h2 class="entry-title">(.*)<\\/h2>/', $html, $matches); if (!empty($matches[1])) { $title = wp_strip_all_tags($matches[1]); } else { preg_match('/<title>(.+)<\\/title>/', $html, $matches); $title = !empty($matches[1]) ? wp_strip_all_tags($matches[1]) : '__UNKNOWN__'; } // just get the body preg_match('/(?:<body[^>]*>)(.*)<\\/body>/isU', $html, $matches); // get rid of stuff we don't need $body = $this->regexSearchReplace($matches[1]); // clean it up $xhtml = $this->tidy($body); $body = $this->kneadHtml($xhtml, $post_type, $domain); $new_post = array('post_title' => $title, 'post_content' => $body, 'post_type' => $post_type, 'post_status' => 'draft'); if ('chapter' == $post_type) { $new_post['post_parent'] = $chapter_parent; } $pid = wp_insert_post(add_magic_quotes($new_post)); if (!empty($author)) { update_post_meta($pid, 'pb_section_author', $author); } if (!empty($license)) { update_post_meta($pid, 'pb_section_license', $license); } update_post_meta($pid, 'pb_show_title', 'on'); update_post_meta($pid, 'pb_export', 'on'); Book::consolidatePost($pid, get_post($pid)); // Reorder }
/** * Pummel then insert HTML into our database * * @param string $html * @param string $title * @param string $post_type (front-matter', 'chapter', 'back-matter') * @param int $chapter_parent */ protected function kneadAndInsert($html, $title, $post_type, $chapter_parent) { $body = $this->tidy($html); $body = $this->kneadHTML($body); $title = wp_strip_all_tags($title); $new_post = array('post_title' => $title, 'post_content' => $body, 'post_type' => $post_type, 'post_status' => 'draft'); if ('chapter' == $post_type) { $new_post['post_parent'] = $chapter_parent; } $pid = wp_insert_post(add_magic_quotes($new_post)); update_post_meta($pid, 'pb_show_title', 'on'); update_post_meta($pid, 'pb_export', 'on'); Book::consolidatePost($pid, get_post($pid)); // Reorder }
/** * @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(); }
/** * Pummel then insert HTML into our database * * @param string $href * @param string $post_type * @param int $chapter_parent */ protected function kneadAndInsert($href, $post_type, $chapter_parent) { $html = $this->getZipContent($href, false); $matches = array(); preg_match('/(?:<title[^>]*>)(.+)<\\/title>/isU', $html, $matches); $title = !empty($matches[1]) ? wp_strip_all_tags($matches[1]) : '__UNKNOWN__'; preg_match('/(?:<body[^>]*>)(.*)<\\/body>/isU', $html, $matches); $body = $this->tidy(@$matches[1]); $body = $this->kneadHtml($body, $post_type, $href); $new_post = array('post_title' => $title, 'post_content' => $body, 'post_type' => $post_type, 'post_status' => 'draft'); if ('chapter' == $post_type) { $new_post['post_parent'] = $chapter_parent; } $pid = wp_insert_post(add_magic_quotes($new_post)); update_post_meta($pid, 'pb_show_title', 'on'); update_post_meta($pid, 'pb_export', 'on'); Book::consolidatePost($pid, get_post($pid)); // Reorder }
/** * @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(); }
/** * @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; $taxonomies = apply_filters('pb_import_custom_taxonomies', array('front-matter-type', 'chapter-type', 'back-matter-type')); $custom_post_types = apply_filters('pb_import_custom_post_types', array()); // set custom terms... $terms = apply_filters('pb_import_custom_terms', $xml['terms']); // and import them if they don't already exist. foreach ($terms as $t) { $term = term_exists($t['term_name'], $t['term_taxonomy']); if (null === $term || 0 === $term) { wp_insert_term($t['term_name'], $t['term_taxonomy'], array('description' => $t['term_description'], 'slug' => $t['slug'])); } } 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 this is a custom post type, // and it has terms associated with it... if (in_array($post_type, $custom_post_types) && true == $p['terms']) { // associate post with terms. foreach ($p['terms'] as $t) { if (in_array($t['domain'], $taxonomies)) { wp_set_object_terms($pid, $t['slug'], $t['domain'], true); } } } 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(); }