From eb2f624e1a00a71faaf7b722ef5c78d9ad737304 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 14 Oct 2019 14:38:40 +1100 Subject: [PATCH 01/21] Add custom thank you and redirect options --- .../components/jetpack-contact-form.js | 60 ++++++++++++++++++- extensions/blocks/contact-form/index.js | 16 +++++ modules/contact-form/grunion-contact-form.php | 37 +++++++++--- 3 files changed, 103 insertions(+), 10 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index 952a3934deb..2d61336c1d8 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -4,7 +4,16 @@ import classnames from 'classnames'; import emailValidator from 'email-validator'; import { __, sprintf } from '@wordpress/i18n'; -import { Button, PanelBody, Path, Placeholder, TextControl } from '@wordpress/components'; +import { + Button, + PanelBody, + Path, + Placeholder, + SelectControl, + TextareaControl, + TextControl, + ToggleControl, +} from '@wordpress/components'; import { Component, Fragment } from '@wordpress/element'; import { compose, withInstanceId } from '@wordpress/compose'; import { InnerBlocks, InspectorControls } from '@wordpress/editor'; @@ -190,6 +199,52 @@ class JetpackContactForm extends Component { ); } + renderOnSubmissionFields() { + const { + customThankyou, + customThankyouType, + customThankyouMessage, + customThankyouRedirect, + } = this.props.attributes; + return ( + + this.props.setAttributes( { customThankyou: value } ) } + /> + { customThankyou && ( + this.props.setAttributes( { customThankyouType: value } ) } + /> + ) } + { customThankyou && ( ! customThankyouType || 'message' === customThankyouType ) && ( + this.props.setAttributes( { customThankyouMessage: value } ) } + /> + ) } + { customThankyou && 'redirect' === customThankyouType && ( + this.props.setAttributes( { customThankyouRedirect: value } ) } + /> + ) } + + ); + } + hasEmailError() { const fieldEmailError = this.state.toError; return fieldEmailError && fieldEmailError.length > 0; @@ -208,6 +263,9 @@ class JetpackContactForm extends Component { { this.renderToAndSubjectFields() } + + { this.renderOnSubmissionFields() } +
{ ! hasFormSettingsSet && ( diff --git a/extensions/blocks/contact-form/index.js b/extensions/blocks/contact-form/index.js index f48e2a12f58..5e0f9227b34 100644 --- a/extensions/blocks/contact-form/index.js +++ b/extensions/blocks/contact-form/index.js @@ -56,6 +56,22 @@ export const settings = { type: 'string', default: null, }, + customThankyou: { + type: 'boolean', + default: false, + }, + customThankyouType: { + type: 'string', + default: 'message', + }, + customThankyouMessage: { + type: 'string', + default: '', + }, + customThankyouRedirect: { + type: 'string', + default: '', + }, // Deprecated has_form_settings_set: { diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index 8a02942651d..a86d2bbbfec 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -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' ), + 'customThankyou' => 'false', + 'customThankyouType' => 'message', + 'customThankyouMessage' => __( 'Thank you for your submission!', 'jetpack' ), + 'customThankyouRedirect' => '', ); $attributes = shortcode_atts( $this->defaults, $attributes, 'contact-form' ); @@ -1991,6 +1995,10 @@ static function parse( $attributes, $content ) { ' (' . esc_html__( 'go back', 'jetpack' ) . ')' . "\n\n"; + if ( $attributes['customThankyou'] && ( ! $attributes['customThankyouType'] || 'message' === $attributes['customThankyouType'] ) ) { + $r_success_message .= wpautop( $attributes['customThankyouMessage'] ); + } + // Don't show the feedback details unless the nonce matches if ( $feedback_id && wp_verify_nonce( stripslashes( $_GET['_wpnonce'] ), "contact-form-sent-{$feedback_id}" ) ) { $r_success_message .= self::success_message( $feedback_id, $form ); @@ -2813,10 +2821,21 @@ function process_submission() { do_action( 'grunion_after_message_sent', $post_id, $to, $subject, $message, $headers, $all_values, $extra_values ); if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { - return self::success_message( $post_id, $this ); + $custom_thankyou = ''; + if ( $this->get_attribute( 'customThankyou' ) && $this->get_attribute( 'customThankyouType' ) === 'message' ) { + $custom_thankyou = wpautop( $this->get_attribute( 'customThankyouMessage' ) ); + } + return $custom_thankyou . self::success_message( $post_id, $this ); + } + + if ( $this->get_attribute( 'customThankyou' ) && $this->get_attribute( 'customThankyouType' ) === 'redirect' ) { + $redirect = $this->get_attribute( 'customThankyouRedirect' ); + } + + if ( ! $redirect ) { + $redirect = wp_get_referer(); } - $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']; } @@ -2845,7 +2864,7 @@ function process_submission() { */ $redirect = apply_filters( 'grunion_contact_form_redirect_url', $redirect, $id, $post_id ); - wp_safe_redirect( $redirect ); + wp_redirect( $redirect ); exit; } From 9597811d3e0349911b87011515eb9959db85f6f7 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 14 Oct 2019 14:42:15 +1100 Subject: [PATCH 02/21] Add beta testing info. --- to-test.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/to-test.md b/to-test.md index 7fb5db57251..114d386d21b 100644 --- a/to-test.md +++ b/to-test.md @@ -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: From d2a1c13b269e873d782c750ae1d0f8ef1a02e929 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 14 Oct 2019 16:52:08 +1100 Subject: [PATCH 03/21] De-duplicate the thank you message generation. --- modules/contact-form/grunion-contact-form.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index a86d2bbbfec..415f9540294 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -1995,10 +1995,6 @@ static function parse( $attributes, $content ) { ' (' . esc_html__( 'go back', 'jetpack' ) . ')' . "\n\n"; - if ( $attributes['customThankyou'] && ( ! $attributes['customThankyouType'] || 'message' === $attributes['customThankyouType'] ) ) { - $r_success_message .= wpautop( $attributes['customThankyouMessage'] ); - } - // Don't show the feedback details unless the nonce matches if ( $feedback_id && wp_verify_nonce( stripslashes( $_GET['_wpnonce'] ), "contact-form-sent-{$feedback_id}" ) ) { $r_success_message .= self::success_message( $feedback_id, $form ); @@ -2108,8 +2104,14 @@ static function parse( $attributes, $content ) { * @return string $message */ static function success_message( $feedback_id, $form ) { + $thankyou = ''; + if ( $form->get_attribute( 'customThankyou' ) && ( ! $form->get_attribute( 'customThankyouType' ) || 'message' === $form->get_attribute( 'customThankyouType' ) ) ) { + $thankyou = wpautop( $form->get_attribute( 'customThankyouMessage' ) ); + } + return wp_kses( - '
' + $thankyou + . '
' . '

' . join( '

', self::get_compiled_form( $feedback_id, $form ) ) . '

' . '
', array( @@ -2821,11 +2823,7 @@ function process_submission() { do_action( 'grunion_after_message_sent', $post_id, $to, $subject, $message, $headers, $all_values, $extra_values ); if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { - $custom_thankyou = ''; - if ( $this->get_attribute( 'customThankyou' ) && $this->get_attribute( 'customThankyouType' ) === 'message' ) { - $custom_thankyou = wpautop( $this->get_attribute( 'customThankyouMessage' ) ); - } - return $custom_thankyou . self::success_message( $post_id, $this ); + return self::success_message( $post_id, $this ); } if ( $this->get_attribute( 'customThankyou' ) && $this->get_attribute( 'customThankyouType' ) === 'redirect' ) { From 792dbe4d07ba687bcc26f96faee4ec01a3cc1b63 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 15 Oct 2019 09:47:41 +1100 Subject: [PATCH 04/21] Use a URLInput for the redirect URL, so it can search posts/pages on the site. --- .../components/jetpack-contact-form.js | 19 +++++++++++++------ extensions/blocks/contact-form/editor.scss | 4 ++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index 2d61336c1d8..3bf9002a6bd 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -5,6 +5,7 @@ import classnames from 'classnames'; import emailValidator from 'email-validator'; import { __, sprintf } from '@wordpress/i18n'; import { + BaseControl, Button, PanelBody, Path, @@ -16,7 +17,7 @@ import { } 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'; /** * Internal dependencies @@ -200,6 +201,7 @@ class JetpackContactForm extends Component { } renderOnSubmissionFields() { + const { instanceId } = this.props; const { customThankyou, customThankyouType, @@ -234,12 +236,17 @@ class JetpackContactForm extends Component { /> ) } { customThankyou && 'redirect' === customThankyouType && ( - this.props.setAttributes( { customThankyouRedirect: value } ) } - /> + id={ `contact-form-${ instanceId }-thankyou-url` } + > + this.props.setAttributes( { customThankyouRedirect: value } ) } + /> + ) } ); diff --git a/extensions/blocks/contact-form/editor.scss b/extensions/blocks/contact-form/editor.scss index f1f69e8a9c7..327cef142f0 100644 --- a/extensions/blocks/contact-form/editor.scss +++ b/extensions/blocks/contact-form/editor.scss @@ -39,6 +39,10 @@ width: 100%; } +.jetpack-contact-form__thankyou-redirect-url input[type="text"] { + width: 100%; +} + .jetpack-field-label { display: flex; flex-direction: row; From 131fe50352a804f6e4919c72f8d7a96e03ab6503 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 15 Oct 2019 09:49:23 +1100 Subject: [PATCH 05/21] Remove the toggle help, and improve the label instead. --- .../blocks/contact-form/components/jetpack-contact-form.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index 3bf9002a6bd..19ac90c451c 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -211,8 +211,7 @@ class JetpackContactForm extends Component { return ( this.props.setAttributes( { customThankyou: value } ) } /> From 853e07ac6b8c8e72b00b4c3ec501d96615852ffe Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 15 Oct 2019 11:54:30 +1100 Subject: [PATCH 06/21] Ensure the `customThankyouType` attribute is always set. --- .../contact-form/components/jetpack-contact-form.js | 9 +++++++-- extensions/blocks/contact-form/index.js | 2 +- modules/contact-form/grunion-contact-form.php | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index 19ac90c451c..c3fe7e62683 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -213,7 +213,12 @@ class JetpackContactForm extends Component { this.props.setAttributes( { customThankyou: value } ) } + onChange={ value => { + this.props.setAttributes( { + customThankyou: value, + customThankyouType: customThankyouType || 'message', + } ); + } } /> { customThankyou && ( this.props.setAttributes( { customThankyouType: value } ) } /> ) } - { customThankyou && ( ! customThankyouType || 'message' === customThankyouType ) && ( + { customThankyou && 'message' === customThankyouType && ( get_attribute( 'customThankyou' ) && ( ! $form->get_attribute( 'customThankyouType' ) || 'message' === $form->get_attribute( 'customThankyouType' ) ) ) { + if ( $form->get_attribute( 'customThankyou' ) && 'message' === $form->get_attribute( 'customThankyouType' ) ) { $thankyou = wpautop( $form->get_attribute( 'customThankyouMessage' ) ); } From aa017992e0e6691a650d92f9b034a1c07079c521 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 15 Oct 2019 13:41:30 +1100 Subject: [PATCH 07/21] Remove the toggle to display the submission options. --- .../components/jetpack-contact-form.js | 49 ++++++------------- extensions/blocks/contact-form/index.js | 4 -- modules/contact-form/grunion-contact-form.php | 19 ++++--- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index c3fe7e62683..43469027e41 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -13,7 +13,6 @@ import { SelectControl, TextareaControl, TextControl, - ToggleControl, } from '@wordpress/components'; import { Component, Fragment } from '@wordpress/element'; import { compose, withInstanceId } from '@wordpress/compose'; @@ -200,38 +199,22 @@ class JetpackContactForm extends Component { ); } - renderOnSubmissionFields() { + renderConfirmationMessageFields() { const { instanceId } = this.props; - const { - customThankyou, - customThankyouType, - customThankyouMessage, - customThankyouRedirect, - } = this.props.attributes; + const { customThankyou, customThankyouMessage, customThankyouRedirect } = this.props.attributes; return ( - { - this.props.setAttributes( { - customThankyou: value, - customThankyouType: customThankyouType || 'message', - } ); - } } + this.props.setAttributes( { customThankyou: value } ) } /> - { customThankyou && ( - this.props.setAttributes( { customThankyouType: value } ) } - /> - ) } - { customThankyou && 'message' === customThankyouType && ( + { 'message' === customThankyou && ( this.props.setAttributes( { customThankyouMessage: value } ) } /> ) } - { customThankyou && 'redirect' === customThankyouType && ( + { 'redirect' === customThankyou && ( - + { this.renderToAndSubjectFields() } - - { this.renderOnSubmissionFields() } + + { this.renderConfirmationMessageFields() }
diff --git a/extensions/blocks/contact-form/index.js b/extensions/blocks/contact-form/index.js index 290f7a09274..00a9600ee71 100644 --- a/extensions/blocks/contact-form/index.js +++ b/extensions/blocks/contact-form/index.js @@ -57,10 +57,6 @@ export const settings = { default: null, }, customThankyou: { - type: 'boolean', - default: false, - }, - customThankyouType: { type: 'string', default: '', }, diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index e66c8970134..1c9ee277f7c 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -1844,8 +1844,7 @@ function __construct( $attributes, $content = null ) { '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' ), - 'customThankyou' => 'false', - 'customThankyouType' => 'message', + 'customThankyou' => '', 'customThankyouMessage' => __( 'Thank you for your submission!', 'jetpack' ), 'customThankyouRedirect' => '', ); @@ -2104,16 +2103,16 @@ static function parse( $attributes, $content ) { * @return string $message */ static function success_message( $feedback_id, $form ) { - $thankyou = ''; - if ( $form->get_attribute( 'customThankyou' ) && 'message' === $form->get_attribute( 'customThankyouType' ) ) { - $thankyou = wpautop( $form->get_attribute( 'customThankyouMessage' ) ); + if ( 'message' === $form->get_attribute( 'customThankyou' ) ) { + $message = wpautop( $form->get_attribute( 'customThankyouMessage' ) ); + } else { + $message = '
' + . '

' . join( '

', self::get_compiled_form( $feedback_id, $form ) ) . '

' + . '
'; } return wp_kses( - $thankyou - . '
' - . '

' . join( '

', self::get_compiled_form( $feedback_id, $form ) ) . '

' - . '
', + $message, array( 'br' => array(), 'blockquote' => array( 'class' => array() ), @@ -2826,7 +2825,7 @@ function process_submission() { return self::success_message( $post_id, $this ); } - if ( $this->get_attribute( 'customThankyou' ) && $this->get_attribute( 'customThankyouType' ) === 'redirect' ) { + if ( $this->get_attribute( 'customThankyou' ) === 'redirect' ) { $redirect = $this->get_attribute( 'customThankyouRedirect' ); } From d8fa34ee95dbbcf7e906f0424aaa0df200390a08 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 15 Oct 2019 14:23:33 +1100 Subject: [PATCH 08/21] Fix an undefined variable PHP notice when submitting the contact form. --- modules/contact-form/grunion-contact-form.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index 1c9ee277f7c..658faa618d8 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -2825,6 +2825,7 @@ function process_submission() { return self::success_message( $post_id, $this ); } + $redirect = ''; if ( $this->get_attribute( 'customThankyou' ) === 'redirect' ) { $redirect = $this->get_attribute( 'customThankyouRedirect' ); } From f70f74528db8d790783679a3582f7b7c01507567 Mon Sep 17 00:00:00 2001 From: Dave Martin Date: Tue, 15 Oct 2019 08:42:17 -0400 Subject: [PATCH 09/21] Title caps - to match labels in color section --- .../blocks/contact-form/components/jetpack-contact-form.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index 43469027e41..ec413dc92c4 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -216,7 +216,7 @@ class JetpackContactForm extends Component { /> { 'message' === customThankyou && ( this.props.setAttributes( { customThankyouMessage: value } ) } @@ -224,7 +224,7 @@ class JetpackContactForm extends Component { ) } { 'redirect' === customThankyou && ( Date: Wed, 16 Oct 2019 13:04:46 +1100 Subject: [PATCH 10/21] Experiment in removing BaseControl --- .../components/jetpack-contact-form.js | 17 +++++------------ extensions/blocks/contact-form/editor.scss | 4 ++++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index ec413dc92c4..f48cf96bcde 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -5,7 +5,6 @@ import classnames from 'classnames'; import emailValidator from 'email-validator'; import { __, sprintf } from '@wordpress/i18n'; import { - BaseControl, Button, PanelBody, Path, @@ -200,7 +199,6 @@ class JetpackContactForm extends Component { } renderConfirmationMessageFields() { - const { instanceId } = this.props; const { customThankyou, customThankyouMessage, customThankyouRedirect } = this.props.attributes; return ( @@ -223,17 +221,12 @@ class JetpackContactForm extends Component { /> ) } { 'redirect' === customThankyou && ( - - this.props.setAttributes( { customThankyouRedirect: value } ) } - /> - + value={ customThankyouRedirect } + className="jetpack-contact-form__thankyou-redirect-url" + onChange={ value => this.props.setAttributes( { customThankyouRedirect: value } ) } + /> ) } ); diff --git a/extensions/blocks/contact-form/editor.scss b/extensions/blocks/contact-form/editor.scss index 327cef142f0..e456b27a7e1 100644 --- a/extensions/blocks/contact-form/editor.scss +++ b/extensions/blocks/contact-form/editor.scss @@ -43,6 +43,10 @@ width: 100%; } +.jetpack-contact-form__thankyou-redirect-url__suggestions { + width: 260px; +} + .jetpack-field-label { display: flex; flex-direction: row; From 1ace1675acbf0b3ce18c10ae67d560e76b0eef34 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 13:04:55 +1100 Subject: [PATCH 11/21] [not verified] Revert "Experiment in removing BaseControl" This reverts commit 290863f516d502a1e1b6c0d110d36f86ce861621. --- .../components/jetpack-contact-form.js | 17 ++++++++++++----- extensions/blocks/contact-form/editor.scss | 4 ---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index f48cf96bcde..ec413dc92c4 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -5,6 +5,7 @@ import classnames from 'classnames'; import emailValidator from 'email-validator'; import { __, sprintf } from '@wordpress/i18n'; import { + BaseControl, Button, PanelBody, Path, @@ -199,6 +200,7 @@ class JetpackContactForm extends Component { } renderConfirmationMessageFields() { + const { instanceId } = this.props; const { customThankyou, customThankyouMessage, customThankyouRedirect } = this.props.attributes; return ( @@ -221,12 +223,17 @@ class JetpackContactForm extends Component { /> ) } { 'redirect' === customThankyou && ( - this.props.setAttributes( { customThankyouRedirect: value } ) } - /> + id={ `contact-form-${ instanceId }-thankyou-url` } + > + this.props.setAttributes( { customThankyouRedirect: value } ) } + /> + ) } ); diff --git a/extensions/blocks/contact-form/editor.scss b/extensions/blocks/contact-form/editor.scss index e456b27a7e1..327cef142f0 100644 --- a/extensions/blocks/contact-form/editor.scss +++ b/extensions/blocks/contact-form/editor.scss @@ -43,10 +43,6 @@ width: 100%; } -.jetpack-contact-form__thankyou-redirect-url__suggestions { - width: 260px; -} - .jetpack-field-label { display: flex; flex-direction: row; From 0d19037033621d2c69c50b091ce8fe40d00e3d2d Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 13:46:45 +1100 Subject: [PATCH 12/21] Improve the URL suggestion list width. --- extensions/blocks/contact-form/editor.scss | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extensions/blocks/contact-form/editor.scss b/extensions/blocks/contact-form/editor.scss index 327cef142f0..e456b27a7e1 100644 --- a/extensions/blocks/contact-form/editor.scss +++ b/extensions/blocks/contact-form/editor.scss @@ -43,6 +43,10 @@ width: 100%; } +.jetpack-contact-form__thankyou-redirect-url__suggestions { + width: 260px; +} + .jetpack-field-label { display: flex; flex-direction: row; From ad2639e29c7e493ccf0304b3e37ddf9c8960d627 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 14:13:56 +1100 Subject: [PATCH 13/21] Add comments for the new parameters. --- modules/contact-form/grunion-contact-form.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index 658faa618d8..eaf2a1273e9 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -1844,9 +1844,9 @@ function __construct( $attributes, $content = null ) { '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' ), - 'customThankyou' => '', - 'customThankyouMessage' => __( 'Thank you for your submission!', 'jetpack' ), - 'customThankyouRedirect' => '', + 'customThankyou' => '', // Whether to show a custom thank you 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'. ); $attributes = shortcode_atts( $this->defaults, $attributes, 'contact-form' ); From cfa59b80d1fb48c796b5f03ccef21f864f85662f Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 14:17:10 +1100 Subject: [PATCH 14/21] Add a PHPCS comment to ignore the usage of wp_redirect() instead of wp_safe_redirect(). --- modules/contact-form/grunion-contact-form.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index eaf2a1273e9..64277f4eaa4 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -2862,6 +2862,7 @@ function process_submission() { */ $redirect = apply_filters( 'grunion_contact_form_redirect_url', $redirect, $id, $post_id ); + // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- We intentially allow external redirects here. wp_redirect( $redirect ); exit; } From 9ade3a6cd05c5cd5368a2669d9d5383ff6838cb9 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 14:22:51 +1100 Subject: [PATCH 15/21] Use Yoda conditions, you must. --- modules/contact-form/grunion-contact-form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index 64277f4eaa4..c685e29193a 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -2826,7 +2826,7 @@ function process_submission() { } $redirect = ''; - if ( $this->get_attribute( 'customThankyou' ) === 'redirect' ) { + if ( 'redirect' === $this->get_attribute( 'customThankyou' ) ) { $redirect = $this->get_attribute( 'customThankyouRedirect' ); } From 6c6c83e4a5a45d39cb88ca34a6330233e76164ad Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 15:22:45 +1100 Subject: [PATCH 16/21] Don't try to migrate the block settings if the submit_button_text attribute isn't set. --- extensions/blocks/contact-form/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/extensions/blocks/contact-form/index.js b/extensions/blocks/contact-form/index.js index 00a9600ee71..69ad647775d 100644 --- a/extensions/blocks/contact-form/index.js +++ b/extensions/blocks/contact-form/index.js @@ -147,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; From e97ae24397060fe75833edc68e02cae64202fffc Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 20:34:34 +1100 Subject: [PATCH 17/21] Nouns and verbs are tricky, okay?! --- modules/contact-form/grunion-contact-form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index c685e29193a..996ccb114d1 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -1844,7 +1844,7 @@ function __construct( $attributes, $content = null ) { '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' ), - 'customThankyou' => '', // Whether to show a custom thank you response after submitting a form. '' for no, 'message' for a custom message, 'redirect' to redirect to a new URL. + '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'. ); From e1833d9bab3e26ccc3af912974187d08c53273de Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 20:36:29 +1100 Subject: [PATCH 18/21] esc_url() the URL that needs esc-ing. --- modules/contact-form/grunion-contact-form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index 996ccb114d1..ccdecafb289 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -2827,7 +2827,7 @@ function process_submission() { $redirect = ''; if ( 'redirect' === $this->get_attribute( 'customThankyou' ) ) { - $redirect = $this->get_attribute( 'customThankyouRedirect' ); + $redirect = esc_url( $this->get_attribute( 'customThankyouRedirect' ) ); } if ( ! $redirect ) { From 3d52262e804377cae0af435d223557165874ce9f Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 20:50:46 +1100 Subject: [PATCH 19/21] Note the use of camel case instead of snake case for the parameter names. --- modules/contact-form/grunion-contact-form.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index ccdecafb289..03faf6eb72c 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -1844,6 +1844,7 @@ function __construct( $attributes, $content = null ) { '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'. From 22e7bfb259cad83584dfb9009efcabf035f94a62 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 21:12:47 +1100 Subject: [PATCH 20/21] Don't add the URL parameters to custom redirect URLs. --- modules/contact-form/grunion-contact-form.php | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/modules/contact-form/grunion-contact-form.php b/modules/contact-form/grunion-contact-form.php index 03faf6eb72c..ccdddb48409 100644 --- a/modules/contact-form/grunion-contact-form.php +++ b/modules/contact-form/grunion-contact-form.php @@ -2827,28 +2827,35 @@ function process_submission() { } $redirect = ''; + $custom_redirect = false; if ( 'redirect' === $this->get_attribute( 'customThankyou' ) ) { - $redirect = esc_url( $this->get_attribute( 'customThankyouRedirect' ) ); + $custom_redirect = true; + $redirect = esc_url( $this->get_attribute( 'customThankyouRedirect' ) ); } if ( ! $redirect ) { - $redirect = wp_get_referer(); + $custom_redirect = false; + $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']; + if ( ! $redirect ) { // wp_get_referer() returns false if the referer is the same as the current page. + $custom_redirect = false; + $redirect = $_SERVER['REQUEST_URI']; } - $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 ( ! $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. From f694283821d7d4b91483eaa2e3b268dbbdc345de Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 16 Oct 2019 21:47:35 +1100 Subject: [PATCH 21/21] Add a comment indicating the URLInput hackery can be undone at a later date. --- .../blocks/contact-form/components/jetpack-contact-form.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extensions/blocks/contact-form/components/jetpack-contact-form.js b/extensions/blocks/contact-form/components/jetpack-contact-form.js index ec413dc92c4..2b5539a0166 100644 --- a/extensions/blocks/contact-form/components/jetpack-contact-form.js +++ b/extensions/blocks/contact-form/components/jetpack-contact-form.js @@ -223,6 +223,8 @@ class JetpackContactForm extends Component { /> ) } { '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