wufoo.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. Plugin Name: Wufoo Shortcode Plugin
  4. Description: Enables shortcode to embed Wufoo forms. Usage: [wufoo username="chriscoyier" formhash="x7w3w3" autoresize="true" height="458" header="show"]
  5. Author: Chris Coyier / Wufoo, evansolomon
  6. Based on https://wordpress.org/extend/plugins/wufoo-shortcode/
  7. https://wufoo.com/docs/code-manager/wordpress-shortcode-plugin/
  8. */
  9. function wufoo_shortcode( $atts ) {
  10. $attr = shortcode_atts(
  11. array(
  12. 'username' => '',
  13. 'formhash' => '',
  14. 'autoresize' => true,
  15. 'height' => '500',
  16. 'header' => 'show',
  17. ), $atts
  18. );
  19. // Check username and formhash to ensure they only have alphanumeric characters or underscores, and aren't empty.
  20. if ( ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['username'] ) || ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['formhash'] ) ) {
  21. /**
  22. * Return an error to the users with instructions if one of these params is invalid
  23. * They don't have default values because they are user/form-specific
  24. */
  25. $return_error = sprintf( __( 'Something is wrong with your Wufoo shortcode. If you copy and paste it from the %sWufoo Code Manager%s, you should be golden.', 'jetpack' ), '<a href="https://wufoo.com/docs/code-manager/" target="_blank">', '</a>' );
  26. return '
  27. <div style="border: 20px solid red; border-radius: 40px; padding: 40px; margin: 50px 0 70px;">
  28. <h3>Uh oh!</h3>
  29. <p style="margin: 0;">' . $return_error . '</p>
  30. </div>';
  31. }
  32. /**
  33. * Placeholder which will tell Wufoo where to render the form.
  34. */
  35. $js_embed_placeholder = '<div id="wufoo-' . $attr['formhash'] . '"></div>';
  36. /**
  37. * Required parameters are present.
  38. * An error will be returned inside the form if they are invalid.
  39. */
  40. $js_embed = '(function(){try{var wufoo_' . $attr['formhash'] . ' = new WufooForm();';
  41. $js_embed .= 'wufoo_' . $attr['formhash'] . '.initialize({';
  42. $js_embed .= "'userName':'" . $attr['username'] . "', ";
  43. $js_embed .= "'formHash':'" . $attr['formhash'] . "', ";
  44. $js_embed .= "'autoResize':" . (bool) ( $attr['autoresize'] ) . ',';
  45. $js_embed .= "'height':'" . (int) $attr['height'] . "',";
  46. $js_embed .= "'header':'" . esc_js( $attr['header'] ) . "',";
  47. $js_embed .= "'ssl':true,'async':true});";
  48. $js_embed .= 'wufoo_' . $attr['formhash'] . '.display();';
  49. $js_embed .= '}catch(e){}})();';
  50. /**
  51. * iframe embed, loaded inside <noscript> tags.
  52. */
  53. $iframe_embed = '<iframe ';
  54. $iframe_embed .= 'height="' . (int) $attr['height'] . '" ';
  55. $iframe_embed .= 'allowTransparency="true" frameborder="0" scrolling="no" style="width:100%;border:none;"';
  56. $iframe_embed .= 'src="https://' . $attr['username'] . '.wufoo.com/embed/' . $attr['formhash'] . '/">';
  57. $iframe_embed .= '<a href="https://' . $attr['username'] . '.wufoo.com/forms/' . $attr['formhash'] . '/" ';
  58. $iframe_embed .= 'rel="nofollow" target="_blank">' . __( 'Fill out my Wufoo form!', 'jetpack' ) . '</a></iframe>';
  59. wp_enqueue_script(
  60. 'wufoo-form',
  61. 'https://www.wufoo.com/scripts/embed/form.js',
  62. array(),
  63. false,
  64. true
  65. );
  66. wp_add_inline_script( 'wufoo-form', $js_embed );
  67. /** This action is already documented in modules/widgets/gravatar-profile.php */
  68. do_action( 'jetpack_stats_extra', 'embeds', 'wufoo' );
  69. /**
  70. * Return embed in JS and iframe.
  71. */
  72. return "$js_embed_placeholder<noscript>$iframe_embed</noscript>";
  73. }
  74. add_shortcode( 'wufoo', 'wufoo_shortcode' );