diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index c1d52706b04654a986a0460629cf12221538ebbd..53149c18c59926051c9cf09d2b8ad4c2861b81d8 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 ee0264c0211d09e6a5ce1f0825458342068e99dc..2162753879b5bfee5a62be28f4e5ccafa3d9b46c 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 980d54c28eb5dfc99e1f2a83a12cedca0355d6c8..ba01363dbf27e08c196be6ec4cb78f1c24b84690 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 de48e33d76b70c89a5f5aba5980b81e5f40260fc..c3c965db86b12fa6ef9ae8c0ed7ca19f5919762b 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 82021cb86ae4f1ce474734856218b743e95139c1..008168aad448403fdd338b290a9a3a2e58aaf35f 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 214417d3968ccc53ed7713ec66e35f2d5ee3b2d3..3068ae66513ef2017dfc3f7164f331c8e5b691eb 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 bae3ce3c40857e82c287fc75e70827a468e288af..65453d6bf978ff14b82704bc3244e5fcbb094d46 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 371013d460c08017247573947a6c2f54e7a9a86a..05e7d29abd4d8744cdffef854dd7ecff536825f1 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 a4844b1c1e8ec1be5ea9b32532c5c0873f0f91ca..6fb3c01b8eafc02dd9d2c809a9753653ec874b07 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 625c316bef04162c50b397f3759d4a623190b36f..edb7e0621ba435257982e3fec164cfc0974c4b35 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 8cf3846d8c3fd9e15175f04951845d4a6f071687..218f8607e1a7d6e12d53964b7fdfeac52eec1314 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() ) : ?>