PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 14.8.1
Jetpack – WP Security, Backup, Speed, & Growth v14.8.1
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / shortcodes / crowdsignal.php
jetpack / modules / shortcodes Last commit date
css 1 year ago images 2 years ago img 2 years ago js 1 year ago archiveorg-book.php 5 years ago archiveorg.php 5 years ago archives.php 5 years ago bandcamp.php 3 years ago brightcove.php 5 years ago cartodb.php 5 years ago class.filter-embedded-html-objects.php 2 years ago codepen.php 5 years ago crowdsignal.php 1 year ago dailymotion.php 3 years ago descript.php 4 years ago facebook.php 1 year ago flatio.php 5 years ago flickr.php 1 year ago getty.php 2 years ago gist.php 1 year ago googleapps.php 2 years ago googlemaps.php 2 years ago googleplus.php 5 years ago gravatar.php 2 years ago houzz.php 5 years ago inline-pdfs.php 4 years ago instagram.php 1 year ago kickstarter.php 3 years ago mailchimp.php 3 years ago medium.php 3 years ago mixcloud.php 2 years ago others.php 1 year ago pinterest.php 2 years ago presentations.php 2 years ago quiz.php 4 years ago recipe.php 1 year ago scribd.php 5 years ago sitemap.php 5 years ago slideshare.php 5 years ago slideshow.php 1 year ago smartframe.php 3 years ago soundcloud.php 3 years ago spotify.php 2 years ago ted.php 5 years ago tweet.php 2 years ago twitchtv.php 5 years ago twitter-timeline.php 5 years ago twitter.php 1 year ago unavailable.php 3 years ago untappd-menu.php 3 years ago upcoming-events.php 1 year ago ustream.php 5 years ago videopress.php 4 years ago vimeo.php 2 years ago vine.php 5 years ago vr.php 2 years ago wufoo.php 3 years ago youtube.php 1 year ago
crowdsignal.php
768 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Crowdsignal (PollDaddy) shortcode.
4 *
5 * Formats:
6 * [polldaddy type="iframe" survey="EB151947E5950FCF" height="auto" domain="jeherve" id="a-survey-with-branches"]
7 * [crowdsignal type="iframe" survey="EB151947E5950FCF" height="auto" domain="jeherve" id="a-survey-with-branches"]
8 * https://polldaddy.com/poll/7910844/
9 * https://jeherve.survey.fm/a-survey
10 * https://jeherve.survey.fm/a-survey-with-branches
11 * [crowdsignal type="iframe" survey="7676FB1FF2B56CE9" height="auto" domain="jeherve" id="a-survey"]
12 * [crowdsignal survey="7676FB1FF2B56CE9"]
13 * [polldaddy survey="7676FB1FF2B56CE9"]
14 * [crowdsignal poll=9541291]
15 * [crowdsignal poll=9541291 type=slider]
16 * [crowdsignal rating=8755352]
17 *
18 * @package automattic/jetpack
19 */
20
21 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
22
23 use Automattic\Jetpack\Assets;
24 use Automattic\Jetpack\Constants;
25
26 // Keep compatibility with the PollDaddy plugin.
27 if (
28 ! class_exists( 'CrowdsignalShortcode' )
29 && ! class_exists( 'PolldaddyShortcode' )
30 ) {
31 /**
32 * Class wrapper for Crowdsignal shortcodes
33 */
34 class CrowdsignalShortcode {
35
36 /**
37 * Should the Crowdsignal JavaScript be added to the page?
38 *
39 * @var bool
40 */
41 private static $add_script = false;
42
43 /**
44 * Array of Polls / Surveys present on the page, and that need to be added.
45 *
46 * @var bool|array
47 */
48 private static $scripts = false;
49
50 /**
51 * Add all the actions & register the shortcode.
52 */
53 public function __construct() {
54 add_action( 'init', array( $this, 'register_scripts' ) );
55
56 add_shortcode( 'crowdsignal', array( $this, 'crowdsignal_shortcode' ) );
57 add_shortcode( 'polldaddy', array( $this, 'polldaddy_shortcode' ) );
58
59 add_filter( 'pre_kses', array( $this, 'crowdsignal_embed_to_shortcode' ) );
60 add_action( 'infinite_scroll_render', array( $this, 'crowdsignal_shortcode_infinite' ), 11 );
61 }
62
63 /**
64 * Register scripts that may be enqueued later on by the shortcode.
65 */
66 public static function register_scripts() {
67 wp_register_script(
68 'crowdsignal-shortcode',
69 Assets::get_file_url_for_environment( '_inc/build/crowdsignal-shortcode.min.js', '_inc/crowdsignal-shortcode.js' ),
70 array(),
71 JETPACK__VERSION,
72 true
73 );
74 wp_register_script(
75 'crowdsignal-survey',
76 Assets::get_file_url_for_environment( '_inc/build/crowdsignal-survey.min.js', '_inc/crowdsignal-survey.js' ),
77 array(),
78 JETPACK__VERSION,
79 true
80 );
81 wp_register_script(
82 'crowdsignal-rating',
83 'https://polldaddy.com/js/rating/rating.js',
84 array(),
85 JETPACK__VERSION,
86 true
87 );
88 }
89
90 /**
91 * JavaScript code for a specific survey / poll.
92 *
93 * @param array $settings Array of information about a survey / poll.
94 * @param string $survey_link HTML link tag for a specific survey or poll.
95 * @param string $survey_url Link to the survey or poll.
96 */
97 private function get_async_code( array $settings, $survey_link, $survey_url ) {
98 wp_enqueue_script( 'crowdsignal-survey' );
99
100 if ( 'button' === $settings['type'] ) {
101 $placeholder = sprintf(
102 '<a class="cs-embed pd-embed" href="%1$s" data-settings="%2$s">%3$s</a>',
103 esc_url( $survey_url ),
104 esc_attr( wp_json_encode( $settings ) ),
105 esc_html( $settings['title'] )
106 );
107 } else {
108 $placeholder = sprintf(
109 '<div class="cs-embed pd-embed" data-settings="%1$s"></div><noscript>%2$s</noscript>',
110 esc_attr( wp_json_encode( $settings ) ),
111 $survey_link
112 );
113 }
114
115 return $placeholder;
116 }
117
118 /**
119 * Crowdsignal Poll Embed script - transforms code that looks like that:
120 * <script type="text/javascript" charset="utf-8" async src="http://static.polldaddy.com/p/123456.js"></script>
121 * <noscript><a href="http://polldaddy.com/poll/123456/">What is your favourite color?</a></noscript>
122 * into the [crowdsignal poll=...] shortcode format
123 *
124 * @param string $content Post content.
125 */
126 public function crowdsignal_embed_to_shortcode( $content ) {
127
128 if ( ! is_string( $content ) || ! str_contains( $content, 'polldaddy.com/p/' ) ) {
129 return $content;
130 }
131
132 $regexes = array();
133
134 $regexes[] = '#<script[^>]+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"[^>]*+>\s*?</script>\r?\n?(<noscript>.*?</noscript>)?#i';
135
136 $regexes[] = '#&lt;script(?:[^&]|&(?!gt;))+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/script&gt;\r?\n?(&lt;noscript&gt;.*?&lt;/noscript&gt;)?#i';
137
138 foreach ( $regexes as $regex ) {
139 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
140 continue;
141 }
142
143 foreach ( $matches as $match ) {
144 if ( ! isset( $match[2] ) ) {
145 continue;
146 }
147
148 $id = (int) $match[2];
149
150 if ( $id > 0 ) {
151 $content = str_replace( $match[0], " [crowdsignal poll=$id]", $content );
152 /** This action is documented in modules/shortcodes/youtube.php */
153 do_action( 'jetpack_embed_to_shortcode', 'crowdsignal', $id );
154 }
155 }
156 }
157
158 return $content;
159 }
160
161 /**
162 * Support for legacy Polldaddy shortcode.
163 *
164 * @param array $atts Shortcode attributes.
165 */
166 public function polldaddy_shortcode( $atts ) {
167 if ( ! is_array( $atts ) ) {
168 return '<!-- Polldaddy shortcode passed invalid attributes -->';
169 }
170
171 $atts['site'] = 'polldaddy.com';
172 return $this->crowdsignal_shortcode( $atts );
173 }
174
175 /**
176 * Shortcode for Crowdsignal
177 * [crowdsignal poll|survey|rating="123456"]
178 *
179 * @param array $atts Shortcode attributes.
180 */
181 public function crowdsignal_shortcode( $atts ) {
182 global $post;
183 global $content_width;
184
185 if ( ! is_array( $atts ) ) {
186 return '<!-- Crowdsignal shortcode passed invalid attributes -->';
187 }
188
189 $attributes = shortcode_atts(
190 array(
191 'survey' => null,
192 'link_text' => esc_html__( 'Take Our Survey', 'jetpack' ),
193 'poll' => 'empty',
194 'rating' => 'empty',
195 'unique_id' => null,
196 'item_id' => null,
197 'title' => null,
198 'permalink' => null,
199 'cb' => 0, // cache buster. Helps with testing.
200 'type' => 'button',
201 'body' => '',
202 'button' => '',
203 'text_color' => '000000',
204 'back_color' => 'FFFFFF',
205 'align' => '',
206 'style' => '',
207 'width' => $content_width,
208 'height' => floor( $content_width * 3 / 4 ),
209 'delay' => 100,
210 'visit' => 'single',
211 'domain' => '',
212 'id' => '',
213 'site' => 'crowdsignal.com',
214 ),
215 $atts,
216 'crowdsignal'
217 );
218
219 $inline = ! in_the_loop()
220 && ! Constants::is_defined( 'TESTING_IN_JETPACK' );
221
222 $no_script = false;
223 $infinite_scroll = false;
224
225 if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) {
226 $infinite_scroll = true;
227 }
228
229 if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) {
230 $inline = true;
231 }
232
233 if ( is_feed() || ( defined( 'DOING_AJAX' ) && ! $infinite_scroll ) ) {
234 $no_script = false;
235 }
236
237 self::$add_script = $infinite_scroll;
238
239 /*
240 * Rating embed.
241 */
242 if ( (int) $attributes['rating'] > 0 && ! $no_script ) {
243 $post_id = $post instanceof WP_Post ? $post->ID : get_the_ID();
244 $post_id = $post_id ?? '';
245
246 if ( empty( $attributes['unique_id'] ) ) {
247 $attributes['unique_id'] = is_page() ? 'wp-page-' . $post_id : 'wp-post-' . $post_id;
248 }
249
250 if ( empty( $attributes['item_id'] ) ) {
251 $attributes['item_id'] = is_page() ? '_page_' . $post_id : '_post_' . $post_id;
252 }
253
254 if ( empty( $attributes['title'] ) ) {
255 $title = $post instanceof WP_Post ? $post->post_title : get_the_title();
256 /** This filter is documented in core/src/wp-includes/general-template.php */
257 $attributes['title'] = apply_filters( 'wp_title', $title, '', '' );
258 }
259
260 if ( empty( $attributes['permalink'] ) ) {
261 if ( $post_id ) {
262 $attributes['permalink'] = get_permalink( $post_id );
263 } else {
264 $attributes['permalink'] = home_url( add_query_arg( array() ) );
265 }
266 }
267
268 $rating = (int) $attributes['rating'];
269 $unique_id = sanitize_key( wp_strip_all_tags( $attributes['unique_id'] ) );
270 $item_id = wp_strip_all_tags( $attributes['item_id'] );
271 $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id );
272
273 $settings = wp_json_encode(
274 array(
275 'id' => $rating,
276 'unique_id' => $unique_id,
277 'title' => rawurlencode( trim( $attributes['title'] ) ),
278 'permalink' => esc_url( $attributes['permalink'] ),
279 'item_id' => $item_id,
280 )
281 );
282
283 $item_id = esc_js( $item_id );
284
285 if (
286 class_exists( 'Jetpack_AMP_Support' )
287 && Jetpack_AMP_Support::is_amp_request()
288 ) {
289 return sprintf(
290 '<a href="%s" target="_blank">%s</a>',
291 esc_url( $attributes['permalink'] ),
292 esc_html( trim( $attributes['title'] ) )
293 );
294 } elseif ( $inline ) {
295 $rating_js = "<!--//--><![CDATA[//><!--\n";
296 $rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};";
297 $rating_js .= "\n//--><!]]>";
298
299 wp_enqueue_script( 'crowdsignal-rating' );
300 wp_add_inline_script(
301 'crowdsignal-rating',
302 $rating_js,
303 'before'
304 );
305
306 return sprintf(
307 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
308 absint( $rating ),
309 esc_attr( $item_id )
310 );
311 } else {
312 if ( false === self::$scripts ) {
313 self::$scripts = array();
314 }
315
316 $data = array(
317 'id' => $rating,
318 'item_id' => $item_id,
319 'settings' => $settings,
320 );
321
322 self::$scripts['rating'][] = $data;
323
324 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
325
326 if ( $infinite_scroll ) {
327 return sprintf(
328 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>',
329 absint( $rating ),
330 esc_attr( $item_id ),
331 esc_attr( wp_json_encode( $data ) )
332 );
333 } else {
334 return sprintf(
335 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
336 absint( $rating ),
337 esc_attr( $item_id )
338 );
339 }
340 }
341 } elseif ( (int) $attributes['poll'] > 0 ) {
342 /*
343 * Poll embed.
344 */
345
346 if ( empty( $attributes['title'] ) ) {
347 $attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' );
348 }
349
350 $poll = (int) $attributes['poll'];
351
352 if ( 'crowdsignal.com' === $attributes['site'] ) {
353 $poll_url = sprintf( 'https://poll.fm/%d', $poll );
354 } else {
355 $poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll );
356 }
357
358 $poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll );
359 $poll_link = sprintf(
360 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
361 esc_url( $poll_url ),
362 esc_html( $attributes['title'] )
363 );
364
365 if (
366 $no_script
367 || ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() )
368 ) {
369 return $poll_link;
370 } elseif ( 'slider' === $attributes['type'] && ! $inline ) { // Slider poll.
371 if ( ! in_array(
372 $attributes['visit'],
373 array( 'single', 'multiple' ),
374 true
375 ) ) {
376 $attributes['visit'] = 'single';
377 }
378
379 $settings = array(
380 'type' => 'slider',
381 'embed' => 'poll',
382 'delay' => (int) $attributes['delay'],
383 'visit' => $attributes['visit'],
384 'id' => (int) $poll,
385 'site' => $attributes['site'],
386 );
387
388 return $this->get_async_code( $settings, $poll_link, $poll_url );
389 } else {
390 if ( 1 === $attributes['cb'] ) {
391 $attributes['cb'] = '?cb=' . time();
392 } else {
393 $attributes['cb'] = false;
394 }
395 $margins = '';
396 $float = '';
397
398 if ( in_array(
399 $attributes['align'],
400 array( 'right', 'left' ),
401 true
402 ) ) {
403 $float = sprintf( 'float: %s;', $attributes['align'] );
404
405 if ( 'left' === $attributes['align'] ) {
406 $margins = 'margin: 0 10px 0 0;';
407 } elseif ( 'right' === $attributes['align'] ) {
408 $margins = 'margin: 0 0 0 10px';
409 }
410 }
411
412 /*
413 * Force the normal style embed on single posts/pages
414 * otherwise it's not rendered on infinite scroll themed blogs
415 * ('infinite_scroll_render' isn't fired)
416 */
417 if ( is_singular() ) {
418 $inline = true;
419 }
420
421 if ( false === $attributes['cb'] && ! $inline ) {
422 if ( false === self::$scripts ) {
423 self::$scripts = array();
424 }
425
426 $data = array( 'url' => $poll_js );
427
428 self::$scripts['poll'][ (int) $poll ] = $data;
429
430 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
431
432 wp_enqueue_script( 'crowdsignal-shortcode' );
433 wp_localize_script(
434 'crowdsignal-shortcode',
435 'crowdsignal_shortcode_options',
436 array(
437 'script_url' => esc_url_raw(
438 Assets::get_file_url_for_environment(
439 '_inc/build/polldaddy-shortcode.min.js',
440 '_inc/polldaddy-shortcode.js'
441 )
442 ),
443 )
444 );
445
446 /**
447 * Hook into the Crowdsignal shortcode before rendering.
448 *
449 * @since 8.4.0
450 *
451 * @param int $poll Poll ID.
452 */
453 do_action( 'crowdsignal_shortcode_before', (int) $poll );
454
455 return sprintf(
456 '<a name="pd_a_%1$d"></a><div class="CSS_Poll PDS_Poll" id="PDI_container%1$d" data-settings="%2$s" style="%3$s%4$s"></div><div id="PD_superContainer"></div><noscript>%5$s</noscript>',
457 absint( $poll ),
458 esc_attr( wp_json_encode( $data ) ),
459 $float,
460 $margins,
461 $poll_link
462 );
463 } else {
464 if ( $inline ) {
465 $attributes['cb'] = '';
466 }
467
468 wp_enqueue_script(
469 'crowdsignal-' . absint( $poll ),
470 esc_url( $poll_js . $attributes['cb'] ),
471 array(),
472 JETPACK__VERSION,
473 true
474 );
475
476 /** This action is already documented in modules/shortcodes/crowdsignal.php */
477 do_action( 'crowdsignal_shortcode_before', (int) $poll );
478
479 return sprintf(
480 '<a id="pd_a_%1$s"></a><div class="CSS_Poll PDS_Poll" id="PDI_container%1$s" style="%2$s%3$s"></div><div id="PD_superContainer"></div><noscript>%4$s</noscript>',
481 absint( $poll ),
482 $float,
483 $margins,
484 $poll_link
485 );
486 }
487 }
488 } elseif ( ! empty( $attributes['survey'] ) ) {
489 /*
490 * Survey embed.
491 */
492
493 if ( in_array(
494 $attributes['type'],
495 array( 'iframe', 'button', 'banner', 'slider' ),
496 true
497 ) ) {
498
499 if ( empty( $attributes['title'] ) ) {
500 $attributes['title'] = esc_html__( 'Take Our Survey', 'jetpack' );
501 if ( ! empty( $attributes['link_text'] ) ) {
502 $attributes['title'] = $attributes['link_text'];
503 }
504 }
505
506 if (
507 'banner' === $attributes['type']
508 || 'slider' === $attributes['type']
509 ) {
510 $inline = false;
511 }
512
513 $survey_url = '';
514
515 if ( 'true' !== $attributes['survey'] ) {
516 $survey = preg_replace( '/[^a-f0-9]/i', '', $attributes['survey'] );
517
518 if ( 'crowdsignal.com' === $attributes['site'] ) {
519 $survey_url = 'https://survey.fm/' . $survey;
520 } else {
521 $survey_url = 'https://polldaddy.com/s/' . $survey;
522 }
523 } elseif ( isset( $attributes['domain'] ) && isset( $attributes['id'] ) ) {
524 $survey_domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
525 $survey_id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
526 $survey_url = sprintf(
527 'https://%1$s.survey.fm/%2$s',
528 $survey_domain,
529 $survey_id
530 );
531 }
532
533 $survey_link = sprintf(
534 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
535 esc_url( $survey_url ),
536 esc_html( $attributes['title'] )
537 );
538
539 $settings = array();
540
541 if ( 'iframe' === $attributes['type'] ) {
542 if ( 'auto' !== $attributes['height'] ) {
543 if (
544 is_numeric( $content_width )
545 && $content_width > 0
546 && is_numeric( $attributes['width'] )
547 && $attributes['width'] > $content_width
548 ) {
549 $attributes['width'] = $content_width;
550 }
551
552 if ( ! $attributes['width'] ) {
553 $attributes['width'] = '100%';
554 } else {
555 $attributes['width'] = (int) $attributes['width'];
556 }
557
558 if ( ! $attributes['height'] ) {
559 $attributes['height'] = '600';
560 } else {
561 $attributes['height'] = (int) $attributes['height'];
562 }
563
564 return sprintf(
565 '<iframe src="%1$s?iframe=1" frameborder="0" width="%2$d" height="%3$d" scrolling="auto" allowtransparency="true" marginheight="0" marginwidth="0">%4$s</iframe>',
566 esc_url( $survey_url ),
567 absint( $attributes['width'] ),
568 absint( $attributes['height'] ),
569 $survey_link
570 );
571 } elseif (
572 ! empty( $attributes['domain'] )
573 && ! empty( $attributes['id'] )
574 ) {
575 $domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
576 $id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
577
578 $auto_src = esc_url( "https://{$domain}.survey.fm/{$id}" );
579 $auto_src = wp_parse_url( $auto_src );
580
581 if ( ! is_array( $auto_src ) || array() === $auto_src ) {
582 return '<!-- no crowdsignal output -->';
583 }
584
585 if ( ! isset( $auto_src['host'] ) || ! isset( $auto_src['path'] ) ) {
586 return '<!-- no crowdsignal output -->';
587 }
588
589 $domain = $auto_src['host'] . '/';
590 $id = ltrim( $auto_src['path'], '/' );
591
592 $settings = array(
593 'type' => $attributes['type'],
594 'auto' => true,
595 'domain' => $domain,
596 'id' => $id,
597 'site' => $attributes['site'],
598 );
599 }
600 } else {
601 $text_color = sanitize_hex_color_no_hash( $attributes['text_color'] );
602 $back_color = sanitize_hex_color_no_hash( $attributes['back_color'] );
603
604 if (
605 ! in_array(
606 $attributes['align'],
607 array(
608 'right',
609 'left',
610 'top-left',
611 'top-right',
612 'middle-left',
613 'middle-right',
614 'bottom-left',
615 'bottom-right',
616 ),
617 true
618 )
619 ) {
620 $attributes['align'] = '';
621 }
622
623 if (
624 ! in_array(
625 $attributes['style'],
626 array(
627 'inline',
628 'side',
629 'corner',
630 'rounded',
631 'square',
632 ),
633 true
634 )
635 ) {
636 $attributes['style'] = '';
637 }
638
639 $settings = array_filter(
640 array(
641 'title' => wp_strip_all_tags( $attributes['title'] ),
642 'type' => $attributes['type'],
643 'body' => wp_strip_all_tags( $attributes['body'] ),
644 'button' => wp_strip_all_tags( $attributes['button'] ),
645 'text_color' => $text_color,
646 'back_color' => $back_color,
647 'align' => $attributes['align'],
648 'style' => $attributes['style'],
649 'id' => $survey,
650 'site' => $attributes['site'],
651 )
652 );
653 }
654
655 if ( empty( $settings ) ) {
656 return '<!-- no crowdsignal output -->';
657 }
658
659 return $this->get_async_code( $settings, $survey_link, $survey_url );
660 }
661 } else {
662 return '<!-- no crowdsignal output -->';
663 }
664 }
665
666 /**
667 * Enqueue JavaScript containing all ratings / polls on the page.
668 * Hooked into wp_footer
669 */
670 public function generate_scripts() {
671 if ( is_array( self::$scripts ) ) {
672 if ( isset( self::$scripts['rating'] ) ) {
673 $script = "<!--//--><![CDATA[//><!--\n";
674 foreach ( self::$scripts['rating'] as $rating ) {
675 $script .= "PDRTJS_settings_{$rating['id']}{$rating['item_id']}={$rating['settings']}; if ( typeof PDRTJS_RATING !== 'undefined' ){if ( typeof PDRTJS_{$rating['id']}{$rating['item_id']} == 'undefined' ){PDRTJS_{$rating['id']}{$rating['item_id']} = new PDRTJS_RATING( PDRTJS_settings_{$rating['id']}{$rating['item_id']} );}}";
676 }
677 $script .= "\n//--><!]]>";
678
679 wp_enqueue_script( 'crowdsignal-rating' );
680 wp_add_inline_script(
681 'crowdsignal-rating',
682 $script,
683 'before'
684 );
685 }
686
687 if ( isset( self::$scripts['poll'] ) ) {
688 foreach ( self::$scripts['poll'] as $poll_id => $poll ) {
689 wp_enqueue_script(
690 'crowdsignal-' . absint( $poll_id ),
691 esc_url( $poll['url'] ),
692 array(),
693 JETPACK__VERSION,
694 true
695 );
696 }
697 }
698 }
699 self::$scripts = false;
700 }
701
702 /**
703 * Dynamically load the .js, if needed
704 *
705 * This hooks in late (priority 11) to infinite_scroll_render to determine
706 * a posteriori if a shortcode has been called.
707 */
708 public function crowdsignal_shortcode_infinite() {
709 // only try to load if a shortcode has been called and theme supports infinite scroll.
710 if ( self::$add_script ) {
711 wp_enqueue_script( 'crowdsignal-shortcode' );
712 wp_localize_script(
713 'crowdsignal-shortcode',
714 'crowdsignal_shortcode_options',
715 array(
716 'script_url' => esc_url_raw(
717 Assets::get_file_url_for_environment(
718 '_inc/build/polldaddy-shortcode.min.js',
719 '_inc/polldaddy-shortcode.js'
720 )
721 ),
722 )
723 );
724 }
725 }
726 }
727
728 // Kick it all off.
729 new CrowdsignalShortcode();
730
731 if ( ! function_exists( 'crowdsignal_link' ) ) {
732 /**
733 * Replace link with shortcode.
734 * Examples: https://poll.fm/10499328 | https://7iger.survey.fm/test-embed
735 *
736 * @param string $content Post content.
737 */
738 function crowdsignal_link( $content ) {
739 if (
740 class_exists( 'Jetpack_AMP_Support' )
741 && Jetpack_AMP_Support::is_amp_request()
742 ) {
743 return $content;
744 }
745
746 // Replace poll links.
747 $content = jetpack_preg_replace_outside_tags(
748 '!(?:\n|\A)https?://(polldaddy\.com/poll|poll\.fm)/([0-9]+?)(/.*)?(?:\n|\Z)!i',
749 '[crowdsignal poll=$2]',
750 $content
751 );
752
753 // Replace survey.fm links.
754 $content = preg_replace(
755 '!(?:\n|\A)https?://(.*).survey.fm/(.*)(/.*)?(?:\n|\Z)!i',
756 '[crowdsignal type="iframe" survey="true" height="auto" domain="$1" id="$2"]',
757 $content
758 );
759
760 return $content;
761 }
762
763 // higher priority because we need it before auto-link and autop get to it.
764 add_filter( 'the_content', 'crowdsignal_link', 1 );
765 add_filter( 'the_content_rss', 'crowdsignal_link', 1 );
766 }
767 }
768