Skip to content

Commit 26168ed

Browse files
authored
feat: add simple checkout form template with auto-generated credentials (#737)
Introduces a new 'simple' checkout form preset that requires only an email address from the customer. Username is derived from the email, password is generated server-side, site title from username, and site URL from site title — all via existing auto-generate mechanisms. Changes: - Signup_Field_Password: add auto_generate_password toggle; emits a hidden flag when enabled so no password field is rendered - Checkout: generate password via wp_generate_password() when the flag is present; skip password/password_conf/valid_password validation rules - Checkout_Form model: add 'simple' to template enum, use_template(), and save() step_types; add get_simple_template() private method - API schemas (create + update): add 'simple' to template enum
1 parent 4f03b8c commit 26168ed

5 files changed

Lines changed: 188 additions & 15 deletions

File tree

inc/apis/schemas/checkout-form-create.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@
6060
'required' => false,
6161
],
6262
'template' => [
63-
'description' => __("Template mode. Can be either 'blank', 'single-step' or 'multi-step'.", 'ultimate-multisite'),
63+
'description' => __("Template mode. Can be either 'blank', 'single-step', 'multi-step' or 'simple'.", 'ultimate-multisite'),
6464
'type' => 'string',
6565
'required' => false,
6666
'enum' => [
6767
'blank',
6868
'single-step',
6969
'multi-step',
70+
'simple',
7071
],
7172
],
7273
'date_created' => [

inc/apis/schemas/checkout-form-update.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@
6060
'required' => false,
6161
],
6262
'template' => [
63-
'description' => __("Template mode. Can be either 'blank', 'single-step' or 'multi-step'.", 'ultimate-multisite'),
63+
'description' => __("Template mode. Can be either 'blank', 'single-step', 'multi-step' or 'simple'.", 'ultimate-multisite'),
6464
'type' => 'string',
6565
'required' => false,
6666
'enum' => [
6767
'blank',
6868
'single-step',
6969
'multi-step',
70+
'simple',
7071
],
7172
],
7273
'date_created' => [

inc/checkout/class-checkout.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,16 @@ protected function maybe_create_customer() {
10561056
$username = wu_username_from_email($this->request_or_session('email_address'));
10571057
}
10581058

1059+
/*
1060+
* Resolve the password: use the submitted value, or generate one
1061+
* when the auto_generate_password flag is present in the session.
1062+
*/
1063+
$password = $this->request_or_session('password');
1064+
1065+
if ($this->request_or_session('auto_generate_password')) {
1066+
$password = wp_generate_password(16, true, false);
1067+
}
1068+
10591069
/*
10601070
* If we get to this point,
10611071
* we don't have an existing customer.
@@ -1065,7 +1075,7 @@ protected function maybe_create_customer() {
10651075
$customer_data = [
10661076
'username' => $username,
10671077
'email' => $this->request_or_session('email_address'),
1068-
'password' => $this->request_or_session('password'),
1078+
'password' => $password,
10691079
'email_verification' => $this->get_customer_email_verification_status(),
10701080
'signup_form' => $form_slug,
10711081
'meta' => [],
@@ -2260,16 +2270,22 @@ public function validation_rules() {
22602270
*
22612271
* First, let's set upm the general rules:
22622272
*/
2273+
/*
2274+
* When the password field is set to auto-generate, the customer does
2275+
* not submit a password value, so we must not require it.
2276+
*/
2277+
$auto_generate_password = $this->request_or_session('auto_generate_password');
2278+
22632279
$rules = [
22642280
'email_address' => 'required_without:user_id|email|unique:\WP_User,email',
22652281
'email_address_confirmation' => 'same:email_address',
22662282
'username' => 'required_without:user_id|alpha_dash|min:4|lowercase|unique:\WP_User,login',
2267-
'password' => 'required_without:user_id|min:6',
2268-
'password_conf' => 'same:password',
2283+
'password' => $auto_generate_password ? '' : 'required_without:user_id|min:6',
2284+
'password_conf' => $auto_generate_password ? '' : 'same:password',
22692285
'template_id' => 'integer|site_template',
22702286
'products' => 'products',
22712287
'gateway' => '',
2272-
'valid_password' => 'accepted',
2288+
'valid_password' => $auto_generate_password ? '' : 'accepted',
22732289
'billing_country' => 'country|required_with:billing_country',
22742290
'billing_zip_code' => 'required_with:billing_zip_code',
22752291
'billing_state' => 'state',

inc/checkout/signup-fields/class-signup-field-password.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function get_icon() {
123123
public function defaults() {
124124

125125
return [
126+
'auto_generate_password' => false,
126127
'password_confirm_field' => false,
127128
'password_confirm_label' => __('Confirm Password', 'ultimate-multisite'),
128129
];
@@ -166,17 +167,33 @@ public function force_attributes() {
166167
public function get_fields() {
167168

168169
return [
170+
'auto_generate_password' => [
171+
'type' => 'toggle',
172+
'title' => __('Auto-generate', 'ultimate-multisite'),
173+
'desc' => __('Check this option to auto-generate a secure password for the customer. The password will be emailed to them after signup.', 'ultimate-multisite'),
174+
'tooltip' => '',
175+
'value' => 0,
176+
'html_attr' => [
177+
'v-model' => 'auto_generate_password',
178+
],
179+
],
169180
'password_strength_meter' => [
170-
'type' => 'toggle',
171-
'title' => __('Display Password Strength Meter', 'ultimate-multisite'),
172-
'desc' => __('Adds a password strength meter below the password field. Enabling this option also enforces passwords to be strong.', 'ultimate-multisite'),
173-
'value' => 1,
181+
'type' => 'toggle',
182+
'title' => __('Display Password Strength Meter', 'ultimate-multisite'),
183+
'desc' => __('Adds a password strength meter below the password field. Enabling this option also enforces passwords to be strong.', 'ultimate-multisite'),
184+
'value' => 1,
185+
'wrapper_html_attr' => [
186+
'v-show' => '!auto_generate_password',
187+
],
174188
],
175189
'password_confirm_field' => [
176-
'type' => 'toggle',
177-
'title' => __('Display Password Confirm Field', 'ultimate-multisite'),
178-
'desc' => __('Adds a "Confirm your Password" field below the default password field to reduce the chance of making a mistake.', 'ultimate-multisite'),
179-
'value' => 1,
190+
'type' => 'toggle',
191+
'title' => __('Display Password Confirm Field', 'ultimate-multisite'),
192+
'desc' => __('Adds a "Confirm your Password" field below the default password field to reduce the chance of making a mistake.', 'ultimate-multisite'),
193+
'value' => 1,
194+
'wrapper_html_attr' => [
195+
'v-show' => '!auto_generate_password',
196+
],
180197
],
181198
];
182199
}
@@ -197,6 +214,20 @@ public function to_fields_array($attributes) {
197214
return [];
198215
}
199216

217+
/*
218+
* Auto-generate mode: emit a hidden flag so the checkout handler
219+
* knows to generate a password server-side. No visible fields needed.
220+
*/
221+
if (! empty($attributes['auto_generate_password'])) {
222+
return [
223+
'auto_generate_password' => [
224+
'type' => 'hidden',
225+
'id' => 'auto_generate_password',
226+
'value' => '1',
227+
],
228+
];
229+
}
230+
200231
$checkout_fields = [];
201232

202233
$checkout_fields['password'] = [

inc/models/class-checkout-form.php

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function validation_rules() {
148148
'allowed_countries' => 'default:',
149149
'thank_you_page_id' => 'integer',
150150
'conversion_snippets' => 'nullable|default:',
151-
'template' => 'in:blank,single-step,multi-step',
151+
'template' => 'in:blank,single-step,multi-step,simple',
152152
];
153153
}
154154

@@ -527,6 +527,8 @@ public function use_template($template = 'single-step'): void {
527527
$this->set_settings($fields);
528528
} elseif ('single-step' === $template) {
529529
$fields = $this->get_single_step_template();
530+
} elseif ('simple' === $template) {
531+
$fields = $this->get_simple_template();
530532
}
531533

532534
$this->set_settings($fields);
@@ -641,6 +643,127 @@ private function get_single_step_template() {
641643
return apply_filters('wu_checkout_form_single_step_template', $steps);
642644
}
643645

646+
/**
647+
* Get the contents of the simple template.
648+
*
649+
* A minimal single-step form where the customer only enters their email
650+
* address. Username, password, site title, and site URL are all
651+
* auto-generated. A product must be pre-selected; template selection is
652+
* shown only when no template has been pre-selected via the URL.
653+
*
654+
* @since 2.0.20
655+
* @return array
656+
*/
657+
private function get_simple_template() {
658+
659+
$plan_ids = wu_get_plans(['fields' => 'ids']);
660+
661+
$steps = [
662+
[
663+
'id' => 'checkout',
664+
'name' => __('Checkout', 'ultimate-multisite'),
665+
'desc' => '',
666+
'fields' => [
667+
[
668+
'step' => 'checkout',
669+
'name' => __('Pre-selected Products', 'ultimate-multisite'),
670+
'type' => 'products',
671+
'id' => 'products',
672+
'products' => implode(',', $plan_ids),
673+
],
674+
[
675+
'step' => 'checkout',
676+
'name' => __('Email', 'ultimate-multisite'),
677+
'type' => 'email',
678+
'id' => 'email_address',
679+
'required' => true,
680+
'placeholder' => '',
681+
'tooltip' => '',
682+
],
683+
[
684+
'step' => 'checkout',
685+
'name' => __('Username', 'ultimate-multisite'),
686+
'type' => 'username',
687+
'id' => 'username',
688+
'required' => true,
689+
'placeholder' => '',
690+
'tooltip' => '',
691+
'auto_generate_username' => true,
692+
],
693+
[
694+
'step' => 'checkout',
695+
'name' => __('Password', 'ultimate-multisite'),
696+
'type' => 'password',
697+
'id' => 'password',
698+
'required' => true,
699+
'placeholder' => '',
700+
'tooltip' => '',
701+
'auto_generate_password' => true,
702+
],
703+
[
704+
'step' => 'checkout',
705+
'name' => __('Site Title', 'ultimate-multisite'),
706+
'type' => 'site_title',
707+
'id' => 'site_title',
708+
'required' => true,
709+
'placeholder' => '',
710+
'tooltip' => '',
711+
'auto_generate_site_title' => true,
712+
],
713+
[
714+
'step' => 'checkout',
715+
'name' => __('Site URL', 'ultimate-multisite'),
716+
'type' => 'site_url',
717+
'id' => 'site_url',
718+
'required' => true,
719+
'placeholder' => '',
720+
'tooltip' => '',
721+
'auto_generate_site_url' => true,
722+
],
723+
[
724+
'step' => 'checkout',
725+
'name' => __('Template Selection', 'ultimate-multisite'),
726+
'type' => 'template_selection',
727+
'id' => 'template_selection',
728+
'template_selection_type' => 'all',
729+
'template_selection_template' => 'clean',
730+
'hide_template_selection_when_pre_selected' => true,
731+
],
732+
[
733+
'step' => 'checkout',
734+
'name' => __('Your Order', 'ultimate-multisite'),
735+
'type' => 'order_summary',
736+
'id' => 'order_summary',
737+
'order_summary_template' => 'clean',
738+
'table_columns' => 'simple',
739+
],
740+
[
741+
'step' => 'checkout',
742+
'name' => __('Payment Method', 'ultimate-multisite'),
743+
'type' => 'payment',
744+
'id' => 'payment',
745+
],
746+
[
747+
'step' => 'checkout',
748+
'name' => __('Billing Address', 'ultimate-multisite'),
749+
'type' => 'billing_address',
750+
'id' => 'billing_address',
751+
'required' => true,
752+
'zip_and_country' => '1',
753+
],
754+
[
755+
'step' => 'checkout',
756+
'name' => __('Checkout', 'ultimate-multisite'),
757+
'type' => 'submit_button',
758+
'id' => 'checkout',
759+
],
760+
],
761+
],
762+
];
763+
764+
return apply_filters('wu_checkout_form_simple_template', $steps);
765+
}
766+
644767
/**
645768
* Get the contents of the multi step template.
646769
*
@@ -1160,6 +1283,7 @@ public function save() {
11601283
$step_types = [
11611284
'multi-step',
11621285
'single-step',
1286+
'simple',
11631287
];
11641288

11651289
if ($this->template && in_array($this->template, $step_types, true)) {

0 commit comments

Comments
 (0)