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