getBookInformation() static public method

Returns book information in a useful, string only, format. Data is converted to HTML.
static public getBookInformation ( $id = '' ) : array
return array
Beispiel #1
0
/**
 * Hook for add_filter('locale ', ...), change the user interface language
 *
 * @param string $lang
 *
 * @return string
 */
function set_locale($lang)
{
    // Cheap cache
    static $loc = '__UNSET__';
    if (is_admin()) {
        // go with the user setting
        // get_current_user_id uses wp_get_current_user which may not be available the first time(s) get_locale is called
        if ('__UNSET__' == $loc && function_exists('wp_get_current_user')) {
            $loc = get_user_option('user_interface_lang');
        }
    } elseif ($GLOBALS['pagenow'] == 'wp-signup.php') {
        // use global setting
        $loc = get_site_option('WPLANG');
    } else {
        // go with the book info setting
        $metadata = \PressBooks\Book::getBookInformation();
        if ('__UNSET__' == $loc && !empty($metadata['pb_language'])) {
            $locations = \PressBooks\L10n\wplang_codes();
            $loc = $locations[$metadata['pb_language']];
        }
    }
    // Return
    if ('__UNSET__' == $loc) {
        return $lang;
    } else {
        return $loc ? $loc : $lang;
    }
}
function pbt_get_microdata_meta_elements()
{
    // map items that are already captured
    $html = '';
    $micro_mapping = array('about' => 'pb_bisac_subject', 'alternativeHeadline' => 'pb_subtitle', 'author' => 'pb_author', 'contributor' => 'pb_contributing_authors', 'copyrightHolder' => 'pb_copyright_holder', 'copyrightYear' => 'pb_copyright_year', 'datePublished' => 'pb_publication_date', 'description' => 'pb_about_50', 'editor' => 'pb_editor', 'image' => 'pb_cover_image', 'inLanguage' => 'pb_language', 'keywords' => 'pb_keywords_tags', 'publisher' => 'pb_publisher');
    $metadata = \PressBooks\Book::getBookInformation();
    // create microdata elements
    foreach ($micro_mapping as $itemprop => $content) {
        if (array_key_exists($content, $metadata)) {
            if ('pb_publication_date' == $content) {
                $content = date('Y-m-d', $metadata[$content]);
            } else {
                $content = $metadata[$content];
            }
            $html .= "<meta itemprop='" . $itemprop . "' content='" . $content . "' id='" . $itemprop . "'>\n";
        }
    }
    // add elements that aren't captured, and don't need user input
    $pb_bisac_subject = '';
    // Pressbooks Metadata: fixes the undefined index pb_bisac_subject notice
    if (isset($metadata['pb_bisac_subject'])) {
        $pb_bisac_subject = $metadata['pb_bisac_subject'];
    }
    $lrmi_meta = array('educationalAlignment' => $pb_bisac_subject, 'educationalUse' => 'Open textbook study', 'audience' => 'student', 'interactivityType' => 'mixed', 'learningResourceType' => 'textbook', 'typicalAgeRange' => '17-');
    foreach ($lrmi_meta as $itemprop => $content) {
        // @todo parse educationalAlignment items into alignmentOjects
        $html .= "<meta itemprop='" . $itemprop . "' content='" . $content . "' id='" . $itemprop . "'>\n";
    }
    return $html;
}
function pbt_get_microdata_meta_elements()
{
    // map items that are already captured
    $html = '';
    $micro_mapping = array('about' => 'pb_bisac_subject', 'alternativeHeadline' => 'pb_subtitle', 'author' => 'pb_author', 'contributor' => 'pb_contributing_authors', 'copyrightHolder' => 'pb_copyright_holder', 'copyrightYear' => 'pb_copyright_year', 'datePublished' => 'pb_publication_date', 'description' => 'pb_about_50', 'editor' => 'pb_editor', 'image' => 'pb_cover_image', 'inLanguage' => 'pb_language', 'keywords' => 'pb_keywords_tags', 'publisher' => 'pb_publisher');
    $metadata = \PressBooks\Book::getBookInformation();
    // create microdata elements
    foreach ($micro_mapping as $itemprop => $content) {
        if (array_key_exists($content, $metadata)) {
            if ('pb_publication_date' == $content) {
                $content = date('Y-m-d', $metadata[$content]);
            } else {
                $content = $metadata[$content];
            }
            $html .= "<meta itemprop='" . $itemprop . "' content='" . $content . "' id='" . $itemprop . "'>\n";
        }
    }
    return $html;
}
 /**
  * Create $this->outputPath
  *
  * @return bool
  */
 function convert()
 {
     // Create ICML
     $vars = array('meta' => \PressBooks\Book::getBookInformation(), 'book_contents' => $this->preProcessBookContents(\PressBooks\Book::getBookContents()));
     $cc_copyright = strip_tags($this->doCopyrightLicense($vars['meta']));
     $vars['do_copyright_license'] = $cc_copyright;
     $book_html = $this->loadTemplate(__DIR__ . '/templates/xhtml.php', $vars);
     $content = $this->transformXML($book_html, PB_PLUGIN_DIR . 'symbionts/icml/tkbr2icml-v044.xsl');
     // Save ICML as file in exports folder
     $filename = $this->timestampedFileName('.icml');
     file_put_contents($filename, $content);
     $this->outputPath = $filename;
     if (!filesize($this->outputPath)) {
         $this->logError($this->bookHtmlError($book_html));
         unlink($this->outputPath);
         return false;
     }
     return true;
 }
 /**
  * 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;
 }
 /**
  * Create $this->outputPath
  *
  * @return bool
  */
 function convert()
 {
     // Sanity check
     if (empty($this->tmpDir) || !is_dir($this->tmpDir)) {
         $this->logError('$this->tmpDir must be set before calling convert().');
         return false;
     }
     if (empty($this->exportStylePath) || !is_file($this->exportStylePath)) {
         $this->logError('$this->exportStylePath must be set before calling convert().');
         return false;
     }
     // Convert
     $metadata = \PressBooks\Book::getBookInformation();
     $book_contents = $this->preProcessBookContents(\PressBooks\Book::getBookContents());
     try {
         $this->createContainer();
         $this->createOEPBS($book_contents, $metadata);
         $this->createOPF($book_contents, $metadata);
         $this->createNCX($book_contents, $metadata);
     } catch (\Exception $e) {
         $this->logError($e->getMessage());
         return false;
     }
     $filename = $this->timestampedFileName($this->suffix);
     if (!$this->zipEpub($filename)) {
         return false;
     }
     $this->outputPath = $filename;
     return true;
 }
 /**
  *
  * @param array $args
  */
 function __construct()
 {
     // don't know who would actually wait for 10 minutes, but it's here
     set_time_limit(600);
     $memory_available = (int) ini_get('memory_limit');
     // lives and dies with the instantiation of the object
     if ($memory_available < $this->memoryNeeded) {
         ini_set('memory_limit', $this->memoryNeeded . 'M');
     }
     $this->options = get_option('pressbooks_theme_options_mpdf');
     $this->globalOptions = get_option('pressbooks_theme_options_global');
     $this->bookTitle = get_bloginfo('name');
     $this->exportStylePath = $this->getExportStylePath('mpdf');
     $this->bookMeta = \PressBooks\Book::getBookInformation();
     $this->numbered = 1 == $this->globalOptions['chapter_numbers'] ? true : false;
 }
 /**
  * Procedure for "format/xhtml" rewrite rule.
  */
 function transform()
 {
     // Check permissions
     if (!current_user_can('manage_options')) {
         $timestamp = absint(@$_REQUEST['timestamp']);
         $hashkey = @$_REQUEST['hashkey'];
         if (!$this->verifyNonce($timestamp, $hashkey)) {
             wp_die(__('Invalid permission error', 'pressbooks'));
         }
     }
     // Override footnote shortcode
     if (!empty($_GET['endnotes'])) {
         add_shortcode('footnote', array($this, 'endnoteShortcode'));
     } else {
         add_shortcode('footnote', array($this, 'footnoteShortcode'));
     }
     // ------------------------------------------------------------------------------------------------------------
     // XHTML, Start!
     $metadata = \PressBooks\Book::getBookInformation();
     $book_contents = $this->preProcessBookContents(\PressBooks\Book::getBookContents());
     $this->echoDocType($book_contents, $metadata);
     echo "<head>\n";
     echo '<meta content="text/html; charset=UTF-8" http-equiv="content-type" />' . "\n";
     echo '<base href="' . trailingslashit(site_url('', 'http')) . '" />' . "\n";
     $this->echoMetaData($book_contents, $metadata);
     echo '<title>' . get_bloginfo('name') . "</title>\n";
     echo "</head>\n<body>\n";
     // Before Title Page
     $this->echoBeforeTitle($book_contents, $metadata);
     // Half-title
     $this->echoHalfTitle($book_contents, $metadata);
     // Cover
     $this->echoCover($book_contents, $metadata);
     // Title
     $this->echoTitle($book_contents, $metadata);
     // Copyright
     $this->echoCopyright($book_contents, $metadata);
     // Dedication and Epigraph (In that order!)
     $this->echoDedicationAndEpigraph($book_contents, $metadata);
     // Table of contents
     $this->echoToc($book_contents, $metadata);
     // Front-matter
     $this->echoFrontMatter($book_contents, $metadata);
     // Promo
     $this->createPromo($book_contents, $metadata);
     // Parts, Chapters
     $this->echoPartsAndChapters($book_contents, $metadata);
     // Back-matter
     $this->echoBackMatter($book_contents, $metadata);
     // XHTML, Stop!
     echo "</body>\n</html>";
 }
