Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact Form: Add post-submission options. #13745

Merged
merged 21 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
eb2f624
Add custom thank you and redirect options
pento Oct 14, 2019
9597811
Add beta testing info.
pento Oct 14, 2019
d2a1c13
De-duplicate the thank you message generation.
pento Oct 14, 2019
792dbe4
Use a URLInput for the redirect URL, so it can search posts/pages on …
pento Oct 14, 2019
131fe50
Remove the toggle help, and improve the label instead.
pento Oct 14, 2019
853e07a
Ensure the `customThankyouType` attribute is always set.
pento Oct 15, 2019
aa01799
Remove the toggle to display the submission options.
pento Oct 15, 2019
d8fa34e
Fix an undefined variable PHP notice when submitting the contact form.
pento Oct 15, 2019
f70f745
Title caps - to match labels in color section
Oct 15, 2019
290863f
Experiment in removing BaseControl
pento Oct 16, 2019
1ace167
[not verified] Revert "Experiment in removing BaseControl"
pento Oct 16, 2019
0d19037
Improve the URL suggestion list width.
pento Oct 16, 2019
ad2639e
Add comments for the new parameters.
pento Oct 16, 2019
cfa59b8
Add a PHPCS comment to ignore the usage of wp_redirect() instead of w…
pento Oct 16, 2019
9ade3a6
Use Yoda conditions, you must.
pento Oct 16, 2019
6c6c83e
Don't try to migrate the block settings if the submit_button_text att…
pento Oct 16, 2019
e97ae24
Nouns and verbs are tricky, okay?!
pento Oct 16, 2019
e1833d9
esc_url() the URL that needs esc-ing.
pento Oct 16, 2019
3d52262
Note the use of camel case instead of snake case for the parameter na…
pento Oct 16, 2019
22e7bfb
Don't add the URL parameters to custom redirect URLs.
pento Oct 16, 2019
f694283
Add a comment indicating the URLInput hackery can be undone at a late…
pento Oct 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions extensions/blocks/contact-form/components/jetpack-contact-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import classnames from 'classnames';
import emailValidator from 'email-validator';
import { __, sprintf } from '@wordpress/i18n';
import { Button, PanelBody, Path, Placeholder, TextControl } from '@wordpress/components';
import {
BaseControl,
Button,
PanelBody,
Path,
Placeholder,
SelectControl,
TextareaControl,
TextControl,
} from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';
import { compose, withInstanceId } from '@wordpress/compose';
import { InnerBlocks, InspectorControls } from '@wordpress/editor';
import { InnerBlocks, InspectorControls, URLInput } from '@wordpress/editor';
jeherve marked this conversation as resolved.
Show resolved Hide resolved

