<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
* Crowdsignal (PollDaddy) shortcode.
* [polldaddy type="iframe" survey="EB151947E5950FCF" height="auto" domain="jeherve" id="a-survey-with-branches"]
* [crowdsignal type="iframe" survey="EB151947E5950FCF" height="auto" domain="jeherve" id="a-survey-with-branches"]
* https://polldaddy.com/poll/7910844/
* https://jeherve.survey.fm/a-survey
* https://jeherve.survey.fm/a-survey-with-branches
* [crowdsignal type="iframe" survey="7676FB1FF2B56CE9" height="auto" domain="jeherve" id="a-survey"]
* [crowdsignal survey="7676FB1FF2B56CE9"]
* [polldaddy survey="7676FB1FF2B56CE9"]
* [crowdsignal poll=9541291]
* [crowdsignal poll=9541291 type=slider]
* [crowdsignal rating=8755352]
* @package automattic/jetpack
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Constants;
if ( ! defined( 'ABSPATH' ) ) {
// Keep compatibility with the PollDaddy plugin.
! class_exists( 'CrowdsignalShortcode' )
&& ! class_exists( 'PolldaddyShortcode' )
* Class wrapper for Crowdsignal shortcodes
* @phan-constructor-used-for-side-effects
class CrowdsignalShortcode {
* Should the Crowdsignal JavaScript be added to the page?
private static $add_script = false;
* Array of Polls / Surveys present on the page, and that need to be added.
private static $scripts = false;
* Add all the actions & register the shortcode.
public function __construct() {
add_action( 'init', array( $this, 'register_scripts' ) );
add_shortcode( 'crowdsignal', array( $this, 'crowdsignal_shortcode' ) );
add_shortcode( 'polldaddy', array( $this, 'polldaddy_shortcode' ) );
if ( jetpack_shortcodes_should_hook_pre_kses() ) {
add_filter( 'pre_kses', array( $this, 'crowdsignal_embed_to_shortcode' ) );
add_action( 'infinite_scroll_render', array( $this, 'crowdsignal_shortcode_infinite' ), 11 );
* Register scripts that may be enqueued later on by the shortcode.
public static function register_scripts() {
Assets::get_file_url_for_environment( '_inc/build/crowdsignal-shortcode.min.js', '_inc/crowdsignal-shortcode.js' ),
Assets::get_file_url_for_environment( '_inc/build/crowdsignal-survey.min.js', '_inc/crowdsignal-survey.js' ),
'https://polldaddy.com/js/rating/rating.js',
* JavaScript code for a specific survey / poll.
* @param array $settings Array of information about a survey / poll.
* @param string $survey_link HTML link tag for a specific survey or poll.
* @param string $survey_url Link to the survey or poll.
private function get_async_code( array $settings, $survey_link, $survey_url ) {
wp_enqueue_script( 'crowdsignal-survey' );
if ( 'button' === $settings['type'] ) {
'<a class="cs-embed pd-embed" href="%1$s" data-settings="%2$s">%3$s</a>',
esc_attr( wp_json_encode( $settings ) ),
esc_html( $settings['title'] )
'<div class="cs-embed pd-embed" data-settings="%1$s"></div><noscript>%2$s</noscript>',
esc_attr( wp_json_encode( $settings ) ),
* Crowdsignal Poll Embed script - transforms code that looks like that:
* <script type="text/javascript" charset="utf-8" async src="http://static.polldaddy.com/p/123456.js"></script>
* <noscript><a href="http://polldaddy.com/poll/123456/">What is your favourite color?</a></noscript>
* into the [crowdsignal poll=...] shortcode format
* @param string $content Post content.
public function crowdsignal_embed_to_shortcode( $content ) {
if ( ! is_string( $content ) || ! str_contains( $content, 'polldaddy.com/p/' ) ) {
$regexes[] = '#<script[^>]+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"[^>]*+>\s*?</script>\r?\n?(<noscript>.*?</noscript>)?#i';
$regexes[] = '#<script(?:[^&]|&(?!gt;))+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"(?:[^&]|&(?!gt;))*+>\s*?</script>\r?\n?(<noscript>.*?</noscript>)?#i';
foreach ( $regexes as $regex ) {
if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $match ) {
if ( ! isset( $match[2] ) ) {
$content = str_replace( $match[0], " [crowdsignal poll=$id]", $content );
/** This action is documented in modules/shortcodes/youtube.php */
do_action( 'jetpack_embed_to_shortcode', 'crowdsignal', $id );
* Support for legacy Polldaddy shortcode.
* @param array $atts Shortcode attributes.
public function polldaddy_shortcode( $atts ) {
if ( ! is_array( $atts ) ) {
return '<!-- Polldaddy shortcode passed invalid attributes -->';
$atts['site'] = 'polldaddy.com';
return $this->crowdsignal_shortcode( $atts );
* Shortcode for Crowdsignal
* [crowdsignal poll|survey|rating="123456"]
* @param array $atts Shortcode attributes.
public function crowdsignal_shortcode( $atts ) {
if ( ! is_array( $atts ) ) {
return '<!-- Crowdsignal shortcode passed invalid attributes -->';
$attributes = shortcode_atts(
'link_text' => esc_html__( 'Take Our Survey', 'jetpack' ),
'cb' => 0, // cache buster. Helps with testing.
'text_color' => '000000',
'back_color' => 'FFFFFF',
'width' => $content_width,
'height' => floor( $content_width * 3 / 4 ),
'site' => 'crowdsignal.com',
$inline = ! in_the_loop()
&& ! Constants::is_defined( 'TESTING_IN_JETPACK' );
$infinite_scroll = false;
if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) {
if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) {
if ( is_feed() || ( defined( 'DOING_AJAX' ) && ! $infinite_scroll ) ) {
self::$add_script = $infinite_scroll;
if ( (int) $attributes['rating'] > 0 && ! $no_script ) {
$post_id = $post instanceof WP_Post ? $post->ID : get_the_ID();
$post_id = $post_id ?? '';
if ( empty( $attributes['unique_id'] ) ) {
$attributes['unique_id'] = is_page() ? 'wp-page-' . $post_id : 'wp-post-' . $post_id;
if ( empty( $attributes['item_id'] ) ) {
$attributes['item_id'] = is_page() ? '_page_' . $post_id : '_post_' . $post_id;
if ( empty( $attributes['title'] ) ) {
$title = $post instanceof WP_Post ? $post->post_title : get_the_title();
/** This filter is documented in core/src/wp-includes/general-template.php */
$attributes['title'] = apply_filters( 'wp_title', $title, '', '' );
if ( empty( $attributes['permalink'] ) ) {
$attributes['permalink'] = get_permalink( $post_id );
$attributes['permalink'] = home_url( add_query_arg( array() ) );
$rating = (int) $attributes['rating'];
$unique_id = sanitize_key( wp_strip_all_tags( $attributes['unique_id'] ) );
$item_id = wp_strip_all_tags( $attributes['item_id'] );
$item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id );
$settings = wp_json_encode(
'unique_id' => $unique_id,
'title' => rawurlencode( trim( $attributes['title'] ) ),
'permalink' => esc_url( $attributes['permalink'] ),
$item_id = esc_js( $item_id );
class_exists( 'Jetpack_AMP_Support' )
&& Jetpack_AMP_Support::is_amp_request()
'<a href="%s" target="_blank">%s</a>',
esc_url( $attributes['permalink'] ),
esc_html( trim( $attributes['title'] ) )
$rating_js = "<!--//--><![CDATA[//><!--\n";
$rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};";
$rating_js .= "\n//--><!]]>";
wp_enqueue_script( 'crowdsignal-rating' );
'<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
if ( false === self::$scripts ) {
self::$scripts = array();
self::$scripts['rating'][] = $data;
add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
if ( $infinite_scroll ) {
'<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>',
esc_attr( wp_json_encode( $data ) )
'<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
} elseif ( (int) $attributes['poll'] > 0 ) {
if ( empty( $attributes['title'] ) ) {
$attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' );
$poll = (int) $attributes['poll'];
if ( 'crowdsignal.com' === $attributes['site'] ) {
$poll_url = sprintf( 'https://poll.fm/%d', $poll );
$poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll );
$poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll );
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
esc_html( $attributes['title'] )
|| ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() )
} elseif ( 'slider' === $attributes['type'] && ! $inline ) { // Slider poll.
array( 'single', 'multiple' ),
$attributes['visit'] = 'single';
'delay' => (int) $attributes['delay'],
'visit' => $attributes['visit'],
'site' => $attributes['site'],
return $this->get_async_code( $settings, $poll_link, $poll_url );
if ( 1 === $attributes['cb'] ) {
$attributes['cb'] = '?cb=' . time();
$attributes['cb'] = false;
array( 'right', 'left' ),
$float = sprintf( 'float: %s;', $attributes['align'] );
if ( 'left' === $attributes['align'] ) {
$margins = 'margin: 0 10px 0 0;';
} elseif ( 'right' === $attributes['align'] ) {
$margins = 'margin: 0 0 0 10px';
* 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)
if ( false === $attributes['cb'] && ! $inline ) {
if ( false === self::$scripts ) {
self::$scripts = array();
$data = array( 'url' => $poll_js );
self::$scripts['poll'][ (int) $poll ] = $data;
add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
wp_enqueue_script( 'crowdsignal-shortcode' );
'crowdsignal_shortcode_options',
'script_url' => esc_url_raw(
Assets::get_file_url_for_environment(
'_inc/build/polldaddy-shortcode.min.js',
'_inc/polldaddy-shortcode.js'
* Hook into the Crowdsignal shortcode before rendering.
* @param int $poll Poll ID.
do_action( 'crowdsignal_shortcode_before', (int) $poll );
'<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>',
esc_attr( wp_json_encode( $data ) ),
'crowdsignal-' . absint( $poll ),
esc_url( $poll_js . $attributes['cb'] ),
/** This action is already documented in modules/shortcodes/crowdsignal.php */
do_action( 'crowdsignal_shortcode_before', (int) $poll );
'<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>',
} elseif ( ! empty( $attributes['survey'] ) ) {