Beispiel #9
0
/**
 * Get path to hyphenation dictionary in a book's language.
 *
 * @return bool|string
 */
function pb_get_hyphens_path()
{
    $loc = false;
    $compare_with = scandir(PB_PLUGIN_DIR . '/symbionts/dictionaries/');
    $book_lang = \PressBooks\Book::getBookInformation();
    $book_lang = @$book_lang['pb_language'];
    foreach ($compare_with as $compare) {
        if (strpos($compare, 'hyph_') !== 0) {
            continue;
        }
        // Skip
        $c = str_replace('hyph_', '', $compare);
        list($check_me) = explode('_', $c);
        // We only care about the first two letters
        if (strpos($book_lang, $check_me) === 0) {
            $loc = $compare;
            break;
        }
    }
    if ($loc) {
        $loc = PB_PLUGIN_DIR . "symbionts/dictionaries/{$loc}";
    }
    return $loc;
}
Beispiel #10
0
function pressbooks_copyright_license()
{
    $option = get_option('pressbooks_theme_options_global');
    $book_meta = \PressBooks\Book::getBookInformation();
    // if they don't want to see it, return
    // at minimum we need book copyright information set
    if (isset($option['copyright_license']) && false == $option['copyright_license'] || !isset($option['copyright_license']) || !isset($book_meta['pb_book_license'])) {
        return '';
    }
    global $post;
    $id = $post->ID;
    $title = is_front_page() ? get_bloginfo('name') : $post->post_title;
    $post_meta = get_post_meta($id);
    $link = get_permalink($id);
    $html = $license = $copyright_holder = '';
    $transient = get_transient("license-inf-{$id}");
    $updated = array($license, $copyright_holder, $title);
    $changed = false;
    $lang = $book_meta['pb_language'];
    // Copyright holder, set in order of precedence
    if (isset($post_meta['pb_section_author'])) {
        // section author overrides book author, copyrightholder
        $copyright_holder = $post_meta['pb_section_author'][0];
    } elseif (isset($book_meta['pb_copyright_holder'])) {
        // book copyright holder overrides book author
        $copyright_holder = $book_meta['pb_copyright_holder'];
    } elseif (isset($book_meta['pb_author'])) {
        // book author is the fallback, default
        $copyright_holder = $book_meta['pb_author'];
    }
    // Copyright license, set in order of precedence
    if (isset($post_meta['pb_section_license'])) {
        // section copyright overrides book
        $license = $post_meta['pb_section_license'][0];
    } elseif (isset($book_meta['pb_book_license'])) {
        // book is the fallback, default
        $license = $book_meta['pb_book_license'];
    }
    //delete_transient("license-inf-$id");
    // check if the user has changed anything
    if (is_array($transient)) {
        foreach ($updated as $val) {
            if (!array_key_exists($val, $transient)) {
                $changed = true;
            }
        }
    }
    // if the cache has expired, or the user changed the license
    if (false === $transient || true == $changed) {
        // get xml response from API
        $response = \PressBooks\Metadata::getLicenseXml($license, $copyright_holder, $link, $title, $lang);
        try {
            // convert to object
            $result = simplexml_load_string($response);
            // evaluate it for errors
            if (!false === $result || !isset($result->html)) {
                throw new \Exception('Creative Commons license API not returning expected results at Pressbooks\\Metadata::getLicenseXml');
            } else {
                // process the response, return html
                $html = \PressBooks\Metadata::getWebLicenseHtml($result->html);
            }
        } catch (\Exception $e) {
            error_log($e->getMessage());
        }
        // store it with the license as a key
        $value = array($license => $html, $copyright_holder => '', $title => '');
        // expires in 24 hours
        set_transient("license-inf-{$id}", $value, 86400);
    } else {
        $html = $transient[$license];
    }
    return $html;
}
Beispiel #11
0
/**
 * Shortcut to \PressBooks\Book::getBookInformation();
 *
 * @return array
 */
