getLicenseXml() static public method

Takes a known string from metadata, builds a url to hit an api which returns an xml response
See also: https://api.creativecommons.org/docs/readme_15.html
static public getLicenseXml ( string $type, string $copyright_holder, string $src_url, string $title, $lang = '' ) : string
$type string license type
$copyright_holder string of the page
$src_url string of the page
$title string of the page
return string $xml response
Example #1
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;
}
Example #2
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;
 }