示例#1
0
 /**
  * Returns book information in a useful, string only, format. Data is converted to HTML.
  *
  * @return array
  */
 static function getBookInformation($id = '')
 {
     // -----------------------------------------------------------------------------
     // Is cached?
     // -----------------------------------------------------------------------------
     if (!empty($id) && is_int($id)) {
         $blog_id = $id;
         switch_to_blog($blog_id);
     } else {
         global $blog_id;
     }
     $cache_id = "book-inf-{$blog_id}";
     $book_information = wp_cache_get($cache_id, 'pb');
     if ($book_information) {
         return $book_information;
     }
     // ----------------------------------------------------------------------------
     // Book Information
     // ----------------------------------------------------------------------------
     $expected_array = array('pb_keywords_tags', 'pb_bisac_subject', 'pb_contributing_authors');
     $expected_the_content = array('pb_custom_copyright', 'pb_about_unlimited');
     $book_information = array();
     $meta = new Metadata();
     $data = $meta->getMetaPostMetadata();
     foreach ($data as $key => $val) {
         // Skip anything not prefixed with pb_
         if (!preg_match('/^pb_/', $key)) {
             continue;
         }
         // We only care about strings
         if (is_array($val)) {
             if (false !== in_array($key, $expected_array)) {
                 $val = implode(', ', $val);
             } else {
                 $val = array_values($val);
                 $val = array_pop($val);
             }
         }
         // Skip empty values
         if (!trim($val)) {
             continue;
         }
         if (false !== in_array($key, $expected_the_content)) {
             $val = wptexturize($val);
             $val = wpautop($val);
         } else {
             $val = htmlspecialchars($val, ENT_NOQUOTES | ENT_XHTML, 'UTF-8', false);
         }
         // Remove invisible control characters that break XML
         $val = \PressBooks\Sanitize\remove_control_characters($val);
         $book_information[$key] = $val;
     }
     // Return our best guess if no book information has been entered.
     if (empty($book_information)) {
         $book_information['pb_title'] = get_bloginfo('name');
         if (!function_exists('get_user_by')) {
             include ABSPATH . 'wp-includes/pluggable.php';
         }
         $author = get_user_by('email', get_bloginfo('admin_email'));
         $book_information['pb_author'] = $author->display_name;
         $book_information['pb_cover_image'] = \PressBooks\Image\default_cover_url();
     }
     // -----------------------------------------------------------------------------
     // Cache & Return
     // -----------------------------------------------------------------------------
     wp_cache_set($cache_id, $book_information, 'pb', 86400);
     if (!empty($id) && is_int($id)) {
         restore_current_blog();
     }
     return $book_information;
 }
示例#2
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;
}
示例#3
0
/**
 * Shortcut to \PressBooks\Metadata::getMicrodataElements();
 * 
 * @return string
 */
function pb_get_microdata_elements()
{
    return \PressBooks\Metadata::getMicrodataElements();
}
示例#4
0
 /**
  * Will create an html blob of copyright information, returns empty string
  * if user doesn't want it displayed 
  * 
  * @param array $metadata
  * @param string $title
  * @param int $id
  * @param string $section_author
  * @return string $html blob
  * @throws \Exception
  */
 protected function doCopyrightLicense($metadata, $title = '', $id = '', $section_author = '')
 {
     $option = get_option('pressbooks_theme_options_global');
     $html = $license = $copyright_holder = '';
     $lang = $metadata['pb_language'];
     // if they don't want to see it, return
     // at minimum we need book copyright information set
     if (false == $option['copyright_license'] || !isset($metadata['pb_book_license'])) {
         return '';
     }
     // if no post $id given, we default to book copyright
     if (!empty($id)) {
         $section_license = get_post_meta($id, 'pb_section_license', true);
         $link = get_permalink($id);
     } else {
         $section_license = '';
         $link = get_bloginfo('url');
         $title = get_bloginfo('name');
     }
     // Copyright holder, set in order of precedence
     if (!empty($section_author)) {
         // section author higher priority than book author, copyrightholder
         $copyright_holder = $section_author;
     } elseif (isset($metadata['pb_copyright_holder'])) {
         // book copyright holder higher priority than book author
         $copyright_holder = $metadata['pb_copyright_holder'];
     } elseif (isset($metadata['pb_author'])) {
         // book author is the fallback, default
         $copyright_holder = $metadata['pb_author'];
     }
     // Copyright license, set in order of precedence
     if (!empty($section_license)) {
         // section copyright higher priority than book
         $license = $section_license;
     } elseif (isset($metadata['pb_book_license'])) {
         // book is the fallback, default
         $license = $metadata['pb_book_license'];
     }
     // get xml response from API
     $response = 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 = Metadata::getWebLicenseHtml($result->html);
         }
     } catch (\Exception $e) {
         $this->logError($e->getMessage());
     }
     return $html;
 }