PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 15.2.1
Jetpack – WP Security, Backup, Speed, & Growth v15.2.1
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / shortcodes / crowdsignal.php
jetpack / modules / shortcodes Last commit date
css 10 months ago images 2 years ago img 2 years ago js 9 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 9 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 9 months 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
769 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 $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 );
286
287 $item_id = esc_js( $item_id );
288
289 if (
290 class_exists( 'Jetpack_AMP_Support' )
291 && Jetpack_AMP_Support::is_amp_request()
292 ) {
293 return sprintf(
294 '<a href="%s" target="_blank">%s</a>',
295 esc_url( $attributes['permalink'] ),
296 esc_html( trim( $attributes['title'] ) )
297 );
298 } elseif ( $inline ) {
299 $rating_js = "<!--//--><![CDATA[//><!--\n";
300 $rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};";
301 $rating_js .= "\n//--><!]]>";
302
303 wp_enqueue_script( 'crowdsignal-rating' );
304 wp_add_inline_script(
305 'crowdsignal-rating',
306 $rating_js,
307 'before'
308 );
309
310 return sprintf(
311 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
312 absint( $rating ),
313 esc_attr( $item_id )
314 );
315 } else {
316 if ( false === self::$scripts ) {
317 self::$scripts = array();
318 }
319
320 $data = array(
321 'id' => $rating,
322 'item_id' => $item_id,
323 'settings' => $settings,
324 );
325
326 self::$scripts['rating'][] = $data;
327
328 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
329
330 if ( $infinite_scroll ) {
331 return sprintf(
332 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>',
333 absint( $rating ),
334 esc_attr( $item_id ),
335 esc_attr( wp_json_encode( $data ) )
336 );
337 } else {
338 return sprintf(
339 '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
340 absint( $rating ),
341 esc_attr( $item_id )
342 );
343 }
344 }
345 } elseif ( (int) $attributes['poll'] > 0 ) {
346 /*
347 * Poll embed.
348 */
349
350 if ( empty( $attributes['title'] ) ) {
351 $attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' );
352 }
353
354 $poll = (int) $attributes['poll'];
355
356 if ( 'crowdsignal.com' === $attributes['site'] ) {
357 $poll_url = sprintf( 'https://poll.fm/%d', $poll );
358 } else {
359 $poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll );
360 }
361
362 $poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll );
363 $poll_link = sprintf(
364 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
365 esc_url( $poll_url ),
366 esc_html( $attributes['title'] )
367 );
368
369 if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
370 return $poll_link;
371 } elseif ( 'slider' === $attributes['type'] && ! $inline ) { // Slider poll.
372 if ( ! in_array(
373 $attributes['visit'],
374 array( 'single', 'multiple' ),
375 true
376 ) ) {
377 $attributes['visit'] = 'single';
378 }
379
380 $settings = array(
381 'type' => 'slider',
382 'embed' => 'poll',
383 'delay' => (int) $attributes['delay'],
384 'visit' => $attributes['visit'],
385 'id' => $poll,
386 'site' => $attributes['site'],
387 );
388
389 return $this->get_async_code( $settings, $poll_link, $poll_url );
390 } else {
391 if ( 1 === $attributes['cb'] ) {
392 $attributes['cb'] = '?cb=' . time();
393 } else {
394 $attributes['cb'] = false;
395 }
396 $margins = '';
397 $float = '';
398
399 if ( in_array(
400 $attributes['align'],
401 array( 'right', 'left' ),
402 true
403 ) ) {
404 $float = sprintf( 'float: %s;', $attributes['align'] );
405
406 if ( 'left' === $attributes['align'] ) {
407 $margins = 'margin: 0 10px 0 0;';
408 } elseif ( 'right' === $attributes['align'] ) {
409 $margins = 'margin: 0 0 0 10px';
410 }
411 }
412
413 /*
414 * Force the normal style embed on single posts/pages
415 * otherwise it's not rendered on infinite scroll themed blogs
416 * ('infinite_scroll_render' isn't fired)
417 */
418 if ( is_singular() ) {
419 $inline = true;
420 }
421
422 if ( false === $attributes['cb'] && ! $inline ) {
423 if ( false === self::$scripts ) {
424 self::$scripts = array();
425 }
426
427 $data = array( 'url' => $poll_js );
428
429 self::$scripts['poll'][ $poll ] = $data;
430
431 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
432
433 wp_enqueue_script( 'crowdsignal-shortcode' );
434 wp_localize_script(
435 'crowdsignal-shortcode',
436 'crowdsignal_shortcode_options',
437 array(
438 'script_url' => esc_url_raw(
439 Assets::get_file_url_for_environment(
440 '_inc/build/polldaddy-shortcode.min.js',
441 '_inc/polldaddy-shortcode.js'
442 )
443 ),
444 )
445 );
446
447 /**
448 * Hook into the Crowdsignal shortcode before rendering.
449 *
450 * @since 8.4.0
451 *
452 * @param int $poll Poll ID.
453 */
454 do_action( 'crowdsignal_shortcode_before', $poll );
455
456 return sprintf(
457 '<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>',
458 absint( $poll ),
459 esc_attr( wp_json_encode( $data ) ),
460 $float,
461 $margins,
462 $poll_link
463 );
464 } else {
465 if ( $inline ) {
466 $attributes['cb'] = '';
467 }
468
469 wp_enqueue_script(
470 'crowdsignal-' . absint( $poll ),
471 esc_url( $poll_js . $attributes['cb'] ),
472 array(),
473 JETPACK__VERSION,
474 true
475 );
476
477 /** This action is already documented in modules/shortcodes/crowdsignal.php */
478 do_action( 'crowdsignal_shortcode_before', $poll );
479
480 return sprintf(
481 '<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>',
482 absint( $poll ),
483 $float,
484 $margins,
485 $poll_link
486 );
487 }
488 }
489 } elseif ( ! empty( $attributes['survey'] ) ) {
490 /*
491 * Survey embed.
492 */
493
494 if ( in_array(
495 $attributes['type'],
496 array( 'iframe', 'button', 'banner', 'slider' ),
497 true
498 ) ) {
499
500 if ( empty( $attributes['title'] ) ) {
501 $attributes['title'] = esc_html__( 'Take Our Survey', 'jetpack' );
502 if ( ! empty( $attributes['link_text'] ) ) {
503 $attributes['title'] = $attributes['link_text'];
504 }
505 }
506
507 if (
508 'banner' === $attributes['type']
509 || 'slider' === $attributes['type']
510 ) {
511 $inline = false;
512 }
513
514 $survey_url = '';
515
516 if ( 'true' !== $attributes['survey'] ) {
517 $survey = preg_replace( '/[^a-f0-9]/i', '', $attributes['survey'] );
518
519 if ( 'crowdsignal.com' === $attributes['site'] ) {
520 $survey_url = 'https://survey.fm/' . $survey;
521 } else {
522 $survey_url = 'https://polldaddy.com/s/' . $survey;
523 }
524 } elseif ( isset( $attributes['domain'] ) && isset( $attributes['id'] ) ) {
525 $survey_domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
526 $survey_id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
527 $survey_url = sprintf(
528 'https://%1$s.survey.fm/%2$s',
529 $survey_domain,
530 $survey_id
531 );
532 }
533
534 $survey_link = sprintf(
535 '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
536 esc_url( $survey_url ),
537 esc_html( $attributes['title'] )
538 );
539
540 $settings = array();
541
542 if ( 'iframe' === $attributes['type'] ) {
543 if ( 'auto' !== $attributes['height'] ) {
544 if (
545 is_numeric( $content_width )
546 && $content_width > 0
547 && is_numeric( $attributes['width'] )
548 && $attributes['width'] > $content_width
549 ) {
550 $attributes['width'] = $content_width;
551 }
552
553 if ( empty( $attributes['width'] ) ) {
554 $attributes['width'] = '100%';
555 } else {
556 $attributes['width'] = (int) $attributes['width'];
557 }
558
559 if ( ! $attributes['height'] ) {
560 $attributes['height'] = '600';
561 } else {
562 $attributes['height'] = (int) $attributes['height'];
563 }
564
565 return sprintf(
566 '<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>',
567 esc_url( $survey_url ),
568 absint( $attributes['width'] ),
569 absint( $attributes['height'] ),
570 $survey_link
571 );
572 } elseif (
573 ! empty( $attributes['domain'] )
574 && ! empty( $attributes['id'] )
575 ) {
576 $domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
577 $id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
578
579 $auto_src = esc_url( "https://{$domain}.survey.fm/{$id}" );
580 $auto_src = wp_parse_url( $auto_src );
581
582 if ( ! is_array( $auto_src ) || array() === $auto_src ) {
583 return '<!-- no crowdsignal output -->';
584 }
585
586 if ( ! isset( $auto_src['host'] ) || ! isset( $auto_src['path'] ) ) {
587 return '<!-- no crowdsignal output -->';
588 }
589
590 $domain = $auto_src['host'] . '/';
591 $id = ltrim( $auto_src['path'], '/' );
592
593 $settings = array(
594 'type' => $attributes['type'],
595 'auto' => true,
596 'domain' => $domain,
597 'id' => $id,
598 'site' => $attributes['site'],
599 );
600 }
601 } else {
602 $text_color = sanitize_hex_color_no_hash( $attributes['text_color'] );
603 $back_color = sanitize_hex_color_no_hash( $attributes['back_color'] );
604
605 if (
606 ! in_array(
607 $attributes['align'],
608 array(
609 'right',
610 'left',
611 'top-left',
612 'top-right',
613 'middle-left',
614 'middle-right',
615 'bottom-left',
616 'bottom-right',
617 ),
618 true
619 )
620 ) {
621 $attributes['align'] = '';
622 }
623
624 if (
625 ! in_array(
626 $attributes['style'],
627 array(
628 'inline',
629 'side',
630 'corner',
631 'rounded',
632 'square',
633 ),
634 true
635 )
636 ) {
637 $attributes['style'] = '';
638 }
639
640 $settings = array_filter(
641 array(
642 'title' => wp_strip_all_tags( $attributes['title'] ),
643 'type' => $attributes['type'],
644 'body' => wp_strip_all_tags( $attributes['body'] ),
645 'button' => wp_strip_all_tags( $attributes['button'] ),
646 'text_color' => $text_color,
647 'back_color' => $back_color,
648 'align' => $attributes['align'],
649 'style' => $attributes['style'],
650 'id' => $survey,
651 'site' => $attributes['site'],
652 )
653 );
654 }
655
656 if ( empty( $settings ) ) {
657 return '<!-- no crowdsignal output -->';
658 }
659
660 return $this->get_async_code( $settings, $survey_link, $survey_url );
661 }
662 } else {
663 return '<!-- no crowdsignal output -->';
664 }
665 }
666
667 /**
668 * Enqueue JavaScript containing all ratings / polls on the page.
669 * Hooked into wp_footer
670 */
671 public function generate_scripts() {
672 if ( is_array( self::$scripts ) ) {
673 if ( isset( self::$scripts['rating'] ) ) {
674 $script = "<!--//--><![CDATA[//><!--\n";
675 foreach ( self::$scripts['rating'] as $rating ) {
676 $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']} );}}";
677 }
678 $script .= "\n//--><!]]>";
679
680 wp_enqueue_script( 'crowdsignal-rating' );
681 wp_add_inline_script(
682 'crowdsignal-rating',
683 $script,
684 'before'
685 );
686 }
687
688 if ( isset( self::$scripts['poll'] ) ) {
689 foreach ( self::$scripts['poll'] as $poll_id => $poll ) {
690 wp_enqueue_script(
691 'crowdsignal-' . absint( $poll_id ),
692 esc_url( $poll['url'] ),
693 array(),
694 JETPACK__VERSION,
695 true
696 );
697 }
698 }
699 }
700 self::$scripts = false;
701 }
702
703 /**
704 * Dynamically load the .js, if needed
705 *
706 * This hooks in late (priority 11) to infinite_scroll_render to determine
707 * a posteriori if a shortcode has been called.
708 */
709 public function crowdsignal_shortcode_infinite() {
710 // only try to load if a shortcode has been called and theme supports infinite scroll.
711 if ( self::$add_script ) {
712 wp_enqueue_script( 'crowdsignal-shortcode' );
713 wp_localize_script(
714 'crowdsignal-shortcode',
715 'crowdsignal_shortcode_options',
716 array(
717 'script_url' => esc_url_raw(
718 Assets::get_file_url_for_environment(
719 '_inc/build/polldaddy-shortcode.min.js',
720 '_inc/polldaddy-shortcode.js'
721 )
722 ),
723 )
724 );
725 }
726 }
727 }
728
729 // Kick it all off.
730 new CrowdsignalShortcode();
731
732 if ( ! function_exists( 'crowdsignal_link' ) ) {
733 /**
734 * Replace link with shortcode.
735 * Examples: https://poll.fm/10499328 | https://7iger.survey.fm/test-embed
736 *
737 * @param string $content Post content.
738 */
739 function crowdsignal_link( $content ) {
740 if (
741 class_exists( 'Jetpack_AMP_Support' )
742 && Jetpack_AMP_Support::is_amp_request()
743 ) {
744 return $content;
745 }
746
747 // Replace poll links.
748 $content = jetpack_preg_replace_outside_tags(
749 '!(?:\n|\A)https?://(polldaddy\.com/poll|poll\.fm)/([0-9]+?)(/.*)?(?:\n|\Z)!i',
750 '[crowdsignal poll=$2]',
751 $content
752 );
753
754 // Replace survey.fm links.
755 $content = jetpack_preg_replace_outside_tags(
756 '!(?:\n|\A)https?:\/\/([^"\'.]+)\.survey\.fm\/([^"\'\/\s]+)(?:\/.*)?(?:\n|\Z)!i',
757 '[crowdsignal type="iframe" survey="true" height="auto" domain="$1" id="$2"]',
758 $content
759 );
760
761 return $content;
762 }
763
764 // higher priority because we need it before auto-link and autop get to it.
765 add_filter( 'the_content', 'crowdsignal_link', 1 );
766 add_filter( 'the_content_rss', 'crowdsignal_link', 1 );
767 }
768 }
769