Nenhuma Descrição

wufoo.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Plugin Name: Wufoo Shortcode
  4. * Based on https://wordpress.org/plugins/wufoo-shortcode/
  5. *
  6. * Examples:
  7. * [wufoo username="jeherve" formhash="z1x13ltw1m8jtrw" autoresize="true" height="338" header="show"]
  8. *
  9. * @package automattic/jetpack
  10. */
  11. /**
  12. * Display the Wufoo shortcode.
  13. *
  14. * @param array $atts Shortcode attributes.
  15. */
  16. function wufoo_shortcode( $atts ) {
  17. $attr = shortcode_atts(
  18. array(
  19. 'username' => '',
  20. 'formhash' => '',
  21. 'autoresize' => true,
  22. 'height' => '500',
  23. 'header' => 'show',
  24. ),
  25. $atts
  26. );
  27. // Check username and formhash to ensure they only have alphanumeric characters or underscores, and aren't empty.
  28. if (
  29. ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['username'] )
  30. || ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['formhash'] )
  31. ) {
  32. /*
  33. * Return an error to the users with instructions if one of these params is invalid
  34. * They don't have default values because they are user/form-specific
  35. */
  36. if ( current_user_can( 'edit_posts' ) ) {
  37. return sprintf(
  38. wp_kses(
  39. /* translators: URL to Wufoo support page. */
  40. __( 'Something is wrong with your Wufoo shortcode. Try following the instructions <a href="%s" target="_blank" rel="noopener noreferrer">here</a> to embed a form on your site.', 'jetpack' ),
  41. array(
  42. 'a' => array(
  43. 'href' => array(),
  44. 'target' => array(),
  45. 'rel' => array(),
  46. ),
  47. )
  48. ),
  49. 'https://help.wufoo.com/articles/en_US/kb/Embed'
  50. );
  51. }
  52. return;
  53. }
  54. /**
  55. * Placeholder which will tell Wufoo where to render the form.
  56. */
  57. $js_embed_placeholder = sprintf(
  58. '<div id="wufoo-%s"></div>',
  59. esc_attr( $attr['formhash'] )
  60. );
  61. /**
  62. * Required parameters are present.
  63. * An error will be returned inside the form if they are invalid.
  64. */
  65. $js_embed = sprintf(
  66. '(function(){try{var wufoo_%1$s = new WufooForm();wufoo_%1$s.initialize({"userName":"%2$s","formHash":"%1$s","autoResize":%3$s,"height":"%4$d","header":"%5$s","ssl":true,"async":true});wufoo_%1$s.display();}catch(e){}})();',
  67. esc_attr( $attr['formhash'] ),
  68. esc_attr( $attr['username'] ),
  69. 'true' == $attr['autoresize'] ? 'true' : 'false', // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
  70. absint( $attr['height'] ),
  71. 'show' === $attr['header'] ? 'show' : 'hide'
  72. );
  73. // Embed URL.
  74. $embed_url = sprintf(
  75. 'https://%1$s.wufoo.com/embed/%2$s/',
  76. $attr['username'],
  77. $attr['formhash']
  78. );
  79. // Form URL.
  80. $form_url = sprintf(
  81. 'https://%1$s.wufoo.com/forms/%2$s/',
  82. $attr['username'],
  83. $attr['formhash']
  84. );
  85. /*
  86. * iframe embed, loaded inside <noscript> tags.
  87. */
  88. $iframe_embed = sprintf(
  89. '<iframe height="%1$d" src="%2$s" allowTransparency="true" frameborder="0" scrolling="no" style="width:100%;border:none;">
  90. <a href="%3$s" target="_blank" rel="noopener noreferrer">%4$s</a>
  91. </iframe>',
  92. absint( $attr['height'] ),
  93. esc_url( $embed_url ),
  94. esc_url( $form_url ),
  95. esc_html__( 'Fill out my Wufoo form!', 'jetpack' )
  96. );
  97. wp_enqueue_script(
  98. 'wufoo-form',
  99. 'https://www.wufoo.com/scripts/embed/form.js',
  100. array(),
  101. JETPACK__VERSION,
  102. true
  103. );
  104. wp_add_inline_script( 'wufoo-form', $js_embed );
  105. /** This action is already documented in modules/widgets/gravatar-profile.php */
  106. do_action( 'jetpack_stats_extra', 'embeds', 'wufoo' );
  107. /**
  108. * Return embed in JS and iframe.
  109. */
  110. return "$js_embed_placeholder<noscript>$iframe_embed</noscript>";
  111. }
  112. add_shortcode( 'wufoo', 'wufoo_shortcode' );