PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/shortcodes/polldaddy.php

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