polldaddy.php
| 1 | <?php |
| 2 | |
| 3 | if ( !class_exists( 'PolldaddyShortcode' ) ) { |
| 4 | /** |
| 5 | * Class wrapper for polldaddy shortcodes |
| 6 | */ |
| 7 | class PolldaddyShortcode { |
| 8 | |
| 9 | static $add_script = false; |
| 10 | static $scripts = false; |
| 11 | |
| 12 | /** |
| 13 | * Add all the actions & resgister the shortcode |
| 14 | */ |
| 15 | function __construct() { |
| 16 | if ( defined( 'GLOBAL_TAGS' ) == false ) |
| 17 | add_shortcode( 'polldaddy', array( $this, 'polldaddy_shortcode' ) ); |
| 18 | add_action( 'wp_enqueue_scripts', array( $this, 'check_infinite' ) ); |
| 19 | add_action( 'infinite_scroll_render', array( $this, 'polldaddy_shortcode_infinite' ), 11 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Shortcode for polldadddy |
| 24 | * [polldaddy poll|survey|rating="123456"] |
| 25 | * |
| 26 | * */ |
| 27 | function polldaddy_shortcode( $atts ) { |
| 28 | global $post; |
| 29 | global $content_width; |
| 30 | |
| 31 | extract( shortcode_atts( array( |
| 32 | 'survey' => null, |
| 33 | 'link_text' => 'Take Our Survey', |
| 34 | 'poll' => 'empty', |
| 35 | 'rating' => 'empty', |
| 36 | 'unique_id' => null, |
| 37 | 'item_id' => null, |
| 38 | 'title' => null, |
| 39 | 'permalink' => null, |
| 40 | 'cb' => 0, |
| 41 | 'type' => 'button', |
| 42 | 'body' => '', |
| 43 | 'button' => '', |
| 44 | 'text_color' => '000000', |
| 45 | 'back_color' => 'FFFFFF', |
| 46 | 'align' => '', |
| 47 | 'style' => '', |
| 48 | 'width' => $content_width, |
| 49 | 'height' => floor( $content_width * 3 / 4 ), |
| 50 | 'delay' => 100, |
| 51 | 'visit' => 'single', |
| 52 | 'domain' => '', |
| 53 | 'id' => '' |
| 54 | ), $atts ) ); |
| 55 | |
| 56 | if ( ! is_array( $atts ) ) { |
| 57 | return '<!-- Polldaddy shortcode passed invalid attributes -->'; |
| 58 | } |
| 59 | |
| 60 | $inline = false; |
| 61 | $no_script = false; |
| 62 | $infinite_scroll = false; |
| 63 | |
| 64 | if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) |
| 65 | $infinite_scroll = true; |
| 66 | |
| 67 | if ( defined( 'PADPRESS_LOADED' ) ) |
| 68 | $inline = true; |
| 69 | |
| 70 | if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) |
| 71 | $inline = true; |
| 72 | |
| 73 | if ( is_feed() || ( defined( 'DOING_AJAX' ) && !$infinite_scroll ) ) |
| 74 | $no_script = false; |
| 75 | |
| 76 | self::$add_script = $infinite_scroll; |
| 77 | |
| 78 | if ( intval( $rating ) > 0 && !$no_script ) { //rating embed |
| 79 | |
| 80 | if ( empty( $unique_id ) ) |
| 81 | $unique_id = is_page() ? 'wp-page-'.$post->ID : 'wp-post-'.$post->ID; |
| 82 | |
| 83 | if ( empty( $item_id ) ) |
| 84 | $item_id = is_page() ? '_page_'.$post->ID : '_post_'.$post->ID; |
| 85 | |
| 86 | if ( empty( $title ) ) |
| 87 | $title = apply_filters( 'wp_title', $post->post_title, '', '' ); |
| 88 | |
| 89 | if ( empty( $permalink ) ) |
| 90 | $permalink = get_permalink( $post->ID ); |
| 91 | |
| 92 | $rating = intval( $rating ); |
| 93 | $unique_id = wp_strip_all_tags( $unique_id ); |
| 94 | $item_id = wp_strip_all_tags( $item_id ); |
| 95 | $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id ); |
| 96 | |
| 97 | $settings = json_encode( array( |
| 98 | 'id' => $rating, |
| 99 | 'unique_id' => $unique_id, |
| 100 | 'title' => rawurlencode( trim( $title ) ), |
| 101 | 'permalink' => esc_url( $permalink ), |
| 102 | 'item_id' => $item_id |
| 103 | ) ); |
| 104 | |
| 105 | $item_id = esc_js( $item_id ); |
| 106 | |
| 107 | if ( $inline ) { |
| 108 | return <<<SCRIPT |
| 109 | <div class="pd-rating" id="pd_rating_holder_{$rating}{$item_id}"></div> |
| 110 | <script type="text/javascript" charset="UTF-8"><!--//--><![CDATA[//><!-- |
| 111 | PDRTJS_settings_{$rating}{$item_id}={$settings}; |
| 112 | //--><!]]></script> |
| 113 | <script type="text/javascript" charset="UTF-8" src="http://i.polldaddy.com/ratings/rating.js"></script> |
| 114 | SCRIPT; |
| 115 | } |
| 116 | else { |
| 117 | if ( self::$scripts === false ) |
| 118 | self::$scripts = array(); |
| 119 | |
| 120 | $data = array( 'id' => $rating, 'item_id' => $item_id, 'settings' => $settings ); |
| 121 | |
| 122 | self::$scripts['rating'][] = $data; |
| 123 | |
| 124 | add_action( 'wp_footer', array( $this, 'generate_scripts' ) ); |
| 125 | |
| 126 | $data = esc_attr( json_encode( $data ) ); |
| 127 | |
| 128 | if ( $infinite_scroll ) |
| 129 | return <<<CONTAINER |
| 130 | <div class="pd-rating" id="pd_rating_holder_{$rating}{$item_id}" data-settings="{$data}"></div> |
| 131 | CONTAINER; |
| 132 | else |
| 133 | return <<<CONTAINER |
| 134 | <div class="pd-rating" id="pd_rating_holder_{$rating}{$item_id}"></div> |
| 135 | CONTAINER; |
| 136 | } |
| 137 | } |
| 138 | elseif ( intval( $poll ) > 0 ) { //poll embed |
| 139 | |
| 140 | $poll = intval( $poll ); |
| 141 | $poll_url = sprintf( 'http://polldaddy.com/poll/%d', $poll ); |
| 142 | $poll_js = sprintf( '%s.polldaddy.com/p/%d.js', ( is_ssl() ? 'https://secure' : 'http://static' ), $poll ); |
| 143 | $poll_link = sprintf( '<a href="%s">Take Our Poll</a>', $poll_url ); |
| 144 | |
| 145 | if ( $no_script ) |
| 146 | return $poll_link; |
| 147 | else { |
| 148 | if ( $type == 'slider' && !$inline ) { |
| 149 | |
| 150 | if( !in_array( $visit, array( 'single', 'multiple' ) ) ) |
| 151 | $visit = 'single'; |
| 152 | |
| 153 | $settings = json_encode( array( |
| 154 | 'type' => 'slider', |
| 155 | 'embed' => 'poll', |
| 156 | 'delay' => intval( $delay ), |
| 157 | 'visit' => $visit, |
| 158 | 'id' => intval( $poll ) |
| 159 | ) ); |
| 160 | |
| 161 | return <<<SCRIPT |
| 162 | <script type="text/javascript" charset="UTF-8" src="http://i0.poll.fm/survey.js"></script> |
| 163 | <script type="text/javascript" charset="UTF-8"><!--//--><![CDATA[//><!-- |
| 164 | polldaddy.add( {$settings} ); |
| 165 | //--><!]]></script> |
| 166 | <noscript>{$poll_link}</noscript> |
| 167 | SCRIPT; |
| 168 | } |
| 169 | else { |
| 170 | $cb = ( $cb == 1 ? '?cb='.mktime() : false ); |
| 171 | $margins = ''; |
| 172 | $float = ''; |
| 173 | |
| 174 | if ( in_array( $align, array( 'right', 'left' ) ) ) { |
| 175 | $float = sprintf( 'float: %s;', $align ); |
| 176 | |
| 177 | if ( $align == 'left') |
| 178 | $margins = 'margin: 0px 10px 0px 0px;'; |
| 179 | elseif ( $align == 'right' ) |
| 180 | $margins = 'margin: 0px 0px 0px 10px'; |
| 181 | } |
| 182 | |
| 183 | if ( $cb === false && !$inline ) { |
| 184 | if ( self::$scripts === false ) |
| 185 | self::$scripts = array(); |
| 186 | |
| 187 | $data = array( 'url' => $poll_js ); |
| 188 | |
| 189 | self::$scripts['poll'][] = $data; |
| 190 | |
| 191 | add_action( 'wp_footer', array( $this, 'generate_scripts' ) ); |
| 192 | |
| 193 | $data = esc_attr( json_encode( $data ) ); |
| 194 | |
| 195 | return <<<CONTAINER |
| 196 | <a id="pd_a_{$poll}"></a> |
| 197 | <div class="PDS_Poll" id="PDI_container{$poll}" data-settings="{$data}" style="display:inline-block;{$float}{$margins}"></div> |
| 198 | <div id="PD_superContainer"></div> |
| 199 | <noscript>{$poll_link}</noscript> |
| 200 | CONTAINER; |
| 201 | } |
| 202 | else { |
| 203 | if ( $inline ) |
| 204 | $cb = ''; |
| 205 | |
| 206 | return <<<CONTAINER |
| 207 | <a id="pd_a_{$poll}"></a> |
| 208 | <div class="PDS_Poll" id="PDI_container{$poll}" style="display:inline-block;{$float}{$margins}"></div> |
| 209 | <div id="PD_superContainer"></div> |
| 210 | <script type="text/javascript" charset="UTF-8" src="{$poll_js}{$cb}"></script> |
| 211 | <noscript>{$poll_link}</noscript> |
| 212 | CONTAINER; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | elseif ( !empty( $survey ) ) { //survey embed |
| 218 | |
| 219 | if ( in_array( $type, array( 'iframe', 'button', 'banner', 'slider' ) ) ) { |
| 220 | |
| 221 | if ( empty( $title ) ) { |
| 222 | $title = 'Take Our Survey'; |
| 223 | if( !empty( $link_text ) ) |
| 224 | $title = $link_text; |
| 225 | } |
| 226 | |
| 227 | $survey = preg_replace( '/[^a-f0-9]/i', '', $survey ); |
| 228 | $survey_url = esc_url( "http://polldaddy.com/s/{$survey}" ); |
| 229 | $survey_link = sprintf( '<a href="%s">%s</a>', $survey_url, esc_html( $title ) ); |
| 230 | |
| 231 | if ( $no_script || $inline || $infinite_scroll ) |
| 232 | return $survey_link; |
| 233 | |
| 234 | if ( $type == 'iframe' ) { |
| 235 | if ( $height != 'auto' ) { |
| 236 | if ( isset( $content_width ) && is_numeric( $width ) && $width > $content_width ) |
| 237 | $width = $content_width; |
| 238 | |
| 239 | if ( !$width ) |
| 240 | $width = '100%'; |
| 241 | else |
| 242 | $width = (int) $width; |
| 243 | |
| 244 | if ( !$height ) |
| 245 | $height = '600'; |
| 246 | else |
| 247 | $height = (int) $height; |
| 248 | |
| 249 | return <<<CONTAINER |
| 250 | <iframe src="{$survey_url}?iframe=1" frameborder="0" width="{$width}" height="{$height}" scrolling="auto" allowtransparency="true" marginheight="0" marginwidth="0">{$survey_link}</iframe> |
| 251 | CONTAINER; |
| 252 | } |
| 253 | elseif ( !empty( $domain ) && !empty( $id ) ) { |
| 254 | |
| 255 | $auto_src = esc_url( "http://{$domain}.polldaddy.com/s/{$id}" ); |
| 256 | $auto_src = parse_url( $auto_src ); |
| 257 | |
| 258 | if ( !is_array( $auto_src ) || count( $auto_src ) == 0 ) |
| 259 | return '<!-- no polldaddy output -->'; |
| 260 | |
| 261 | if ( !isset( $auto_src['host'] ) || !isset( $auto_src['path'] ) ) |
| 262 | return '<!-- no polldaddy output -->'; |
| 263 | |
| 264 | $domain = $auto_src['host'].'/s/'; |
| 265 | $id = str_ireplace( '/s/', '', $auto_src['path'] ); |
| 266 | |
| 267 | $settings = json_encode( array( |
| 268 | 'type' => $type, |
| 269 | 'auto' => true, |
| 270 | 'domain' => $domain, |
| 271 | 'id' => $id |
| 272 | ) ); |
| 273 | } |
| 274 | } |
| 275 | else { |
| 276 | $text_color = preg_replace( '/[^a-f0-9]/i', '', $text_color ); |
| 277 | $back_color = preg_replace( '/[^a-f0-9]/i', '', $back_color ); |
| 278 | |
| 279 | if ( !in_array( $align, array( 'right', 'left', 'top-left', 'top-right', 'middle-left', 'middle-right', 'bottom-left', 'bottom-right' ) ) ) |
| 280 | $align = ''; |
| 281 | |
| 282 | if ( !in_array( $style, array( 'inline', 'side', 'corner', 'rounded', 'square' ) ) ) |
| 283 | $style = ''; |
| 284 | |
| 285 | $title = wp_strip_all_tags( $title ); |
| 286 | $body = wp_strip_all_tags( $body ); |
| 287 | $button = wp_strip_all_tags( $button ); |
| 288 | |
| 289 | $settings = json_encode( array_filter( array( |
| 290 | 'title' => $title, |
| 291 | 'type' => $type, |
| 292 | 'body' => $body, |
| 293 | 'button' => $button, |
| 294 | 'text_color' => $text_color, |
| 295 | 'back_color' => $back_color, |
| 296 | 'align' => $align, |
| 297 | 'style' => $style, |
| 298 | 'id' => $survey |
| 299 | ) ) ); |
| 300 | } |
| 301 | return <<<CONTAINER |
| 302 | <script type="text/javascript" charset="UTF-8" src="http://i0.poll.fm/survey.js"></script> |
| 303 | <script type="text/javascript" charset="UTF-8"><!--//--><![CDATA[//><!-- |
| 304 | polldaddy.add( {$settings} ); |
| 305 | //--><!]]></script> |
| 306 | <noscript>{$survey_link}</noscript> |
| 307 | CONTAINER; |
| 308 | } |
| 309 | } |
| 310 | else |
| 311 | return '<!-- no polldaddy output -->'; |
| 312 | } |
| 313 | |
| 314 | function generate_scripts() { |
| 315 | $script = ''; |
| 316 | |
| 317 | if ( is_array( self::$scripts ) ) { |
| 318 | if ( isset( self::$scripts['rating'] ) ) { |
| 319 | $script = "<script type='text/javascript' charset='UTF-8' id='polldaddyRatings'><!--//--><![CDATA[//><!--\n"; |
| 320 | foreach( self::$scripts['rating'] as $rating ) { |
| 321 | $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']} );}}"; |
| 322 | } |
| 323 | $script .= "\n//--><!]]></script><script type='text/javascript' charset='UTF-8' src='http://i.polldaddy.com/ratings/rating.js'></script>"; |
| 324 | |
| 325 | } |
| 326 | |
| 327 | if ( isset( self::$scripts['poll'] ) ) { |
| 328 | foreach( self::$scripts['poll'] as $poll ) { |
| 329 | $script .= "<script type='text/javascript' charset='UTF-8' src='{$poll['url']}'></script>"; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | self::$scripts = false; |
| 335 | echo $script; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * If the theme uses infinite scroll, include jquery at the start |
| 340 | */ |
| 341 | function check_infinite() { |
| 342 | if ( current_theme_supports( 'infinite-scroll' ) ) { |
| 343 | wp_enqueue_script( 'jquery' ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Dynamically load the .js, if needed |
| 349 | * |
| 350 | * This hooks in late (priority 11) to infinite_scroll_render to determine |
| 351 | * a posteriori if a shortcode has been called. |
| 352 | */ |
| 353 | function polldaddy_shortcode_infinite() { |
| 354 | // only try to load if a shortcode has been called and theme supports infinite scroll |
| 355 | if( self::$add_script ) { |
| 356 | $script_url = json_encode( esc_url_raw( plugins_url( 'js/polldaddy-shortcode.js', __FILE__ ) ) ); |
| 357 | |
| 358 | // if the script hasn't been loaded, load it |
| 359 | // if the script loads successfully, fire an 'as-script-load' event |
| 360 | echo <<<SCRIPT |
| 361 | <script type='text/javascript'> |
| 362 | //<![CDATA[ |
| 363 | if ( typeof window.polldaddyshortcode === 'undefined' ) { |
| 364 | var wp_pd_js = document.createElement( 'script' ); |
| 365 | wp_pd_js.type = 'text/javascript'; |
| 366 | wp_pd_js.src = $script_url; |
| 367 | wp_pd_js.async = true; |
| 368 | wp_pd_js.onload = function() { |
| 369 | jQuery( document.body ).trigger( 'pd-script-load' ); |
| 370 | }; |
| 371 | document.getElementsByTagName( 'head' )[0].appendChild( wp_pd_js ); |
| 372 | } else { |
| 373 | jQuery( document.body ).trigger( 'pd-script-load' ); |
| 374 | } |
| 375 | //]]> |
| 376 | </script> |
| 377 | SCRIPT; |
| 378 | |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // kick it all off |
| 384 | new PolldaddyShortcode(); |
| 385 | |
| 386 | if ( !function_exists( 'polldaddy_link' ) ) { |
| 387 | // http://polldaddy.com/poll/1562975/?view=results&msg=voted |
| 388 | function polldaddy_link( $content ) { |
| 389 | return jetpack_preg_replace_outside_tags( '!(?:\n|\A)http://polldaddy.com/poll/([0-9]+?)/(.+)?(?:\n|\Z)!i', "\n<script type='text/javascript' language='javascript' charset='utf-8' src='http://static.polldaddy.com/p/$1.js'></script><noscript> <a href='http://polldaddy.com/poll/$1/'>View Poll</a></noscript>\n", $content, 'polldaddy.com/poll' ); |
| 390 | } |
| 391 | |
| 392 | // higher priority because we need it before auto-link and autop get to it |
| 393 | add_filter( 'the_content', 'polldaddy_link', 1 ); |
| 394 | add_filter( 'the_content_rss', 'polldaddy_link', 1 ); |
| 395 | } |
| 396 | |
| 397 | } |
| 398 |