/**
  * Provides shortcodes for retrieving licence metadata from licence generator for use in product definition
  * shortcodes [osgi_licence_metadata] retrieving licence specification from licence generator every time page viewed
  *
  * @param unknown $atts        	
  */
 public function osgi_licence_metadata_shortcode($atts)
 {
     // set time metadata uploaded
     $objDateTime = new DateTime('NOW');
     $content = "";
     $content .= "<div id=\"osgi_licence_metadata_shortcode\" class=\"osgi_metadata\">\n";
     try {
         $edd_osgiProductIdStr = get_post_meta(get_the_ID(), '_edd_osgiProductIdStr', true);
         if (!isset($edd_osgiProductIdStr)) {
             throw new Exception('edd-osgi: You must set productId on the download definition');
         }
         $user_can_modify_metadata = is_user_logged_in() && current_user_can('view_shop_sensitive_data');
         if ($this->osgipub_osgi_debug) {
             $content .= "<p>debug: Running osgi_licence_metadata_shortcode shortcode </p>\n";
             if ($user_can_modify_metadata) {
                 $content .= "<p>debug: osgi_licence_metadata_shortcode: user permitted to modify licence metadata</p>\n";
             } else {
                 $content .= "<p>debug: osgi_licence_metadata_shortcode: user NOT permitted to modify licence metadata</p>";
             }
             $content .= "<p>debug: Attributes:";
             if ("" != $atts) {
                 foreach ($atts as $key => $value) {
                     $content .= "   " . $key . "=" . $value . "<br>\n";
                 }
             } else {
                 $content .= " Not Set";
             }
             $content .= "</p>\n";
         }
         // If user can modify shop data then they will see licence metadata and can edit it
         // load supporting class
         $eddOsgiLicences = new EddOsgiLicences();
         if ($user_can_modify_metadata) {
             // load unmodified metadata spec from licence manager
             $licenceMetadataSpecStr = $eddOsgiLicences->getLicenceMetadataSpec($edd_osgiProductIdStr);
             $osgiLicenceMetadataSpec = new SimpleXMLElement($licenceMetadataSpecStr);
             // try loading modified LicenceMetadataSpecStr from this post
             $edd_modified_osgiLicenceMetadataSpecStr = get_post_meta(get_the_ID(), '_edd_modified_osgiLicenceMetadataSpecStr', true);
             // if no modified LicenceMetadataSpecStr in post then create new one
             if (!isset($edd_modified_osgiLicenceMetadataSpecStr) || '' == $edd_modified_osgiLicenceMetadataSpecStr) {
                 $edd_modified_osgiLicenceMetadataSpecStr = $licenceMetadataSpecStr;
                 // set the update time
                 $edd_osgiLicenceMetadataStrUpdateTime = $objDateTime->format(DateTime::COOKIE);
                 update_post_meta(get_the_ID(), '_edd_osgiLicenceMetadataStrUpdateTime', $edd_osgiLicenceMetadataStrUpdateTime);
             }
             $edd_modified_osgiLicenceMetadataSpec = new SimpleXMLElement($edd_modified_osgiLicenceMetadataSpecStr);
             // check if request asks to reset the modified Licence Metadata Spec
             if (isset($_POST['_resetLicenceMetadataSpec']) && $_POST['_resetLicenceMetadataSpec'] == 'true') {
                 // if reset then load unmodified metadata spec from licence manager
                 $licenceMetadataSpecStr = $eddOsgiLicences->getLicenceMetadataSpec($edd_osgiProductIdStr);
                 $osgiLicenceMetadataSpec = new SimpleXMLElement($licenceMetadataSpecStr);
                 $edd_modified_osgiLicenceMetadataSpec = $osgiLicenceMetadataSpec;
                 $payload = (string) $edd_modified_osgiLicenceMetadataSpec->asXML();
                 update_post_meta(get_the_ID(), '_edd_modified_osgiLicenceMetadataSpecStr', $payload);
                 // set the update time
                 $edd_osgiLicenceMetadataStrUpdateTime = $objDateTime->format(DateTime::COOKIE);
                 update_post_meta(get_the_ID(), '_edd_osgiLicenceMetadataStrUpdateTime', $edd_osgiProductMetadataStrUpdateTime);
             } elseif (isset($_POST['_modifyLicenceMetadataSpec']) && $_POST['_modifyLicenceMetadataSpec'] == 'true') {
                 // check if request asks to modify the Licence Metadata Spec
                 // parse post data into modified licene metadata spec an update the post
                 $edd_modified_osgiLicenceMetadataSpec = $eddOsgiLicences->modifyLicenceMetadataFromPost($osgiLicenceMetadataSpec, $edd_modified_osgiLicenceMetadataSpec, $_POST);
                 $payload = (string) $edd_modified_osgiLicenceMetadataSpec->asXML();
                 update_post_meta(get_the_ID(), '_edd_modified_osgiLicenceMetadataSpecStr', $payload);
                 // set the update time
                 $edd_osgiLicenceMetadataStrUpdateTime = $objDateTime->format(DateTime::COOKIE);
                 update_post_meta(get_the_ID(), '_edd_osgiLicenceMetadataStrUpdateTime', $edd_osgiLicenceMetadataStrUpdateTime);
             }
             $noinput = false;
             // update time string is time when matadata last updated
             $edd_osgiLicenceMetadataStrUpdateTime = get_post_meta(get_the_ID(), '_edd_osgiLicenceMetadataStrUpdateTime', true);
             if (isset($edd_osgiLicenceMetadataStrUpdateTime)) {
                 $content .= "<div style=\" line-height: 80%;\">Licence Metadata (Last Updated: " . $edd_osgiLicenceMetadataStrUpdateTime . ")</div>\n";
             }
             $content .= "<form method=\"post\" action=\"\" enctype=\"multipart/form-data\">\n";
             $content .= $eddOsgiLicences->licenceMetadataForm($edd_modified_osgiLicenceMetadataSpec, $osgiLicenceMetadataSpec, $noinput);
             // set up form to update licence metadata
             $content .= "<p>As a privileged user you can modify the standard metadata spec for this product instance</p>\n";
             $content .= "<p>(e.g. you can change the options or duration of the licence)</p>\n";
             $content .= "<input type=\"hidden\" name=\"_modifyLicenceMetadataSpec\" value=\"true\">\n";
             $content .= "<button type=\"submit\">Update Licence Metadata Spec</button>\n";
             $content .= "</form>\n";
             $content .= "<form method=\"post\" action=\"\" enctype=\"multipart/form-data\">\n";
             $content .= "<input type=\"hidden\" name=\"_resetLicenceMetadataSpec\" value=\"true\">\n";
             $content .= "<button type=\"submit\">Reset Licence Metadata Spec</button>\n";
             $content .= "</form>\n";
         }
     } catch (Exception $e) {
         $content .= "<p>" . "Exception in osgi_licence_metadata shortcode: Exception: " . $e->getMessage() . "</p>\n";
     }
     $content .= "</div> <!-- id=\"osgi_licence_metadata_shortcode\" -->\n";
     return $content;
 }
 if (!isset($edd_osgiLicenceMetadataSpecStr) || $edd_osgiLicenceMetadataSpecStr == NULL || $edd_osgiLicenceMetadataSpecStr == '') {
     if ($osgipub_osgi_debug) {
         echo "debug: the first time page is viewed, we populate edd_osgiLicenceMetadataSpecStr property  with edd_modified_osgiLicenceMetadataSpecStr\n";
     }
     $edd_modified_osgiLicenceMetadataSpecStr = get_post_meta($post_id, '_edd_modified_osgiLicenceMetadataSpecStr', true);
     if (isset($edd_modified_osgiLicenceMetadataSpecStr) && '' != $edd_modified_osgiLicenceMetadataSpecStr) {
         $edd_osgiLicenceMetadataSpecStr = $edd_modified_osgiLicenceMetadataSpecStr;
         if ($osgipub_osgi_debug) {
             echo "debug: _edd_modified_osgiLicenceMetadataSpecStr has been set in product description =" . $edd_modified_osgiLicenceMetadataSpecStr . "\n";
         }
     } else {
         if ($osgipub_osgi_debug) {
             echo "debug: _edd_modified_osgiLicenceMetadataSpecStr has not been set in product description. Using raw spec from licence generator\n";
         }
         // if we cant talk to the licence generator error and leave page
         $edd_osgiLicenceMetadataSpecStr = (string) $eddOsgiLicences->getLicenceMetadataSpec($edd_osgiProductIdStr);
     }
     update_post_meta($post_id, 'edd_osgiLicenceMetadataSpecStr', $edd_osgiLicenceMetadataSpecStr);
 } elseif ($osgipub_osgi_debug) {
     echo "debug: not first time page has been viewed so edd_osgiLicenceMetadataSpecStr is already populated\n";
 }
 // parse the licence matadata specification we have just saved as a string
 try {
     $osgilicenceMetadataSpec = new SimpleXMLElement($edd_osgiLicenceMetadataSpecStr);
 } catch (Exception $e) {
     throw new Exception('edd-osgi: cannot parse edd_osgiLicenceMetadataSpecStr', $e->getMessage(), " line:", $e->getLine());
 }
 // the first time page is viewed, we populate licence metadata with licence metadata specification
 // and with local user information in order to create a licence
 // TODO include user / cutomer metadata in the licence metadata
 if (!isset($edd_osgiLicenceMetadataStr) || $edd_osgiLicenceMetadataStr == NULL || $edd_osgiLicenceMetadataStr == '') {