| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\utils; |
| 4 |
|
| 5 |
use Leadin\data\Filters; |
| 6 |
use Leadin\AssetsManager; |
| 7 |
|
| 8 |
/** |
| 9 |
* Util Class responsible for rendering shortcodes. |
| 10 |
*/ |
| 11 |
class ShortcodeRenderUtils { |
| 12 |
|
| 13 |
/** |
| 14 |
* Render leadin shortcodes output |
| 15 |
* |
| 16 |
* @param array $shortcode_attrs Shortcode attributes. |
| 17 |
*/ |
| 18 |
public static function render_shortcode( $shortcode_attrs ) { |
| 19 |
$parsed_attributes = shortcode_atts( array( 'type' => null ), $shortcode_attrs ); |
| 20 |
|
| 21 |
$type = $parsed_attributes['type']; |
| 22 |
if ( ! isset( $type ) ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
switch ( $type ) { |
| 27 |
case 'form': |
| 28 |
return self::render_form( $shortcode_attrs ); |
| 29 |
case 'cta': |
| 30 |
return self::render_cta( $shortcode_attrs ); |
| 31 |
case 'meeting': |
| 32 |
return self::render_meeting( $shortcode_attrs ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Render form leadin shortcodes |
| 38 |
* |
| 39 |
* @param array $attrs Shortcode attributes. |
| 40 |
*/ |
| 41 |
private static function render_form( $attrs ) { |
| 42 |
$parsed_attributes = shortcode_atts( |
| 43 |
array( |
| 44 |
'portal' => null, |
| 45 |
'id' => null, |
| 46 |
), |
| 47 |
$attrs |
| 48 |
); |
| 49 |
|
| 50 |
$portal_id = $parsed_attributes['portal']; |
| 51 |
$id = $parsed_attributes['id']; |
| 52 |
|
| 53 |
if ( ! isset( $portal_id ) || ! isset( $id ) || ! is_numeric( $portal_id ) || |
| 54 |
! ( self::is_valid_uuid( $id ) || is_numeric( $id ) ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$form_div_uuid = self::generate_div_uuid(); |
| 59 |
$hublet = Filters::apply_hublet_filters(); |
| 60 |
AssetsManager::enqueue_forms_script(); |
| 61 |
return ' |
| 62 |
<script> |
| 63 |
window.hsFormsOnReady = window.hsFormsOnReady || []; |
| 64 |
window.hsFormsOnReady.push(()=>{ |
| 65 |
hbspt.forms.create({ |
| 66 |
portalId: ' . $portal_id . ', |
| 67 |
formId: "' . $id . '", |
| 68 |
target: "#hbspt-form-' . $form_div_uuid . '", |
| 69 |
region: "' . $hublet . '", |
| 70 |
' . Filters::apply_forms_payload_filters() . ' |
| 71 |
})}); |
| 72 |
</script> |
| 73 |
<div class="hbspt-form" id="hbspt-form-' . $form_div_uuid . '"></div>'; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Render cta leadin shortcodes |
| 78 |
* |
| 79 |
* @param array $attrs Shortcode attributes. |
| 80 |
*/ |
| 81 |
private static function render_cta( $attrs ) { |
| 82 |
$parsed_attributes = shortcode_atts( |
| 83 |
array( |
| 84 |
'portal' => null, |
| 85 |
'id' => null, |
| 86 |
), |
| 87 |
$attrs |
| 88 |
); |
| 89 |
|
| 90 |
$portal_id = $parsed_attributes['portal']; |
| 91 |
$id = $parsed_attributes['id']; |
| 92 |
|
| 93 |
if ( ! isset( $portal_id ) || ! isset( $id ) || ! is_numeric( $portal_id ) || |
| 94 |
! ( self::is_valid_uuid( $id ) || is_numeric( $id ) ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
return ' |
| 99 |
<!--HubSpot Call-to-Action Code --> |
| 100 |
<span class="hs-cta-wrapper" id="hs-cta-wrapper-' . $id . '"> |
| 101 |
<span class="hs-cta-node hs-cta-' . $id . '" id="' . $id . '"> |
| 102 |
<!--[if lte IE 8]> |
| 103 |
<div id="hs-cta-ie-element"></div> |
| 104 |
<![endif]--> |
| 105 |
<a href="https://cta-redirect.hubspot.com/cta/redirect/' . $portal_id . '/' . $id . '" > |
| 106 |
<img class="hs-cta-img" id="hs-cta-img-' . $id . '" style="border-width:0px;" src="https://no-cache.hubspot.com/cta/default/' . $portal_id . '/' . $id . '.png" alt="New call-to-action"/> |
| 107 |
</a> |
| 108 |
</span> |
| 109 |
<' . 'script charset="utf-8" src="//js.hubspot.com/cta/current.js"></script> |
| 110 |
<script> |
| 111 |
hbspt.cta.load(' . $portal_id . ', \'' . $id . '\', {}); |
| 112 |
</script> |
| 113 |
</span> |
| 114 |
<!-- end HubSpot Call-to-Action Code --> |
| 115 |
'; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Render meeting leadin shortcodes |
| 120 |
* |
| 121 |
* @param array $attrs Shortcode attributes. |
| 122 |
*/ |
| 123 |
private static function render_meeting( $attrs ) { |
| 124 |
$parsed_attributes = shortcode_atts( |
| 125 |
array( |
| 126 |
'url' => null, |
| 127 |
), |
| 128 |
$attrs |
| 129 |
); |
| 130 |
|
| 131 |
$url = esc_url_raw( $parsed_attributes['url'] ); |
| 132 |
|
| 133 |
if ( filter_var( $url, FILTER_VALIDATE_URL ) === false ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
AssetsManager::enqueue_meetings_script(); |
| 138 |
|
| 139 |
return ' |
| 140 |
<div class="meetings-iframe-container" data-src="' . $url . '?embed=true"></div> |
| 141 |
<script> |
| 142 |
if(window.hbspt && window.hbspt.meetings){ |
| 143 |
window.hbspt.meetings.create(".meetings-iframe-container"); |
| 144 |
} |
| 145 |
</script> |
| 146 |
'; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Generates 10 characters long string with random values |
| 151 |
*/ |
| 152 |
private static function get_random_number_string() { |
| 153 |
$result = ''; |
| 154 |
for ( $i = 0; $i < 10; $i++ ) { |
| 155 |
$result .= wp_rand( 0, 9 ); |
| 156 |
} |
| 157 |
return $result; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Generates a unique uuid |
| 162 |
*/ |
| 163 |
private static function generate_div_uuid() { |
| 164 |
return time() * 1000 . '-' . self::get_random_number_string(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Checks if input is a valid uuid. |
| 169 |
* |
| 170 |
* @param String $uuid input to validate. |
| 171 |
*/ |
| 172 |
private static function is_valid_uuid( $uuid ) { |
| 173 |
if ( ! is_string( $uuid ) || ( preg_match( '/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $uuid ) !== 1 ) ) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|