Returns an array representing the entire structure of a book, in correct order,
with a minimum amount of fields. Data is raw and must be post-processed.
static public getBookStructure ( string $id = '' ) : array | ||
$id | string | |
return | array |
/** * Displays a Book widget */ function display_book_widget() { $book_structure = \PressBooks\Book::getBookStructure(); // front-matter echo "<ul><li><h4>" . __('Front Matter', 'pressbooks') . "</h4></li><ul>"; foreach ($book_structure['front-matter'] as $fm) { $title = !empty($fm['post_title']) ? $fm['post_title'] : '…'; echo "<li class='front-matter'><a href='post.php?post=" . $fm['ID'] . "&action=edit'>" . $title . "</a></li>\n"; } echo "</ul>"; // parts foreach ($book_structure['part'] as $part) { $title = !empty($part['post_title']) ? $part['post_title'] : '…'; echo "<ul><li><h4><a href='post.php?post=" . $part['ID'] . "&action=edit'>" . $title . "</a></h4></li><ul>\n"; // chapters foreach ($part['chapters'] as $chapter) { $title = !empty($chapter['post_title']) ? $chapter['post_title'] : '…'; echo "<li class='chapter'><a href='post.php?post=" . $chapter['ID'] . "&action=edit'>" . $title . "</a></li>\n"; } echo "</ul>\n"; } // back-matter echo "<li><h4>" . __('Back Matter', 'pressbooks') . "</h4></li><ul>"; foreach ($book_structure['back-matter'] as $bm) { $title = !empty($bm['post_title']) ? $bm['post_title'] : '…'; echo "<li class='back-matter'><a href='post.php?post=" . $bm['ID'] . "&action=edit'>" . $title . "</a></li>\n"; } echo "</ul>"; // add, organize echo "</ul>\n"; echo '<div class="part-buttons"><a href="post-new.php?post_type=chapter">' . __('Add', 'pressbooks') . '</a> | <a class="remove" href="admin.php?page=pressbooks">' . __('Organize', 'pressbooks') . '</a></div>'; }
function thincc_ajax() { $sitename = sanitize_key(get_bloginfo('name')); if (!empty($sitename)) { $sitename .= '.'; } $filename = $sitename . 'wordpress.' . date('Y-m-d'); $options = process_thincc_options($_POST); if (isset($_POST['download']) && $_POST['download'] == '0') { $options['version'] = 'thin'; $options['inline'] = true; $manifest = new \CC\Manifest(\PressBooks\Book::getBookStructure('', true), $options); $manifest->build_manifest(); header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename=' . $filename . '.xml'); header('Content-Type: text/plain; charset=' . get_option('blog_charset'), true); echo '<pre>', htmlentities($manifest), '</pre>'; } else { if (!isset($options['version'])) { $options['version'] = '1.2'; } $manifest = new \CC\Manifest(\PressBooks\Book::getBookStructure('', true), $options); $manifest->build_manifest(); $file = $manifest->build_zip(); header('Content-Type: application/vnd.ims.imsccv1p2+application/zip'); header('Content-Length: ' . filesize($file)); header('Content-Disposition: attachment; filename="' . $filename . '.zip"'); readfile($file); } }
/** * Fetch next or previous Pressbooks post ID * This is taken from PB's inner code to find the next page * * @param string $what prev, next * * @return ID of requested post */ function get_pb_page_id($what = 'next') { global $blog_id; global $post; $current_post_id = $post->ID; $book_structure = \PressBooks\Book::getBookStructure(); $order = $book_structure['__order']; $pos = array_keys($order); $what = $what == 'next' ? 'next' : 'prev'; // Move internal pointer to correct position reset($pos); while ($find_me = current($pos)) { if ($find_me == $current_post_id) { break; } else { next($pos); } } // Get next/previous $what($pos); while ($post_id = current($pos)) { if ($order[$post_id]['post_status'] == 'publish') { break; } elseif (current_user_can_for_blog($blog_id, 'read')) { break; } else { $what($pos); } } return $post_id; }
/** * Expose public information about a book * * @param array $args * @return array of book information */ protected function getBooksById(array $args) { $book = array(); if (empty($args['id'])) { foreach ($this->public_books as $book_id) { @$book[$book_id]; $book[$book_id]['book_id'] = $book_id; $book[$book_id]['book_url'] = get_blogaddress_by_id($book_id); $book[$book_id]['book_meta'] = \PressBooks\Book::getBookInformation(intval($book_id)); $book_structure = \PressBooks\Book::getBookStructure(intval($book_id)); $book[$book_id]['book_toc'] = $this->getToc($book_structure, $book_id); } } else { // check if blog_id is in the collection if (!in_array($args['id'], $this->public_books)) { return $this->apiErrors('empty'); } $book[$args['id']]['book_id'] = $args['id']; $book[$args['id']]['book_url'] = get_blogaddress_by_id($args['id']); $book[$args['id']]['book_meta'] = \PressBooks\Book::getBookInformation(intval($args['id'])); $book_structure = \PressBooks\Book::getBookStructure(intval($args['id'])); $book[$args['id']]['book_toc'] = $this->getToc($book_structure, $args['id']); } return $book; }
/** * Try to determine if a URL is pointing to internal content. * * @param $url * @param string $type front-matter, part, chapter, back-matter, ... * @param int $pos (optional) position of content, used when creating filenames like: chapter-001, chapter-002, ... * * @return bool|string */ protected function fuzzyHrefMatch($url, $type, $pos) { if (!$pos) { return false; } $url = trim($url); $url = rtrim($url, '/'); $last_part = explode('/', $url); $last_pos = count($last_part) - 1; $anchor = ''; // Look for #anchors if ($last_pos > 0 && '#' == substr(trim($last_part[$last_pos]), 0, 1)) { $anchor = trim($last_part[$last_pos]); $last_part = trim($last_part[$last_pos - 1]); } elseif (false !== strpos($last_part[$last_pos], '#')) { list($last_part, $anchor) = explode('#', $last_part[$last_pos]); $anchor = trim("#{$anchor}"); $last_part = trim($last_part); } else { $last_part = trim($last_part[$last_pos]); } if (!$last_part) { return false; } $lookup = \PressBooks\Book::getBookStructure(); $lookup = $lookup['__export_lookup']; if (!isset($lookup[$last_part])) { return false; } $domain = parse_url($url); $domain = @$domain['host']; if ($domain) { $domain2 = parse_url(wp_guess_url()); if ($domain != @$domain2['host']) { return false; } } // Seems legit... $new_type = $lookup[$last_part]; $new_pos = 0; foreach ($lookup as $p => $t) { if ($t == $new_type) { ++$new_pos; } if ($p == $last_part) { break; } } $new_url = "{$new_type}-" . sprintf("%03s", $new_pos) . "-{$last_part}.{$this->filext}"; if ($anchor) { $new_url .= $anchor; } return $new_url; }
/** * Get "real" chapter number * * @param $post_name * * @return int */ function pb_get_chapter_number($post_name) { $options = get_option('pressbooks_theme_options_global'); if (!@$options['chapter_numbers']) { return 0; } $lookup = \PressBooks\Book::getBookStructure(); $lookup = $lookup['__export_lookup']; if ('chapter' != @$lookup[$post_name]) { return 0; } $i = 0; foreach ($lookup as $key => $val) { if ('chapter' == $val) { $chapter = get_posts(array('name' => $key, 'post_type' => 'chapter', 'post_status' => 'publish', 'numberposts' => 1)); if (isset($chapter[0])) { $type = pb_get_section_type($chapter[0]); if ($type !== 'numberless') { ++$i; } } else { return 0; } if ($key == $post_name) { break; } } } if ($type == 'numberless') { $i = 0; } return $i; }
/** * Upgrade book metadata. */ function upgradeBook() { $book_structure = Book::getBookStructure(); foreach ($book_structure['__order'] as $post_id => $_) { $meta = get_post_meta($post_id); $compare = $this->getDeprecatedComparisonTable(get_post_type($post_id)); foreach ($meta as $meta_key => $meta_value) { $new_meta_key = @$compare[$meta_key]; if ($new_meta_key) { $meta_id = $this->getMidByKey($post_id, $meta_key); if ($meta_id) { if (isset($this->upgradeCheckboxes[$meta_key])) { $meta_value = 'on'; } elseif (is_array($meta_value)) { $meta_value = array_values($meta_value); $meta_value = array_pop($meta_value); } // Updating [$meta_key] to [$new_meta_key] update_metadata_by_mid('post', $meta_id, $meta_value, $new_meta_key); } } } } }
/** * Try to determine if a URL is pointing to internal content. TODO: Refactor, for the love of all that is holy. * * @param $url * @param string $type front-matter, part, chapter, back-matter, ... * @param int $pos (optional) position of content, used when creating filenames like: chapter-001, chapter-002, ... * * @return bool|string */ protected function fuzzyHrefMatch($url, $type, $pos) { if (!$pos) { return false; } $url = trim($url); $url = rtrim($url, '/'); $domain = parse_url($url); $domain = @$domain['host']; if ($domain) { $domain2 = parse_url(wp_guess_url()); if (@$domain2['host'] != $domain) { return false; // If there is a domain name and it =/= ours, bail. } } $last_part = explode('/', $url); $last_pos = count($last_part) - 1; $posttype = @$last_part[$last_pos - 1]; $anchor = ''; // Look for #anchors if ($last_pos > 0 && '#' == substr(trim($last_part[$last_pos]), 0, 1)) { $anchor = trim($last_part[$last_pos]); $last_part = trim($last_part[$last_pos - 1]); } elseif (false !== strpos($last_part[$last_pos], '#')) { list($last_part, $anchor) = explode('#', $last_part[$last_pos]); $anchor = trim("#{$anchor}"); $last_part = trim($last_part); } else { $last_part = trim($last_part[$last_pos]); } if (!$last_part) { return false; } $lookup = \Pressbooks\Book::getBookStructure(); if ('part' !== $posttype && !isset($lookup['__export_lookup'][$last_part])) { return false; } elseif ('part' !== $posttype && isset($lookup['__export_lookup'][$last_part])) { // Handle front/back matter and chapters $new_type = $lookup['__export_lookup'][$last_part]; $new_pos = 0; foreach ($lookup['__export_lookup'] as $p => $t) { if ($t == $new_type) { ++$new_pos; } if ($p == $last_part) { break; } } $new_url = "{$new_type}-" . sprintf('%03s', $new_pos) . "-{$last_part}.{$this->filext}"; } elseif ('part' == $posttype && !isset($lookup['__export_lookup'][$last_part])) { // Handle parts $new_type = 'part'; foreach ($lookup['part'] as $key => $part) { if ($part['post_name'] == $last_part) { $new_url = 'part-' . sprintf('%03s', $key + 1) . "-{$last_part}.{$this->filext}"; } } } if ($anchor) { $new_url .= $anchor; } return $new_url; }
<?php if (!defined('ABSPATH')) { exit; } /* Outputs the content of the Organize page for a book */ global $user_ID; $statuses = get_post_statuses(); $book_structure = \Pressbooks\Book::getBookStructure(); $book_is_public = 1 == get_option('blog_public'); ?> <div class="wrap"> <?php if (current_user_can('manage_options')) { ?> <div id="publicize-panel" class="postbox"> <div class="inside"> <?php if ($book_is_public) { ?> <h4 class="publicize-alert public"><?php _e('This book\'s global privacy is set to', 'pressbooks'); ?> <span><?php _e('Public', 'pressbooks'); ?> </span></h4> <?php } else { ?>