Skip to content

Commit

Permalink
Fix the check that lets you prefills the form fields
Browse files Browse the repository at this point in the history
  • Loading branch information
enejb committed Jan 17, 2025
1 parent 1d1c5b7 commit efba70f
Showing 1 changed file with 60 additions and 46 deletions.
106 changes: 60 additions & 46 deletions projects/packages/forms/src/contact-form/class-contact-form-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ public function get_option_value( $value, $index, $options ) {
* @return string HTML
*/
public function render() {
global $current_user, $user_identity;

$field_id = $this->get_attribute( 'id' );
$field_type = $this->maybe_override_type();
Expand Down Expand Up @@ -359,46 +358,7 @@ public function render() {
*/
$field_class = apply_filters( 'jetpack_contact_form_input_class', $class );

if ( isset( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
if ( is_array( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
$this->value = array_map( 'sanitize_textarea_field', wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
} else {
$this->value = sanitize_textarea_field( wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
}
} elseif ( isset( $_GET[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
$this->value = sanitize_textarea_field( wp_unslash( $_GET[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
} elseif (
is_user_logged_in() &&
( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
/**
* Allow third-party tools to prefill the contact form with the user's details when they're logged in.
*
* @module contact-form
*
* @since 3.2.0
*
* @param bool false Should the Contact Form be prefilled with your details when you're logged in. Default to false.
*/
true === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
)
) {
// Special defaults for logged-in users
switch ( $field_type ) {
case 'email':
$this->value = $current_user->data->user_email;
break;
case 'name':
$this->value = $user_identity;
break;
case 'url':
$this->value = $current_user->data->user_url;
break;
default:
$this->value = $this->get_attribute( 'default' );
}
} else {
$this->value = $this->get_attribute( 'default' );
}
$this->value = $this->get_computed_field_value( $field_type, $field_id );

$field_value = Contact_Form_Plugin::strip_tags( $this->value );
$field_label = Contact_Form_Plugin::strip_tags( $field_label );
Expand All @@ -418,6 +378,65 @@ public function render() {
*/
return apply_filters( 'grunion_contact_form_field_html', $rendered_field, $field_label, ( in_the_loop() ? get_the_ID() : null ) );
}
/**
* Returns the computed field value for a field. It uses the POST, GET, Logged in data.
*
* @module contact-form
*
* @param string $field_type The field type.
* @param string $field_id The field id.
*
* @return string;

Check failure on line 389 in projects/packages/forms/src/contact-form/class-contact-form-field.php

View workflow job for this annotation

GitHub Actions / Static analysis

CommentError PhanUnextractableAnnotationSuffix Saw a token Phan may have failed to parse after '* @return string;': after string, saw ';'
*/
public function get_computed_field_value( $field_type, $field_id ) {
global $current_user, $user_identity;
// Use the POST Field if it is available.
if ( isset( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
if ( is_array( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
return array_map( 'sanitize_textarea_field', wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
}

return sanitize_textarea_field( wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
}

// Use the GET Field if it is available.
if ( isset( $_GET[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
if ( is_array( $_GET[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
return array_map( 'sanitize_textarea_field', wp_unslash( $_GET[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
}

return sanitize_textarea_field( wp_unslash( $_GET[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
}

if ( ! is_user_logged_in() ) {
return $this->get_attribute( 'default' );
}

/**
* Allow third-party tools to prefill the contact form with the user's details when they're logged in.
*
* @module contact-form
*
* @since 3.2.0
*
* @param bool false Should the Contact Form be prefilled with your details when you're logged in. Default to false.
*/
$filter_value = apply_filters( 'jetpack_auto_fill_logged_in_user', false );
if ( ( ! current_user_can( 'manage_options' ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) || $filter_value ) {
switch ( $field_type ) {
case 'email':
return $current_user->data->user_email;

case 'name':
return ! empty( $user_identity ) ? $user_identity : $current_user->data->display_name;

case 'url':
return $current_user->data->user_url;
}
}

return $this->get_attribute( 'default' );
}

/**
* Return the HTML for the label.
Expand Down Expand Up @@ -1032,11 +1051,6 @@ public function render_field( $type, $id, $label, $value, $class, $placeholder,

$field .= "\n<div {$block_style} {$shell_field_class} >\n"; // new in Jetpack 6.8.0

// If they are logged in, and this is their site, don't pre-populate fields
if ( current_user_can( 'manage_options' ) && isset( $type ) && $type !== 'checkbox' ) {
$value = '';
}

switch ( $type ) {
case 'email':
$field .= $this->render_email_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
Expand Down

0 comments on commit efba70f

Please sign in to comment.