Source of file PardotShortCode.php
Size: 8,997 Bytes - Last Modified: 2021-12-24T06:33:45+00:00
/var/www/docs.ssmods.com/process/src/src/PardotShortCode.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 | <?php namespace BluehouseGroup\Pardot; use Pardot_API; use SilverStripe\CMS\Model\SiteTree; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Injector\Injector; use SilverStripe\SiteConfig\SiteConfig; /** * Class for parsing shortcode for pardot forms and dynamic content. * * PardotForm() and PardotDynamicContent() are the two endpoints for * the pardot shortcode api. * * The Class is designed so that the cache does not need to be reset by a user. * The API is called when the the cache is empty, or if the content in the cache * doesn't match the requested content name or title, effectively resetting the cache. * * shortcode endpoints configured in _config.php */ class PardotShortCode extends SiteTree { private static $casting = array( 'PardotForm' => 'HTMLText', 'PardotDynamicContent'=>'HTMLText' ); /** * call back for pardot form shortcode. * * @param array $arguments Values 'title' supported * @return string embed_code if the title of the form exists */ public static function PardotForm($arguments, $content = null, $parser = null, $tagName) { if (isset($arguments["title"])) { if ($embed_code = self::getFormEmbedCodeFromCache($arguments["title"])) { return self::addAttributes($embed_code, $arguments, 'Form'); } else { // refresh the cache and look again self::cacheFormsFromPardotApi(); if ($embed_code = self::getFormEmbedCodeFromCache($arguments["title"])) { return self::addAttributes($embed_code, $arguments, 'Form'); } } } return ""; } /** * call back for pardot dynamic content * *@param array $arguments Values 'name' supported *@return string embed_code if the name of the dynamic content exists */ public static function PardotDynamicContent($arguments, $content = null, $parser = null, $tagName) { if (isset($arguments["name"])) { if ($embed_code = self::getDynamicContentEmbedCodeFromCache($arguments["name"])) { return self::addAttributes($embed_code, $arguments, 'DynamicContent'); } else { // refresh the cache and look again self::cacheDynamicContentFromPardotApi(); if ($embed_code = self::getDynamicContentFromCache($arguments["name"])) { return self::addAttributes($embed_code, $arguments, 'DynamicContent'); } } } return ""; } /** * Gets html to embed the pardot form based on name of the form. * * @param string $formTitle name of form * @return string | bool html to embed pardot form w/name if exists, false otherwise */ public static function getFormEmbedCodeFromCache($formTitle) { $forms = self::getFormsFromCache(); foreach ($forms as $form) { if (self::checkNameOrTitleEqual($formTitle, $form->name)) { return $form->embedCode; } } return false; } /** * Gets html to embed the pardot dynamic content based on name of content. * * @param string dynamicContentTitle title of dynamic title * @return string | bool html to embed pardot dynamic content w/name if exists, false otherwise */ public static function getDynamicContentEmbedCodeFromCache($dynamicContentTitle) { $dynamicContents = self::getDynamicContentFromCache(); foreach ($dynamicContents as $dynamicContent) { if (self::checkNameOrTitleEqual($dynamicContentTitle, $dynamicContent->name)) { return $dynamicContent->embedCode; } } return false; } /** * Gets array of form objects * If forms are not available in the cache, then make them available * * @return array Array of form objects */ public static function getFormsFromCache() { $pardot_cache = Injector::inst()->get(CacheInterface::class . '.Pardot'); if (!$serialized_pardot_forms = $pardot_cache->get('serialized_forms')) { $unserialized_pardot_forms = self::cacheFormsFromPardotApi(); } else { $unserialized_pardot_forms = unserialize($serialized_pardot_forms); } return $unserialized_pardot_forms; } /** * Gets array of dynamic content objects * If forms are not available in the cache, then make them available * * @return array Array of dynamic content objects */ public static function getDynamicContentFromCache() { $pardot_cache = Injector::inst()->get(CacheInterface::class . '.Pardot'); if (!$serialized_pardot_dynamic_content = $pardot_cache->get('serialized_dynamic_content')) { $unserialized_pardot_dynamic_content = self::cacheDynamicContentFromPardotApi(); } else { $unserialized_pardot_dynamic_content = unserialize($serialized_pardot_dynamic_content); } return $unserialized_pardot_dynamic_content; } /** * caches pardot forms from the pardot api. * * @return array Array of form objects */ public static function cacheFormsFromPardotApi() { $pardot = new Pardot_API(PardotConfig::getPardotCredentials()); $forms = $pardot->get_forms(); $pardot_cache = Injector::inst()->get(CacheInterface::class . '.Pardot'); $pardot_cache->set('serialized_forms', serialize($forms)); return $forms; } /** * caches pardot dynamic content from the pardot api. * * @return array Array of dynamic content objects */ public static function cacheDynamicContentFromPardotApi() { $pardot = new Pardot_API(PardotConfig::getPardotCredentials()); $dynamicContent = $pardot->get_dynamicContent(); $pardot_cache = Injector::inst()->get(CacheInterface::class . '.Pardot'); $pardot_cache->set('serialized_dynamic_content', serialize($dynamicContent)); return $dynamicContent; } /** * checks equivalence of two strings white space and capitalization doesnt matter * * Used to make api parameters more forgiving * @param string item1 * @param string item2 * bool true if strings are equal after removing whitespace and converted to lowercase. */ private static function checkNameOrTitleEqual($item1, $item2) { return strtolower(str_replace(" ", "", $item1)) == strtolower(str_replace(" ", "", $item2)); } /** * add the other attributes included to the embed code * supports type = 'Form' or 'DynamicContent' * also forces https if selected in config */ public static function addAttributes($embed_code, $arguments, $type) { $config = SiteConfig::current_site_config(); if ($config->pardot_https) { $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; preg_match($reg_exUrl, $embed_code, $url); $urlpieces = parse_url($url[0]); $httpsurl = 'https://go.pardot.com' . $urlpieces['path']; $embed_code = preg_replace($reg_exUrl, $httpsurl, $embed_code); } if ($type == 'Form') { if (isset($arguments['height'])) { if (preg_match('#height="[^"]+"#', $embed_code, $matches)) { $embed_code = str_replace($matches[0], "height=\"{$arguments['height']}\"", $embed_code); } else { $embed_code = str_replace('iframe', "iframe height=\"{$arguments['height']}\"", $embed_code); } } if (isset($arguments['width'])) { if (preg_match('#width="[^"]+"#', $embed_code, $matches)) { $embed_code = str_replace($matches[0], "width=\"{$arguments['width']}\"", $embed_code); } else { $embed_code = str_replace('iframe', "iframe width=\"{$arguments['width']}\"", $embed_code); } } if (isset($arguments['classes'])) { $embed_code = str_replace('<iframe', "<iframe class=\"pardotform {$arguments['classes']}\"", $embed_code); } return $embed_code; } elseif ($type == 'DynamicContent') { if (isset($arguments['height'])) { $embed_code = str_replace('height:auto', "height:{$arguments['height']}", $embed_code); } if (isset($arguments['width'])) { $embed_code = str_replace('width:auto', "width:{$arguments['width']}", $embed_code); } if (isset($arguments['classes'])) { $embed_code = str_replace('pardotdc', "pardotdc {$arguments['classes']}", $embed_code); } return $embed_code; } return $embed_code; } } |