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.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 month ago images 1 year ago img 2 months ago js 7 months ago archiveorg-book.php 7 months ago archiveorg.php 1 month ago archives.php 1 week ago bandcamp.php 7 months ago brightcove.php 6 months ago cartodb.php 7 months ago class.filter-embedded-html-objects.php 7 months ago codepen.php 7 months ago crowdsignal.php 6 months ago dailymotion.php 7 months ago descript.php 7 months ago facebook.php 7 months ago flatio.php 7 months ago flickr.php 7 months ago getty.php 7 months ago gist.php 7 months ago googleapps.php 7 months ago googlemaps.php 1 month ago googleplus.php 7 months ago gravatar.php 7 months ago houzz.php 7 months ago inline-pdfs.php 7 months ago instagram.php 7 months ago kickstarter.php 7 months ago mailchimp.php 1 month ago medium.php 7 months ago mixcloud.php 7 months ago others.php 7 months ago pinterest.php 7 months ago presentations.php 7 months ago quiz.php 7 months ago recipe.php 2 weeks ago scribd.php 7 months ago shortcode-utils.php 7 months ago sitemap.php 7 months ago slideshare.php 7 months ago slideshow.php 2 weeks ago smartframe.php 7 months ago soundcloud.php 1 month ago spotify.php 7 months ago ted.php 7 months ago tweet.php 7 months ago twitchtv.php 2 weeks ago twitter-timeline.php 7 months ago twitter.php 7 months ago unavailable.php 7 months ago untappd-menu.php 7 months ago upcoming-events.php 7 months ago ustream.php 7 months ago videopress.php 7 months ago vimeo.php 1 month ago vine.php 7 months ago vr.php 1 month ago wufoo.php 7 months ago youtube.php 4 months 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 if ( ! defined( 'ABSPATH' ) ) {
27 exit( 0 );
28 }
29
30 // Keep compatibility with the PollDaddy plugin.
31 if (
32 ! class_exists( 'CrowdsignalShortcode' )
33 && ! class_exists( 'PolldaddyShortcode' )
34 ) {
35 /**
36 * Class wrapper for Crowdsignal shortcodes
37 *
38 * @phan-constructor-used-for-side-effects
39 */
40 class CrowdsignalShortcode {
41
42 /**
43 * Should the Crowdsignal JavaScript be added to the page?
44 *
45 * @var bool
46 */
47 private static $add_script = false;
48
49 /**
50 * Array of Polls / Surveys present on the page, and that need to be added.
51 *
52 * @var bool|array
53 */
54 private static $scripts = false;
55
56 /**
57 * Add all the actions & register the shortcode.
58 */
59 public function __construct() {
60 add_action( 'init', array( $this, 'register_scripts' ) );
61
62 add_shortcode( 'crowdsignal', array( $this, 'crowdsignal_shortcode' ) );
63 add_shortcode( 'polldaddy', array( $this, 'polldaddy_shortcode' ) );
64
65 if ( jetpack_shortcodes_should_hook_pre_kses() ) {
66 add_filter( 'pre_kses', array( $this, 'crowdsignal_embed_to_shortcode' ) );
67 }
68
69 add_action( 'infinite_scroll_render', array( $this, 'crowdsignal_shortcode_infinite' ), 11 );
70 }
71
72 /**
73 * Register scripts that may be enqueued later on by the shortcode.
74 */
75 public static function register_scripts() {
76 wp_register_script(
77 'crowdsignal-shortcode',
78 Assets::get_file_url_for_environment( '_inc/build/crowdsignal-shortcode.min.js', '_inc/crowdsignal-shortcode.js' ),
79 array(),
80 JETPACK__VERSION,
81 true
82 );
83 wp_register_script(
84 'crowdsignal-survey',
85 Assets::get_file_url_for_environment( '_inc/build/crowdsignal-survey.min.js', '_inc/crowdsignal-survey.js' ),
86 array(),
87 JETPACK__VERSION,
88 true
89 );
90 wp_register_script(
91 'crowdsignal-rating',
92 'https://polldaddy.com/js/rating/rating.js',
93 array(),
94 JETPACK__VERSION,
95 true
96 );
97 }
98
99 /**
100 * JavaScript code for a specific survey / poll.
101 *
102 * @param array $settings Array of information about a survey / poll.
103 * @param string $survey_link HTML link tag for a specific survey or poll.
104 * @param string $survey_url Link to the survey or poll.
105 */
106 private function get_async_code( array $settings, $survey_link, $survey_url ) {
107 wp_enqueue_script( 'crowdsignal-survey' );
108
109 if ( 'button' === $settings['type'] ) {
110 $placeholder = sprintf(
111 '<a class="cs-embed pd-embed" href="%1$s" data-settings="%2$s">%3$s</a>',
112 esc_url( $survey_url ),
113 esc_attr( wp_json_encode( $settings, JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ),
114 esc_html( $settings['title'] )
115 );
116 } else {
117 $placeholder = sprintf(
118 '<div class="cs-embed pd-embed" data-settings="%1$s"></div><noscript>%2$s</noscript>',
119 esc_attr( wp_json_encode( $settings, JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ),
120 $survey_link
121 );
122 }
123
124 return $placeholder;
125 }
126
127 /**
128 * Crowdsignal Poll Embed script - transforms code that looks like that:
129 * <script type="text/javascript" charset="utf-8" async src="http://static.polldaddy.com/p/123456.js"></script>
130 * <noscript><a href="http://polldaddy.com/poll/123456/">What is your favourite color?</a></noscript>
131 * into the [crowdsignal poll=...] shortcode format
132 *
133 * @param string $content Post content.
134 */
135 public function crowdsignal_embed_to_shortcode( $content ) {
136
137 if ( ! is_string( $content ) || ! str_contains( $content, 'polldaddy.com/p/' ) ) {
138 return $content;
139 }
140
141 $regexes = array();
142
143 $regexes[] = '#<script[^>]+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"[^>]*+>\s*?</script>\r?\n?(<noscript>.*?</noscript>)?#i';
144
145 $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';
146
147 foreach ( $regexes as $regex ) {
148 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
149 continue;
150 }
151
152 foreach ( $matches as $match ) {
153 if ( ! isset( $match[2] ) ) {
154 continue;
155 }
156
157 $id = (int) $match[2];
158
159 if ( $id > 0 ) {
160 $content = str_replace( $match[0], " [crowdsignal poll=$id]", $content );
161 /** This action is documented in modules/shortcodes/youtube.php */
162 do_action( 'jetpack_embed_to_shortcode', 'crowdsignal', $id );
163 }
164 }
165 }
166
167 return $content;
168 }
169
170 /**
171 * Support for legacy Polldaddy shortcode.
172 *
173 * @param array $atts Shortcode attributes.
174 */
175 public function polldaddy_shortcode( $atts ) {
176 if ( ! is_array( $atts ) ) {
177 return '<!-- Polldaddy shortcode passed invalid attributes -->';
178 }
179
180 $atts['site'] = 'polldaddy.com';
181 return $this->crowdsignal_shortcode( $atts );
182 }
183
184 /**
185 * Shortcode for Crowdsignal
186 * [crowdsignal poll|survey|rating="123456"]
187 *
188 * @param array $atts Shortcode attributes.
189 */
190 public function crowdsignal_shortcode( $atts ) {
191 global $post;
192 global $content_width;
193
194 if ( ! is_array( $atts ) ) {
195 return '<!-- Crowdsignal shortcode passed invalid attributes -->';
196 }
197
198 $attributes = shortcode_atts(
199 array(
200 'survey' => null,
201 'link_text' => esc_html__( 'Take Our Survey', 'jetpack' ),
202 'poll' => 'empty',
203 'rating' => 'empty',
204 'unique_id' => null,
205 'item_id' => null,
206 'title' => null,
207 'permalink' => null,
208 'cb' => 0, // cache buster. Helps with testing.
209 'type' => 'button',
210 'body' => '',
211 'button' => '',
212 'text_color' => '000000',
213 'back_color' => 'FFFFFF',
214 'align' => '',
215 'style' => '',
216 'width' => $content_width,
217 'height' => floor( $content_width * 3 / 4 ),
218 'delay' => 100,
219 'visit' => 'single',
220 'domain' => '',
221 'id' => '',
222 'site' => 'crowdsignal.com',
223 ),
224 $atts,
225 'crowdsignal'
226 );
227
228 $inline = ! in_the_loop()
229 && ! Constants::is_defined( 'TESTING_IN_JETPACK' );
230
231 $infinite_scroll = false;
232
233 if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) {
234 $infinite_scroll = true;
235 }
236
237 if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) {
238 $inline = true;
239 }
240
241 self::$add_script = $infinite_scroll;
242
243 /*
244 * Rating embed.
245 */
246 if ( (int) $attributes['rating'] > 0 ) {
247 $post_id = $post instanceof WP_Post ? $post->ID : get_the_ID();
248 $post_id = $post_id ?? '';
249
250 if ( empty( $attributes['unique_id'] ) ) {
251 $attributes['unique_id'] = is_page() ? 'wp-page-' . $post_id : 'wp-post-' . $post_id;
252 }
253
254 if ( empty( $attributes['item_id'] ) ) {
255 $attributes['item_id'] = is_page() ? '_page_' . $post_id : '_post_' . $post_id;
256 }
257
258 if ( empty( $attributes['title'] ) ) {
259 $title = $post instanceof WP_Post ? $post->post_title : get_the_title();
260 /** This filter is documented in core/src/wp-includes/general-template.php */
261 $attributes['title'] = apply_filters( 'wp_title', $title, '', '' );
262 }
263
264 if ( empty( $attributes['permalink'] ) ) {
265 if ( $post_id ) {
266 $attributes['permalink'] = get_permalink( $post_id );
267 } else {
268 $attributes['permalink'] = home_url( add_query_arg( array() ) );
269 }
270 }
271
272 $rating = (int) $attributes['rating'];
273 $unique_id = sanitize_key( wp_strip_all_tags( $attributes['unique_id'] ) );
274 $item_id = wp_strip_all_tags( $attributes['item_id'] );
275 $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id );
276
277 $settings = wp_json_encode(
278 array(
279 'id' => $rating,
280 'unique_id' => $unique_id,
281 'title' => rawurlencode( trim( $attributes['title'] ) ),
282 'permalink' => esc_url( $attributes['permalink'] ),
283 'item_id' => $item_id,
284 ),
285 JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
286 );
287
288 if (
289 class_exists( 'Jetpack_AMP_Support' )
290 && Jetpack_AMP_Support::is_amp_request()
291 ) {
292 return sprintf(
293 '<a href="%s" target="_blank">%s</a>',
294 esc_url( $attributes['permalink'] ),
295 esc_html( trim( $attributes['title'] ) )
296 );
297 } elseif ( $inline ) {
298 $rating_js = "<!--//--><![CDATA[//><!--\n";
299 $rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};";
300 $rating_js .= "\n//--><!]]>";
301
302 wp_enqueue_script( 'crowdsignal-rating' );
303 wp_add_inline_script(
304 'crowdsignal-rating',
305 $rating_js,
306 'before'
307 );
308
309 return sprintf(
310 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
311 absint( $rating ),
312 esc_attr( $item_id )
313 );
314 } else {
315 if ( false === self::$scripts ) {
316 self::$scripts = array();
317 }
318
319 $data = array(
320 'id' => $rating,
321 'item_id' => $item_id,
322 'settings' => $settings,
323 );
324
325 self::$scripts['rating'][] = $data;
326
327 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
328
329 if ( $infinite_scroll ) {
330 return sprintf(
331 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>',
332 absint( $rating ),
333 esc_attr( $item_id ),
334 esc_attr( wp_json_encode( $data, JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) )
335 );
336 } else {
337 return sprintf(
338 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
339 absint( $rating ),
340 esc_attr( $item_id )
341 );
342 }
343 }
344 } elseif ( (int) $attributes['poll'] > 0 ) {
345 /*
346 * Poll embed.
347 */
348
349 if ( empty( $attributes['title'] ) ) {
350 $attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' );
351 }
352
353 $poll = (int) $attributes['poll'];
354
355 if ( 'crowdsignal.com' === $attributes['site'] ) {
356 $poll_url = sprintf( 'https://poll.fm/%d', $poll );
357 } else {
358 $poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll );
359 }
360
361 $poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll );
362 $poll_link = sprintf(
363 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
364 esc_url( $poll_url ),
365 esc_html( $attributes['title'] )
366 );
367
368 if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
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' => $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'][ $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', $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, JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ),
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', $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 ( empty( $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 ?? null,
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 = jetpack_preg_replace_outside_tags(
755 '!(?:\n|\A)https?:\/\/([^"\'.]+)\.survey\.fm\/([^"\'\/\s]+)(?:\/.*)?(?:\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