-
Notifications
You must be signed in to change notification settings - Fork 41
Move address fields to Lite #3171
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
base: master
Are you sure you want to change the base?
Changes from all commits
3937b09
a6e0d55
6f89c2a
5064f67
5f9e02f
f50bfa0
1f427dc
67a23bd
eb87a06
1d910f9
d69ed83
372908d
64e957c
cf0fc0c
3f29e79
1f36656
d48c1bf
54cc72c
27ee592
929b0dc
960f704
daeef13
9683be3
23097d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,189 @@ | ||||||||||||
| <?php | ||||||||||||
|
|
||||||||||||
| if ( ! defined( 'ABSPATH' ) ) { | ||||||||||||
| die( 'You are not allowed to call this page directly.' ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Base controller for combo fields (fields with multiple sub-fields like Address). | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| */ | ||||||||||||
| class FrmComboFieldsController { | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Fill values with defaults. | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param mixed $value | ||||||||||||
| * @param array $defaults | ||||||||||||
| * | ||||||||||||
| * @return void | ||||||||||||
| */ | ||||||||||||
| public static function fill_values( &$value, $defaults ) { | ||||||||||||
| $value = $value ? array_merge( $defaults, (array) $value ) : $defaults; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Include placeholder attribute for sub-field. | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param mixed $default_value | ||||||||||||
| * @param string $sub_field | ||||||||||||
| * @param array $field | ||||||||||||
| * | ||||||||||||
| * @return void | ||||||||||||
| */ | ||||||||||||
| public static function include_placeholder( $default_value, $sub_field, $field = array() ) { | ||||||||||||
| if ( ! is_array( $default_value ) ) { | ||||||||||||
| $default_value = array(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ( $field ) { | ||||||||||||
| $use_placeholder = ! FrmField::is_option_empty( $field, 'placeholder' ); | ||||||||||||
|
|
||||||||||||
| if ( ( ! $use_placeholder || empty( $default_value[ $sub_field ] ) ) && $sub_field === 'line1' ) { | ||||||||||||
| // Allow for 'inside' label position. | ||||||||||||
| $default_value[ $sub_field ] = FrmFieldsController::get_default_value_from_name( $field ); | ||||||||||||
| $use_placeholder = $default_value[ $sub_field ] !== ''; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ( ! $use_placeholder ) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ( ! $default_value ) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| $placeholder = $default_value[ $sub_field ] ?? ''; | ||||||||||||
| echo ' placeholder="' . esc_attr( $placeholder ) . '" data-placeholder="' . esc_attr( $placeholder ) . '"'; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Get dropdown label for select sub-fields. | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param array $atts | ||||||||||||
| * | ||||||||||||
| * @return string | ||||||||||||
| */ | ||||||||||||
| public static function get_dropdown_label( $atts ) { | ||||||||||||
| $default = $atts['sub_field']['placeholder'] ?? ' '; | ||||||||||||
|
|
||||||||||||
| if ( ! is_string( $default ) ) { | ||||||||||||
| $default = ''; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param string $default | ||||||||||||
| * @param array $atts | ||||||||||||
| */ | ||||||||||||
| $result = apply_filters( 'frm_combo_dropdown_label', $default, $atts ); | ||||||||||||
|
|
||||||||||||
| return is_string( $result ) ? $result : $default; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Add attributes to input field. | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param array $atts | ||||||||||||
| * | ||||||||||||
| * @return void | ||||||||||||
| */ | ||||||||||||
| public static function add_atts_to_input( $atts ) { | ||||||||||||
| $placeholder = $atts['field']['placeholder'] ?? ''; | ||||||||||||
| $default_value = $atts['field']['default_value']; | ||||||||||||
| $field_obj = FrmFieldFactory::get_field_type( $atts['field']['type'], $atts['field'] ); | ||||||||||||
|
|
||||||||||||
| if ( $atts['field']['type'] === 'address' ) { | ||||||||||||
| /** | ||||||||||||
| * @var FrmFieldAddress $field_obj | ||||||||||||
| */ | ||||||||||||
| $placeholder = $field_obj->address_string_to_array( $placeholder ); | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
| $default_value = $field_obj->address_string_to_array( $default_value ); | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| self::include_placeholder( $placeholder, $atts['key'], $atts['field'] ); | ||||||||||||
| $atts['field']['placeholder'] = ''; | ||||||||||||
|
|
||||||||||||
| $atts['field']['default_value'] = $default_value[ $atts['key'] ] ?? ''; | ||||||||||||
|
|
||||||||||||
|
Comment on lines
+115
to
+119
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Align sub-field key contract ( These helpers read Suggested fix public static function add_atts_to_input( $atts ) {
+ $sub_field_key = $atts['key'] ?? $atts['name'] ?? '';
+ if ( '' === $sub_field_key ) {
+ return;
+ }
+
$placeholder = $atts['field']['placeholder'] ?? '';
$default_value = $atts['field']['default_value'];
$field_obj = FrmFieldFactory::get_field_type( $atts['field']['type'], $atts['field'] );
@@
- self::include_placeholder( $placeholder, $atts['key'], $atts['field'] );
+ self::include_placeholder( $placeholder, $sub_field_key, $atts['field'] );
$atts['field']['placeholder'] = '';
- $atts['field']['default_value'] = $default_value[ $atts['key'] ] ?? '';
+ $atts['field']['default_value'] = $default_value[ $sub_field_key ] ?? '';
@@
public static function maybe_add_error_class( $atts ) {
+ $sub_field_key = $atts['key'] ?? $atts['name'] ?? '';
+ if ( '' === $sub_field_key ) {
+ return;
+ }
+
$temp_id = ! empty( $atts['atts']['field_id'] ) ? $atts['atts']['field_id'] : $atts['field']['id'];
- $has_error = isset( $atts['errors'][ 'field' . $temp_id . '-' . $atts['key'] ] );
+ $has_error = isset( $atts['errors'][ 'field' . $temp_id . '-' . $sub_field_key ] );Also applies to: 179-180 🤖 Prompt for AI Agents |
||||||||||||
| if ( 'select' === $atts['sub_field']['type'] && ! empty( $atts['field']['read_only'] ) ) { | ||||||||||||
| $atts['sub_field']['atts']['disabled'] = 'disabled'; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ( ! empty( $atts['sub_field']['optional'] ) ) { | ||||||||||||
| add_filter( 'frm_field_classes', 'FrmAddressesController::add_optional_class', 20, 2 ); | ||||||||||||
| do_action( 'frm_field_input_html', $atts['field'] ); | ||||||||||||
| remove_filter( 'frm_field_classes', 'FrmAddressesController::add_optional_class', 20 ); | ||||||||||||
| } else { | ||||||||||||
| do_action( 'frm_field_input_html', $atts['field'] ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ( ! isset( $atts['sub_field']['atts'] ) ) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| foreach ( $atts['sub_field']['atts'] as $att_name => $att_value ) { | ||||||||||||
| echo ' ' . esc_attr( $att_name ) . '="' . esc_attr( $att_value ) . '"'; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Include sub-label for field. | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param array $atts | ||||||||||||
| * | ||||||||||||
| * @return void | ||||||||||||
| */ | ||||||||||||
| public static function include_sub_label( $atts ) { | ||||||||||||
| self::show_sub_label( $atts ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Show sub-label for field. | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param array $atts | ||||||||||||
| * | ||||||||||||
| * @return void | ||||||||||||
| */ | ||||||||||||
| public static function show_sub_label( $atts ) { | ||||||||||||
| $field = $atts['field']; | ||||||||||||
| $option_name = $atts['option_name']; | ||||||||||||
|
|
||||||||||||
| if ( $field[ $option_name ] !== '' ) { | ||||||||||||
| echo '<div class="frm_description">' . wp_kses_post( $field[ $option_name ] ) . '</div>'; | ||||||||||||
|
Comment on lines
+167
to
+168
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Guard sub-label array access before dereferencing.
Suggested fix- if ( $field[ $option_name ] !== '' ) {
+ if ( isset( $field[ $option_name ] ) && '' !== $field[ $option_name ] ) {
echo '<div class="frm_description">' . wp_kses_post( $field[ $option_name ] ) . '</div>';
}📝 Committable suggestion
Suggested change
🧰 Tools🪛 ast-grep (0.44.0)[warning] 164-164: Avoid side effects in a file that defines symbols ' . wp_kses_post( $field[ $option_name ] ) . ' ';Note: [CWE-710] Improper Adherence to Coding Standards. (no-side-effect) 🤖 Prompt for AI Agents |
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Add error class if field has error. | ||||||||||||
| * | ||||||||||||
| * @since x.x | ||||||||||||
| * | ||||||||||||
| * @param array $atts | ||||||||||||
| * | ||||||||||||
| * @return void | ||||||||||||
| */ | ||||||||||||
| public static function maybe_add_error_class( $atts ) { | ||||||||||||
| $temp_id = ! empty( $atts['atts']['field_id'] ) ? $atts['atts']['field_id'] : $atts['field']['id']; | ||||||||||||
| $has_error = isset( $atts['errors'][ 'field' . $temp_id . '-' . $atts['key'] ] ); | ||||||||||||
|
|
||||||||||||
| if ( $has_error ) { | ||||||||||||
| echo ' frm_blank_field'; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function call is not valid, which will result in a fatal runtime error.