Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
wufoo_shortcode
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 1
42
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
12if ( ! defined( 'ABSPATH' ) ) {
13    exit( 0 );
14}
15
16/**
17 * Display the Wufoo shortcode.
18 *
19 * @param array $atts Shortcode attributes.
20 */
21function wufoo_shortcode( $atts ) {
22    $attr = shortcode_atts(
23        array(
24            'username'   => '',
25            'formhash'   => '',
26            'autoresize' => true,
27            'height'     => '500',
28            'header'     => 'show',
29        ),
30        $atts
31    );
32
33    // Check username and formhash to ensure they only have alphanumeric characters or underscores, and aren't empty.
34    if (
35        ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['username'] )
36        || ! preg_match( '/^[a-zA-Z0-9_]+$/', $attr['formhash'] )
37    ) {
38        /*
39         * Return an error to the users with instructions if one of these params is invalid
40         * They don't have default values because they are user/form-specific
41         */
42        if ( current_user_can( 'edit_posts' ) ) {
43            return sprintf(
44                wp_kses(
45                    /* translators: URL to Wufoo support page. */
46                    __( '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' ),
47                    array(
48                        'a' => array(
49                            'href'   => array(),
50                            'target' => array(),
51                            'rel'    => array(),
52                        ),
53                    )
54                ),
55                'https://help.wufoo.com/articles/en_US/kb/Embed'
56            );
57        }
58
59        return;
60    }
61
62    /**
63     * Placeholder which will tell Wufoo where to render the form.
64     */
65    $js_embed_placeholder = sprintf(
66        '<div id="wufoo-%s"></div>',
67        esc_attr( $attr['formhash'] )
68    );
69
70    /**
71     * Required parameters are present.
72     * An error will be returned inside the form if they are invalid.
73     */
74    $js_embed = sprintf(
75        '(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){}})();',
76        esc_attr( $attr['formhash'] ),
77        esc_attr( $attr['username'] ),
78        'true' == $attr['autoresize'] ? 'true' : 'false', // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
79        absint( $attr['height'] ),
80        'show' === $attr['header'] ? 'show' : 'hide'
81    );
82
83    // Embed URL.
84    $embed_url = sprintf(
85        'https://%1$s.wufoo.com/embed/%2$s/',
86        $attr['username'],
87        $attr['formhash']
88    );
89
90    // Form URL.
91    $form_url = sprintf(
92        'https://%1$s.wufoo.com/forms/%2$s/',
93        $attr['username'],
94        $attr['formhash']
95    );
96
97    /*
98     * iframe embed, loaded inside <noscript> tags.
99     */
100    $iframe_embed = sprintf(
101        '<iframe height="%1$d" src="%2$s" allowTransparency="true" frameborder="0" scrolling="no" style="width:100%%;border:none;">
102            <a href="%3$s" target="_blank" rel="noopener noreferrer">%4$s</a>
103        </iframe>',
104        absint( $attr['height'] ),
105        esc_url( $embed_url ),
106        esc_url( $form_url ),
107        esc_html__( 'Fill out my Wufoo form!', 'jetpack' )
108    );
109
110    wp_enqueue_script(
111        'wufoo-form',
112        'https://www.wufoo.com/scripts/embed/form.js',
113        array(),
114        JETPACK__VERSION,
115        true
116    );
117    wp_add_inline_script( 'wufoo-form', $js_embed );
118
119    /** This action is already documented in modules/widgets/gravatar-profile.php */
120    do_action( 'jetpack_stats_extra', 'embeds', 'wufoo' );
121
122    /**
123     * Return embed in JS and iframe.
124     */
125    return "$js_embed_placeholder<noscript>$iframe_embed</noscript>";
126}
127add_shortcode( 'wufoo', 'wufoo_shortcode' );