From 4194136993983cdb5504534df5637cba2d53ecaa Mon Sep 17 00:00:00 2001 From: potreb <potreb@gmail.com> Date: Wed, 6 Oct 2021 12:21:10 +0200 Subject: [PATCH] fix: html attributes, templates fix --- src/Field/BasicField.php | 29 ++++++++++++------------- src/Field/SelectField.php | 2 +- src/Field/Traits/HtmlAttributes.php | 26 +++++++--------------- src/Form/FormWithFields.php | 15 ++++++++----- src/Renderer/JsonNormalizedRenderer.php | 28 +++++++++--------------- templates/button.php | 23 +++++--------------- templates/input-submit.php | 12 ---------- templates/input-text-multiple.php | 12 ---------- templates/input.php | 16 -------------- templates/select.php | 17 --------------- templates/textarea.php | 17 --------------- 11 files changed, 48 insertions(+), 149 deletions(-) diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index c1d5270..53149c1 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -25,12 +25,12 @@ abstract class BasicField implements Field { /** @var array{default_value: string, possible_values?: string[], sublabel?: string, priority: int, label: string, description: string, description_tip: string, data: array<string|int>} */ protected $meta = [ - 'priority' => self::DEFAULT_PRIORITY, - 'default_value' => '', - 'label' => '', - 'description' => '', - 'description_tip' => '', - 'data' => [], + 'priority' => self::DEFAULT_PRIORITY, + 'default_value' => '', + 'label' => '', + 'description' => '', + 'description_tip' => '', + 'data' => [], ]; public function should_override_form_template(): bool { @@ -59,7 +59,7 @@ abstract class BasicField implements Field { } public function get_serializer(): Serializer { - throw new BadMethodCallException('You must define your serializer in a child class.'); + throw new BadMethodCallException( 'You must define your serializer in a child class.' ); } final public function get_name(): string { @@ -156,33 +156,32 @@ abstract class BasicField implements Field { return $this->attributes['id'] ?? sanitize_title( $this->get_name() ); } - final public function is_multiple(): bool { - return $this->attributes['multiple']; + return isset( $this->attributes['multiple'] ); } final public function set_disabled(): Field { - $this->attributes['disabled'] = true; + $this->attributes['disabled'] = 'disabled'; return $this; } final public function is_disabled(): bool { - return $this->attributes['disabled']; + return $this->attributes['disabled'] ?? false; } final public function set_readonly(): Field { - $this->attributes['readonly'] = true; + $this->attributes['readonly'] = 'readonly'; return $this; } final public function is_readonly(): bool { - return $this->attributes['readonly']; + return $this->attributes['readonly'] ?? false; } final public function set_required(): Field { - $this->attributes['required'] = true; + $this->attributes['required'] = 'required'; return $this; } @@ -233,7 +232,7 @@ abstract class BasicField implements Field { } final public function is_required(): bool { - return $this->attributes['required']; + return isset( $this->attributes['required'] ); } final public function get_priority(): int { diff --git a/src/Field/SelectField.php b/src/Field/SelectField.php index ee0264c..2162753 100644 --- a/src/Field/SelectField.php +++ b/src/Field/SelectField.php @@ -18,7 +18,7 @@ class SelectField extends BasicField { } public function set_multiple(): Field { - $this->attributes['multiple'] = true; + $this->attributes['multiple'] = 'multiple'; return $this; } diff --git a/src/Field/Traits/HtmlAttributes.php b/src/Field/Traits/HtmlAttributes.php index 980d54c..ba01363 100644 --- a/src/Field/Traits/HtmlAttributes.php +++ b/src/Field/Traits/HtmlAttributes.php @@ -12,19 +12,8 @@ use WPDesk\Forms\Form; */ trait HtmlAttributes { - /** @var array{placeholder: string, name: string, id: string, class: string[], readonly: bool, multiple: bool, disabled: bool, required: bool, method: string, action: string} */ - protected $attributes = [ - 'placeholder' => '', - 'name' => '', - 'id' => '', - 'class' => [], - 'action' => '', - 'method' => 'POST', - 'readonly' => false, - 'multiple' => false, - 'disabled' => false, - 'required' => false, - ]; + /** @var array{placeholder: string, name: string, id: string, class: string[]} */ + protected $attributes = []; /** * Get list of all attributes except given. @@ -33,18 +22,18 @@ trait HtmlAttributes { * * @return array<string[]|string|bool> */ - final public function get_attributes( array $except = [ 'name', 'class', 'method', 'action' ] ): array { + final public function get_attributes( array $except = [ 'name', 'class' ] ): array { return array_filter( $this->attributes, - static function ( $attribute, $key ) use ( $except ) { - return ! in_array( $key, $except, true ) && ! empty( $attribute ); + static function ( $key ) use ( $except ) { + return ! in_array( $key, $except, true ); }, - ARRAY_FILTER_USE_BOTH + ARRAY_FILTER_USE_KEY ); } /** - * @param string $name + * @param string $name * @param string[]|string|bool $value * * @return Field|Form @@ -73,6 +62,7 @@ trait HtmlAttributes { // Be aware of coercing - if implode returns string(0) '', then return $default value. return implode( ' ', $this->attributes[ $name ] ) ?: $default ?? ''; } + return (string) $this->attributes[ $name ] ?? $default ?? ''; } } diff --git a/src/Form/FormWithFields.php b/src/Form/FormWithFields.php index de48e33..c3c965d 100644 --- a/src/Form/FormWithFields.php +++ b/src/Form/FormWithFields.php @@ -26,12 +26,14 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { /** * FormWithFields constructor. * - * @param Field[] $fields - * @param string $form_id + * @param Field[] $fields + * @param string $form_id */ public function __construct( array $fields, string $form_id = 'form' ) { $this->fields = $fields; $this->form_id = $form_id; + $this->set_action( '' ); + $this->set_method( 'POST' ); } /** Set Form action attribute. */ @@ -41,6 +43,10 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { return $this; } + public function get_action(): string { + return $this->attributes['action']; + } + /** Set Form method attribute ie. GET/POST. */ public function set_method( string $method ): self { $this->attributes['method'] = $method; @@ -52,9 +58,6 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { return $this->attributes['method']; } - public function get_action(): string { - return $this->attributes['action']; - } public function is_submitted(): bool { return null !== $this->updated_data; @@ -146,7 +149,7 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { } public function render_form( Renderer $renderer ): string { - $content = $renderer->render( + $content = $renderer->render( 'form-start', [ 'form' => $this, diff --git a/src/Renderer/JsonNormalizedRenderer.php b/src/Renderer/JsonNormalizedRenderer.php index 82021cb..008168a 100644 --- a/src/Renderer/JsonNormalizedRenderer.php +++ b/src/Renderer/JsonNormalizedRenderer.php @@ -13,28 +13,23 @@ use WPDesk\Forms\FieldRenderer; class JsonNormalizedRenderer implements FieldRenderer { /** * @param FieldProvider $provider - * @param array $fields_data - * @param string $name_prefix + * @param array $fields_data + * @param string $name_prefix * * @return array Normalized fields with data. */ public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ): array { $rendered_fields = []; foreach ( $provider->get_fields() as $field ) { - $rendered = [ - 'name' => $field->get_name(), - 'template' => $field->get_template_name(), - 'multiple' => $field->is_multiple(), - 'disabled' => $field->is_disabled(), - 'readonly' => $field->is_readonly(), - 'required' => $field->is_required(), - 'prefix' => $name_prefix, - 'value ' => $fields_data[ $field->get_name() ] ?? $field->get_default_value(), - ]; + $rendered = []; + foreach ( $field->get_attributes() as $key => $attribute ) { + $rendered[ $key ] = $attribute; + } + $rendered['name'] = $field->get_name(); + $rendered['template'] = $field->get_template_name(); + $rendered['prefix'] = $name_prefix; + $rendered['value'] = $fields_data[ $field->get_name() ] ?? $field->get_default_value(); - if ( $field->has_classes() ) { - $rendered['class'] = $field->get_classes(); - } if ( $field->has_description() ) { $rendered['description'] = $field->get_description(); } @@ -44,9 +39,6 @@ class JsonNormalizedRenderer implements FieldRenderer { if ( $field->has_label() ) { $rendered['label'] = $field->get_label(); } - if ( $field->has_placeholder() ) { - $rendered['placeholder'] = $field->get_placeholder(); - } $options = $field->get_possible_values(); if ( $options ) { $rendered['options'] = $options; diff --git a/templates/button.php b/templates/button.php index 214417d..3068ae6 100644 --- a/templates/button.php +++ b/templates/button.php @@ -10,27 +10,16 @@ ?> <button -<?php if ( $field->has_classes() ) : ?> - class="<?php echo \esc_attr( $field->get_classes() ); ?>" -<?php endif; ?> - -<?php foreach ( $field->get_attributes( [] ) as $key => $val ) : ?> - <?php echo \esc_attr( $key ) . '="' . \esc_attr( $val ) . '"'; ?> -<?php endforeach; ?> - type="<?php echo \esc_attr( $field->get_type() ); ?>" name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>" id="<?php echo \esc_attr( $field->get_id() ); ?>" value="<?php echo \esc_html( $value ); ?>" - - <?php - if ( $field->is_disabled() ) : - ?> - disabled="disabled"<?php endif; ?> - <?php - if ( $field->is_readonly() ) : - ?> - readonly="readonly"<?php endif; ?> + <?php if ( $field->has_classes() ) : ?> + class="<?php echo \esc_attr( $field->get_classes() ); ?>" + <?php endif; ?> + <?php foreach ( $field->get_attributes() as $key => $val ) : ?> + <?php echo \esc_attr( $key ) . '="' . \esc_attr( $val ) . '"'; ?> + <?php endforeach; ?> ><?php echo \esc_html( $field->get_label() ); ?></button> diff --git a/templates/input-submit.php b/templates/input-submit.php index bae3ce3..65453d6 100644 --- a/templates/input-submit.php +++ b/templates/input-submit.php @@ -24,18 +24,6 @@ name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( $field->get_name() ); ?>]" id="<?php echo \esc_attr( $field->get_id() ); ?>" value="<?php echo \esc_html( $field->get_label() ); ?>" - <?php - if ( $field->is_required() ) : - ?> - required="required"<?php endif; ?> - <?php - if ( $field->is_disabled() ) : - ?> - disabled="disabled"<?php endif; ?> - <?php - if ( $field->is_readonly() ) : - ?> - readonly="readonly"<?php endif; ?> /> </p> </td> diff --git a/templates/input-text-multiple.php b/templates/input-text-multiple.php index 371013d..05e7d29 100644 --- a/templates/input-text-multiple.php +++ b/templates/input-text-multiple.php @@ -43,18 +43,6 @@ if ( empty( $value ) || is_string( $value ) ) { ?> <?php endforeach; ?> - <?php - if ( $field->is_required() ) : - ?> - required="required"<?php endif; ?> - <?php - if ( $field->is_disabled() ) : - ?> - disabled="disabled"<?php endif; ?> - <?php - if ( $field->is_readonly() ) : - ?> - readonly="readonly"<?php endif; ?> <?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ], true ) ) : ?> value="<?php echo \esc_html( $text_value ); ?>" <?php else : ?> diff --git a/templates/input.php b/templates/input.php index a4844b1..6fb3c01 100644 --- a/templates/input.php +++ b/templates/input.php @@ -23,28 +23,12 @@ if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) : class="<?php echo \esc_attr( $field->get_classes() ); ?>" <?php endif; ?> - <?php if ( $field->get_type() === 'text' && $field->has_placeholder() ) : ?> - placeholder="<?php echo \esc_html( $field->get_placeholder() ); ?>" - <?php endif; ?> - <?php foreach ( $field->get_attributes() as $key => $atr_val ) : echo \esc_attr( $key ) . '="' . \esc_attr( $atr_val ) . '"'; ?> <?php endforeach; ?> - <?php - if ( $field->is_required() ) : - ?> - required="required"<?php endif; ?> - <?php - if ( $field->is_disabled() ) : - ?> - disabled="disabled"<?php endif; ?> - <?php - if ( $field->is_readonly() ) : - ?> - readonly="readonly"<?php endif; ?> <?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ], true ) ) : ?> value="<?php echo \esc_html( $value ); ?>" <?php else : ?> diff --git a/templates/select.php b/templates/select.php index 625c316..edb7e06 100644 --- a/templates/select.php +++ b/templates/select.php @@ -17,23 +17,6 @@ <?php foreach ( $field->get_attributes() as $key => $attr_val ) : ?> <?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>" <?php endforeach; ?> - - <?php - if ( $field->is_required() ) : - ?> - required="required"<?php endif; ?> - <?php - if ( $field->is_disabled() ) : - ?> - disabled="disabled"<?php endif; ?> - <?php - if ( $field->is_readonly() ) : - ?> - readonly="readonly"<?php endif; ?> - <?php - if ( $field->is_multiple() ) : - ?> - multiple="multiple"<?php endif; ?> > <?php if ( $field->has_placeholder() ) : diff --git a/templates/textarea.php b/templates/textarea.php index 8cf3846..218f860 100644 --- a/templates/textarea.php +++ b/templates/textarea.php @@ -18,23 +18,6 @@ <?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>" <?php endforeach; ?> - <?php - if ( $field->is_required() ) : - ?> - required="required"<?php endif; ?> - <?php - if ( $field->is_disabled() ) : - ?> - disabled="disabled"<?php endif; ?> - <?php - if ( $field->is_readonly() ) : - ?> - readonly="readonly"<?php endif; ?> - <?php - if ( $field->is_multiple() ) : - ?> - multiple="multiple"<?php endif; ?> - <?php if ( $field->has_placeholder() ) : ?> -- GitLab