PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 14.9.2
Jetpack – WP Security, Backup, Speed, & Growth v14.9.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 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 1 year 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 1 year 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 1 year ago untappd-menu.php 1 year 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
770 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 * @phan-constructor-used-for-side-effects
35 */
36 class CrowdsignalShortcode {
37
38 /**
39 * Should the Crowdsignal JavaScript be added to the page?
40 *
41 * @var bool
42 */
43 private static $add_script = false;
44
45 /**
46 * Array of Polls / Surveys present on the page, and that need to be added.
47 *
48 * @var bool|array
49 */
50 private static $scripts = false;
51
52 /**
53 * Add all the actions & register the shortcode.
54 */
55 public function __construct() {
56 add_action( 'init', array( $this, 'register_scripts' ) );
57
58 add_shortcode( 'crowdsignal', array( $this, 'crowdsignal_shortcode' ) );
59 add_shortcode( 'polldaddy', array( $this, 'polldaddy_shortcode' ) );
60
61 add_filter( 'pre_kses', array( $this, 'crowdsignal_embed_to_shortcode' ) );
62 add_action( 'infinite_scroll_render', array( $this, 'crowdsignal_shortcode_infinite' ), 11 );
63 }
64
65 /**
66 * Register scripts that may be enqueued later on by the shortcode.
67 */
68 public static function register_scripts() {
69 wp_register_script(
70 'crowdsignal-shortcode',
71 Assets::get_file_url_for_environment( '_inc/build/crowdsignal-shortcode.min.js', '_inc/crowdsignal-shortcode.js' ),
72 array(),
73 JETPACK__VERSION,
74 true
75 );
76 wp_register_script(
77 'crowdsignal-survey',
78 Assets::get_file_url_for_environment( '_inc/build/crowdsignal-survey.min.js', '_inc/crowdsignal-survey.js' ),
79 array(),
80 JETPACK__VERSION,
81 true
82 );
83 wp_register_script(
84 'crowdsignal-rating',
85 'https://polldaddy.com/js/rating/rating.js',
86 array(),
87 JETPACK__VERSION,
88 true
89 );
90 }
91
92 /**
93 * JavaScript code for a specific survey / poll.
94 *
95 * @param array $settings Array of information about a survey / poll.
96 * @param string $survey_link HTML link tag for a specific survey or poll.
97 * @param string $survey_url Link to the survey or poll.
98 */
99 private function get_async_code( array $settings, $survey_link, $survey_url ) {
100 wp_enqueue_script( 'crowdsignal-survey' );
101
102 if ( 'button' === $settings['type'] ) {
103 $placeholder = sprintf(
104 '<a class="cs-embed pd-embed" href="%1$s" data-settings="%2$s">%3$s</a>',
105 esc_url( $survey_url ),
106 esc_attr( wp_json_encode( $settings ) ),
107 esc_html( $settings['title'] )
108 );
109 } else {
110 $placeholder = sprintf(
111 '<div class="cs-embed pd-embed" data-settings="%1$s"></div><noscript>%2$s</noscript>',
112 esc_attr( wp_json_encode( $settings ) ),
113 $survey_link
114 );
115 }
116
117 return $placeholder;
118 }
119
120 /**
121 * Crowdsignal Poll Embed script - transforms code that looks like that:
122 * <script type="text/javascript" charset="utf-8" async src="http://static.polldaddy.com/p/123456.js"></script>
123 * <noscript><a href="http://polldaddy.com/poll/123456/">What is your favourite color?</a></noscript>
124 * into the [crowdsignal poll=...] shortcode format
125 *
126 * @param string $content Post content.
127 */
128 public function crowdsignal_embed_to_shortcode( $content ) {
129
130 if ( ! is_string( $content ) || ! str_contains( $content, 'polldaddy.com/p/' ) ) {
131 return $content;
132 }
133
134 $regexes = array();
135
136 $regexes[] = '#<script[^>]+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"[^>]*+>\s*?</script>\r?\n?(<noscript>.*?</noscript>)?#i';
137
138 $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';
139
140 foreach ( $regexes as $regex ) {
141 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
142 continue;
143 }
144
145 foreach ( $matches as $match ) {
146 if ( ! isset( $match[2] ) ) {
147 continue;
148 }
149
150 $id = (int) $match[2];
151
152 if ( $id > 0 ) {
153 $content = str_replace( $match[0], " [crowdsignal poll=$id]", $content );
154 /** This action is documented in modules/shortcodes/youtube.php */
155 do_action( 'jetpack_embed_to_shortcode', 'crowdsignal', $id );
156 }
157 }
158 }
159
160 return $content;
161 }
162
163 /**
164 * Support for legacy Polldaddy shortcode.
165 *
166 * @param array $atts Shortcode attributes.
167 */
168 public function polldaddy_shortcode( $atts ) {
169 if ( ! is_array( $atts ) ) {
170 return '<!-- Polldaddy shortcode passed invalid attributes -->';
171 }
172
173 $atts['site'] = 'polldaddy.com';
174 return $this->crowdsignal_shortcode( $atts );
175 }
176
177 /**
178 * Shortcode for Crowdsignal
179 * [crowdsignal poll|survey|rating="123456"]
180 *
181 * @param array $atts Shortcode attributes.
182 */
183 public function crowdsignal_shortcode( $atts ) {
184 global $post;
185 global $content_width;
186
187 if ( ! is_array( $atts ) ) {
188 return '<!-- Crowdsignal shortcode passed invalid attributes -->';
189 }
190
191 $attributes = shortcode_atts(
192 array(
193 'survey' => null,
194 'link_text' => esc_html__( 'Take Our Survey', 'jetpack' ),
195 'poll' => 'empty',
196 'rating' => 'empty',
197 'unique_id' => null,
198 'item_id' => null,
199 'title' => null,
200 'permalink' => null,
201 'cb' => 0, // cache buster. Helps with testing.
202 'type' => 'button',
203 'body' => '',
204 'button' => '',
205 'text_color' => '000000',
206 'back_color' => 'FFFFFF',
207 'align' => '',
208 'style' => '',
209 'width' => $content_width,
210 'height' => floor( $content_width * 3 / 4 ),
211 'delay' => 100,
212 'visit' => 'single',
213 'domain' => '',
214 'id' => '',
215 'site' => 'crowdsignal.com',
216 ),
217 $atts,
218 'crowdsignal'
219 );
220
221 $inline = ! in_the_loop()
222 && ! Constants::is_defined( 'TESTING_IN_JETPACK' );
223
224 $no_script = false;
225 $infinite_scroll = false;
226
227 if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) {
228 $infinite_scroll = true;
229 }
230
231 if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) {
232 $inline = true;
233 }
234
235 if ( is_feed() || ( defined( 'DOING_AJAX' ) && ! $infinite_scroll ) ) {
236 $no_script = false;
237 }
238
239 self::$add_script = $infinite_scroll;
240
241 /*
242 * Rating embed.
243 */
244 if ( (int) $attributes['rating'] > 0 && ! $no_script ) {
245 $post_id = $post instanceof WP_Post ? $post->ID : get_the_ID();
246 $post_id = $post_id ?? '';
247
248 if ( empty( $attributes['unique_id'] ) ) {
249 $attributes['unique_id'] = is_page() ? 'wp-page-' . $post_id : 'wp-post-' . $post_id;
250 }
251
252 if ( empty( $attributes['item_id'] ) ) {
253 $attributes['item_id'] = is_page() ? '_page_' . $post_id : '_post_' . $post_id;
254 }
255
256 if ( empty( $attributes['title'] ) ) {
257 $title = $post instanceof WP_Post ? $post->post_title : get_the_title();
258 /** This filter is documented in core/src/wp-includes/general-template.php */
259 $attributes['title'] = apply_filters( 'wp_title', $title, '', '' );
260 }
261
262 if ( empty( $attributes['permalink'] ) ) {
263 if ( $post_id ) {
264 $attributes['permalink'] = get_permalink( $post_id );
265 } else {
266 $attributes['permalink'] = home_url( add_query_arg( array() ) );
267 }
268 }
269
270 $rating = (int) $attributes['rating'];
271 $unique_id = sanitize_key( wp_strip_all_tags( $attributes['unique_id'] ) );
272 $item_id = wp_strip_all_tags( $attributes['item_id'] );
273 $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id );
274
275 $settings = wp_json_encode(
276 array(
277 'id' => $rating,
278 'unique_id' => $unique_id,
279 'title' => rawurlencode( trim( $attributes['title'] ) ),
280 'permalink' => esc_url( $attributes['permalink'] ),
281 'item_id' => $item_id,
282 )
283 );
284
285 $item_id = esc_js( $item_id );
286
287 if (
288 class_exists( 'Jetpack_AMP_Support' )
289 && Jetpack_AMP_Support::is_amp_request()
290 ) {
291 return sprintf(
292 '<a href="%s" target="_blank">%s</a>',
293 esc_url( $attributes['permalink'] ),
294 esc_html( trim( $attributes['title'] ) )
295 );
296 } elseif ( $inline ) {
297 $rating_js = "<!--//--><![CDATA[//><!--\n";
298 $rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};";
299 $rating_js .= "\n//--><!]]>";
300
301 wp_enqueue_script( 'crowdsignal-rating' );
302 wp_add_inline_script(
303 'crowdsignal-rating',
304 $rating_js,
305 'before'
306 );
307
308 return sprintf(
309 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
310 absint( $rating ),
311 esc_attr( $item_id )
312 );
313 } else {
314 if ( false === self::$scripts ) {
315 self::$scripts = array();
316 }
317
318 $data = array(
319 'id' => $rating,
320 'item_id' => $item_id,
321 'settings' => $settings,
322 );
323
324 self::$scripts['rating'][] = $data;
325
326 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
327
328 if ( $infinite_scroll ) {
329 return sprintf(
330 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>',
331 absint( $rating ),
332 esc_attr( $item_id ),
333 esc_attr( wp_json_encode( $data ) )
334 );
335 } else {
336 return sprintf(
337 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
338 absint( $rating ),
339 esc_attr( $item_id )
340 );
341 }
342 }
343 } elseif ( (int) $attributes['poll'] > 0 ) {
344 /*
345 * Poll embed.
346 */
347
348 if ( empty( $attributes['title'] ) ) {
349 $attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' );
350 }
351
352 $poll = (int) $attributes['poll'];
353
354 if ( 'crowdsignal.com' === $attributes['site'] ) {
355 $poll_url = sprintf( 'https://poll.fm/%d', $poll );
356 } else {
357 $poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll );
358 }
359
360 $poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll );
361 $poll_link = sprintf(
362 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
363 esc_url( $poll_url ),
364 esc_html( $attributes['title'] )
365 );
366
367 if (
368 $no_script
369 || ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() )
370 ) {
371 return $poll_link;
372 } elseif ( 'slider' === $attributes['type'] && ! $inline ) { // Slider poll.
373 if ( ! in_array(
374 $attributes['visit'],
375 array( 'single', 'multiple' ),
376 true
377 ) ) {
378 $attributes['visit'] = 'single';
379 }
380
381 $settings = array(
382 'type' => 'slider',
383 'embed' => 'poll',
384 'delay' => (int) $attributes['delay'],
385 'visit' => $attributes['visit'],
386 'id' => (int) $poll,
387 'site' => $attributes['site'],
388 );
389
390 return $this->get_async_code( $settings, $poll_link, $poll_url );
391 } else {
392 if ( 1 === $attributes['cb'] ) {
393 $attributes['cb'] = '?cb=' . time();
394 } else {
395 $attributes['cb'] = false;
396 }
397 $margins = '';
398 $float = '';
399
400 if ( in_array(
401 $attributes['align'],
402 array( 'right', 'left' ),
403 true
404 ) ) {
405 $float = sprintf( 'float: %s;', $attributes['align'] );
406
407 if ( 'left' === $attributes['align'] ) {
408 $margins = 'margin: 0 10px 0 0;';
409 } elseif ( 'right' === $attributes['align'] ) {
410 $margins = 'margin: 0 0 0 10px';
411 }
412 }
413
414 /*
415 * Force the normal style embed on single posts/pages
416 * otherwise it's not rendered on infinite scroll themed blogs
417 * ('infinite_scroll_render' isn't fired)
418 */
419 if ( is_singular() ) {
420 $inline = true;
421 }
422
423 if ( false === $attributes['cb'] && ! $inline ) {
424 if ( false === self::$scripts ) {
425 self::$scripts = array();
426 }
427
428 $data = array( 'url' => $poll_js );
429
430 self::$scripts['poll'][ (int) $poll ] = $data;
431
432 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
433
434 wp_enqueue_script( 'crowdsignal-shortcode' );
435 wp_localize_script(
436 'crowdsignal-shortcode',
437 'crowdsignal_shortcode_options',
438 array(
439 'script_url' => esc_url_raw(
440 Assets::get_file_url_for_environment(
441 '_inc/build/polldaddy-shortcode.min.js',
442 '_inc/polldaddy-shortcode.js'
443 )
444 ),
445 )
446 );
447
448 /**
449 * Hook into the Crowdsignal shortcode before rendering.
450 *
451 * @since 8.4.0
452 *
453 * @param int $poll Poll ID.
454 */
455 do_action( 'crowdsignal_shortcode_before', (int) $poll );
456
457 return sprintf(
458 '<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>',
459 absint( $poll ),
460 esc_attr( wp_json_encode( $data ) ),
461 $float,
462 $margins,
463 $poll_link
464 );
465 } else {
466 if ( $inline ) {
467 $attributes['cb'] = '';
468 }
469
470 wp_enqueue_script(
471 'crowdsignal-' . absint( $poll ),
472 esc_url( $poll_js . $attributes['cb'] ),
473 array(),
474 JETPACK__VERSION,
475 true
476 );
477
478 /** This action is already documented in modules/shortcodes/crowdsignal.php */
479 do_action( 'crowdsignal_shortcode_before', (int) $poll );
480
481 return sprintf(
482 '<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>',
483 absint( $poll ),
484 $float,
485 $margins,
486 $poll_link
487 );
488 }
489 }
490 } elseif ( ! empty( $attributes['survey'] ) ) {
491 /*
492 * Survey embed.
493 */
494
495 if ( in_array(
496 $attributes['type'],
497 array( 'iframe', 'button', 'banner', 'slider' ),
498 true
499 ) ) {
500
501 if ( empty( $attributes['title'] ) ) {
502 $attributes['title'] = esc_html__( 'Take Our Survey', 'jetpack' );
503 if ( ! empty( $attributes['link_text'] ) ) {
504 $attributes['title'] = $attributes['link_text'];
505 }
506 }
507
508 if (
509 'banner' === $attributes['type']
510 || 'slider' === $attributes['type']
511 ) {
512 $inline = false;
513 }
514
515 $survey_url = '';
516
517 if ( 'true' !== $attributes['survey'] ) {
518 $survey = preg_replace( '/[^a-f0-9]/i', '', $attributes['survey'] );
519
520 if ( 'crowdsignal.com' === $attributes['site'] ) {
521 $survey_url = 'https://survey.fm/' . $survey;
522 } else {
523 $survey_url = 'https://polldaddy.com/s/' . $survey;
524 }
525 } elseif ( isset( $attributes['domain'] ) && isset( $attributes['id'] ) ) {
526 $survey_domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
527 $survey_id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
528 $survey_url = sprintf(
529 'https://%1$s.survey.fm/%2$s',
530 $survey_domain,
531 $survey_id
532 );
533 }
534
535 $survey_link = sprintf(
536 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
537 esc_url( $survey_url ),
538 esc_html( $attributes['title'] )
539 );
540
541 $settings = array();
542
543 if ( 'iframe' === $attributes['type'] ) {
544 if ( 'auto' !== $attributes['height'] ) {
545 if (
546 is_numeric( $content_width )
547 && $content_width > 0
548 && is_numeric( $attributes['width'] )
549 && $attributes['width'] > $content_width
550 ) {
551 $attributes['width'] = $content_width;
552 }
553
554 if ( ! $attributes['width'] ) {
555 $attributes['width'] = '100%';
556 } else {
557 $attributes['width'] = (int) $attributes['width'];
558 }
559
560 if ( ! $attributes['height'] ) {
561 $attributes['height'] = '600';
562 } else {
563 $attributes['height'] = (int) $attributes['height'];
564 }
565
566 return sprintf(
567 '<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>',
568 esc_url( $survey_url ),
569 absint( $attributes['width'] ),
570 absint( $attributes['height'] ),
571 $survey_link
572 );
573 } elseif (
574 ! empty( $attributes['domain'] )
575 && ! empty( $attributes['id'] )
576 ) {
577 $domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
578 $id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
579
580 $auto_src = esc_url( "https://{$domain}.survey.fm/{$id}" );
581 $auto_src = wp_parse_url( $auto_src );
582
583 if ( ! is_array( $auto_src ) || array() === $auto_src ) {
584 return '<!-- no crowdsignal output -->';
585 }
586
587 if ( ! isset( $auto_src['host'] ) || ! isset( $auto_src['path'] ) ) {
588 return '<!-- no crowdsignal output -->';
589 }
590
591 $domain = $auto_src['host'] . '/';
592 $id = ltrim( $auto_src['path'], '/' );
593
594 $settings = array(
595 'type' => $attributes['type'],
596 'auto' => true,
597 'domain' => $domain,
598 'id' => $id,
599 'site' => $attributes['site'],
600 );
601 }
602 } else {
603 $text_color = sanitize_hex_color_no_hash( $attributes['text_color'] );
604 $back_color = sanitize_hex_color_no_hash( $attributes['back_color'] );
605
606 if (
607 ! in_array(
608 $attributes['align'],
609 array(
610 'right',
611 'left',
612 'top-left',
613 'top-right',
614 'middle-left',
615 'middle-right',
616 'bottom-left',
617 'bottom-right',
618 ),
619 true
620 )
621 ) {
622 $attributes['align'] = '';
623 }
624
625 if (
626 ! in_array(
627 $attributes['style'],
628 array(
629 'inline',
630 'side',
631 'corner',
632 'rounded',
633 'square',
634 ),
635 true
636 )
637 ) {
638 $attributes['style'] = '';
639 }
640
641 $settings = array_filter(
642 array(
643 'title' => wp_strip_all_tags( $attributes['title'] ),
644 'type' => $attributes['type'],
645 'body' => wp_strip_all_tags( $attributes['body'] ),
646 'button' => wp_strip_all_tags( $attributes['button'] ),
647 'text_color' => $text_color,
648 'back_color' => $back_color,
649 'align' => $attributes['align'],
650 'style' => $attributes['style'],
651 'id' => $survey,
652 'site' => $attributes['site'],
653 )
654 );
655 }
656
657 if ( empty( $settings ) ) {
658 return '<!-- no crowdsignal output -->';
659 }
660
661 return $this->get_async_code( $settings, $survey_link, $survey_url );
662 }
663 } else {
664 return '<!-- no crowdsignal output -->';
665 }
666 }
667
668 /**
669 * Enqueue JavaScript containing all ratings / polls on the page.
670 * Hooked into wp_footer
671 */
672 public function generate_scripts() {
673 if ( is_array( self::$scripts ) ) {
674 if ( isset( self::$scripts['rating'] ) ) {
675 $script = "<!--//--><![CDATA[//><!--\n";
676 foreach ( self::$scripts['rating'] as $rating ) {
677 $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']} );}}";
678 }
679 $script .= "\n//--><!]]>";
680
681 wp_enqueue_script( 'crowdsignal-rating' );
682 wp_add_inline_script(
683 'crowdsignal-rating',
684 $script,
685 'before'
686 );
687 }
688
689 if ( isset( self::$scripts['poll'] ) ) {
690 foreach ( self::$scripts['poll'] as $poll_id => $poll ) {
691 wp_enqueue_script(
692 'crowdsignal-' . absint( $poll_id ),
693 esc_url( $poll['url'] ),
694 array(),
695 JETPACK__VERSION,
696 true
697 );
698 }
699 }
700 }
701 self::$scripts = false;
702 }
703
704 /**
705 * Dynamically load the .js, if needed
706 *
707 * This hooks in late (priority 11) to infinite_scroll_render to determine
708 * a posteriori if a shortcode has been called.
709 */
710 public function crowdsignal_shortcode_infinite() {
711 // only try to load if a shortcode has been called and theme supports infinite scroll.
712 if ( self::$add_script ) {
713 wp_enqueue_script( 'crowdsignal-shortcode' );
714 wp_localize_script(
715 'crowdsignal-shortcode',
716 'crowdsignal_shortcode_options',
717 array(
718 'script_url' => esc_url_raw(
719 Assets::get_file_url_for_environment(
720 '_inc/build/polldaddy-shortcode.min.js',
721 '_inc/polldaddy-shortcode.js'
722 )
723 ),
724 )
725 );
726 }
727 }
728 }
729
730 // Kick it all off.
731 new CrowdsignalShortcode();
732
733 if ( ! function_exists( 'crowdsignal_link' ) ) {
734 /**
735 * Replace link with shortcode.
736 * Examples: https://poll.fm/10499328 | https://7iger.survey.fm/test-embed
737 *
738 * @param string $content Post content.
739 */
740 function crowdsignal_link( $content ) {
741 if (
742 class_exists( 'Jetpack_AMP_Support' )
743 && Jetpack_AMP_Support::is_amp_request()
744 ) {
745 return $content;
746 }
747
748 // Replace poll links.
749 $content = jetpack_preg_replace_outside_tags(
750 '!(?:\n|\A)https?://(polldaddy\.com/poll|poll\.fm)/([0-9]+?)(/.*)?(?:\n|\Z)!i',
751 '[crowdsignal poll=$2]',
752 $content
753 );
754
755 // Replace survey.fm links.
756 $content = preg_replace(
757 '!(?:\n|\A)https?://(.*).survey.fm/(.*)(/.*)?(?:\n|\Z)!i',
758 '[crowdsignal type="iframe" survey="true" height="auto" domain="$1" id="$2"]',
759 $content
760 );
761
762 return $content;
763 }
764
765 // higher priority because we need it before auto-link and autop get to it.
766 add_filter( 'the_content', 'crowdsignal_link', 1 );
767 add_filter( 'the_content_rss', 'crowdsignal_link', 1 );
768 }
769 }
770