wufoo.php
| 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" ssl="true"] |
| 5 | Author: Chris Coyier / Wufoo, evansolomon |
| 6 | |
| 7 | Based on https://wordpress.org/extend/plugins/wufoo-shortcode/ |
| 8 | http://wufoo.com/docs/code-manager/wordpress-shortcode-plugin/ |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | function wufoo_shortcode( $atts ) { |
| 13 | $attr = shortcode_atts( |
| 14 | array( |
| 15 | 'username' => '', |
| 16 | 'formhash' => '', |
| 17 | 'autoresize' => true, |
| 18 | 'height' => '500', |
| 19 | 'header' => 'show', |
| 20 | 'ssl' => '', |
| 21 | ), $atts |
| 22 | ); |
| 23 | |
| 24 | // Check username and formhash to ensure they only have alphanumeric characters or underscores, and aren't empty. |
| 25 | if ( ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['username'] ) || ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['formhash'] ) ) { |
| 26 | |
| 27 | /** |
| 28 | * Return an error to the users with instructions if one of these params is invalid |
| 29 | * They don't have default values because they are user/form-specific |
| 30 | */ |
| 31 | $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="http://wufoo.com/docs/code-manager/" target="_blank">', '</a>' ); |
| 32 | |
| 33 | return ' |
| 34 | <div style="border: 20px solid red; border-radius: 40px; padding: 40px; margin: 50px 0 70px;"> |
| 35 | <h3>Uh oh!</h3> |
| 36 | <p style="margin: 0;">' . $return_error . '</p> |
| 37 | </div>'; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Required parameters are present. |
| 42 | * An error will be returned inside the form if they are invalid. |
| 43 | */ |
| 44 | $js_embed = '<script type="text/javascript">var host = (("https:" == document.location.protocol) ? "https://secure." : "http://");document.write(unescape("%3Cscript src=\'" + host + "wufoo.com/scripts/embed/form.js\' type=\'text/javascript\'%3E%3C/script%3E"));</script>'; |
| 45 | $js_embed .= "<script type='text/javascript'>"; |
| 46 | $js_embed .= 'var wufoo_' . $attr['formhash'] . ' = new WufooForm();'; |
| 47 | $js_embed .= 'wufoo_' . $attr['formhash'] . ' .initialize({'; |
| 48 | $js_embed .= "'userName':'" . $attr['username'] . "', "; |
| 49 | $js_embed .= "'formHash':'" . $attr['formhash'] . "', "; |
| 50 | $js_embed .= "'autoResize':" . (bool) ( $attr['autoresize'] ) . ','; |
| 51 | $js_embed .= "'height':'" . (int) $attr['height'] . "',"; |
| 52 | $js_embed .= "'header':'" . esc_js( $attr['header'] ) . "' "; |
| 53 | |
| 54 | /** |
| 55 | * Only output SSL value if passes as param. |
| 56 | * Lower tier plans don't show this param (don't offer SSL). |
| 57 | */ |
| 58 | $js_embed .= ( $attr['ssl'] ) ? ",'ssl':" . (bool) $attr['ssl'] : ''; |
| 59 | $js_embed .= '});'; |
| 60 | $js_embed .= 'wufoo_' . $attr['formhash'] . '.display();'; |
| 61 | $js_embed .= '</script>'; |
| 62 | |
| 63 | /** |
| 64 | * iframe embed, loaded inside <noscript> tags. |
| 65 | */ |
| 66 | $iframe_embed = '<iframe '; |
| 67 | $iframe_embed .= 'height="' . (int) $attr['height'] . '" '; |
| 68 | $iframe_embed .= 'allowTransparency="true" frameborder="0" scrolling="no" style="width:100%;border:none;"'; |
| 69 | $iframe_embed .= 'src="https://' . $attr['username'] . '.wufoo.com/embed/' . $attr['formhash'] . '/">'; |
| 70 | $iframe_embed .= '<a href="https://' . $attr['username'] . '.wufoo.com/forms/' . $attr['formhash'] . '/" '; |
| 71 | $iframe_embed .= 'rel="nofollow" target="_blank">' . __( 'Fill out my Wufoo form!', 'jetpack' ) . '</a></iframe>'; |
| 72 | |
| 73 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
| 74 | do_action( 'jetpack_stats_extra', 'embeds', 'wufoo' ); |
| 75 | |
| 76 | /** |
| 77 | * Return embed in JS and iframe. |
| 78 | */ |
| 79 | return "$js_embed <noscript> $iframe_embed </noscript>"; |
| 80 | } |
| 81 | |
| 82 | add_shortcode( 'wufoo', 'wufoo_shortcode' ); |
| 83 |