diff --git a/composer.json b/composer.json index 8150bcff8112a4b9fb1e678d405162de43077a7c..03b48b4c3c4904c3545fedbd94b7cb7f6ff56239 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "ext-curl": "*", "ext-json": "*", "wpdesk/wp-persistence": "^2.0|^3.0", - "wpdesk/wp-view": "^1.1" + "wpdesk/wp-view": "^2" }, "require-dev": { "phpunit/phpunit": "<7", diff --git a/src/ContainerForm.php b/src/ContainerForm.php index d79b57f1f3513659926a2ba8bee926df3b67e047..e3b6dde63d31bf31e483b37b6a3c69de2012a3f7 100644 --- a/src/ContainerForm.php +++ b/src/ContainerForm.php @@ -11,6 +11,7 @@ use WPDesk\Persistence\PersistentContainer; * @package WPDesk\Forms */ interface ContainerForm { + /** * @param ContainerInterface $data * diff --git a/src/Escaper.php b/src/Escaper.php index 8c460dd16b8dee8f060777b542f1aa933456b274..462295c4515f0adc3321ae31f4fa0b19050768b7 100644 --- a/src/Escaper.php +++ b/src/Escaper.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms; interface Escaper { + /** @param mixed $value */ public function escape( $value ): string; } diff --git a/src/Field.php b/src/Field.php index 08329e7f0a4166ed8d3551075b60117770dae779..b932f273f01a7dff40ffeedd12be722d943e0869 100644 --- a/src/Field.php +++ b/src/Field.php @@ -11,6 +11,7 @@ namespace WPDesk\Forms; * @package WPDesk\Forms */ interface Field { + public function get_name(): string; /** @return mixed */ diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 4c1dc0cf40c58b7eb0291921b66f1260f5c2b9f7..53149c18c59926051c9cf09d2b8ad4c2861b81d8 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -2,6 +2,7 @@ namespace WPDesk\Forms\Field; +use BadMethodCallException; use WPDesk\Forms\Field; use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\NoSanitize; @@ -17,18 +18,19 @@ use WPDesk\Forms\Validator\RequiredValidator; * @package WPDesk\Forms */ abstract class BasicField implements Field { + use Field\Traits\HtmlAttributes; const DEFAULT_PRIORITY = 10; /** @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 { @@ -57,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 { @@ -154,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; } @@ -231,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/ButtonField.php b/src/Field/ButtonField.php index 6d01a1f5f2302cc70fb347e22c54120b58c9ad26..1a43c3ff81524108364c1f240dd2337059a9510c 100644 --- a/src/Field/ButtonField.php +++ b/src/Field/ButtonField.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms\Field; class ButtonField extends NoValueField { + public function get_template_name(): string { return 'button'; } diff --git a/src/Field/CheckboxField.php b/src/Field/CheckboxField.php index d55269e464a004ba77fb25bb3b3b62e77b7cea6d..14110296cb1f03405c8928913d9fd1a6ac1b4a00 100644 --- a/src/Field/CheckboxField.php +++ b/src/Field/CheckboxField.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Field; use WPDesk\Forms\Field; class CheckboxField extends BasicField { + const VALUE_TRUE = 'yes'; const VALUE_FALSE = 'no'; diff --git a/src/Field/DatePickerField.php b/src/Field/DatePickerField.php index 7a692476c8768911417278fc4b435389065950dc..2ec2160afab95c3475dca1eed2d3682f988a9b01 100644 --- a/src/Field/DatePickerField.php +++ b/src/Field/DatePickerField.php @@ -6,6 +6,7 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class DatePickerField extends BasicField { + public function __construct() { $this->add_class( 'date-picker' ); $this->set_placeholder( 'YYYY-MM-DD' ); diff --git a/src/Field/Header.php b/src/Field/Header.php index a704f4006f50cca89415b439239ee53e722df362..b588b8f3362872393e8ec50316829a1cd9343ca6 100644 --- a/src/Field/Header.php +++ b/src/Field/Header.php @@ -5,7 +5,9 @@ namespace WPDesk\Forms\Field; use WPDesk\Forms\Field; class Header extends NoValueField { + public function __construct() { + parent::__construct(); $this->meta['header_size'] = ''; } diff --git a/src/Field/HiddenField.php b/src/Field/HiddenField.php index e48742c8e01e03109a003b2b55797b3966064e48..ff06bdc0dc4044a92c69e88bd0c03abe361ed7e2 100644 --- a/src/Field/HiddenField.php +++ b/src/Field/HiddenField.php @@ -6,6 +6,7 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class HiddenField extends BasicField { + public function get_type(): string { return 'hidden'; } diff --git a/src/Field/InputEmailField.php b/src/Field/InputEmailField.php index a54ab4af3c8a36fec22aa793a953970826257ad5..36325f3fd9fcd61472448300199d8d64813bc9bd 100644 --- a/src/Field/InputEmailField.php +++ b/src/Field/InputEmailField.php @@ -6,6 +6,7 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\EmailSanitizer; class InputEmailField extends BasicField { + public function get_type(): string { return 'email'; } diff --git a/src/Field/InputNumberField.php b/src/Field/InputNumberField.php index 380da445bde005887fd21ad4854f6da884932a29..84aac99e37786d2729f0c4826c4b113002c50b7e 100644 --- a/src/Field/InputNumberField.php +++ b/src/Field/InputNumberField.php @@ -6,6 +6,7 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class InputNumberField extends BasicField { + public function get_type(): string { return 'number'; } diff --git a/src/Field/InputTextField.php b/src/Field/InputTextField.php index 24b0fa6174307da073a4fc6e5ec1e487acfcfdb0..92f473d24a7fad512ba5866e77442c77914eee67 100644 --- a/src/Field/InputTextField.php +++ b/src/Field/InputTextField.php @@ -6,6 +6,7 @@ use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class InputTextField extends BasicField { + public function get_sanitizer(): Sanitizer { return new TextFieldSanitizer(); } diff --git a/src/Field/MultipleInputTextField.php b/src/Field/MultipleInputTextField.php index 6648e2ab8647a546094bca6ccadc13700d3954a3..cf976fe79f6f1309984fd24886a4187bd76b5cf1 100644 --- a/src/Field/MultipleInputTextField.php +++ b/src/Field/MultipleInputTextField.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms\Field; class MultipleInputTextField extends InputTextField { + public function get_template_name(): string { return 'input-text-multiple'; } diff --git a/src/Field/NoValueField.php b/src/Field/NoValueField.php index 53091700da201919178d7b2317dc7e222df105e2..a84a1a7ad56664f423f9e4861293a5a2edb7103b 100644 --- a/src/Field/NoValueField.php +++ b/src/Field/NoValueField.php @@ -8,6 +8,7 @@ namespace WPDesk\Forms\Field; * @package WPDesk\Forms */ abstract class NoValueField extends BasicField { + public function __construct() { $this->set_name( '' ); } diff --git a/src/Field/Paragraph.php b/src/Field/Paragraph.php index 6e74de31020521deeaaf0e6063a11a9b8b9b64c3..253eea02338e56780ff33b1c64c4d2d2124ede23 100644 --- a/src/Field/Paragraph.php +++ b/src/Field/Paragraph.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms\Field; class Paragraph extends NoValueField { + public function get_template_name(): string { return 'paragraph'; } diff --git a/src/Field/ProductSelect.php b/src/Field/ProductSelect.php index 8d8b5e63620d46d216ce67cede2f1477f02809b4..ff14f8eb1b7b218588bf2dde2fb9538a1bdcc04e 100644 --- a/src/Field/ProductSelect.php +++ b/src/Field/ProductSelect.php @@ -6,6 +6,7 @@ use WPDesk\Forms\Serializer\ProductSelectSerializer; use WPDesk\Forms\Serializer; class ProductSelect extends SelectField { + public function __construct() { $this->set_multiple(); } diff --git a/src/Field/RadioField.php b/src/Field/RadioField.php index 3e20d1b01cd8d07c64d9b96c88a1cc7daf76cccf..cd8fd513fb28d0561224a3b1f24187a147e2fdd7 100644 --- a/src/Field/RadioField.php +++ b/src/Field/RadioField.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms\Field; class RadioField extends BasicField { + public function get_template_name(): string { return 'input-radio'; } 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/SubmitField.php b/src/Field/SubmitField.php index bcb00f782883d2218e3060bb03a21d40ab9b0c82..0decb84d2695c2a820c04aab7d54a3c8cfecb084 100644 --- a/src/Field/SubmitField.php +++ b/src/Field/SubmitField.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms\Field; class SubmitField extends NoValueField { + public function get_template_name(): string { return 'input-submit'; } diff --git a/src/Field/TimepickerField.php b/src/Field/TimepickerField.php index c7c1a5d12ad2c222720f2ce62392151c71fe5590..0d5f0e3191e5f43fbdd49e98415b8f5f49bdf489 100644 --- a/src/Field/TimepickerField.php +++ b/src/Field/TimepickerField.php @@ -6,6 +6,7 @@ use WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer\JsonSerializer; class TimepickerField extends BasicField { + public function has_serializer(): bool { return true; } diff --git a/src/Field/Traits/HtmlAttributes.php b/src/Field/Traits/HtmlAttributes.php index 71eccba6e2c40c78bf9560a79b093394b7806c50..ba01363dbf27e08c196be6ec4cb78f1c24b84690 100644 --- a/src/Field/Traits/HtmlAttributes.php +++ b/src/Field/Traits/HtmlAttributes.php @@ -2,6 +2,9 @@ namespace WPDesk\Forms\Field\Traits; +use WPDesk\Forms\Field; +use WPDesk\Forms\Form; + /** * Implementation of HTML attributes like id, name, action etc. * @@ -9,19 +12,8 @@ namespace WPDesk\Forms\Field\Traits; */ 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. @@ -30,7 +22,7 @@ trait HtmlAttributes { * * @return array<string[]|string|bool> */ - final public function get_attributes( array $except = [ 'name' ] ): array { + final public function get_attributes( array $except = [ 'name', 'class' ] ): array { return array_filter( $this->attributes, static function ( $key ) use ( $except ) { @@ -41,10 +33,10 @@ trait HtmlAttributes { } /** - * @param string $name + * @param string $name * @param string[]|string|bool $value * - * @return \WPDesk\Forms\Field|\WPDesk\Forms\Form + * @return Field|Form */ final public function set_attribute( string $name, $value ) { $this->attributes[ $name ] = $value; @@ -53,7 +45,7 @@ trait HtmlAttributes { } /** - * @return \WPDesk\Forms\Field|\WPDesk\Forms\Form + * @return HtmlAttributes */ final public function unset_attribute( string $name ) { unset( $this->attributes[ $name ] ); @@ -70,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/Field/WooSelect.php b/src/Field/WooSelect.php index ec2719c54f8c72013de838310298c6c90f7271cd..be348f932d60aa37e5540f9b8f7e01273837d49e 100644 --- a/src/Field/WooSelect.php +++ b/src/Field/WooSelect.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms\Field; class WooSelect extends SelectField { + public function __construct() { $this->set_multiple(); $this->add_class( 'wc-enhanced-select' ); diff --git a/src/Field/WyswigField.php b/src/Field/WyswigField.php index a2f41684d3d4441a85fb73f4cbd77773cfc9a962..145908fb3f8cf8be7af652ce1e9292e4235beeec 100644 --- a/src/Field/WyswigField.php +++ b/src/Field/WyswigField.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms\Field; class WyswigField extends BasicField { + public function get_template_name(): string { return 'wyswig'; } diff --git a/src/FieldProvider.php b/src/FieldProvider.php index e693d005d2399f7b87ff6f02e486147bf7758d89..d8d281e43319fc784db71631ddbec2747115c334 100644 --- a/src/FieldProvider.php +++ b/src/FieldProvider.php @@ -6,6 +6,7 @@ namespace WPDesk\Forms; * FieldProvider is owner of FormFields. These fields can be used to render forms and process values. */ interface FieldProvider { + /** * Returns owned fields. * diff --git a/src/FieldRenderer.php b/src/FieldRenderer.php index f60c1b01f0b651a8d17efdca0d1be4e002367604..3e2191e20b80099d501c5e7811f4b3a6edebca34 100644 --- a/src/FieldRenderer.php +++ b/src/FieldRenderer.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms; interface FieldRenderer { + /** @return string|array String or normalized array */ public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ); } diff --git a/src/FieldsDataReceiver.php b/src/FieldsDataReceiver.php index e41743dfc59ef0c9f78d2ad6f7fe454b74c8e13e..961f8974d206a2e647bd55bd1a24d51dc125ef72 100644 --- a/src/FieldsDataReceiver.php +++ b/src/FieldsDataReceiver.php @@ -2,6 +2,8 @@ namespace WPDesk\Forms; +use Psr\Container\ContainerInterface; + /** * Some field owners can receive and process field data. * Probably should be used with FieldProvider interface. @@ -9,10 +11,11 @@ namespace WPDesk\Forms; * @package WPDesk\Forms */ interface FieldsDataReceiver { + /** * Set values corresponding to fields. * * @return void */ - public function update_fields_data( \Psr\Container\ContainerInterface $data ); + public function update_fields_data( ContainerInterface $data ); } diff --git a/src/Form.php b/src/Form.php index 6fc5c0f98f80701d4480d49e6773107323925c9e..b5014ff6368e1b32d1e918d83cb9e914a5223ba2 100644 --- a/src/Form.php +++ b/src/Form.php @@ -10,6 +10,7 @@ use WPDesk\View\Renderer\Renderer; * @package WPDesk\Forms */ interface Form { + /** * For some reason you may want to disable a form. Returns false when disabled. */ 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/src/Sanitizer.php b/src/Sanitizer.php index 3f57eac2413c691d3469cedc909df1ad241f838d..000d3aa9419a2c2e76bebb69bf46fc50d17539c8 100644 --- a/src/Sanitizer.php +++ b/src/Sanitizer.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms; interface Sanitizer { + /** * @param mixed $value * diff --git a/src/Sanitizer/EmailSanitizer.php b/src/Sanitizer/EmailSanitizer.php index 1a51a783627f72a8097d908f8637329cb0ab73be..160ef3c7d20e6f1fdc38fbee9a7be97edd8ca896 100644 --- a/src/Sanitizer/EmailSanitizer.php +++ b/src/Sanitizer/EmailSanitizer.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer; class EmailSanitizer implements Sanitizer { + public function sanitize( $value ): string { return sanitize_email( $value ); } diff --git a/src/Sanitizer/NoSanitize.php b/src/Sanitizer/NoSanitize.php index a3fe4a61160f130cae2821f2bf9faa8352d761c7..2647e663aed006e51bd7c0bf89a5b981d03618dd 100644 --- a/src/Sanitizer/NoSanitize.php +++ b/src/Sanitizer/NoSanitize.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer; class NoSanitize implements Sanitizer { + public function sanitize( $value ) { return $value; } diff --git a/src/Sanitizer/TextFieldSanitizer.php b/src/Sanitizer/TextFieldSanitizer.php index 325c6524e1d252a85908e44bf0909a9953f1123b..c0df5c3b6d6ccfd41e45345f6f6480aa78374765 100644 --- a/src/Sanitizer/TextFieldSanitizer.php +++ b/src/Sanitizer/TextFieldSanitizer.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer; class TextFieldSanitizer implements Sanitizer { + public function sanitize( $value ): string { return sanitize_text_field( $value ); } diff --git a/src/Serializer.php b/src/Serializer.php index 2096b0fe050aebbc6e3c4254a785f7f3b9e7eeb5..ad5f888bd91d800cd3f964ff966ea9d47b378f21 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms; interface Serializer { + /** * @param mixed $value */ diff --git a/src/Serializer/JsonSerializer.php b/src/Serializer/JsonSerializer.php index 9ded0640ffb3cf8663b3c4edbd2eaba2e9b6b42f..0048820b331ff11ec4200a8148cd71d8a1a27a29 100644 --- a/src/Serializer/JsonSerializer.php +++ b/src/Serializer/JsonSerializer.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer; class JsonSerializer implements Serializer { + public function serialize( $value ): string { return (string) json_encode( $value ); } diff --git a/src/Serializer/ProductSelectSerializer.php b/src/Serializer/ProductSelectSerializer.php index 7c4f0e8b97b1500385422f4fccf7d5c82d488eee..dd034f0786c4085f2621148e1249259ba1bfd38b 100644 --- a/src/Serializer/ProductSelectSerializer.php +++ b/src/Serializer/ProductSelectSerializer.php @@ -10,6 +10,7 @@ use WPDesk\Forms\Serializer; * @package WPDesk\Forms\Serializer */ class ProductSelectSerializer implements Serializer { + public function serialize( $value ): string { $products_with_names = []; if ( is_array( $value ) ) { diff --git a/src/Serializer/SerializeSerializer.php b/src/Serializer/SerializeSerializer.php index dde47cce57c3cb22b340f6c7e276ae57b2478dbb..776bd76f22628335d58466c3dc257761ca28b8b4 100644 --- a/src/Serializer/SerializeSerializer.php +++ b/src/Serializer/SerializeSerializer.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer; class SerializeSerializer implements Serializer { + public function serialize( $value ): string { return serialize( $value ); } diff --git a/src/Validator.php b/src/Validator.php index 228182004c03280489b076b126a22dc7542e5de1..9422fdb4bd63013fa5553fc702545dd3380b9ac7 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -3,6 +3,7 @@ namespace WPDesk\Forms; interface Validator { + /** @param mixed $value */ public function is_valid( $value ): bool; diff --git a/src/Validator/NoValidateValidator.php b/src/Validator/NoValidateValidator.php index 04cf59cdaad70077d60e484bcaefa5f09c952916..d3bf32f4dfb919ab53b5f2c5c53657f6ab9a4e71 100644 --- a/src/Validator/NoValidateValidator.php +++ b/src/Validator/NoValidateValidator.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Validator; use WPDesk\Forms\Validator; class NoValidateValidator implements Validator { + public function is_valid( $value ): bool { return true; } diff --git a/src/Validator/RequiredValidator.php b/src/Validator/RequiredValidator.php index 4e78faf0c4cf67dab01646b8381080370aa97c81..5d4851abba5c34a9e000727c672dc94195dba496 100644 --- a/src/Validator/RequiredValidator.php +++ b/src/Validator/RequiredValidator.php @@ -5,6 +5,7 @@ namespace WPDesk\Forms\Validator; use WPDesk\Forms\Validator; class RequiredValidator implements Validator { + public function is_valid( $value ): bool { return $value !== null; } diff --git a/templates/button.php b/templates/button.php index fd95750148a4356e87383c2aaaef789f4cc9a6fe..3068ae66513ef2017dfc3f7164f331c8e5b691eb 100644 --- a/templates/button.php +++ b/templates/button.php @@ -10,26 +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/form-field.php b/templates/form-field.php index 0fd07d5ef880bdee34de0d879e5c05299c846860..09a0fe911492c4a6f8c5713b937f83f77c915aa3 100644 --- a/templates/form-field.php +++ b/templates/form-field.php @@ -11,13 +11,12 @@ <tr valign="top"> <?php if ( $field->has_label() ) : ?> - <?php echo wp_kses_post( $renderer->render( 'form-label', [ 'field' => $field ] ) ); ?> + <?php $renderer->output_render( 'form-label', [ 'field' => $field ] ); ?> <?php endif; ?> <td class="forminp"> <?php - echo wp_kses_post( - $renderer->render( + $renderer->output_render( $template_name, [ 'field' => $field, @@ -25,9 +24,8 @@ 'name_prefix' => $name_prefix, 'value' => $value, ] - ) - ); - ?> + ); + ?> <?php if ( $field->has_description() ) : ?> <p class="description"><?php echo wp_kses_post( $field->get_description() ); ?></p> diff --git a/templates/form-label.php b/templates/form-label.php index 8eeaf286cfcf1622a9e3a73d2b93841e9c495944..9d960fe6adef8f6006108e456d25814dd99c0c0d 100644 --- a/templates/form-label.php +++ b/templates/form-label.php @@ -9,7 +9,7 @@ <th class="titledesc" scope="row"> <label for="<?php echo \esc_attr( $field->get_id() ); ?>"><?php echo \esc_html( $field->get_label() ); ?> <?php if ( $field->has_description_tip() ) : ?> - <?php echo esc_html( wc_help_tip( $field->get_description_tip() ) ); ?> + <?php echo wp_kses_post( wc_help_tip( $field->get_description_tip() ) ); ?> <?php endif ?> </label> </th> diff --git a/templates/input-checkbox.php b/templates/input-checkbox.php index 8474091fbb6d8db0e476178312bb6f471f409e00..55f544f0d13826047a9ff2bcc918c5ba387aaa33 100644 --- a/templates/input-checkbox.php +++ b/templates/input-checkbox.php @@ -10,14 +10,12 @@ ?> <?php -echo wp_kses_post( - $renderer->render( - 'input', - [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, - ] - ) +$renderer->output_render( + 'input', + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] ); diff --git a/templates/input-date-picker.php b/templates/input-date-picker.php index 3caa4c6b576606555c250c489df9f4b9bdd44a76..51e9715717acbc8e41f9cbe8001caf12515072b5 100644 --- a/templates/input-date-picker.php +++ b/templates/input-date-picker.php @@ -7,14 +7,12 @@ * @var string $template_name Real field template. */ -echo wp_kses_post( - $renderer->render( - 'input', - [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, - ] - ) +$renderer->output_render( + 'input', + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] ); diff --git a/templates/input-hidden.php b/templates/input-hidden.php index 638855251f89759e07211a11a1d4d797fc026a58..7668c7fd3b03d3590e49d8a54c832b24c90675d1 100644 --- a/templates/input-hidden.php +++ b/templates/input-hidden.php @@ -9,14 +9,12 @@ ?> <?php -echo wp_kses_post( - $renderer->render( - 'input', - [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, - ] - ) +$renderer->output_render( + 'input', + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] ); diff --git a/templates/input-image.php b/templates/input-image.php index 5111c4c2c5af3cc159eb7e44597f4b46a5a5bf22..ce099e45930db93421a05db736286b4ddf9f7b47 100644 --- a/templates/input-image.php +++ b/templates/input-image.php @@ -13,17 +13,25 @@ $media_container_id = 'media_' . sanitize_key( $field->get_id() ); id="<?php echo \esc_attr( $field->get_id() ); ?>"/> <div class="custom-img-container"> <?php if ( $value ) : ?> - <img src="<?php echo \esc_url( $value ) ?>" alt="" width="100"/> + <img src="<?php echo \esc_url( $value ); ?>" alt="" width="100"/> <?php endif; ?> - </div> - <p class="hide-if-no-js"> - <a class="upload-custom-img <?php if ( $value ): ?>hidden<?php endif ?>" href="<?php echo \esc_url( $value ) ?>"> - <?php \esc_html_e( 'Set image', 'wp-forms' ) ?> - </a> - <a class="delete-custom-img <?php if ( ! $value ): ?>hidden<?php endif ?>" href="#"> - <?php \esc_html_e( 'Remove image', 'wp-forms' ) ?> - </a> - </p> + </div> + <p class="hide-if-no-js"> + <a class="upload-custom-img + <?php + if ( $value ) : + ?> + hidden<?php endif ?>" href="<?php echo \esc_url( $value ); ?>"> + <?php \esc_html_e( 'Set image', 'wp-forms' ); ?> + </a> + <a class="delete-custom-img + <?php + if ( ! $value ) : + ?> + hidden<?php endif ?>" href="#"> + <?php \esc_html_e( 'Remove image', 'wp-forms' ); ?> + </a> + </p> </div> <script> jQuery( function ( $ ) { diff --git a/templates/input-number.php b/templates/input-number.php index 3caa4c6b576606555c250c489df9f4b9bdd44a76..51e9715717acbc8e41f9cbe8001caf12515072b5 100644 --- a/templates/input-number.php +++ b/templates/input-number.php @@ -7,14 +7,12 @@ * @var string $template_name Real field template. */ -echo wp_kses_post( - $renderer->render( - 'input', - [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, - ] - ) +$renderer->output_render( + 'input', + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] ); diff --git a/templates/input-radio.php b/templates/input-radio.php index 638855251f89759e07211a11a1d4d797fc026a58..7668c7fd3b03d3590e49d8a54c832b24c90675d1 100644 --- a/templates/input-radio.php +++ b/templates/input-radio.php @@ -9,14 +9,12 @@ ?> <?php -echo wp_kses_post( - $renderer->render( - 'input', - [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, - ] - ) +$renderer->output_render( + 'input', + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] ); 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-text.php b/templates/input-text.php index 3caa4c6b576606555c250c489df9f4b9bdd44a76..51e9715717acbc8e41f9cbe8001caf12515072b5 100644 --- a/templates/input-text.php +++ b/templates/input-text.php @@ -7,14 +7,12 @@ * @var string $template_name Real field template. */ -echo wp_kses_post( - $renderer->render( - 'input', - [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, - ] - ) +$renderer->output_render( + 'input', + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] ); 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() ) : ?>