function pb_get_book_information()
{
    return \PressBooks\Book::getBookInformation();
}
Beispiel #12
0
 /**
  * Hook for add_filter('locale ', ...), change the book language
  *
  * @param string $lang
  *
  * @return string
  */
 static function setLocale($lang)
 {
     // Cheap cache
     static $loc = '__UNSET__';
     if ('__UNSET__' == $loc && function_exists('get_available_languages')) {
         $compare_with = get_available_languages(PB_PLUGIN_DIR . '/languages/');
         $book_lang = Book::getBookInformation();
         $book_lang = @$book_lang['pb_language'];
         foreach ($compare_with as $compare) {
             $compare = str_replace('pressbooks-', '', $compare);
             list($check_me) = explode('_', $compare);
             // We only care about the first two letters
             if (strpos($book_lang, $check_me) === 0) {
                 $loc = $compare;
                 break;
             }
         }
         if ('__UNSET__' == $loc) {
             $loc = 'en_US';
         }
         // No match found, default to english
     }
     // Return
     if ('__UNSET__' == $loc) {
         return $lang;
     } else {
         return $loc ? $loc : $lang;
     }
 }
 /**
  * Hook for add_filter('locale ', ...), change the book language
  *
  * @param string $lang
  *
  * @return string
  */
 static function setLocale($lang)
 {
     // Cheap cache
     static $loc = '__UNSET__';
     if ('__UNSET__' == $loc && function_exists('get_available_languages')) {
         $compare_with = get_available_languages(PB_PLUGIN_DIR . '/languages/');
         $codes = \Pressbooks\L10n\wplang_codes();
         $book_lang = Book::getBookInformation();
         $book_lang = isset($book_lang['pb_language']) ? $book_lang['pb_language'] : 'en';
         $book_lang = $codes[$book_lang];
         foreach ($compare_with as $compare) {
             $compare = str_replace('pressbooks-', '', $compare);
             if (strpos($book_lang, $compare) === 0) {
                 $loc = $compare;
                 break;
             }
         }
         if ('__UNSET__' == $loc) {
             $loc = 'en_US';
             // No match found, default to english
         }
     }
     // Return
     if ('__UNSET__' == $loc) {
         return $lang;
     } else {
         return $loc ? $loc : $lang;
     }
 }