PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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