-
Notifications
You must be signed in to change notification settings - Fork 53
/
shortcodes.php
234 lines (208 loc) · 5.54 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Shortcode definition
*
* @package wp-mautic
*/
// Prevent direct access to this file.
if ( ! defined( 'ABSPATH' ) ) {
header( 'HTTP/1.0 403 Forbidden' );
echo 'This file should not be accessed directly!';
exit; // Exit if accessed directly.
}
add_shortcode( 'mautic', 'wpmautic_shortcode' );
// Backward compatibilities.
add_shortcode( 'mauticcontent', 'wpmautic_dwc_shortcode' );
add_shortcode( 'mauticvideo', 'wpmautic_video_shortcode' );
add_shortcode( 'mauticform', 'wpmautic_form_shortcode' );
add_shortcode( 'mautictags', 'wpmautic_tags_shortcode' );
add_shortcode( 'mauticfocus', 'wpmautic_focus_shortcode' );
/**
* Handle mautic shortcode. Must include a type attribute.
*
* @param array $atts Shortcode attributes.
* @param string|null $content Default content to be displayed.
*
* @return string
*/
function wpmautic_shortcode( $atts, $content = null ) {
$default = shortcode_atts(
array(
'type' => null,
),
$atts
);
switch ( $default['type'] ) {
case 'form':
return wpmautic_form_shortcode( $atts );
case 'content':
return wpmautic_dwc_shortcode( $atts, $content );
case 'video':
return wpmautic_video_shortcode( $atts );
case 'tags':
return wpmautic_tags_shortcode( $atts );
case 'focus':
return wpmautic_focus_shortcode( $atts );
}
return false;
}
/**
* Handle mauticform shortcode
* example: [mautic type="form" id="1"]
*
* @param array $atts Shortcode attributes.
*
* @return string
*/
function wpmautic_form_shortcode( $atts ) {
$base_url = wpmautic_option( 'base_url', '' );
if ( '' === $base_url ) {
return false;
}
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);
if ( empty( $atts['id'] ) ) {
return false;
}
return '<script type="text/javascript" ' . sprintf(
'src="%s/form/generate.js?id=%s" async',
esc_url( $base_url ),
esc_attr( $atts['id'] )
) . '></script>';
}
/**
* Dynamic content shortcode handling
* example: [mautic type="content" slot="slot_name"]Default Content[/mautic]
*
* @param array $atts Shortcode attributes.
* @param string|null $content Default content to be displayed.
*
* @return string
*/
function wpmautic_dwc_shortcode( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'slot' => '',
),
$atts,
'mautic'
);
return sprintf(
'<div class="mautic-slot" data-slot-name="%s">%s</div>',
esc_attr( $atts['slot'] ),
wp_kses( $content, wp_kses_allowed_html( 'post' ) )
);
}
/**
* Video shortcode handling
* example: [mautic type="video" gate-time="15" form-id="1" src="https://www.youtube.com/watch?v=QT6169rdMdk"]
*
* @param array $atts Shortcode attributes.
*
* @return string
*/
function wpmautic_video_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'gate-time' => 15,
'form-id' => '',
'src' => '',
'video-type' => '',
'mautic-video' => 'true',
'width' => 640,
'height' => 360,
),
$atts
);
if ( empty( $atts['src'] ) ) {
return __( 'You must provide a video source. Add a src="URL" attribute to your shortcode. Replace URL with the source url for your video.', 'wp-mautic' );
}
if ( empty( $atts['form-id'] ) && 'true' !== $atts['mautic-video'] ) {
return __( 'You must provide a mautic form id. Add a form-id="#" attribute to your shortcode. Replace # with the id of the form you want to use.', 'wp-mautic' );
}
if ( preg_match( '/^.*((youtu.be)|(youtube.com))\/((v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))?\??v?=?([^#\&\?]*).*/', $atts['src'] ) ) {
$atts['video-type'] = 'youtube';
}
if ( preg_match( '/^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/', $atts['src'] ) ) {
$atts['video-type'] = 'vimeo';
}
if ( strtolower( substr( $atts['src'], -3 ) ) === 'mp4' ) {
$atts['video-type'] = 'mp4';
}
if ( empty( $atts['video-type'] ) ) {
return __( 'Please define a valid video type with video-type="#".', 'wp-mautic' );
}
return sprintf(
'<video height="%1$s" width="%2$s"' . ( empty( $atts['form-id'] ) ? '' : ' data-form-id="%3$s"' ) . ' data-gate-time="%4$s" data-mautic-video="%5$s">' .
'<source type="video/%6$s" src="%7$s" />' .
'</video>',
esc_attr( $atts['height'] ),
esc_attr( $atts['width'] ),
esc_attr( $atts['form-id'] ),
esc_attr( $atts['gate-time'] ),
esc_attr( $atts['mautic-video'] ),
esc_attr( $atts['video-type'] ),
esc_attr( $atts['src'] )
);
}
/**
* Handle mautic tags by WordPress shortcodes
* example: [mautic type="tags" values="addtag,-removetag"]
*
* @param array $atts Shortcode attributes.
*
* @return string
*/
function wpmautic_tags_shortcode( $atts ) {
$base_url = wpmautic_option( 'base_url', '' );
if ( '' === $base_url ) {
return false;
}
$atts = shortcode_atts(
array(
'values' => '',
),
$atts
);
if ( empty( $atts['values'] ) ) {
return false;
}
return sprintf(
'<img src="%s/mtracking.gif?tags=%s" alt="%s" style="display:none;" />',
esc_url( $base_url ),
esc_attr( $atts['values'] ),
esc_attr__( 'Mautic Tags', 'wp-mautic' )
);
}
/**
* Handle mautic focus itens on WordPress Page
* example: [mautic type="focus" id="1"]
*
* @param array $atts Shortcode attributes.
*
* @return string
*/
function wpmautic_focus_shortcode( $atts ) {
$base_url = wpmautic_option( 'base_url', '' );
if ( '' === $base_url ) {
return false;
}
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);
if ( empty( $atts['id'] ) ) {
return false;
}
return '<script type="text/javascript" ' . sprintf(
'src="%s/focus/%s.js" async',
esc_url( $base_url ),
esc_attr( $atts['id'] )
) . ' async="async"></script>';
}