Code

< 40 %
40-60 %
> 60 %
1
<?php
2
/**
3
 * Shortcode definition
4
 *
5
 * @package wp-mautic
6
 */
7
8
// Prevent direct access to this file.
9
if ( ! defined( 'ABSPATH' ) ) {
10
	header( 'HTTP/1.0 403 Forbidden' );
11
	echo 'This file should not be accessed directly!';
12
	exit; // Exit if accessed directly.
13
}
14
15
add_shortcode( 'mautic', 'wpmautic_shortcode' );
16
17
// Backward compatibilities.
18
add_shortcode( 'mauticcontent', 'wpmautic_dwc_shortcode' );
19
add_shortcode( 'mauticvideo', 'wpmautic_video_shortcode' );
20
add_shortcode( 'mauticform', 'wpmautic_form_shortcode' );
21
add_shortcode( 'mautictags', 'wpmautic_tags_shortcode' );
22
add_shortcode( 'mauticfocus', 'wpmautic_focus_shortcode' );
23
24
/**
25
 * Handle mautic shortcode. Must include a type attribute.
26
 *
27
 * @param array       $atts    Shortcode attributes.
28
 * @param string|null $content Default content to be displayed.
29
 *
30
 * @return string
31
 */
32
function wpmautic_shortcode( $atts, $content = null ) {
33 25
	$default = shortcode_atts(
34
		array(
35 25
			'type' => null,
36
		),
37 25
		$atts
38
	);
39
40 25
	switch ( $default['type'] ) {
41 25
		case 'form':
42 3
			return wpmautic_form_shortcode( $atts );
43 22
		case 'content':
44 3
			return wpmautic_dwc_shortcode( $atts, $content );
45 19
		case 'video':
46 11
			return wpmautic_video_shortcode( $atts );
47 8
		case 'tags':
48 4
			return wpmautic_tags_shortcode( $atts );
49 4
		case 'focus':
50 3
			return wpmautic_focus_shortcode( $atts );
51
	}
52
53 1
	return false;
54
}
55
56
/**
57
 * Handle mauticform shortcode
58
 * example: [mautic type="form" id="1"]
59
 *
60
 * @param  array $atts Shortcode attributes.
61
 *
62
 * @return string
63
 */
64 View Code Duplication
function wpmautic_form_shortcode( $atts ) {
65 7
	$base_url = wpmautic_option( 'base_url', '' );
66 7
	if ( '' === $base_url ) {
67 2
		return false;
68
	}
69
70 5
	$atts = shortcode_atts(
71
		array(
72 5
			'id' => '',
73
		),
74 5
		$atts
75
	);
76
77 5
	if ( empty( $atts['id'] ) ) {
78 3
		return false;
79
	}
80
81 2
	return '<script type="text/javascript" ' . sprintf(
82 2
		'src="%s/form/generate.js?id=%s"',
83 2
		esc_url( $base_url ),
84 2
		esc_attr( $atts['id'] )
85 2
	) . '></script>';
86
}
87
88
/**
89
 * Dynamic content shortcode handling
90
 * example: [mautic type="content" slot="slot_name"]Default Content[/mautic]
91
 *
92
 * @param  array       $atts    Shortcode attributes.
93
 * @param  string|null $content Default content to be displayed.
94
 *
95
 * @return string
96
 */
97
function wpmautic_dwc_shortcode( $atts, $content = null ) {
98 4
	$atts = shortcode_atts(
99
		array(
100 4
			'slot' => '',
101
		),
102 4
		$atts,
103 4
		'mautic'
104
	);
105
106 4
	return sprintf(
107 4
		'<div class="mautic-slot" data-slot-name="%s">%s</div>',
108 4
		esc_attr( $atts['slot'] ),
109 4
		wp_kses( $content, wp_kses_allowed_html( 'post' ) )
110
	);
111
}
112
113
/**
114
 * Video shortcode handling
115
 * example: [mautic type="video" gate-time="15" form-id="1" src="https://www.youtube.com/watch?v=QT6169rdMdk"]
116
 *
117
 * @param  array $atts Shortcode attributes.
118
 *
119
 * @return string
120
 */
