PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.3
Jetpack – WP Security, Backup, Speed, & Growth v12.3
16.2-beta 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 All 501 releases
jetpack / modules / shortcodes / crowdsignal.php
crowdsignal.php
755 lines 21.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ) || false === strpos( $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
244 if ( empty( $attributes['unique_id'] ) ) {
245 $attributes['unique_id'] = is_page() ? 'wp-page-' . $post->ID : 'wp-post-' . $post->ID;
246 }
247
248 if ( empty( $attributes['item_id'] ) ) {
249 $attributes['item_id'] = is_page() ? '_page_' . $post->ID : '_post_' . $post->ID;
250 }
251
252 if ( empty( $attributes['title'] ) ) {
253 /** This filter is documented in core/src/wp-includes/general-template.php */
254 $attributes['title'] = apply_filters( 'wp_title', $post->post_title, '', '' );
255 }
256
257 if ( empty( $attributes['permalink'] ) ) {
258 $attributes['permalink'] = get_permalink( $post->ID );
259 }
260
261 $rating = (int) $attributes['rating'];
262 $unique_id = preg_replace( '/[^\-_a-z0-9]/i', '', wp_strip_all_tags( $attributes['unique_id'] ) );
263 $item_id = wp_strip_all_tags( $attributes['item_id'] );
264 $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id );
265
266 $settings = wp_json_encode(
267 array(
268 'id' => $rating,
269 'unique_id' => $unique_id,
270 'title' => rawurlencode( trim( $attributes['title'] ) ),
271 'permalink' => esc_url( $attributes['permalink'] ),
272 'item_id' => $item_id,
273 )
274 );
275
276 $item_id = esc_js( $item_id );
277
278 if (
279 class_exists( 'Jetpack_AMP_Support' )
280 && Jetpack_AMP_Support::is_amp_request()
281 ) {
282 return sprintf(
283 '<a href="%s" target="_blank">%s</a>',
284 esc_url( $attributes['permalink'] ),
285 esc_html( trim( $attributes['title'] ) )
286 );
287 } elseif ( $inline ) {
288 $rating_js = "<!--//--><![CDATA[//><!--\n";
289 $rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};";
290 $rating_js .= "\n//--><!]]>";
291
292 wp_enqueue_script( 'crowdsignal-rating' );
293 wp_add_inline_script(
294 'crowdsignal-rating',
295 $rating_js,
296 'before'
297 );
298
299 return sprintf(
300 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
301 absint( $rating ),
302 esc_attr( $item_id )
303 );
304 } else {
305 if ( false === self::$scripts ) {
306 self::$scripts = array();
307 }
308
309 $data = array(
310 'id' => $rating,
311 'item_id' => $item_id,
312 'settings' => $settings,
313 );
314
315 self::$scripts['rating'][] = $data;
316
317 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
318
319 if ( $infinite_scroll ) {
320 return sprintf(
321 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>',
322 absint( $rating ),
323 esc_attr( $item_id ),
324 esc_attr( wp_json_encode( $data ) )
325 );
326 } else {
327 return sprintf(
328 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
329 absint( $rating ),
330 esc_attr( $item_id )
331 );
332 }
333 }
334 } elseif ( (int) $attributes['poll'] > 0 ) {
335 /*
336 * Poll embed.
337 */
338
339 if ( empty( $attributes['title'] ) ) {
340 $attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' );
341 }
342
343 $poll = (int) $attributes['poll'];
344
345 if ( 'crowdsignal.com' === $attributes['site'] ) {
346 $poll_url = sprintf( 'https://poll.fm/%d', $poll );
347 } else {
348 $poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll );
349 }
350
351 $poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll );
352 $poll_link = sprintf(
353 '<a href="%s" target="_blank">%s</a>',
354 esc_url( $poll_url ),
355 esc_html( $attributes['title'] )
356 );
357
358 if (
359 $no_script
360 || ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() )
361 ) {
362 return $poll_link;
363 } elseif ( 'slider' === $attributes['type'] && ! $inline ) { // Slider poll.
364 if ( ! in_array(
365 $attributes['visit'],
366 array( 'single', 'multiple' ),
367 true
368 ) ) {
369 $attributes['visit'] = 'single';
370 }
371
372 $settings = array(
373 'type' => 'slider',
374 'embed' => 'poll',
375 'delay' => (int) $attributes['delay'],
376 'visit' => $attributes['visit'],
377 'id' => (int) $poll,
378 'site' => $attributes['site'],
379 );
380
381 return $this->get_async_code( $settings, $poll_link, $poll_url );
382 } else {
383 if ( 1 === $attributes['cb'] ) {
384 $attributes['cb'] = '?cb=' . time();
385 } else {
386 $attributes['cb'] = false;
387 }
388 $margins = '';
389 $float = '';
390
391 if ( in_array(
392 $attributes['align'],
393 array( 'right', 'left' ),
394 true
395 ) ) {
396 $float = sprintf( 'float: %s;', $attributes['align'] );
397
398 if ( 'left' === $attributes['align'] ) {
399 $margins = 'margin: 0px 10px 0px 0px;';
400 } elseif ( 'right' === $attributes['align'] ) {
401 $margins = 'margin: 0px 0px 0px 10px';
402 }
403 }
404
405 /*
406 * Force the normal style embed on single posts/pages
407 * otherwise it's not rendered on infinite scroll themed blogs
408 * ('infinite_scroll_render' isn't fired)
409 */
410 if ( is_singular() ) {
411 $inline = true;
412 }
413
414 if ( false === $attributes['cb'] && ! $inline ) {
415 if ( false === self::$scripts ) {
416 self::$scripts = array();
417 }
418
419 $data = array( 'url' => $poll_js );
420
421 self::$scripts['poll'][ (int) $poll ] = $data;
422
423 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
424
425 wp_enqueue_script( 'crowdsignal-shortcode' );
426 wp_localize_script(
427 'crowdsignal-shortcode',
428 'crowdsignal_shortcode_options',
429 array(
430 'script_url' => esc_url_raw(
431 Assets::get_file_url_for_environment(
432 '_inc/build/polldaddy-shortcode.min.js',
433 '_inc/polldaddy-shortcode.js'
434 )
435 ),
436 )
437 );
438
439 /**
440 * Hook into the Crowdsignal shortcode before rendering.
441 *
442 * @since 8.4.0
443 *
444 * @param int $poll Poll ID.
445 */
446 do_action( 'crowdsignal_shortcode_before', (int) $poll );
447
448 return sprintf(
449 '<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>',
450 absint( $poll ),
451 esc_attr( wp_json_encode( $data ) ),
452 $float,
453 $margins,
454 $poll_link
455 );
456 } else {
457 if ( $inline ) {
458 $attributes['cb'] = '';
459 }
460
461 wp_enqueue_script(
462 'crowdsignal-' . absint( $poll ),
463 esc_url( $poll_js . $attributes['cb'] ),
464 array(),
465 JETPACK__VERSION,
466 true
467 );
468
469 /** This action is already documented in modules/shortcodes/crowdsignal.php */
470 do_action( 'crowdsignal_shortcode_before', (int) $poll );
471
472 return sprintf(
473 '<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>',
474 absint( $poll ),
475 $float,
476 $margins,
477 $poll_link
478 );
479 }
480 }
481 } elseif ( ! empty( $attributes['survey'] ) ) {
482 /*
483 * Survey embed.
484 */
485
486 if ( in_array(
487 $attributes['type'],
488 array( 'iframe', 'button', 'banner', 'slider' ),
489 true
490 ) ) {
491
492 if ( empty( $attributes['title'] ) ) {
493 $attributes['title'] = esc_html__( 'Take Our Survey', 'jetpack' );
494 if ( ! empty( $attributes['link_text'] ) ) {
495 $attributes['title'] = $attributes['link_text'];
496 }
497 }
498
499 if (
500 'banner' === $attributes['type']
501 || 'slider' === $attributes['type']
502 ) {
503 $inline = false;
504 }
505
506 $survey_url = '';
507
508 if ( 'true' !== $attributes['survey'] ) {
509 $survey = preg_replace( '/[^a-f0-9]/i', '', $attributes['survey'] );
510
511 if ( 'crowdsignal.com' === $attributes['site'] ) {
512 $survey_url = 'https://survey.fm/' . $survey;
513 } else {
514 $survey_url = 'https://polldaddy.com/s/' . $survey;
515 }
516 } elseif ( isset( $attributes['domain'] ) && isset( $attributes['id'] ) ) {
517 $survey_url = 'https://' . $attributes['domain'] . '.survey.fm/' . $attributes['id'];
518 }
519
520 $survey_link = sprintf(
521 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
522 esc_url( $survey_url ),
523 esc_html( $attributes['title'] )
524 );
525
526 $settings = array();
527
528 if ( 'iframe' === $attributes['type'] ) {
529 if ( 'auto' !== $attributes['height'] ) {
530 if (
531 is_numeric( $content_width )
532 && $content_width > 0
533 && is_numeric( $attributes['width'] )
534 && $attributes['width'] > $content_width
535 ) {
536 $attributes['width'] = $content_width;
537 }
538
539 if ( ! $attributes['width'] ) {
540 $attributes['width'] = '100%';
541 } else {
542 $attributes['width'] = (int) $attributes['width'];
543 }
544
545 if ( ! $attributes['height'] ) {
546 $attributes['height'] = '600';
547 } else {
548 $attributes['height'] = (int) $attributes['height'];
549 }
550
551 return sprintf(
552 '<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>',
553 esc_url( $survey_url ),
554 absint( $attributes['width'] ),
555 absint( $attributes['height'] ),
556 $survey_link
557 );
558 } elseif (
559 ! empty( $attributes['domain'] )
560 && ! empty( $attributes['id'] )
561 ) {
562 $domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
563 $id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
564
565 $auto_src = esc_url( "https://{$domain}.survey.fm/{$id}" );
566 $auto_src = wp_parse_url( $auto_src );
567
568 if ( ! is_array( $auto_src ) || array() === $auto_src ) {
569 return '<!-- no crowdsignal output -->';
570 }
571
572 if ( ! isset( $auto_src['host'] ) || ! isset( $auto_src['path'] ) ) {
573 return '<!-- no crowdsignal output -->';
574 }
575
576 $domain = $auto_src['host'] . '/';
577 $id = ltrim( $auto_src['path'], '/' );
578
579 $settings = array(
580 'type' => $attributes['type'],
581 'auto' => true,
582 'domain' => $domain,
583 'id' => $id,
584 'site' => $attributes['site'],
585 );
586 }
587 } else {
588 $text_color = preg_replace( '/[^a-f0-9]/i', '', $attributes['text_color'] );
589 $back_color = preg_replace( '/[^a-f0-9]/i', '', $attributes['back_color'] );
590
591 if (
592 ! in_array(
593 $attributes['align'],
594 array(
595 'right',
596 'left',
597 'top-left',
598 'top-right',
599 'middle-left',
600 'middle-right',
601 'bottom-left',
602 'bottom-right',
603 ),
604 true
605 )
606 ) {
607 $attributes['align'] = '';
608 }
609
610 if (
611 ! in_array(
612 $attributes['style'],
613 array(
614 'inline',
615 'side',
616 'corner',
617 'rounded',
618 'square',
619 ),
620 true
621 )
622 ) {
623 $attributes['style'] = '';
624 }
625
626 $settings = array_filter(
627 array(
628 'title' => wp_strip_all_tags( $attributes['title'] ),
629 'type' => $attributes['type'],
630 'body' => wp_strip_all_tags( $attributes['body'] ),
631 'button' => wp_strip_all_tags( $attributes['button'] ),
632 'text_color' => $text_color,
633 'back_color' => $back_color,
634 'align' => $attributes['align'],
635 'style' => $attributes['style'],
636 'id' => $survey,
637 'site' => $attributes['site'],
638 )
639 );
640 }
641
642 if ( empty( $settings ) ) {
643 return '<!-- no crowdsignal output -->';
644 }
645
646 return $this->get_async_code( $settings, $survey_link, $survey_url );
647 }
648 } else {
649 return '<!-- no crowdsignal output -->';
650 }
651 }
652
653 /**
654 * Enqueue JavaScript containing all ratings / polls on the page.
655 * Hooked into wp_footer
656 */
657 public function generate_scripts() {
658 if ( is_array( self::$scripts ) ) {
659 if ( isset( self::$scripts['rating'] ) ) {
660 $script = "<!--//--><![CDATA[//><!--\n";
661 foreach ( self::$scripts['rating'] as $rating ) {
662 $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']} );}}";
663 }
664 $script .= "\n//--><!]]>";
665
666 wp_enqueue_script( 'crowdsignal-rating' );
667 wp_add_inline_script(
668 'crowdsignal-rating',
669 $script,
670 'before'
671 );
672 }
673
674 if ( isset( self::$scripts['poll'] ) ) {
675 foreach ( self::$scripts['poll'] as $poll_id => $poll ) {
676 wp_enqueue_script(
677 'crowdsignal-' . absint( $poll_id ),
678 esc_url( $poll['url'] ),
679 array(),
680 JETPACK__VERSION,
681 true
682 );
683 }
684 }
685 }
686 self::$scripts = false;
687 }
688
689 /**
690 * Dynamically load the .js, if needed
691 *
692 * This hooks in late (priority 11) to infinite_scroll_render to determine
693 * a posteriori if a shortcode has been called.
694 */
695 public function crowdsignal_shortcode_infinite() {
696 // only try to load if a shortcode has been called and theme supports infinite scroll.
697 if ( self::$add_script ) {
698 wp_enqueue_script( 'crowdsignal-shortcode' );
699 wp_localize_script(
700 'crowdsignal-shortcode',
701 'crowdsignal_shortcode_options',
702 array(
703 'script_url' => esc_url_raw(
704 Assets::get_file_url_for_environment(
705 '_inc/build/polldaddy-shortcode.min.js',
706 '_inc/polldaddy-shortcode.js'
707 )
708 ),
709 )
710 );
711 }
712 }
713 }
714
715 // Kick it all off.
716 new CrowdsignalShortcode();
717
718 if ( ! function_exists( 'crowdsignal_link' ) ) {
719 /**
720 * Replace link with shortcode.
721 * Examples: https://poll.fm/10499328 | https://7iger.survey.fm/test-embed
722 *
723 * @param string $content Post content.
724 */
725 function crowdsignal_link( $content ) {
726 if (
727 class_exists( 'Jetpack_AMP_Support' )
728 && Jetpack_AMP_Support::is_amp_request()
729 ) {
730 return $content;
731 }
732
733 // Replace poll links.
734 $content = jetpack_preg_replace_outside_tags(
735 '!(?:\n|\A)https?://(polldaddy\.com/poll|poll\.fm)/([0-9]+?)(/.*)?(?:\n|\Z)!i',
736 '[crowdsignal poll=$2]',
737 $content
738 );
739
740 // Replace survey.fm links.
741 $content = preg_replace(
742 '!(?:\n|\A)https?://(.*).survey.fm/(.*)(/.*)?(?:\n|\Z)!i',
743 '[crowdsignal type="iframe" survey="true" height="auto" domain="$1" id="$2"]',
744 $content
745 );
746
747 return $content;
748 }
749
750 // higher priority because we need it before auto-link and autop get to it.
751 add_filter( 'the_content', 'crowdsignal_link', 1 );
752 add_filter( 'the_content_rss', 'crowdsignal_link', 1 );
753 }
754 }
755