PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.6
Jetpack – WP Security, Backup, Speed, & Growth v6.6
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / polldaddy.php

polldaddy.php in Jetpack – WP Security, Backup, Speed, & Growth 6.6, at modules/shortcodes/polldaddy.php

595 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! class_exists( 'PolldaddyShortcode' ) ) {
4 /**
5 * Class wrapper for polldaddy shortcodes
6 */
7
8 class PolldaddyShortcode {
9
10 static $add_script = false;
11 static $scripts = false;
12
13 /**
14 * Add all the actions & resgister the shortcode
15 */
16 function __construct() {
17 if ( defined( 'GLOBAL_TAGS' ) == false ) {
18 add_shortcode( 'polldaddy', array( $this, 'polldaddy_shortcode' ) );
19 add_filter( 'pre_kses', array( $this, 'polldaddy_embed_to_shortcode' ) );
20 }
21 add_action( 'wp_enqueue_scripts', array( $this, 'check_infinite' ) );
22 add_action( 'infinite_scroll_render', array( $this, 'polldaddy_shortcode_infinite' ), 11 );
23 }
24
25 private function get_async_code( array $settings, $survey_link ) {
26 $embed_src = 'http://i0.poll.fm/survey.js';
27 $embed_src_ssl = 'https://polldaddy.com/survey.js';
28
29 $include = <<<CONTAINER
30 ( function( d, c, j ) {
31 if ( !d.getElementById( j ) ) {
32 var pd = d.createElement( c ), s;
33 pd.id = j;
34 pd.src = ( 'https:' == d.location.protocol ) ? '{$embed_src_ssl}' : '{$embed_src}';
35 s = d.getElementsByTagName( c )[0];
36 s.parentNode.insertBefore( pd, s );
37 }
38 }( document, 'script', 'pd-embed' ) );
39 CONTAINER;
40
41 // Compress it a bit
42 $include = $this->compress_it( $include );
43
44 $placeholder =
45 '<div class="pd-embed" data-settings="'
46 . esc_attr( json_encode( $settings ) )
47 . '"></div>';
48 if ( 'button' === $settings['type'] ) {
49 $placeholder =
50 '<a class="pd-embed" href="'
51 . esc_attr( $survey_link )
52 . '" data-settings="'
53 . esc_attr( json_encode( $settings ) )
54 . '">'
55 . esc_html( $settings['title'] )
56 . '</a>';
57 }
58
59 $js_include = $placeholder . "\n";
60 $js_include .= '<script type="text/javascript"><!--//--><![CDATA[//><!--' . "\n";
61 $js_include .= $include . "\n";
62 $js_include .= "//--><!]]></script>\n";
63
64 if ( 'button' !== $settings['type'] ) {
65 $js_include .= '<noscript>' . $survey_link . "</noscript>\n";
66 }
67
68 return $js_include;
69 }
70
71 private function compress_it( $js ) {
72 $js = str_replace( array( "\n", "\t", "\r" ), '', $js );
73 $js = preg_replace( '/\s*([,:\?\{;\-=\(\)])\s*/', '$1', $js );
74 return $js;
75 }
76
77 /*
78 * Polldaddy Poll Embed script - transforms code that looks like that:
79 * <script type="text/javascript" charset="utf-8" async src="http://static.polldaddy.com/p/123456.js"></script>
80 * <noscript><a href="http://polldaddy.com/poll/123456/">What is your favourite color?</a></noscript>
81 * into the [polldaddy poll=...] shortcode format
82 */
83 function polldaddy_embed_to_shortcode( $content ) {
84
85 if ( ! is_string( $content ) || false === strpos( $content, 'polldaddy.com/p/' ) ) {
86 return $content;
87 }
88
89 $regexes = array();
90
91 $regexes[] = '#<script[^>]+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"[^>]*+>\s*?</script>\r?\n?(<noscript>.*?</noscript>)?#i';
92
93 $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';
94
95 foreach ( $regexes as $regex ) {
96 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
97 continue;
98 }
99
100 foreach ( $matches as $match ) {
101 if ( ! isset( $match[2] ) ) {
102 continue;
103 }
104
105 $id = (int) $match[2];
106
107 if ( $id > 0 ) {
108 $content = str_replace( $match[0], " [polldaddy poll=$id]", $content );
109 /** This action is documented in modules/shortcodes/youtube.php */
110 do_action( 'jetpack_embed_to_shortcode', 'polldaddy', $id );
111 }
112 }
113 }
114
115 return $content;
116 }
117
118 /**
119 * Shortcode for polldadddy
120 * [polldaddy poll|survey|rating="123456"]
121 * */
122 function polldaddy_shortcode( $atts ) {
123 global $post;
124 global $content_width;
125
126 extract(
127 shortcode_atts(
128 array(
129 'survey' => null,
130 'link_text' => 'Take Our Survey',
131 'poll' => 'empty',
132 'rating' => 'empty',
133 'unique_id' => null,
134 'item_id' => null,
135 'title' => null,
136 'permalink' => null,
137 'cb' => 0,
138 'type' => 'button',
139 'body' => '',
140 'button' => '',
141 'text_color' => '000000',
142 'back_color' => 'FFFFFF',
143 'align' => '',
144 'style' => '',
145 'width' => $content_width,
146 'height' => floor( $content_width * 3 / 4 ),
147 'delay' => 100,
148 'visit' => 'single',
149 'domain' => '',
150 'id' => '',
151 ),
152 $atts,
153 'polldaddy'
154 )
155 );
156
157 if ( ! is_array( $atts ) ) {
158 return '<!-- Polldaddy shortcode passed invalid attributes -->';
159 }
160
161 $inline = ! in_the_loop();
162 $no_script = false;
163 $infinite_scroll = false;
164
165 if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) {
166 $infinite_scroll = true;
167 }
168
169 if ( defined( 'PADPRESS_LOADED' ) ) {
170 $inline = true;
171 }
172
173 if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) {
174 $inline = true;
175 }
176
177 if ( is_feed() || ( defined( 'DOING_AJAX' ) && ! $infinite_scroll ) ) {
178 $no_script = false;
179 }
180
181 self::$add_script = $infinite_scroll;
182
183 if ( intval( $rating ) > 0 && ! $no_script ) { // rating embed
184
185 if ( empty( $unique_id ) ) {
186 $unique_id = is_page() ? 'wp-page-' . $post->ID : 'wp-post-' . $post->ID;
187 }
188
189 if ( empty( $item_id ) ) {
190 $item_id = is_page() ? '_page_' . $post->ID : '_post_' . $post->ID;
191 }
192
193 if ( empty( $title ) ) {
194 /** This filter is documented in core/src/wp-includes/general-template.php */
195 $title = apply_filters( 'wp_title', $post->post_title, '', '' );
196 }
197
198 if ( empty( $permalink ) ) {
199 $permalink = get_permalink( $post->ID );
200 }
201
202 $rating = intval( $rating );
203 $unique_id = preg_replace( '/[^\-_a-z0-9]/i', '', wp_strip_all_tags( $unique_id ) );
204 $item_id = wp_strip_all_tags( $item_id );
205 $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id );
206
207 $settings = json_encode(
208 array(
209 'id' => $rating,
210 'unique_id' => $unique_id,
211 'title' => rawurlencode( trim( $title ) ),
212 'permalink' => esc_url( $permalink ),
213 'item_id' => $item_id,
214 )
215 );
216
217 $item_id = esc_js( $item_id );
218
219 if ( is_ssl() ) {
220 $rating_js_file = 'https://polldaddy.com/js/rating/rating.js';
221 } else {
222 $rating_js_file = 'http://i0.poll.fm/js/rating/rating.js';
223 }
224
225 if ( $inline ) {
226 return <<<SCRIPT
227 <div class="pd-rating" id="pd_rating_holder_{$rating}{$item_id}"></div>
228 <script type="text/javascript" charset="UTF-8"><!--//--><![CDATA[//><!--
229 PDRTJS_settings_{$rating}{$item_id}={$settings};
230 //--><!]]></script>
231 <script type="text/javascript" charset="UTF-8" async src="{$rating_js_file}"></script>
232 SCRIPT;
233 } else {
234 if ( false === self::$scripts ) {
235 self::$scripts = array();
236 }
237
238 $data = array(
239 'id' => $rating,
240 'item_id' => $item_id,
241 'settings' => $settings,
242 );
243
244 self::$scripts['rating'][] = $data;
245
246 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
247
248 $data = esc_attr( json_encode( $data ) );
249
250 if ( $infinite_scroll ) {
251 return <<<CONTAINER
252 <div class="pd-rating" id="pd_rating_holder_{$rating}{$item_id}" data-settings="{$data}"></div>
253 CONTAINER;
254 } else {
255 return <<<CONTAINER
256 <div class="pd-rating" id="pd_rating_holder_{$rating}{$item_id}"></div>
257 CONTAINER;
258 }
259 }
260 } elseif ( intval( $poll ) > 0 ) { // poll embed
261
262 $poll = intval( $poll );
263 $poll_url = sprintf( 'http://polldaddy.com/poll/%d', $poll );
264 $poll_js = sprintf( '%s.polldaddy.com/p/%d.js', ( is_ssl() ? 'https://secure' : 'http://static' ), $poll );
265 $poll_link = sprintf( '<a href="%s" target="_blank">Take Our Poll</a>', $poll_url );
266
267 if ( $no_script ) {
268 return $poll_link;
269 } else {
270 if ( $type == 'slider' && ! $inline ) {
271
272 if ( ! in_array( $visit, array( 'single', 'multiple' ) ) ) {
273 $visit = 'single';
274 }
275
276 $settings = array(
277 'type' => 'slider',
278 'embed' => 'poll',
279 'delay' => intval( $delay ),
280 'visit' => $visit,
281 'id' => intval( $poll ),
282 );
283
284 return $this->get_async_code( $settings, $poll_link );
285 } else {
286 $cb = ( $cb == 1 ? '?cb=' . mktime() : false );
287 $margins = '';
288 $float = '';
289
290 if ( in_array( $align, array( 'right', 'left' ) ) ) {
291 $float = sprintf( 'float: %s;', $align );
292
293 if ( $align == 'left' ) {
294 $margins = 'margin: 0px 10px 0px 0px;';
295 } elseif ( $align == 'right' ) {
296 $margins = 'margin: 0px 0px 0px 10px';
297 }
298 }
299
300 // Force the normal style embed on single posts/pages otherwise it's not rendered on infinite scroll themed blogs ('infinite_scroll_render' isn't fired)
301 if ( is_singular() ) {
302 $inline = true;
303 }
304
305 if ( false === $cb && ! $inline ) {
306 if ( false === self::$scripts ) {
307 self::$scripts = array();
308 }
309
310 $data = array( 'url' => $poll_js );
311
312 self::$scripts['poll'][ intval( $poll ) ] = $data;
313
314 add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
315
316 $data = esc_attr( json_encode( $data ) );
317
318 $script_url = esc_url_raw( plugins_url( 'js/polldaddy-shortcode.js', __FILE__ ) );
319 $str = <<<CONTAINER
320 <a name="pd_a_{$poll}"></a>
321 <div class="PDS_Poll" id="PDI_container{$poll}" data-settings="{$data}" style="display:inline-block;{$float}{$margins}"></div>
322 <div id="PD_superContainer"></div>
323 <noscript>{$poll_link}</noscript>
324 CONTAINER;
325
326 $loader = <<<SCRIPT
327 ( function( d, c, j ) {
328 if ( ! d.getElementById( j ) ) {
329 var pd = d.createElement( c ), s;
330 pd.id = j;
331 pd.src = '{$script_url}';
332 s = d.getElementsByTagName( c )[0];
333 s.parentNode.insertBefore( pd, s );
334 } else if ( typeof jQuery !== 'undefined' ) {
335 jQuery( d.body ).trigger( 'pd-script-load' );
336 }
337 } ( document, 'script', 'pd-polldaddy-loader' ) );
338 SCRIPT;
339
340 $loader = $this->compress_it( $loader );
341 $loader = "<script type='text/javascript'>\n" . $loader . "\n</script>";
342
343 return $str . $loader;
344 } else {
345 if ( $inline ) {
346 $cb = '';
347 }
348
349 return <<<CONTAINER
350 <a id="pd_a_{$poll}"></a>
351 <div class="PDS_Poll" id="PDI_container{$poll}" style="display:inline-block;{$float}{$margins}"></div>
352 <div id="PD_superContainer"></div>
353 <script type="text/javascript" charset="UTF-8" async src="{$poll_js}{$cb}"></script>
354 <noscript>{$poll_link}</noscript>
355 CONTAINER;
356 }
357 }
358 }
359 } elseif ( ! empty( $survey ) ) { // survey embed
360
361 if ( in_array( $type, array( 'iframe', 'button', 'banner', 'slider' ) ) ) {
362
363 if ( empty( $title ) ) {
364 $title = __( 'Take Our Survey', 'jetpack' );
365 if ( ! empty( $link_text ) ) {
366 $title = $link_text;
367 }
368 }
369
370 if ( $type == 'banner' || $type == 'slider' ) {
371 $inline = false;
372 }
373
374 $survey = preg_replace( '/[^a-f0-9]/i', '', $survey );
375 $survey_url = esc_url( "http://polldaddy.com/s/{$survey}" );
376 $survey_link = sprintf( '<a href="%s" target="_blank">%s</a>', $survey_url, esc_html( $title ) );
377
378 $settings = array();
379
380 // Do we want a full embed code or a link?
381 if ( $no_script || $inline || $infinite_scroll ) {
382 return $survey_link;
383 }
384
385 if ( $type == 'iframe' ) {
386 if ( $height != 'auto' ) {
387 if ( isset( $content_width ) && is_numeric( $width ) && $width > $content_width ) {
388 $width = $content_width;
389 }
390
391 if ( ! $width ) {
392 $width = '100%';
393 } else {
394 $width = (int) $width;
395 }
396
397 if ( ! $height ) {
398 $height = '600';
399 } else {
400 $height = (int) $height;
401 }
402
403 return <<<CONTAINER
404 <iframe src="{$survey_url}?iframe=1" frameborder="0" width="{$width}" height="{$height}" scrolling="auto" allowtransparency="true" marginheight="0" marginwidth="0">{$survey_link}</iframe>
405 CONTAINER;
406 } elseif ( ! empty( $domain ) && ! empty( $id ) ) {
407
408 $domain = preg_replace( '/[^a-z0-9\-]/i', '', $domain );
409 $id = preg_replace( '/[\/\?&\{\}]/', '', $id );
410
411 $auto_src = esc_url( "http://{$domain}.polldaddy.com/s/{$id}" );
412 $auto_src = parse_url( $auto_src );
413
414 if ( ! is_array( $auto_src ) || count( $auto_src ) == 0 ) {
415 return '<!-- no polldaddy output -->';
416 }
417
418 if ( ! isset( $auto_src['host'] ) || ! isset( $auto_src['path'] ) ) {
419 return '<!-- no polldaddy output -->';
420 }
421
422 $domain = $auto_src['host'] . '/s/';
423 $id = str_ireplace( '/s/', '', $auto_src['path'] );
424
425 $settings = array(
426 'type' => $type,
427 'auto' => true,
428 'domain' => $domain,
429 'id' => $id,
430 );
431 }
432 } else {
433 $text_color = preg_replace( '/[^a-f0-9]/i', '', $text_color );
434 $back_color = preg_replace( '/[^a-f0-9]/i', '', $back_color );
435
436 if (
437 ! in_array(
438 $align,
439 array(
440 'right',
441 'left',
442 'top-left',
443 'top-right',
444 'middle-left',
445 'middle-right',
446 'bottom-left',
447 'bottom-right',
448 )
449 )
450 ) {
451 $align = '';
452 }
453
454 if (
455 ! in_array(
456 $style,
457 array(
458 'inline',
459 'side',
460 'corner',
461 'rounded',
462 'square',
463 )
464 )
465 ) {
466 $style = '';
467 }
468
469 $title = wp_strip_all_tags( $title );
470 $body = wp_strip_all_tags( $body );
471 $button = wp_strip_all_tags( $button );
472
473 $settings = array_filter(
474 array(
475 'title' => $title,
476 'type' => $type,
477 'body' => $body,
478 'button' => $button,
479 'text_color' => $text_color,
480 'back_color' => $back_color,
481 'align' => $align,
482 'style' => $style,
483 'id' => $survey,
484 )
485 );
486 }
487
488 if ( empty( $settings ) ) {
489 return '<!-- no polldaddy output -->';
490 }
491
492 return $this->get_async_code( $settings, $survey_link );
493 }
494 } else {
495 return '<!-- no polldaddy output -->';
496 }
497 }
498
499 function generate_scripts() {
500 $script = '';
501
502 if ( is_array( self::$scripts ) ) {
503 if ( is_ssl() ) {
504 $rating_js_file = 'https://polldaddy.com/js/rating/rating.js';
505 } else {
506 $rating_js_file = 'http://i0.poll.fm/js/rating/rating.js';
507 }
508
509 if ( isset( self::$scripts['rating'] ) ) {
510 $script = "<script type='text/javascript' charset='UTF-8' id='polldaddyRatings'><!--//--><![CDATA[//><!--\n";
511 foreach ( self::$scripts['rating'] as $rating ) {
512 $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']} );}}";
513 }
514 $script .= "\n//--><!]]></script><script type='text/javascript' charset='UTF-8' async src='{$rating_js_file}'></script>";
515
516 }
517
518 if ( isset( self::$scripts['poll'] ) ) {
519 foreach ( self::$scripts['poll'] as $poll ) {
520 $script .= "<script type='text/javascript' charset='UTF-8' async src='{$poll['url']}'></script>";
521 }
522 }
523 }
524
525 self::$scripts = false;
526 echo $script;
527 }
528
529 /**
530 * If the theme uses infinite scroll, include jquery at the start
531 */
532 function check_infinite() {
533 if (
534 current_theme_supports( 'infinite-scroll' )
535 && class_exists( 'The_Neverending_Home_Page' )
536 && The_Neverending_Home_Page::archive_supports_infinity()
537 ) {
538 wp_enqueue_script( 'jquery' );
539 }
540 }
541
542 /**
543 * Dynamically load the .js, if needed
544 *
545 * This hooks in late (priority 11) to infinite_scroll_render to determine
546 * a posteriori if a shortcode has been called.
547 */
548 function polldaddy_shortcode_infinite() {
549 // only try to load if a shortcode has been called and theme supports infinite scroll
550 if ( self::$add_script ) {
551 $script_url = esc_url_raw( plugins_url( 'js/polldaddy-shortcode.js', __FILE__ ) );
552
553 // if the script hasn't been loaded, load it
554 // if the script loads successfully, fire an 'pd-script-load' event
555 echo <<<SCRIPT
556 <script type='text/javascript'>
557 //<![CDATA[
558 ( function( d, c, j ) {
559 if ( !d.getElementById( j ) ) {
560 var pd = d.createElement( c ), s;
561 pd.id = j;
562 pd.async = true;
563 pd.src = '{$script_url}';
564 s = d.getElementsByTagName( c )[0];
565 s.parentNode.insertBefore( pd, s );
566 } else if ( typeof jQuery !== 'undefined' ) {
567 jQuery( d.body ).trigger( 'pd-script-load' );
568 }
569 } ( document, 'script', 'pd-polldaddy-loader' ) );
570 //]]>
571 </script>
572 SCRIPT;
573
574 }
575 }
576 }
577
578 // kick it all off
579 new PolldaddyShortcode();
580
581 if ( ! function_exists( 'polldaddy_link' ) ) {
582 // http://polldaddy.com/poll/1562975/?view=results&msg=voted
583 function polldaddy_link( $content ) {
584 return jetpack_preg_replace_outside_tags( '!(?:\n|\A)http://polldaddy.com/poll/([0-9]+?)/(.+)?(?:\n|\Z)!i', "\n<script type='text/javascript' charset='utf-8' async src='//static.polldaddy.com/p/$1.js'></script><noscript> <a href='http://polldaddy.com/poll/$1/'>View Poll</a></noscript>\n", $content, 'polldaddy.com/poll' );
585 }
586
587 // higher priority because we need it before auto-link and autop get to it
588 add_filter( 'the_content', 'polldaddy_link', 1 );
589 add_filter( 'the_content_rss', 'polldaddy_link', 1 );
590 }
591
592 wp_oembed_add_provider( '#http://poll\.fm/.*#i', 'http://polldaddy.com/oembed/', true );
593
594 }
595