/**
* Internal dependencies
Expand Down Expand Up @@ -190,6 +199,48 @@ class JetpackContactForm extends Component {
);
}

renderConfirmationMessageFields() {
const { instanceId } = this.props;
const { customThankyou, customThankyouMessage, customThankyouRedirect } = this.props.attributes;
return (
<Fragment>
<SelectControl
label={ __( 'On Submission', 'jetpack' ) }
pento marked this conversation as resolved.
Show resolved Hide resolved
value={ customThankyou }
options={ [
{ label: __( 'Show a summary of submitted fields', 'jetpack' ), value: '' },
{ label: __( 'Show a custom text message', 'jetpack' ), value: 'message' },
{ label: __( 'Redirect to another webpage', 'jetpack' ), value: 'redirect' },
] }
onChange={ value => this.props.setAttributes( { customThankyou: value } ) }
/>
{ 'message' === customThankyou && (
<TextareaControl
label={ __( 'Message Text', 'jetpack' ) }
value={ customThankyouMessage }
placeholder={ __( 'Thank you for your submission!', 'jetpack' ) }
onChange={ value => this.props.setAttributes( { customThankyouMessage: value } ) }
/>
) }
{ 'redirect' === customThankyou && (
// @todo This can likely be simplified when WP 5.4 is the minimum supported version.
// See https://github.com/Automattic/jetpack/pull/13745#discussion_r334712381
<BaseControl
pento marked this conversation as resolved.
Show resolved Hide resolved
label={ __( 'Redirect Address', 'jetpack' ) }
id={ `contact-form-${ instanceId }-thankyou-url` }
>
<URLInput
id={ `contact-form-${ instanceId }-thankyou-url` }
value={ customThankyouRedirect }
className="jetpack-contact-form__thankyou-redirect-url"
onChange={ value => this.props.setAttributes( { customThankyouRedirect: value } ) }
/>
</BaseControl>
) }
</Fragment>
);
}

hasEmailError() {
const fieldEmailError = this.state.toError;
return fieldEmailError && fieldEmailError.length > 0;
Expand All @@ -205,9 +256,12 @@ class JetpackContactForm extends Component {
return (
<Fragment>
<InspectorControls>
<PanelBody title={ __( 'Email feedback settings', 'jetpack' ) }>
<PanelBody title={ __( 'Email Feedback Settings', 'jetpack' ) }>
{ this.renderToAndSubjectFields() }
</PanelBody>
<PanelBody title={ __( 'Confirmation Message', 'jetpack' ) }>
{ this.renderConfirmationMessageFields() }
</PanelBody>
</InspectorControls>
<div className={ formClassnames }>
{ ! hasFormSettingsSet && (
Expand Down
8 changes: 8 additions & 0 deletions extensions/blocks/contact-form/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
width: 100%;
}

.jetpack-contact-form__thankyou-redirect-url input[type="text"] {
width: 100%;
}

.jetpack-contact-form__thankyou-redirect-url__suggestions {
width: 260px;
pento marked this conversation as resolved.
Show resolved Hide resolved
}

.jetpack-field-label {
display: flex;
flex-direction: row;
Expand Down
17 changes: 16 additions & 1 deletion extensions/blocks/contact-form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ export const settings = {
type: 'string',
default: null,
},
customThankyou: {
pento marked this conversation as resolved.
Show resolved Hide resolved
type: 'string',
default: '',
},
customThankyouMessage: {
type: 'string',
default: '',
},
customThankyouRedirect: {
type: 'string',
default: '',
},

// Deprecated
has_form_settings_set: {
Expand Down Expand Up @@ -135,7 +147,10 @@ export const settings = {

isEligible: attr => {
// when the deprecated, snake_case values are default, no need to migrate
if ( ! attr.has_form_settings_set && attr.submit_button_text === 'Submit' ) {
if (
! attr.has_form_settings_set &&
( ! attr.submit_button_text || attr.submit_button_text === 'Submit' )
) {
return false;
}
return true;
Expand Down
72 changes: 49 additions & 23 deletions modules/contact-form/grunion-contact-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1838,12 +1838,16 @@ function __construct( $attributes, $content = null ) {
self::$current_form = $this;

$this->defaults = array(
'to' => $default_to,
'subject' => $default_subject,
'show_subject' => 'no', // only used in back-compat mode
'widget' => 0, // Not exposed to the user. Works with Grunion_Contact_Form_Plugin::widget_atts()
'id' => null, // Not exposed to the user. Set above.
'submit_button_text' => __( 'Submit', 'jetpack' ),
'to' => $default_to,
'subject' => $default_subject,
'show_subject' => 'no', // only used in back-compat mode
'widget' => 0, // Not exposed to the user. Works with Grunion_Contact_Form_Plugin::widget_atts()
'id' => null, // Not exposed to the user. Set above.
'submit_button_text' => __( 'Submit', 'jetpack' ),
// These attributes come from the block editor, so use camel case instead of snake case.
'customThankyou' => '', // Whether to show a custom thankyou response after submitting a form. '' for no, 'message' for a custom message, 'redirect' to redirect to a new URL.
'customThankyouMessage' => __( 'Thank you for your submission!', 'jetpack' ), // The message to show when customThankyou is set to 'message'.
'customThankyouRedirect' => '', // The URL to redirect to when customThankyou is set to 'redirect'.
pento marked this conversation as resolved.
Show resolved Hide resolved
);

$attributes = shortcode_atts( $this->defaults, $attributes, 'contact-form' );
Expand Down Expand Up @@ -2100,10 +2104,16 @@ static function parse( $attributes, $content ) {
* @return string $message
*/
static function success_message( $feedback_id, $form ) {
return wp_kses(
'<blockquote class="contact-form-submission">'
if ( 'message' === $form->get_attribute( 'customThankyou' ) ) {
$message = wpautop( $form->get_attribute( 'customThankyouMessage' ) );
pento marked this conversation as resolved.
Show resolved Hide resolved
pento marked this conversation as resolved.
Show resolved Hide resolved
} else {
$message = '<blockquote class="contact-form-submission">'
. '<p>' . join( '</p><p>', self::get_compiled_form( $feedback_id, $form ) ) . '</p>'
. '</blockquote>',
. '</blockquote>';
}

return wp_kses(
$message,
array(
'br' => array(),
'blockquote' => array( 'class' => array() ),
Expand Down Expand Up @@ -2816,21 +2826,36 @@ function process_submission() {
return self::success_message( $post_id, $this );
}

$redirect = wp_get_referer();
if ( ! $redirect ) { // wp_get_referer() returns false if the referer is the same as the current page
$redirect = $_SERVER['REQUEST_URI'];
$redirect = '';
$custom_redirect = false;
if ( 'redirect' === $this->get_attribute( 'customThankyou' ) ) {
$custom_redirect = true;
$redirect = esc_url( $this->get_attribute( 'customThankyouRedirect' ) );
}

$redirect = add_query_arg(
urlencode_deep(
array(
'contact-form-id' => $id,
'contact-form-sent' => $post_id,
'contact-form-hash' => $this->hash,
'_wpnonce' => wp_create_nonce( "contact-form-sent-{$post_id}" ), // wp_nonce_url HTMLencodes :(
)
), $redirect
);
if ( ! $redirect ) {
$custom_redirect = false;
$redirect = wp_get_referer();
}

if ( ! $redirect ) { // wp_get_referer() returns false if the referer is the same as the current page.
$custom_redirect = false;
$redirect = $_SERVER['REQUEST_URI'];
}

if ( ! $custom_redirect ) {
$redirect = add_query_arg(
urlencode_deep(
array(
'contact-form-id' => $id,
'contact-form-sent' => $post_id,
'contact-form-hash' => $this->hash,
'_wpnonce' => wp_create_nonce( "contact-form-sent-{$post_id}" ), // wp_nonce_url HTMLencodes :( .
)
),
$redirect
);
}

/**
* Filter the URL where the reader is redirected after submitting a form.
Expand All @@ -2845,7 +2870,8 @@ function process_submission() {
*/
pento marked this conversation as resolved.
Show resolved Hide resolved
$redirect = apply_filters( 'grunion_contact_form_redirect_url', $redirect, $id, $post_id );

wp_safe_redirect( $redirect );
// phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- We intentially allow external redirects here.
wp_redirect( $redirect );
pento marked this conversation as resolved.
Show resolved Hide resolved
pento marked this conversation as resolved.
Show resolved Hide resolved
exit;
}

Expand Down
4 changes: 4 additions & 0 deletions to-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ We've made some changes to simplify the Jetpack Dashboard interface when your us

We've made some changes to ensure that blocks are properly translated in the block editor. If you switch to a language that offers language packs, like French or Spanish, you should see that Jetpack Blocks will now be translated in the editor.

## Contact Form Block

The Contact Form Block now includes options for showing a custom post-submission message, or to redirect to a different URL.

### Carousel

In this release, we've made some changes to how the Carousel metadata was added to each gallery. To test this:
Expand Down