121
function wpmautic_video_shortcode( $atts ) {
122 19
	$atts = shortcode_atts(
123
		array(
124 19
			'gate-time'    => 15,
125
			'form-id'      => '',
126
			'src'          => '',
127
			'video-type'   => '',
128
			'mautic-video' => 'true',
129
			'width'        => 640,
130
			'height'       => 360,
131
		),
132 19
		$atts
133
	);
134
135 19
	if ( empty( $atts['src'] ) ) {
136 1
		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' );
137
	}
138
139 18
	if ( empty( $atts['form-id'] ) && 'true' !== $atts['mautic-video'] ) {
140 1
		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' );
141
	}
142
143 17
	if ( preg_match( '/^.*((youtu.be)|(youtube.com))\/((v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))?\??v?=?([^#\&\?]*).*/', $atts['src'] ) ) {
144 6
		$atts['video-type'] = 'youtube';
145
	}
146 17
	if ( preg_match( '/^.*(vimeo\.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/', $atts['src'] ) ) {
147 6
		$atts['video-type'] = 'vimeo';
148
	}
149 17
	if ( strtolower( substr( $atts['src'], -3 ) ) === 'mp4' ) {
150 2
		$atts['video-type'] = 'mp4';
151
	}
152
153 17
	if ( empty( $atts['video-type'] ) ) {
154 1
		return __( 'Please define a valid video type with video-type="#".', 'wp-mautic' );
155
	}
156
157 16
	return sprintf(
158 16
		'<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">' .
159 16
			'<source type="video/%6$s" src="%7$s" />' .
160 16
		'</video>',
161 16
		esc_attr( $atts['height'] ),
162 16
		esc_attr( $atts['width'] ),
163 16
		esc_attr( $atts['form-id'] ),
164 16
		esc_attr( $atts['gate-time'] ),
165 16
		esc_attr( $atts['mautic-video'] ),
166 16
		esc_attr( $atts['video-type'] ),
167 16
		esc_attr( $atts['src'] )
168
	);
169
}
170
171
/**
172
 * Handle mautic tags by WordPress shortcodes
173
 * example: [mautic type="tags" values="addtag,-removetag"]
174
 *
175
 * @param  array $atts Shortcode attributes.
176
 *
177
 * @return string
178
 */
179 View Code Duplication
function wpmautic_tags_shortcode( $atts ) {
180 8
	$base_url = wpmautic_option( 'base_url', '' );
181 8
	if ( '' === $base_url ) {
182 2
		return false;
183
	}
184
185 6
	$atts = shortcode_atts(
186
		array(
187 6
			'values' => '',
188
		),
189 6
		$atts
190
	);
191
192 6
	if ( empty( $atts['values'] ) ) {
193 2
		return false;
194
	}
195
196 4
	return sprintf(
197 4
		'<img src="%s/mtracking.gif?tags=%s" alt="%s" style="display:none;" />',
198 4
		esc_url( $base_url ),
199 4
		esc_attr( $atts['values'] ),
200 4
		esc_attr__( 'Mautic Tags', 'wp-mautic' )
201
	);
202
}
203
204
/**
205
 * Handle mautic focus itens on WordPress Page
206
 * example: [mautic type="focus" id="1"]
207
 *
208
 * @param  array $atts Shortcode attributes.
209
 *
210
 * @return string
211
 */
212 View Code Duplication
function wpmautic_focus_shortcode( $atts ) {
213 6
	$base_url = wpmautic_option( 'base_url', '' );
214 6
	if ( '' === $base_url ) {
215 2
		return false;
216
	}
217
218 4
	$atts = shortcode_atts(
219
		array(
220 4
			'id' => '',
221
		),
222 4
		$atts
223
	);
224
225 4
	if ( empty( $atts['id'] ) ) {
226 2
		return false;
227
	}
228
229 2
	return '<script type="text/javascript" ' . sprintf(
230 2
		'src="%s/focus/%s.js"',
231 2
		esc_url( $base_url ),
232 2
		esc_attr( $atts['id'] )
233 2
	) . ' async="async"></script>';
234
}
235