PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

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