Skip to content

Commit a8623b9

Browse files
authored
Forms: Fixes checkbox checked state for admins (#40847)
* Fix: #14912 * changelog * Fix the check that lets you prefills the form fields * Add tests
1 parent f573986 commit a8623b9

File tree

3 files changed

+226
-46
lines changed

3 files changed

+226
-46
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Form: fix the default checkstate for admins

projects/packages/forms/src/contact-form/class-contact-form-field.php

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ public function get_option_value( $value, $index, $options ) {
280280
* @return string HTML
281281
*/
282282
public function render() {
283-
global $current_user, $user_identity;
284283

285284
$field_id = $this->get_attribute( 'id' );
286285
$field_type = $this->maybe_override_type();
@@ -359,46 +358,7 @@ public function render() {
359358
*/
360359
$field_class = apply_filters( 'jetpack_contact_form_input_class', $class );
361360

362-
if ( isset( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
363-
if ( is_array( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
364-
$this->value = array_map( 'sanitize_textarea_field', wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
365-
} else {
366-
$this->value = sanitize_textarea_field( wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
367-
}
368-
} elseif ( isset( $_GET[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
369-
$this->value = sanitize_textarea_field( wp_unslash( $_GET[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
370-
} elseif (
371-
is_user_logged_in() &&
372-
( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
373-
/**
374-
* Allow third-party tools to prefill the contact form with the user's details when they're logged in.
375-
*
376-
* @module contact-form
377-
*
378-
* @since 3.2.0
379-
*
380-
* @param bool false Should the Contact Form be prefilled with your details when you're logged in. Default to false.
381-
*/
382-
true === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
383-
)
384-
) {
385-
// Special defaults for logged-in users
386-
switch ( $field_type ) {
387-
case 'email':
388-
$this->value = $current_user->data->user_email;
389-
break;
390-
case 'name':
391-
$this->value = $user_identity;
392-
break;
393-
case 'url':
394-
$this->value = $current_user->data->user_url;
395-
break;
396-
default:
397-
$this->value = $this->get_attribute( 'default' );
398-
}
399-
} else {
400-
$this->value = $this->get_attribute( 'default' );
401-
}
361+
$this->value = $this->get_computed_field_value( $field_type, $field_id );
402362

403363
$field_value = Contact_Form_Plugin::strip_tags( $this->value );
404364
$field_label = Contact_Form_Plugin::strip_tags( $field_label );
@@ -418,6 +378,65 @@ public function render() {
418378
*/
419379
return apply_filters( 'grunion_contact_form_field_html', $rendered_field, $field_label, ( in_the_loop() ? get_the_ID() : null ) );
420380
}
381+
/**
382+
* Returns the computed field value for a field. It uses the POST, GET, Logged in data.
383+
*
384+
* @module contact-form
385+
*
386+
* @param string $field_type The field type.
387+
* @param string $field_id The field id.
388+
*
389+
* @return string
390+
*/
391+
public function get_computed_field_value( $field_type, $field_id ) {
392+
global $current_user, $user_identity;
393+
// Use the POST Field if it is available.
394+
if ( isset( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
395+
if ( is_array( $_POST[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
396+
return array_map( 'sanitize_textarea_field', wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
397+
}
398+
399+
return sanitize_textarea_field( wp_unslash( $_POST[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes.
400+
}
401+
402+
// Use the GET Field if it is available.
403+
if ( isset( $_GET[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
404+
if ( is_array( $_GET[ $field_id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
405+
return array_map( 'sanitize_textarea_field', wp_unslash( $_GET[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
406+
}
407+
408+
return sanitize_textarea_field( wp_unslash( $_GET[ $field_id ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
409+
}
410+
411+
if ( ! is_user_logged_in() ) {
412+
return $this->get_attribute( 'default' );
413+
}
414+
415+
/**
416+
* Allow third-party tools to prefill the contact form with the user's details when they're logged in.
417+
*
418+
* @module contact-form
419+
*
420+
* @since 3.2.0
421+
*
422+
* @param bool false Should the Contact Form be prefilled with your details when you're logged in. Default to false.
423+
*/
424+
$filter_value = apply_filters( 'jetpack_auto_fill_logged_in_user', false );
425+
if ( ( ! current_user_can( 'manage_options' ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) || $filter_value ) {
426+
switch ( $field_type ) {
427+
case 'email':
428+
return $current_user->data->user_email;
429+
430+
case 'name':
431+
return ! empty( $user_identity ) ? $user_identity : $current_user->data->display_name;
432+
433+
case 'url':
434+
return $current_user->data->user_url;
435+
}
436+
}
437+
438+
return $this->get_attribute( 'default' );
439+
}
421440

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

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

1035-
// If they are logged in, and this is their site, don't pre-populate fields
1036-
if ( current_user_can( 'manage_options' ) ) {
1037-
$value = '';
1038-
}
1039-
10401054
switch ( $type ) {
10411055
case 'email':
10421056
$field .= $this->render_email_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Unit Tests for Automattic\Jetpack\Forms\Contact_Form.
4+
*
5+
* @package automattic/jetpack-forms
6+
*/
7+
8+
namespace Automattic\Jetpack\Forms\ContactForm;
9+
10+
use WorDBless\BaseTestCase;
11+
12+
/**
13+
* Test class for Contact_Form
14+
*
15+
* @covers Automattic\Jetpack\Forms\ContactForm\Contact_Form
16+
*/
17+
class WP_Test_Contact_Form_Field extends BaseTestCase {
18+
19+
protected function setUp(): void {
20+
parent::setUp();
21+
22+
// Mock global variables
23+
global $user_identity;
24+
25+
$user_id = wp_insert_user(
26+
array(
27+
'user_login' => 'admin',
28+
'user_pass' => 'pass',
29+
'user_email' => '[email protected]',
30+
'role' => 'reader',
31+
'user_url' => 'https://example.com',
32+
)
33+
);
34+
35+
// Simulate a logged-in user
36+
wp_set_current_user( $user_id );
37+
$user_identity = 'Test User';
38+
}
39+
40+
protected function tearDown(): void {
41+
parent::tearDown();
42+
global $current_user, $user_identity;
43+
44+
// Clean up globals
45+
unset( $_POST, $_GET, $current_user, $user_identity );
46+
}
47+
48+
/**
49+
* Helper function to invoke the function from the class.
50+
*/
51+
private function invoke_get_computed_field_value( $field_type, $field_id ) {
52+
$field = $this->get_new_field_instance(
53+
array(
54+
'type' => $field_type,
55+
'id' => $field_id,
56+
)
57+
);
58+
return $field->get_computed_field_value( $field_type, $field_id );
59+
}
60+
61+
private function get_new_field_instance( $attributes ) {
62+
$defaults = array(
63+
'type' => 'text',
64+
'id' => 'id',
65+
'default' => 'default',
66+
);
67+
68+
return new Contact_Form_Field( wp_parse_args( $attributes, $defaults ) );
69+
}
70+
71+
/**
72+
* Test handling $_POST single value
73+
*/
74+
public function test_handles_post_single_value() {
75+
$_POST['test_field'] = 'Post Value';
76+
77+
$result = $this->invoke_get_computed_field_value( 'text', 'test_field' );
78+
79+
$this->assertEquals( 'Post Value', $result );
80+
}
81+
82+
/**
83+
* Test handling $_POST array value
84+
*/
85+
public function test_handles_post_array_value() {
86+
$_POST['test_field'] = array( 'value1', 'value2' );
87+
88+
$result = $this->invoke_get_computed_field_value( 'text', 'test_field' );
89+
90+
$this->assertEquals( array( 'value1', 'value2' ), $result );
91+
}
92+
93+
/**
94+
* Test handling $_GET single value
95+
*/
96+
public function test_handles_get_single_value() {
97+
$_GET['test_field'] = 'Get Value';
98+
99+
$result = $this->invoke_get_computed_field_value( 'text', 'test_field' );
100+
101+
$this->assertEquals( 'Get Value', $result );
102+
}
103+
104+
/**
105+
* Test handling $_GET array value
106+
*/
107+
public function test_handles_get_array_value() {
108+
$_GET['test_field'] = array( 'value1', 'value2' );
109+
110+
$result = $this->invoke_get_computed_field_value( 'text', 'test_field' );
111+
112+
$this->assertEquals( array( 'value1', 'value2' ), $result );
113+
}
114+
115+
/**
116+
* Test logged-in user email return
117+
*/
118+
public function test_returns_logged_in_user_email() {
119+
add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
120+
$result = $this->invoke_get_computed_field_value( 'email', 'test_field' );
121+
remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
122+
123+
$this->assertEquals( '[email protected]', $result );
124+
}
125+
126+
/**
127+
* Test logged-in user name return
128+
*/
129+
public function test_returns_logged_in_user_name() {
130+
add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
131+
$result = $this->invoke_get_computed_field_value( 'name', 'test_field' );
132+
remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
133+
134+
$this->assertEquals( 'Test User', $result );
135+
}
136+
137+
/**
138+
* Test logged-in user URL return
139+
*/
140+
public function test_returns_logged_in_user_url() {
141+
add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
142+
$result = $this->invoke_get_computed_field_value( 'url', 'test_field' );
143+
remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
144+
145+
$this->assertEquals( 'https://example.com', $result );
146+
}
147+
148+
/**
149+
* Test logged-in user URL return
150+
*/
151+
public function test_returns_logged_out_user_url() {
152+
global $current_user;
153+
unset( $current_user );
154+
wp_set_current_user( 0 );
155+
156+
add_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
157+
$result = $this->invoke_get_computed_field_value( 'url', 'test_field' );
158+
remove_filter( 'jetpack_auto_fill_logged_in_user', '__return_true' );
159+
160+
$this->assertEquals( 'default', $result );
161+
}
162+
} // end class

0 commit comments

Comments
 (0)