コード例 #1
0
 /**
  * @param array $book_contents
  * @param array $metadata
  */
 protected function echoMetaData($book_contents, $metadata)
 {
     foreach ($metadata as $name => $content) {
         $name = Sanitize\sanitize_xml_id(str_replace('_', '-', $name));
         $content = trim(strip_tags(html_entity_decode($content)));
         // Plain text
         $content = preg_replace('/\\s+/', ' ', preg_replace('/\\n+/', ' ', $content));
         // Normalize whitespaces
         $content = Sanitize\sanitize_xml_attribute($content);
         printf('<meta name="%s" content="%s" />', $name, $content);
         echo "\n";
     }
 }
コード例 #2
0
 /**
  * @param $book_contents
  *
  * @return mixed
  */
 protected function preProcessBookContents($book_contents)
 {
     // We need to change global $id for shortcodes, the_content, ...
     global $id;
     $old_id = $id;
     // Do root level structures first.
     foreach ($book_contents as $type => $struct) {
         if (preg_match('/^__/', $type)) {
             continue;
         }
         // Skip __magic keys
         foreach ($struct as $i => $val) {
             if (isset($val['post_content'])) {
                 $id = $val['ID'];
                 $book_contents[$type][$i]['post_content'] = $this->preProcessPostContent($val['post_content']);
             }
             if (isset($val['post_title'])) {
                 $book_contents[$type][$i]['post_title'] = Sanitize\sanitize_xml_attribute($val['post_title']);
             }
             if (isset($val['post_name'])) {
                 $book_contents[$type][$i]['post_name'] = $this->preProcessPostName($val['post_name']);
             }
             if ('part' == $type) {
                 // Do chapters, which are embedded in part structure
                 foreach ($book_contents[$type][$i]['chapters'] as $j => $val2) {
                     if (isset($val2['post_content'])) {
                         $id = $val2['ID'];
                         $book_contents[$type][$i]['chapters'][$j]['post_content'] = $this->preProcessPostContent($val2['post_content']);
                     }
                     if (isset($val2['post_title'])) {
                         $book_contents[$type][$i]['chapters'][$j]['post_title'] = Sanitize\sanitize_xml_attribute($val2['post_title']);
                     }
                     if (isset($val2['post_name'])) {
                         $book_contents[$type][$i]['chapters'][$j]['post_name'] = $this->preProcessPostName($val2['post_name']);
                     }
                 }
             }
         }
     }
     $id = $old_id;
     return $book_contents;
 }
コード例 #3
0
 /**
  * Takes a known string from metadata, builds a url to hit an api which returns an xml response
  * @see https://api.creativecommons.org/docs/readme_15.html
  *
  * @param string $type license type
  * @param string $copyright_holder of the page
  * @param string $src_url of the page
  * @param string $title of the page
  * @return string $xml response
  */
 static function getLicenseXml($type, $copyright_holder, $src_url, $title, $lang = '')
 {
     $endpoint = 'https://api.creativecommons.org/rest/1.5/';
     $lang = !empty($lang) ? substr($lang, 0, 2) : '';
     $expected = array('public-domain' => array('license' => 'zero', 'commercial' => 'y', 'derivatives' => 'y'), 'cc-by' => array('license' => 'standard', 'commercial' => 'y', 'derivatives' => 'y'), 'cc-by-sa' => array('license' => 'standard', 'commercial' => 'y', 'derivatives' => 'sa'), 'cc-by-nd' => array('license' => 'standard', 'commercial' => 'y', 'derivatives' => 'n'), 'cc-by-nc' => array('license' => 'standard', 'commercial' => 'n', 'derivatives' => 'y'), 'cc-by-nc-sa' => array('license' => 'standard', 'commercial' => 'n', 'derivatives' => 'sa'), 'cc-by-nc-nd' => array('license' => 'standard', 'commercial' => 'n', 'derivatives' => 'n'), 'all-rights-reserved' => array());
     // nothing meaningful to hit the api with, so bail
     if (!array_key_exists($type, $expected)) {
         return '';
     }
     switch ($type) {
         // api doesn't have an 'all-rights-reserved' endpoint, so manual build necessary
         case 'all-rights-reserved':
             $xml = '<result><html>' . "<span property='dct:title'>" . Sanitize\sanitize_xml_attribute($title) . '</span> &#169; ' . Sanitize\sanitize_xml_attribute($copyright_holder) . '. ' . __('All Rights Reserved', 'pressbooks') . '.</html></result>';
             break;
             //          case 'other':
             //               //@TODO
             //              break;
         //          case 'other':
         //               //@TODO
         //              break;
         default:
             $key = array_keys($expected[$type]);
             $val = array_values($expected[$type]);
             // build the url
             $url = $endpoint . $key[0] . '/' . $val[0] . '/get?' . $key[1] . '=' . $val[1] . '&' . $key[2] . '=' . $val[2] . '&creator=' . urlencode($copyright_holder) . '&attribution_url=' . urlencode($src_url) . '&title=' . urlencode($title) . '&locale=' . $lang;
             $xml = wp_remote_get($url);
             $ok = wp_remote_retrieve_response_code($xml);
             // if server response is not ok
             if (200 != $ok) {
                 return '';
             }
             // if remote call went sideways
             if (!is_wp_error($xml)) {
                 $xml = $xml['body'];
             } else {
                 // Something went wrong
                 \error_log('\\Pressbooks\\Metadata::getLicenseXml error: ' . $xml->get_error_message());
             }
             break;
     }
     return $xml;
 }