PolldaddyShortcode

Class wrapper for polldaddy shortcodes.

Defined (1)

The class is defined in the following location(s).

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