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