| 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 |
|
| 7 |
Based on https://wordpress.org/extend/plugins/wufoo-shortcode/ |
| 8 |
https://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 |
), |
| 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 %1$sWufoo Code Manager%2$s, you should be golden.', 'jetpack' ), '<a href="https://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 |
* Placeholder which will tell Wufoo where to render the form. |
| 42 |
*/ |
| 43 |
$js_embed_placeholder = '<div id="wufoo-' . $attr['formhash'] . '"></div>'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Required parameters are present. |
| 47 |
* An error will be returned inside the form if they are invalid. |
| 48 |
*/ |
| 49 |
$js_embed = '(function(){try{var wufoo_' . $attr['formhash'] . ' = new WufooForm();'; |
| 50 |
$js_embed .= 'wufoo_' . $attr['formhash'] . '.initialize({'; |
| 51 |
$js_embed .= "'userName':'" . $attr['username'] . "', "; |
| 52 |
$js_embed .= "'formHash':'" . $attr['formhash'] . "', "; |
| 53 |
$js_embed .= "'autoResize':" . (bool) ( $attr['autoresize'] ) . ','; |
| 54 |
$js_embed .= "'height':'" . (int) $attr['height'] . "',"; |
| 55 |
$js_embed .= "'header':'" . esc_js( $attr['header'] ) . "',"; |
| 56 |
$js_embed .= "'ssl':true,'async':true});"; |
| 57 |
$js_embed .= 'wufoo_' . $attr['formhash'] . '.display();'; |
| 58 |
$js_embed .= '}catch(e){}})();'; |
| 59 |
|
| 60 |
/** |
| 61 |
* iframe embed, loaded inside <noscript> tags. |
| 62 |
*/ |
| 63 |
$iframe_embed = '<iframe '; |
| 64 |
$iframe_embed .= 'height="' . (int) $attr['height'] . '" '; |
| 65 |
$iframe_embed .= 'allowTransparency="true" frameborder="0" scrolling="no" style="width:100%;border:none;"'; |
| 66 |
$iframe_embed .= 'src="https://' . $attr['username'] . '.wufoo.com/embed/' . $attr['formhash'] . '/">'; |
| 67 |
$iframe_embed .= '<a href="https://' . $attr['username'] . '.wufoo.com/forms/' . $attr['formhash'] . '/" '; |
| 68 |
$iframe_embed .= 'rel="nofollow" target="_blank">' . __( 'Fill out my Wufoo form!', 'jetpack' ) . '</a></iframe>'; |
| 69 |
|
| 70 |
wp_enqueue_script( |
| 71 |
'wufoo-form', |
| 72 |
'https://www.wufoo.com/scripts/embed/form.js', |
| 73 |
array(), |
| 74 |
false, |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
wp_add_inline_script( 'wufoo-form', $js_embed ); |
| 79 |
|
| 80 |
/** This action is already documented in modules/widgets/gravatar-profile.php */ |
| 81 |
do_action( 'jetpack_stats_extra', 'embeds', 'wufoo' ); |
| 82 |
|
| 83 |
/** |
| 84 |
* Return embed in JS and iframe. |
| 85 |
*/ |
| 86 |
return "$js_embed_placeholder<noscript>$iframe_embed</noscript>"; |
| 87 |
} |
| 88 |
|
| 89 |
add_shortcode( 'wufoo', 'wufoo_shortcode' ); |
| 90 |
|