/** * Upgrade metadata. * * @param int $version */ function upgrade($version) { if ($version < 1) { // Upgrade from version 0 (closed source service) to version 1 (initial open source offering) $this->upgradeEcommerce(); $this->upgradeBookInformation(); $this->upgradeBook(); } if ($version < 3) { $this->upgradeCustomCss(); } if ($version < 4) { $this->fixDoubleSlashBug(); } if ($version < 5) { $this->changeDefaultBookCover(); } if ($version < 6 || $version < 7) { $this->makeThumbnailsForBookCover(); } if ($version < 8) { $this->resetLandingPage(); } if ($version < 10) { \Pressbooks\Taxonomy::insertTerms(); flush_rewrite_rules(false); } }
/** * Adds front matter, resets the page numbering on the first loop, * romanizes the numeric style * * @param array $contents */ function addFrontMatter(array $contents) { $first_iteration = true; $page_options = array('pagenumstyle' => 'i', 'margin-left' => 15, 'margin-right' => 15); foreach ($contents as $front_matter) { // safety $type = \Pressbooks\Taxonomy::getFrontMatterType($front_matter['ID']); if ('dedication' == $type || 'epigraph' == $type || 'title-page' == $type || 'before-title' == $type) { continue; // Skip } // only reset the page number on first iteration true == $first_iteration ? $page_options['resetpagenum'] = 1 : ($page_options['resetpagenum'] = 0); // assumes the array of book contents is in order if ('front-matter' != $front_matter['post_type']) { return; } if (!empty($front_matter['post_content'])) { $this->addPage($front_matter, $page_options, true, true); $first_iteration = false; } } }
/** * Uses $this->manifest to generate itself. * * @param array $book_contents * @param array $metadata */ protected function createToc($book_contents, $metadata) { $vars = array('post_title' => '', 'stylesheet' => $this->stylesheet, 'post_content' => '', 'isbn' => @$metadata['pb_ebook_isbn'], 'lang' => $this->lang); $options = get_option('pressbooks_theme_options_global'); foreach (array('copyright_license') as $requiredGlobalOption) { if (!isset($options[$requiredGlobalOption])) { $options[$requiredGlobalOption] = 0; } } // Start by inserting self into correct manifest position $array_pos = $this->positionOfToc(); $file_id = 'table-of-contents'; $filename = "{$file_id}.{$this->filext}"; $vars['post_title'] = __('Table Of Contents', 'pressbooks'); $this->manifest = array_slice($this->manifest, 0, $array_pos + 1, true) + array($file_id => array('ID' => -1, 'post_title' => $vars['post_title'], 'filename' => $filename)) + array_slice($this->manifest, $array_pos + 1, count($this->manifest) - 1, true); // HTML $li_count = 0; $i = 1; $html = '<div id="toc"><h1>' . __('Contents', 'pressbooks') . '</h1><ul>'; foreach ($this->manifest as $k => $v) { // We only care about front-matter, part, chapter, back-matter // Skip the rest $subtitle = ''; $author = ''; $license = ''; $title = Sanitize\strip_br($v['post_title']); if (preg_match('/^front-matter-/', $k)) { $class = 'front-matter '; $class .= \Pressbooks\Taxonomy::getFrontMatterType($v['ID']); $subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true)); $author = trim(get_post_meta($v['ID'], 'pb_section_author', true)); $license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : ''; } elseif (preg_match('/^part-/', $k)) { $class = 'part'; if (get_post_meta($v['ID'], 'pb_part_invisible', true) == 'on') { $class .= ' display-none'; } } elseif (preg_match('/^chapter-/', $k)) { $class = 'chapter'; $class .= \Pressbooks\Taxonomy::getChapterType($v['ID']); $subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true)); $author = trim(get_post_meta($v['ID'], 'pb_section_author', true)); $license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : ''; if ($this->numbered && \Pressbooks\Taxonomy::getChapterType($v['ID']) !== 'numberless') { $title = " {$i}. " . $title; } if (\Pressbooks\Taxonomy::getChapterType($v['ID']) !== 'numberless') { ++$i; } } elseif (preg_match('/^back-matter-/', $k)) { $class = 'back-matter '; $class .= \Pressbooks\Taxonomy::getBackMatterType($v['ID']); $subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true)); $author = trim(get_post_meta($v['ID'], 'pb_section_author', true)); $license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : ''; } else { continue; } $html .= sprintf('<li class="%s"><a href="%s"><span class="toc-chapter-title">%s</span>', $class, $v['filename'], Sanitize\decode($title)); if ($subtitle) { $html .= ' <span class="chapter-subtitle">' . Sanitize\decode($subtitle) . '</span>'; } if ($author) { $html .= ' <span class="chapter-author">' . Sanitize\decode($author) . '</span>'; } if ($license) { $html .= ' <span class="chapter-license">' . $license . '</span> '; } $html .= '</a>'; if (\Pressbooks\Modules\Export\Export::isParsingSubsections() == true) { $sections = \Pressbooks\Book::getSubsections($v['ID']); if ($sections) { $html .= '<ul class="sections">'; foreach ($sections as $id => $title) { $html .= '<li class="section"><a href="' . $v['filename'] . '#' . $id . '"><span class="toc-subsection-title">' . Sanitize\decode($title) . '</span></a></li>'; } $html .= '</ul>'; } } $html .= "</li>\n"; ++$li_count; } if (0 == $li_count) { $html .= '<li></li>'; } $html .= "</ul></div>\n"; // Create file $vars['post_content'] = $html; file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/epub201/html.php', $vars)); }
/** * @param array $book_contents * @param array $metadata */ protected function echoBackMatter($book_contents, $metadata) { $back_matter_printf = '<div class="back-matter %s" id="%s">'; $back_matter_printf .= '<div class="back-matter-title-wrap"><h3 class="back-matter-number">%s</h3><h1 class="back-matter-title">%s</h1></div>'; $back_matter_printf .= '<div class="ugc back-matter-ugc">%s</div>%s%s'; $back_matter_printf .= '</div>'; $i = $s = 1; foreach ($book_contents['back-matter'] as $back_matter) { if (!$back_matter['export']) { continue; } $back_matter_id = $back_matter['ID']; $subclass = \Pressbooks\Taxonomy::getBackMatterType($back_matter_id); $slug = $back_matter['post_name']; $title = get_post_meta($back_matter_id, 'pb_show_title', true) ? $back_matter['post_title'] : '<span class="display-none">' . $back_matter['post_title'] . '</span>'; // Preserve auto-indexing in Prince using hidden span $content = $back_matter['post_content']; $append_back_matter_content = apply_filters('pb_append_back_matter_content', '', $back_matter_id); $short_title = trim(get_post_meta($back_matter_id, 'pb_short_title', true)); $subtitle = trim(get_post_meta($back_matter_id, 'pb_subtitle', true)); $author = trim(get_post_meta($back_matter_id, 'pb_section_author', true)); if (\Pressbooks\Modules\Export\Export::isParsingSubsections() == true) { $sections = \Pressbooks\Book::getSubsections($back_matter_id); if ($sections) { $content = \Pressbooks\Book::tagSubsections($content, $back_matter_id); } } if ($author) { $content = '<h2 class="chapter-author">' . Sanitize\decode($author) . '</h2>' . $content; } if ($subtitle) { $content = '<h2 class="chapter-subtitle">' . Sanitize\decode($subtitle) . '</h2>' . $content; } if ($short_title) { $content = '<h6 class="short-title">' . Sanitize\decode($short_title) . '</h6>' . $content; } printf($back_matter_printf, $subclass, $slug, $i, Sanitize\decode($title), $content, $append_back_matter_content, $this->doEndnotes($back_matter_id)); echo "\n"; ++$i; } }
/** * Set up default terms for Front Matter and Back Matter * Insert default part, chapter, front matter, and back matter * Insert default pages (Authors, Cover, TOC, About, Buy, and Access Denied) * Remove content generated by wp_install_defaults * Anything which needs to run on blog activation must go in this function */ private function wpmuActivate() { /** @var $wpdb \wpdb */ global $wpdb; \Pressbooks\Taxonomy::insertTerms(); $posts = array(array('post_title' => __('Main Body', 'pressbooks'), 'post_name' => 'main-body', 'post_type' => 'part', 'menu_order' => 1), array('post_title' => __('Introduction', 'pressbooks'), 'post_name' => 'introduction', 'post_content' => __('This is where you can write your introduction.', 'pressbooks'), 'post_type' => 'front-matter', 'menu_order' => 1), array('post_title' => __('Chapter 1', 'pressbooks'), 'post_name' => 'chapter-1', 'post_content' => __('This is the first chapter in the main body of the text. You can change the text, rename the chapter, add new chapters, and add new parts.', 'pressbooks'), 'post_type' => 'chapter', 'menu_order' => 1), array('post_title' => __('Appendix', 'pressbooks'), 'post_name' => 'appendix', 'post_content' => __('This is where you can add appendices or other back matter.', 'pressbooks'), 'post_type' => 'back-matter', 'menu_order' => 1), array('post_title' => __('Authors', 'pressbooks'), 'post_name' => 'authors', 'post_type' => 'page'), array('post_title' => __('Cover', 'pressbooks'), 'post_name' => 'cover', 'post_type' => 'page'), array('post_title' => __('Table of Contents', 'pressbooks'), 'post_name' => 'table-of-contents', 'post_type' => 'page'), array('post_title' => __('About', 'pressbooks'), 'post_name' => 'about', 'post_type' => 'page'), array('post_title' => __('Buy', 'pressbooks'), 'post_name' => 'buy', 'post_type' => 'page'), array('post_title' => __('Access Denied', 'pressbooks'), 'post_name' => 'access-denied', 'post_content' => __('This book is private, and accessible only to registered users. If you have an account you can login <a href="/wp-login.php">here</a>. You can also set up your own Pressbooks book at: <a href="http://pressbooks.com">Pressbooks.com</a>.', 'pressbooks'), 'post_type' => 'page'), array('post_title' => __('Custom CSS for Ebook', 'pressbooks'), 'post_name' => 'epub', 'post_type' => 'custom-css'), array('post_title' => __('Custom CSS for PDF', 'pressbooks'), 'post_name' => 'prince', 'post_type' => 'custom-css'), array('post_title' => __('Custom CSS for Web', 'pressbooks'), 'post_name' => 'web', 'post_type' => 'custom-css'), array('post_title' => __('Book Information', 'pressbooks'), 'post_name' => 'book-information', 'post_type' => 'metadata')); $post = array('post_status' => 'publish', 'comment_status' => 'open', 'post_author' => $this->user_id); $page = array('post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_content' => '<!-- Here be dragons.-->', 'post_author' => $this->user_id, 'tags_input' => __('Default Data', 'pressbooks')); update_option('blogdescription', __('Simple Book Production', 'pressbooks')); $parent_part = 0; $intro = 0; $appendix = 0; $chapter1 = 0; foreach ($posts as $item) { $exists = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s AND post_name = %s AND post_status = 'publish' ", array($item['post_title'], $item['post_type'], $item['post_name']))); if (empty($exists)) { if ('page' == $item['post_type']) { $data = array_merge($item, $page); } else { $data = array_merge($item, $post); } $newpost = wp_insert_post($data, true); if (!is_wp_error($newpost)) { switch ($item['post_name']) { case 'cover': $this->opts['page_on_front'] = (int) $newpost; break; case 'table-of-contents': $this->opts['page_for_posts'] = (int) $newpost; break; } if ('part' == $item['post_type']) { $parent_part = $newpost; } elseif ('chapter' == $item['post_type']) { $my_post = array(); $my_post['ID'] = $newpost; $my_post['post_parent'] = $parent_part; wp_update_post($my_post); $chapter1 = $newpost; } elseif ('front-matter' == $item['post_type']) { $intro = $newpost; } elseif ('back-matter' == $item['post_type']) { $appendix = $newpost; } elseif ('metadata' == $item['post_type']) { $metadata_id = $newpost; if (0 !== get_current_user_id()) { $user_info = get_userdata(get_current_user_id()); $name = $user_info->display_name; update_post_meta($metadata_id, 'pb_author', $name); } $locale = get_site_option('WPLANG'); if (!$locale) { $locale = 'en'; } else { $locale = array_search($locale, \Pressbooks\L10n\wplang_codes()); } update_post_meta($metadata_id, 'pb_title', get_option('blogname')); update_post_meta($metadata_id, 'pb_language', $locale); update_post_meta($metadata_id, 'pb_cover_image', \Pressbooks\Image\default_cover_url()); } } else { trigger_error($newpost->get_error_message(), E_USER_ERROR); } } } // Apply 'introduction' front matter type to 'introduction' post wp_set_object_terms($intro, 'introduction', 'front-matter-type'); // Apply 'appendix' front matter type to 'appendix' post wp_set_object_terms($appendix, 'appendix', 'back-matter-type'); // Apply 'standard' chapter type to 'chapter 1' post wp_set_object_terms($chapter1, 'standard', 'chapter-type'); // Remove content generated by wp_install_defaults if (!wp_delete_post(1, true)) { return; } if (!wp_delete_post(2, true)) { return; } if (!wp_delete_comment(1, true)) { return; } $this->opts['pb_activated'] = time(); refresh_blog_details($this->blog_id); }
/** * Get chapter, front or back matter type * * @param $post * * @return string */ function pb_get_section_type($post) { $type = $post->post_type; switch ($type) { case 'chapter': $type = \Pressbooks\Taxonomy::getChapterType($post->ID); break; case 'front-matter': $type = \Pressbooks\Taxonomy::getFrontMatterType($post->ID); break; case 'back-matter': $type = \Pressbooks\Taxonomy::getBackMatterType($post->ID); break; } return $type; }