-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypeform-shortcodes.php
71 lines (54 loc) · 1.81 KB
/
typeform-shortcodes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
class TypeformShortcode {
static function renderTypeform($attrs)
{
$options = self::extractOptions($attrs);
wp_enqueue_script('typeform/embed');
if($options->style === 'button') {
wp_enqueue_style('typeform/embed/button');
}
$template = self::renderTemplate($options);
return $template;
}
static function extractOptions($attrs)
{
$options = shortcode_atts(array(
'url' => '',
'height' => '500px',
'width' => '100%',
'type' => 'embed',
'style' => '',
'builder' => '',
'button_text' => __('Launch me!', 'typeform')
), $attrs);
if (!self::isValidValue($options['height'])) {
$options['height'] = (string) $options['height'] . 'px';
}
if (!self::isValidValue($options['width'])) {
$options['width'] = (string) $options['width'] . 'px';
}
$options['url'] = apply_filters('typeform_embed_url', $options['url'], $options['builder']);
return (object) $options;
}
static function isValidValue($value)
{
return strpos($value, '%') || strpos($value, 'px');
}
static function isEmbed($type)
{
return !in_array($type, array('classic', 'popup', 'drawer'));
}
static function renderTemplate($options)
{
$templateToRender = !self::isEmbed($options->type)
? 'parts/typeform-popup.php'
: 'parts/typeform-embed.php';
ob_start();
extract((array) $options);
include($templateToRender);
$template = ob_get_contents();
@ob_end_clean();
return $template;
}
}
add_shortcode('typeform_embed', 'TypeformShortCode::renderTypeform');