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