PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/remyvianne/krowkaramel
PHP | 776 lines | 560 code | 101 blank | 115 comment | 94 complexity | 5db5bb55aa2f7b5803338c7368624241 MD5 | raw file
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Crowdsignal (PollDaddy) shortcode.
  4. *
  5. * Formats:
  6. * [polldaddy type="iframe" survey="EB151947E5950FCF" height="auto" domain="jeherve" id="a-survey-with-branches"]
  7. * [crowdsignal type="iframe" survey="EB151947E5950FCF" height="auto" domain="jeherve" id="a-survey-with-branches"]
  8. * https://polldaddy.com/poll/7910844/
  9. * https://jeherve.survey.fm/a-survey
  10. * https://jeherve.survey.fm/a-survey-with-branches
  11. * [crowdsignal type="iframe" survey="7676FB1FF2B56CE9" height="auto" domain="jeherve" id="a-survey"]
  12. * [crowdsignal survey="7676FB1FF2B56CE9"]
  13. * [polldaddy survey="7676FB1FF2B56CE9"]
  14. * [crowdsignal poll=9541291]
  15. * [crowdsignal poll=9541291 type=slider]
  16. * [crowdsignal rating=8755352]
  17. *
  18. * @package automattic/jetpack
  19. */
  20. use Automattic\Jetpack\Assets;
  21. use Automattic\Jetpack\Constants;
  22. // Keep compatibility with the PollDaddy plugin.
  23. if (
  24. ! class_exists( 'CrowdsignalShortcode' )
  25. && ! class_exists( 'PolldaddyShortcode' )
  26. ) {
  27. /**
  28. * Class wrapper for Crowdsignal shortcodes
  29. */
  30. class CrowdsignalShortcode {
  31. /**
  32. * Should the Crowdsignal JavaScript be added to the page?
  33. *
  34. * @var bool
  35. */
  36. private static $add_script = false;
  37. /**
  38. * Array of Polls / Surveys present on the page, and that need to be added.
  39. *
  40. * @var bool|array
  41. */
  42. private static $scripts = false;
  43. /**
  44. * Add all the actions & register the shortcode.
  45. */
  46. public function __construct() {
  47. add_action( 'init', array( $this, 'register_scripts' ) );
  48. add_shortcode( 'crowdsignal', array( $this, 'crowdsignal_shortcode' ) );
  49. add_shortcode( 'polldaddy', array( $this, 'polldaddy_shortcode' ) );
  50. add_filter( 'pre_kses', array( $this, 'crowdsignal_embed_to_shortcode' ) );
  51. add_action( 'wp_enqueue_scripts', array( $this, 'check_infinite' ) );
  52. add_action( 'infinite_scroll_render', array( $this, 'crowdsignal_shortcode_infinite' ), 11 );
  53. }
  54. /**
  55. * Register scripts that may be enqueued later on by the shortcode.
  56. */
  57. public static function register_scripts() {
  58. wp_register_script(
  59. 'crowdsignal-shortcode',
  60. Assets::get_file_url_for_environment( '_inc/build/crowdsignal-shortcode.min.js', '_inc/crowdsignal-shortcode.js' ),
  61. array( 'jquery' ),
  62. JETPACK__VERSION,
  63. true
  64. );
  65. wp_register_script(
  66. 'crowdsignal-survey',
  67. Assets::get_file_url_for_environment( '_inc/build/crowdsignal-survey.min.js', '_inc/crowdsignal-survey.js' ),
  68. array(),
  69. JETPACK__VERSION,
  70. true
  71. );
  72. wp_register_script(
  73. 'crowdsignal-rating',
  74. 'https://polldaddy.com/js/rating/rating.js',
  75. array(),
  76. JETPACK__VERSION,
  77. true
  78. );
  79. }
  80. /**
  81. * JavaScript code for a specific survey / poll.
  82. *
  83. * @param array $settings Array of information about a survey / poll.
  84. * @param string $survey_link HTML link tag for a specific survey or poll.
  85. * @param string $survey_url Link to the survey or poll.
  86. */
  87. private function get_async_code( array $settings, $survey_link, $survey_url ) {
  88. wp_enqueue_script( 'crowdsignal-survey' );
  89. if ( 'button' === $settings['type'] ) {
  90. $placeholder = sprintf(
  91. '<a class="cs-embed pd-embed" href="%1$s" data-settings="%2$s">%3$s</a>',
  92. esc_url( $survey_url ),
  93. esc_attr( wp_json_encode( $settings ) ),
  94. esc_html( $settings['title'] )
  95. );
  96. } else {
  97. $placeholder = sprintf(
  98. '<div class="cs-embed pd-embed" data-settings="%1$s"></div><noscript>%2$s</noscript>',
  99. esc_attr( wp_json_encode( $settings ) ),
  100. $survey_link
  101. );
  102. }
  103. return $placeholder;
  104. }
  105. /**
  106. * Crowdsignal Poll Embed script - transforms code that looks like that:
  107. * <script type="text/javascript" charset="utf-8" async src="http://static.polldaddy.com/p/123456.js"></script>
  108. * <noscript><a href="http://polldaddy.com/poll/123456/">What is your favourite color?</a></noscript>
  109. * into the [crowdsignal poll=...] shortcode format
  110. *
  111. * @param string $content Post content.
  112. */
  113. public function crowdsignal_embed_to_shortcode( $content ) {
  114. if ( ! is_string( $content ) || false === strpos( $content, 'polldaddy.com/p/' ) ) {
  115. return $content;
  116. }
  117. $regexes = array();
  118. $regexes[] = '#<script[^>]+?src="https?://(secure|static)\.polldaddy\.com/p/([0-9]+)\.js"[^>]*+>\s*?</script>\r?\n?(<noscript>.*?</noscript>)?#i';
  119. $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';
  120. foreach ( $regexes as $regex ) {
  121. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
  122. continue;
  123. }
  124. foreach ( $matches as $match ) {
  125. if ( ! isset( $match[2] ) ) {
  126. continue;
  127. }
  128. $id = (int) $match[2];
  129. if ( $id > 0 ) {
  130. $content = str_replace( $match[0], " [crowdsignal poll=$id]", $content );
  131. /** This action is documented in modules/shortcodes/youtube.php */
  132. do_action( 'jetpack_embed_to_shortcode', 'crowdsignal', $id );
  133. }
  134. }
  135. }
  136. return $content;
  137. }
  138. /**
  139. * Support for legacy Polldaddy shortcode.
  140. *
  141. * @param array $atts Shortcode attributes.
  142. */
  143. public function polldaddy_shortcode( $atts ) {
  144. if ( ! is_array( $atts ) ) {
  145. return '<!-- Polldaddy shortcode passed invalid attributes -->';
  146. }
  147. $atts['site'] = 'polldaddy.com';
  148. return $this->crowdsignal_shortcode( $atts );
  149. }
  150. /**
  151. * Shortcode for Crowdsignal
  152. * [crowdsignal poll|survey|rating="123456"]
  153. *
  154. * @param array $atts Shortcode attributes.
  155. */
  156. public function crowdsignal_shortcode( $atts ) {
  157. global $post;
  158. global $content_width;
  159. if ( ! is_array( $atts ) ) {
  160. return '<!-- Crowdsignal shortcode passed invalid attributes -->';
  161. }
  162. $attributes = shortcode_atts(
  163. array(
  164. 'survey' => null,
  165. 'link_text' => esc_html__( 'Take Our Survey', 'jetpack' ),
  166. 'poll' => 'empty',
  167. 'rating' => 'empty',
  168. 'unique_id' => null,
  169. 'item_id' => null,
  170. 'title' => null,
  171. 'permalink' => null,
  172. 'cb' => 0, // cache buster. Helps with testing.
  173. 'type' => 'button',
  174. 'body' => '',
  175. 'button' => '',
  176. 'text_color' => '000000',
  177. 'back_color' => 'FFFFFF',
  178. 'align' => '',
  179. 'style' => '',
  180. 'width' => $content_width,
  181. 'height' => floor( $content_width * 3 / 4 ),
  182. 'delay' => 100,
  183. 'visit' => 'single',
  184. 'domain' => '',
  185. 'id' => '',
  186. 'site' => 'crowdsignal.com',
  187. ),
  188. $atts,
  189. 'crowdsignal'
  190. );
  191. $inline = ! in_the_loop()
  192. && ! Constants::is_defined( 'TESTING_IN_JETPACK' );
  193. $no_script = false;
  194. $infinite_scroll = false;
  195. if ( is_home() && current_theme_supports( 'infinite-scroll' ) ) {
  196. $infinite_scroll = true;
  197. }
  198. if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) ) {
  199. $inline = true;
  200. }
  201. if ( is_feed() || ( defined( 'DOING_AJAX' ) && ! $infinite_scroll ) ) {
  202. $no_script = false;
  203. }
  204. self::$add_script = $infinite_scroll;
  205. /*
  206. * Rating embed.
  207. */
  208. if ( (int) $attributes['rating'] > 0 && ! $no_script ) {
  209. if ( empty( $attributes['unique_id'] ) ) {
  210. $attributes['unique_id'] = is_page() ? 'wp-page-' . $post->ID : 'wp-post-' . $post->ID;
  211. }
  212. if ( empty( $attributes['item_id'] ) ) {
  213. $attributes['item_id'] = is_page() ? '_page_' . $post->ID : '_post_' . $post->ID;
  214. }
  215. if ( empty( $attributes['title'] ) ) {
  216. /** This filter is documented in core/src/wp-includes/general-template.php */
  217. $attributes['title'] = apply_filters( 'wp_title', $post->post_title, '', '' );
  218. }
  219. if ( empty( $attributes['permalink'] ) ) {
  220. $attributes['permalink'] = get_permalink( $post->ID );
  221. }
  222. $rating = (int) $attributes['rating'];
  223. $unique_id = preg_replace( '/[^\-_a-z0-9]/i', '', wp_strip_all_tags( $attributes['unique_id'] ) );
  224. $item_id = wp_strip_all_tags( $attributes['item_id'] );
  225. $item_id = preg_replace( '/[^_a-z0-9]/i', '', $item_id );
  226. $settings = wp_json_encode(
  227. array(
  228. 'id' => $rating,
  229. 'unique_id' => $unique_id,
  230. 'title' => rawurlencode( trim( $attributes['title'] ) ),
  231. 'permalink' => esc_url( $attributes['permalink'] ),
  232. 'item_id' => $item_id,
  233. )
  234. );
  235. $item_id = esc_js( $item_id );
  236. if (
  237. class_exists( 'Jetpack_AMP_Support' )
  238. && Jetpack_AMP_Support::is_amp_request()
  239. ) {
  240. return sprintf(
  241. '<a href="%s" target="_blank">%s</a>',
  242. esc_url( $attributes['permalink'] ),
  243. esc_html( trim( $attributes['title'] ) )
  244. );
  245. } elseif ( $inline ) {
  246. $rating_js = "<!--//--><![CDATA[//><!--\n";
  247. $rating_js .= "PDRTJS_settings_{$rating}{$item_id}={$settings};";
  248. $rating_js .= "\n//--><!]]>";
  249. wp_enqueue_script( 'crowdsignal-rating' );
  250. wp_add_inline_script(
  251. 'crowdsignal-rating',
  252. $rating_js,
  253. 'before'
  254. );
  255. return sprintf(
  256. '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
  257. absint( $rating ),
  258. esc_attr( $item_id )
  259. );
  260. } else {
  261. if ( false === self::$scripts ) {
  262. self::$scripts = array();
  263. }
  264. $data = array(
  265. 'id' => $rating,
  266. 'item_id' => $item_id,
  267. 'settings' => $settings,
  268. );
  269. self::$scripts['rating'][] = $data;
  270. add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
  271. if ( $infinite_scroll ) {
  272. return sprintf(
  273. '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s" data-settings="%3$s"></div>',
  274. absint( $rating ),
  275. esc_attr( $item_id ),
  276. esc_attr( wp_json_encode( $data ) )
  277. );
  278. } else {
  279. return sprintf(
  280. '<div class="cs-rating pd-rating" id="pd_rating_holder_%1$d%2$s"></div>',
  281. absint( $rating ),
  282. esc_attr( $item_id )
  283. );
  284. }
  285. }
  286. } elseif ( (int) $attributes['poll'] > 0 ) {
  287. /*
  288. * Poll embed.
  289. */
  290. if ( empty( $attributes['title'] ) ) {
  291. $attributes['title'] = esc_html__( 'Take Our Poll', 'jetpack' );
  292. }
  293. $poll = (int) $attributes['poll'];
  294. if ( 'crowdsignal.com' === $attributes['site'] ) {
  295. $poll_url = sprintf( 'https://poll.fm/%d', $poll );
  296. } else {
  297. $poll_url = sprintf( 'https://polldaddy.com/p/%d', $poll );
  298. }
  299. $poll_js = sprintf( 'https://secure.polldaddy.com/p/%d.js', $poll );
  300. $poll_link = sprintf(
  301. '<a href="%s" target="_blank">%s</a>',
  302. esc_url( $poll_url ),
  303. esc_html( $attributes['title'] )
  304. );
  305. if (
  306. $no_script
  307. || ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() )
  308. ) {
  309. return $poll_link;
  310. } else {
  311. /*
  312. * Slider poll.
  313. */
  314. if (
  315. 'slider' === $attributes['type']
  316. && ! $inline
  317. ) {
  318. if ( ! in_array(
  319. $attributes['visit'],
  320. array( 'single', 'multiple' ),
  321. true
  322. ) ) {
  323. $attributes['visit'] = 'single';
  324. }
  325. $settings = array(
  326. 'type' => 'slider',
  327. 'embed' => 'poll',
  328. 'delay' => (int) $attributes['delay'],
  329. 'visit' => $attributes['visit'],
  330. 'id' => (int) $poll,
  331. 'site' => $attributes['site'],
  332. );
  333. return $this->get_async_code( $settings, $poll_link, $poll_url );
  334. } else {
  335. if ( 1 === $attributes['cb'] ) {
  336. $attributes['cb'] = '?cb=' . time();
  337. } else {
  338. $attributes['cb'] = false;
  339. }
  340. $margins = '';
  341. $float = '';
  342. if ( in_array(
  343. $attributes['align'],
  344. array( 'right', 'left' ),
  345. true
  346. ) ) {
  347. $float = sprintf( 'float: %s;', $attributes['align'] );
  348. if ( 'left' === $attributes['align'] ) {
  349. $margins = 'margin: 0px 10px 0px 0px;';
  350. } elseif ( 'right' === $attributes['align'] ) {
  351. $margins = 'margin: 0px 0px 0px 10px';
  352. }
  353. }
  354. /*
  355. * Force the normal style embed on single posts/pages
  356. * otherwise it's not rendered on infinite scroll themed blogs
  357. * ('infinite_scroll_render' isn't fired)
  358. */
  359. if ( is_singular() ) {
  360. $inline = true;
  361. }
  362. if ( false === $attributes['cb'] && ! $inline ) {
  363. if ( false === self::$scripts ) {
  364. self::$scripts = array();
  365. }
  366. $data = array( 'url' => $poll_js );
  367. self::$scripts['poll'][ (int) $poll ] = $data;
  368. add_action( 'wp_footer', array( $this, 'generate_scripts' ) );
  369. wp_enqueue_script( 'crowdsignal-shortcode' );
  370. wp_localize_script(
  371. 'crowdsignal-shortcode',
  372. 'crowdsignal_shortcode_options',
  373. array(
  374. 'script_url' => esc_url_raw(
  375. Assets::get_file_url_for_environment(
  376. '_inc/build/polldaddy-shortcode.min.js',
  377. '_inc/polldaddy-shortcode.js'
  378. )
  379. ),
  380. )
  381. );
  382. /**
  383. * Hook into the Crowdsignal shortcode before rendering.
  384. *
  385. * @since 8.4.0
  386. *
  387. * @param int $poll Poll ID.
  388. */
  389. do_action( 'crowdsignal_shortcode_before', (int) $poll );
  390. return sprintf(
  391. '<a name="pd_a_%1$d"></a><div class="CSS_Poll PDS_Poll" id="PDI_container%1$d" data-settings="%2$s" style="%3$s%4$s"></div><div id="PD_superContainer"></div><noscript>%5$s</noscript>',
  392. absint( $poll ),
  393. esc_attr( wp_json_encode( $data ) ),
  394. $float,
  395. $margins,
  396. $poll_link
  397. );
  398. } else {
  399. if ( $inline ) {
  400. $attributes['cb'] = '';
  401. }
  402. wp_enqueue_script(
  403. 'crowdsignal-' . absint( $poll ),
  404. esc_url( $poll_js . $attributes['cb'] ),
  405. array(),
  406. JETPACK__VERSION,
  407. true
  408. );
  409. /** This action is already documented in modules/shortcodes/crowdsignal.php */
  410. do_action( 'crowdsignal_shortcode_before', (int) $poll );
  411. return sprintf(
  412. '<a id="pd_a_%1$s"></a><div class="CSS_Poll PDS_Poll" id="PDI_container%1$s" style="%2$s%3$s"></div><div id="PD_superContainer"></div><noscript>%4$s</noscript>',
  413. absint( $poll ),
  414. $float,
  415. $margins,
  416. $poll_link
  417. );
  418. }
  419. }
  420. }
  421. } elseif ( ! empty( $attributes['survey'] ) ) {
  422. /*
  423. * Survey embed.
  424. */
  425. if ( in_array(
  426. $attributes['type'],
  427. array( 'iframe', 'button', 'banner', 'slider' ),
  428. true
  429. ) ) {
  430. if ( empty( $attributes['title'] ) ) {
  431. $attributes['title'] = esc_html__( 'Take Our Survey', 'jetpack' );
  432. if ( ! empty( $attributes['link_text'] ) ) {
  433. $attributes['title'] = $attributes['link_text'];
  434. }
  435. }
  436. if (
  437. 'banner' === $attributes['type']
  438. || 'slider' === $attributes['type']
  439. ) {
  440. $inline = false;
  441. }
  442. $survey_url = '';
  443. if ( 'true' !== $attributes['survey'] ) {
  444. $survey = preg_replace( '/[^a-f0-9]/i', '', $attributes['survey'] );
  445. if ( 'crowdsignal.com' === $attributes['site'] ) {
  446. $survey_url = 'https://survey.fm/' . $survey;
  447. } else {
  448. $survey_url = 'https://polldaddy.com/s/' . $survey;
  449. }
  450. } else {
  451. if ( isset( $attributes['domain'] ) && isset( $attributes['id'] ) ) {
  452. $survey_url = 'https://' . $attributes['domain'] . '.survey.fm/' . $attributes['id'];
  453. }
  454. }
  455. $survey_link = sprintf(
  456. '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
  457. esc_url( $survey_url ),
  458. esc_html( $attributes['title'] )
  459. );
  460. $settings = array();
  461. if ( 'iframe' === $attributes['type'] ) {
  462. if ( 'auto' !== $attributes['height'] ) {
  463. if (
  464. isset( $content_width )
  465. && is_numeric( $attributes['width'] )
  466. && $attributes['width'] > $content_width
  467. ) {
  468. $attributes['width'] = $content_width;
  469. }
  470. if ( ! $attributes['width'] ) {
  471. $attributes['width'] = '100%';
  472. } else {
  473. $attributes['width'] = (int) $attributes['width'];
  474. }
  475. if ( ! $attributes['height'] ) {
  476. $attributes['height'] = '600';
  477. } else {
  478. $attributes['height'] = (int) $attributes['height'];
  479. }
  480. return sprintf(
  481. '<iframe src="%1$s?iframe=1" frameborder="0" width="%2$d" height="%3$d" scrolling="auto" allowtransparency="true" marginheight="0" marginwidth="0">%4$s</iframe>',
  482. esc_url( $survey_url ),
  483. absint( $attributes['width'] ),
  484. absint( $attributes['height'] ),
  485. $survey_link
  486. );
  487. } elseif (
  488. ! empty( $attributes['domain'] )
  489. && ! empty( $attributes['id'] )
  490. ) {
  491. $domain = preg_replace( '/[^a-z0-9\-]/i', '', $attributes['domain'] );
  492. $id = preg_replace( '/[\/\?&\{\}]/', '', $attributes['id'] );
  493. $auto_src = esc_url( "https://{$domain}.survey.fm/{$id}" );
  494. $auto_src = wp_parse_url( $auto_src );
  495. if ( ! is_array( $auto_src ) || 0 === count( $auto_src ) ) {
  496. return '<!-- no crowdsignal output -->';
  497. }
  498. if ( ! isset( $auto_src['host'] ) || ! isset( $auto_src['path'] ) ) {
  499. return '<!-- no crowdsignal output -->';
  500. }
  501. $domain = $auto_src['host'] . '/';
  502. $id = ltrim( $auto_src['path'], '/' );
  503. $settings = array(
  504. 'type' => $attributes['type'],
  505. 'auto' => true,
  506. 'domain' => $domain,
  507. 'id' => $id,
  508. 'site' => $attributes['site'],
  509. );
  510. }
  511. } else {
  512. $text_color = preg_replace( '/[^a-f0-9]/i', '', $attributes['text_color'] );
  513. $back_color = preg_replace( '/[^a-f0-9]/i', '', $attributes['back_color'] );
  514. if (
  515. ! in_array(
  516. $attributes['align'],
  517. array(
  518. 'right',
  519. 'left',
  520. 'top-left',
  521. 'top-right',
  522. 'middle-left',
  523. 'middle-right',
  524. 'bottom-left',
  525. 'bottom-right',
  526. ),
  527. true
  528. )
  529. ) {
  530. $attributes['align'] = '';
  531. }
  532. if (
  533. ! in_array(
  534. $attributes['style'],
  535. array(
  536. 'inline',
  537. 'side',
  538. 'corner',
  539. 'rounded',
  540. 'square',
  541. ),
  542. true
  543. )
  544. ) {
  545. $attributes['style'] = '';
  546. }
  547. $settings = array_filter(
  548. array(
  549. 'title' => wp_strip_all_tags( $attributes['title'] ),
  550. 'type' => $attributes['type'],
  551. 'body' => wp_strip_all_tags( $attributes['body'] ),
  552. 'button' => wp_strip_all_tags( $attributes['button'] ),
  553. 'text_color' => $text_color,
  554. 'back_color' => $back_color,
  555. 'align' => $attributes['align'],
  556. 'style' => $attributes['style'],
  557. 'id' => $survey,
  558. 'site' => $attributes['site'],
  559. )
  560. );
  561. }
  562. if ( empty( $settings ) ) {
  563. return '<!-- no crowdsignal output -->';
  564. }
  565. return $this->get_async_code( $settings, $survey_link, $survey_url );
  566. }
  567. } else {
  568. return '<!-- no crowdsignal output -->';
  569. }
  570. }
  571. /**
  572. * Enqueue JavaScript containing all ratings / polls on the page.
  573. * Hooked into wp_footer
  574. */
  575. public function generate_scripts() {
  576. if ( is_array( self::$scripts ) ) {
  577. if ( isset( self::$scripts['rating'] ) ) {
  578. $script = "<!--//--><![CDATA[//><!--\n";
  579. foreach ( self::$scripts['rating'] as $rating ) {
  580. $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']} );}}";
  581. }
  582. $script .= "\n//--><!]]>";
  583. wp_enqueue_script( 'crowdsignal-rating' );
  584. wp_add_inline_script(
  585. 'crowdsignal-rating',
  586. $script,
  587. 'before'
  588. );
  589. }
  590. if ( isset( self::$scripts['poll'] ) ) {
  591. foreach ( self::$scripts['poll'] as $poll_id => $poll ) {
  592. wp_enqueue_script(
  593. 'crowdsignal-' . absint( $poll_id ),
  594. esc_url( $poll['url'] ),
  595. array(),
  596. JETPACK__VERSION,
  597. true
  598. );
  599. }
  600. }
  601. }
  602. self::$scripts = false;
  603. }
  604. /**
  605. * If the theme uses infinite scroll, include jquery at the start
  606. */
  607. public function check_infinite() {
  608. if (
  609. current_theme_supports( 'infinite-scroll' )
  610. && class_exists( 'The_Neverending_Home_Page' )
  611. && The_Neverending_Home_Page::archive_supports_infinity()
  612. ) {
  613. wp_enqueue_script( 'jquery' );
  614. }
  615. }
  616. /**
  617. * Dynamically load the .js, if needed
  618. *
  619. * This hooks in late (priority 11) to infinite_scroll_render to determine
  620. * a posteriori if a shortcode has been called.
  621. */
  622. public function crowdsignal_shortcode_infinite() {
  623. // only try to load if a shortcode has been called and theme supports infinite scroll.
  624. if ( self::$add_script ) {
  625. wp_enqueue_script( 'crowdsignal-shortcode' );
  626. wp_localize_script(
  627. 'crowdsignal-shortcode',
  628. 'crowdsignal_shortcode_options',
  629. array(
  630. 'script_url' => esc_url_raw(
  631. Assets::get_file_url_for_environment(
  632. '_inc/build/polldaddy-shortcode.min.js',
  633. '_inc/polldaddy-shortcode.js'
  634. )
  635. ),
  636. )
  637. );
  638. }
  639. }
  640. }
  641. // Kick it all off.
  642. new CrowdsignalShortcode();
  643. if ( ! function_exists( 'crowdsignal_link' ) ) {
  644. /**
  645. * Replace link with shortcode.
  646. * Examples: https://poll.fm/10499328 | https://7iger.survey.fm/test-embed
  647. *
  648. * @param string $content Post content.
  649. */
  650. function crowdsignal_link( $content ) {
  651. if (
  652. class_exists( 'Jetpack_AMP_Support' )
  653. && Jetpack_AMP_Support::is_amp_request()
  654. ) {
  655. return $content;
  656. }
  657. // Replace poll links.
  658. $content = jetpack_preg_replace_outside_tags(
  659. '!(?:\n|\A)https?://(polldaddy\.com/poll|poll\.fm)/([0-9]+?)(/.*)?(?:\n|\Z)!i',
  660. '[crowdsignal poll=$2]',
  661. $content
  662. );
  663. // Replace survey.fm links.
  664. $content = preg_replace(
  665. '!(?:\n|\A)https?://(.*).survey.fm/(.*)(/.*)?(?:\n|\Z)!i',
  666. '[crowdsignal type="iframe" survey="true" height="auto" domain="$1" id="$2"]',
  667. $content
  668. );
  669. return $content;
  670. }
  671. // higher priority because we need it before auto-link and autop get to it.
  672. add_filter( 'the_content', 'crowdsignal_link', 1 );
  673. add_filter( 'the_content_rss', 'crowdsignal_link', 1 );
  674. }
  675. }