/**
  * Main Instance
  *
  * Ensures that only one instance exists in memory at any one
  * time. Also prevents needing to define globals all over the place.
  *
  * @since 1.0
  *       
  */
 public static function instance()
 {
     // return self::$instance;
     if (!self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 /**
  * 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;
 }
// $msg = 'null';
// $devmsg = 'null';
// $code = $response->code;
// if (isset ( $response->errorMessage )) {
// $devmsg = ( string ) $response->errorMessage->developerMessage;
// $msg = ( string ) $response->errorMessage->message;
// }
// throw new Exception ( "edd-osgi: http error code='" . $code . "\n" . " Cannot generate licence from url=' . $uri . '\n" . " Reason=' . $msg . '\n" . " Developer Message='" . $devmsg . "'\n" );
// }
// $licenceStr = $response->body->licence;
// return ( string ) $licenceStr;
// }
// start of page response
try {
    // load supporting class
    $eddOsgiLicences = new EddOsgiLicences();
    // get the local post id
    $post_id = get_the_ID();
    // debug data
    // update_post_meta ( $post_id, 'edd_osgiLicenceMetadataSpecStr', '' ); //TODO DEBUG REMOVE
    // update_post_meta ( $post_id, 'edd_osgiLicenceMetadataStr', '' ); //TODO DEBUG REMOVE
    // see http://www.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/
    // contains maven unique id of product to which this licence applies
    // TODO REMOVE - THIS IS ONLY FOR TEST
    // update_post_meta ( $post_id, 'edd_osgiProductIdStr', 'org.opennms.co.uk/org.opennms.co.uk.newfeature/0.0.1-SNAPSHOT' );
    // end debug data
    // get the plugin settings and throw an error if setting not set
    // get the base url URL of the OSGi licence generator service from settings
    // $osgiLicenceGeneratorUrl = edd_get_option ( 'osgipub_osgi_licence_pub_url' );
    // if (! isset ( $osgiLicenceGeneratorUrl ) || '' == $osgiLicenceGeneratorUrl) {
    // throw new Exception ( 'edd-osgi: You must set the OSGi Licence Publisher URL in the plugin settings' );