diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000000000000000000000000000000000..3fe55c887ecc46752a9b1f64b0f31e256f647207 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,91 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# WordPress Coding Standards +# https://make.wordpress.org/core/handbook/coding-standards/ + +# Configuration for PhpStorm + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = tab +indent_size = 4 +tab_width = 4 +ij_continuation_indent_size = 4 + +[*.scss] +ij_scss_enforce_quotes_on_format = true +ij_scss_hex_color_long_format = true +ij_scss_hex_color_lower_case = true + +[{*.cjs,*.js}] +indent_style = space +indent_size = 2 +ij_javascript_do_while_brace_force = always +ij_javascript_if_brace_force = always +ij_javascript_import_sort_module_name = true +ij_javascript_reformat_c_style_comments = true +ij_javascript_space_after_unary_not = true +ij_javascript_space_before_unary_not = true +ij_javascript_spaces_within_array_initializer_brackets = true +ij_javascript_spaces_within_brackets = true +ij_javascript_spaces_within_catch_parentheses = true +ij_javascript_spaces_within_for_parentheses = true +ij_javascript_spaces_within_if_parentheses = true +ij_javascript_spaces_within_imports = true +ij_javascript_spaces_within_interpolation_expressions = true +ij_javascript_spaces_within_method_call_parentheses = true +ij_javascript_spaces_within_method_parentheses = true +ij_javascript_spaces_within_object_literal_braces = true +ij_javascript_spaces_within_parentheses = true +ij_javascript_spaces_within_switch_parentheses = true +ij_javascript_spaces_within_while_parentheses = true +ij_javascript_while_brace_force = always + +[{*.ctp,*.hphp,*.inc,*.module,*.php,*.php4,*.php5,*.phtml}] +ij_php_align_assignments = true +ij_php_align_class_constants = true +ij_php_align_key_value_pairs = true +ij_php_align_phpdoc_comments = true +ij_php_align_phpdoc_param_names = true +ij_php_class_brace_style = end_of_line +ij_php_comma_after_last_array_element = true +ij_php_else_if_style = combine +ij_php_force_short_declaration_array_style = true +ij_php_getters_setters_naming_style = snake_case +ij_php_if_brace_force = always +ij_php_lower_case_boolean_const = true +ij_php_lower_case_null_const = true +ij_php_method_brace_style = end_of_line +ij_php_phpdoc_blank_line_before_tags = true +ij_php_phpdoc_blank_lines_around_parameters = true +ij_php_phpdoc_keep_blank_lines = false +ij_php_phpdoc_wrap_long_lines = true +ij_php_space_after_type_cast = true +ij_php_space_after_unary_not = true +ij_php_space_before_unary_not = true +ij_php_spaces_around_var_within_brackets = true +ij_php_spaces_within_array_initializer_braces = true +ij_php_spaces_within_catch_parentheses = true +ij_php_spaces_within_for_parentheses = true +ij_php_spaces_within_if_parentheses = true +ij_php_spaces_within_method_call_parentheses = true +ij_php_spaces_within_method_parentheses = true +ij_php_spaces_within_parentheses = true +ij_php_spaces_within_switch_parentheses = true +ij_php_spaces_within_while_parentheses = true +ij_php_ternary_operation_signs_on_next_line = true +ij_php_variable_naming_style = snake_case + +[{*.htm,*.html,*.ng,*.sht,*.shtm,*.shtml}] +ij_html_add_new_line_before_tags = div,p,a +ij_html_align_attributes = false +ij_html_do_not_indent_children_of_tags = html,thead,tbody,tfoot +ij_html_new_line_after_last_attribute = when multiline +ij_html_space_inside_empty_tag = true +ij_html_uniform_ident = true diff --git a/.env.testing b/.env.testing new file mode 100644 index 0000000000000000000000000000000000000000..3aa2f5f455a4ead5271cb5c983ec33844801b86d --- /dev/null +++ b/.env.testing @@ -0,0 +1,17 @@ +WP_ROOT_FOLDER="${APACHE_DOCUMENT_ROOT}" +TEST_SITE_WP_ADMIN_PATH="/wp-admin" +TEST_SITE_DB_NAME="wptest" +TEST_SITE_DB_HOST="mysqltests" +TEST_SITE_DB_USER="mysql" +TEST_SITE_DB_PASSWORD="mysql" +TEST_SITE_TABLE_PREFIX="wp_" +TEST_DB_NAME="wptest" +TEST_DB_HOST="mysqltests" +TEST_DB_USER="mysql" +TEST_DB_PASSWORD="mysql" +TEST_TABLE_PREFIX="wp_" +TEST_SITE_WP_URL="http://${WOOTESTS_IP}" +TEST_SITE_WP_DOMAIN="${WOOTESTS_IP}" +TEST_SITE_ADMIN_EMAIL="grola@seostudio.pl" +TEST_SITE_ADMIN_USERNAME="admin" +TEST_SITE_ADMIN_PASSWORD="admin" diff --git a/.gitignore b/.gitignore index 4b86223d6cae8ddb86d7cc3d12e0fcca43717f54..16e7a8c8b2234275f15fcd36090ce9e2badf7e36 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea composer.lock build-coverage -swagger \ No newline at end of file +swagger +.phpcs-cache diff --git a/composer.json b/composer.json index afcddb9856f7c0f001f1b88f1f0839929ec10a7d..8150bcff8112a4b9fb1e678d405162de43077a7c 100644 --- a/composer.json +++ b/composer.json @@ -1,52 +1,53 @@ { - "name": "wpdesk/wp-forms", - "description": "WP Forms", - "authors": [ - { - "name": "Marcin", - "email": "marcin@wpdesk.pl" - }, - { - "name": "Krzysiek", - "email": "krzysiek@wpdesk.pl" - }, - { - "name": "Piotr Potrebka", - "email": "piotr.potrebka@wpdesk.pl" - } - ], - "require": { - "php": ">=7.0", - "ext-curl": "*", - "ext-json": "*", - "wpdesk/wp-persistence": "^2.0|^3.0", - "wpdesk/wp-view": "^1.1" - }, - "require-dev": { - "phpunit/phpunit": "<7", - "wp-coding-standards/wpcs": "^0.14.1", - "squizlabs/php_codesniffer": "^3.0.2", - "mockery/mockery": "*", - "10up/wp_mock": "*" - }, - "autoload": { - "psr-4": { - "WPDesk\\Forms\\": "src/" - } - }, - "autoload-dev": { - }, - "extra": { - "text-domain": "wp-forms", - "translations-folder": "lang", - "po-files": { - "pl_PL": "pl_PL.po" - } - }, - "scripts": { - "phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never", - "phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage", - "phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never", - "phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage" - } + "name": "wpdesk/wp-forms", + "description": "WP Forms", + "authors": [ + { + "name": "Marcin", + "email": "marcin@wpdesk.pl" + }, + { + "name": "Krzysiek", + "email": "krzysiek@wpdesk.pl" + }, + { + "name": "Piotr Potrebka", + "email": "piotr.potrebka@wpdesk.pl" + } + ], + "require": { + "php": ">=7.0", + "ext-curl": "*", + "ext-json": "*", + "wpdesk/wp-persistence": "^2.0|^3.0", + "wpdesk/wp-view": "^1.1" + }, + "require-dev": { + "phpunit/phpunit": "<7", + "phpstan/phpstan": "^0.12", + "wpdesk/wp-code-sniffer": "^1.2.3", + "10up/wp_mock": "*", + "szepeviktor/phpstan-wordpress": "^0.7.7", + "phpstan/extension-installer": "^1.1" + }, + "autoload": { + "psr-4": { + "WPDesk\\Forms\\": "src/" + } + }, + "autoload-dev": { + }, + "extra": { + "text-domain": "wp-forms", + "translations-folder": "lang", + "po-files": { + "pl_PL": "pl_PL.po" + } + }, + "scripts": { + "phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never", + "phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage", + "phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never", + "phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage" + } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000000000000000000000000000000000000..6ef3f27172956c049b0c95b9905528c3b5216dc1 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,48 @@ +<?xml version="1.0"?> +<ruleset name="WordPress Coding Standards for WP Desk Plugin"> + + <!-- + ############################################################################# + COMMAND LINE ARGUMENTS + https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml + ############################################################################# + --> + + <!-- Scan all files. --> + <file>./src</file> + <file>./templates</file> + + <!-- Only check PHP files. --> + <arg name="extensions" value="php"/> + + <!-- Show progress, show the error codes for each message (source). --> + <arg value="sp"/> + + <!-- Check up to 8 files simultaneously. --> + <arg name="parallel" value="8"/> + + <!-- Cache outcomes for better performance. Remember to add the file to .gitignore. --> + <arg name="cache" value="./.phpcs-cache"/> + + <!-- + ############################################################################# + USE THE WPDeskCS RULESET + ############################################################################# + --> + + <!-- Define plugin text domain for i18n. --> + <config name="text_domain" value="shopmagic-for-woocommerce"/> + + <!-- This value should be aligned with WordPress support version declared in plugin header --> + <config name="minimum_supported_wp_version" value="5.0"/> + + <!-- Set value aligned with supported PHP Version for PHPCompatibilityWP check. --> + <config name="testVersion" value="7.0-"/> + + <rule ref="WPDeskPlugin"/> + + <rule ref="Squiz.Commenting.ClassComment.Missing"> + <exclude name="Squiz.Commenting.ClassComment.Missing"/> + </rule> + +</ruleset> diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000000000000000000000000000000000000..200d200a6b18db3d5abbcd4e9ee9edbd96924c25 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + level: max + paths: + - src/ diff --git a/phpunit-integration.xml b/phpunit-integration.xml index c78958bbc0d69e1ecbc39361c73862ad29b6e373..8341e36c040a27097cd60e3e4d3330f7c5da9298 100644 --- a/phpunit-integration.xml +++ b/phpunit-integration.xml @@ -13,11 +13,5 @@ </whitelist> </filter> - <logging> - <log type="junit" target="build-coverage/report.junit.xml"/> - <log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/> - <log type="coverage-text" target="build-coverage/coverage.txt"/> - <log type="coverage-clover" target="build-coverage/clover.xml"/> - </logging> </phpunit> \ No newline at end of file diff --git a/src/ContainerForm.php b/src/ContainerForm.php index 1d056b6cacd598007ecc177b29be31b4d7770ed0..05107067d10d357fa2a4b8b6a73de9a9333de828 100644 --- a/src/ContainerForm.php +++ b/src/ContainerForm.php @@ -2,7 +2,6 @@ namespace WPDesk\Forms; - use Psr\Container\ContainerInterface; use WPDesk\Persistence\PersistentContainer; @@ -12,18 +11,12 @@ use WPDesk\Persistence\PersistentContainer; * @package WPDesk\Forms */ interface ContainerForm { - /** - * @param ContainerInterface $data - * - * @return void - */ - public function set_data( $data ); + /** @return void */ + public function set_data( ContainerInterface $data ); /** * Put data from form into a container. * - * @param PersistentContainer $container Target container. - * * @return void */ public function put_data( PersistentContainer $container ); diff --git a/src/Escaper.php b/src/Escaper.php index cb20d5824a44f3890c8c48007c9b0d6bb04be1c4..8c460dd16b8dee8f060777b542f1aa933456b274 100644 --- a/src/Escaper.php +++ b/src/Escaper.php @@ -3,10 +3,6 @@ namespace WPDesk\Forms; interface Escaper { - /** - * @param mixed $value - * - * @return string - */ - public function escape( $value ); + /** @param mixed $value */ + public function escape( $value ): string; } diff --git a/src/Field.php b/src/Field.php index 4205df4652abc3089ee641fef09bfcec9b88a65d..2a6569e5e01070f82b912b0cc811b67348cf4730 100644 --- a/src/Field.php +++ b/src/Field.php @@ -2,8 +2,6 @@ namespace WPDesk\Forms; -use WPDesk\Forms\Field\BasicField; - /** * The idea is that from the moment the factory returns this interface it's values cannot be changed. * And that is why here are only the getters. @@ -13,157 +11,88 @@ use WPDesk\Forms\Field\BasicField; * @package WPDesk\Forms */ interface Field { - /** @return string */ - public function get_name(); + public function get_name(): string; /** @return mixed */ public function get_default_value(); - /** @return string */ - public function get_template_name(); + public function get_template_name(): string; - /** - * When this field is used on form this field will force it's own template. - * - * return bool - */ - public function should_override_form_template(); + /** When this field is used on form this field will force it's own template. */ + public function should_override_form_template(): bool; - /** - * HTML label. - * - * @return string - */ - public function get_label(); + /** HTML label. */ + public function get_label(): string; - /** bool */ - public function has_label(); + public function has_label(): bool; - /** - * Description for field. It can be shown near the field. - * - * @return string - */ - public function get_description(); + public function get_description(): string; - /** - * Additional field description that should be shown in optional hover tip. - * - * @return string - */ - public function get_description_tip(); + /** Additional field description that should be shown in optional hover tip. */ + public function get_description_tip(): string; - /** @return bool */ - public function has_description_tip(); + public function has_description_tip(): bool; - /** @return bool */ - public function has_description(); + public function has_description(): bool; - /** - * @return bool - */ - public function is_readonly(); + public function is_readonly(): bool; - /** @return bool */ - public function is_disabled(); + public function is_disabled(): bool; - /** @return string */ - public function get_id(); + public function get_id(): string; - /** @bool */ - public function is_required(); + public function is_required(): bool; - /** @return bool */ - public function has_placeholder(); + public function has_placeholder(): bool; - /** @return string */ - public function get_placeholder(); + public function get_placeholder(): string; /** * @param string[] $except * * @return string[] name->value */ - public function get_attributes( $except = [] ); + public function get_attributes( array $except = [] ): array; /** - * @param string $name - * @param string $default + * @param string $name + * @param ?string $default * * @return string */ - public function get_attribute( $name, $default = null ); + public function get_attribute( string $name, $default = null ): string; - /** @return bool */ - public function is_attribute_set( $name ); + public function is_attribute_set( string $name ): bool; - /** - * @param string $name - * - * @return string - */ - public function get_meta_value( $name ); + public function get_meta_value( string $name ): string; - /** @return bool */ - public function is_meta_value_set( $name ); + public function is_meta_value_set( string $name ): bool; - /** - * @return string - */ - public function get_classes(); + public function get_classes(): string; - /** bool */ - public function has_classes(); + public function has_classes(): bool; - /** @return bool */ - public function is_class_set( $name ); + public function is_class_set( string $name ): bool; - /** bool */ - public function has_data(); + public function has_data(): bool; - /** - * @return array - */ - public function get_data(); + /** @return array<string,int> */ + public function get_data(): array; - /** - * @param string $data_name - * @param string $data_value - * - * @return $this - */ - public function add_data( $data_name, $data_value ); + public function add_data( string $data_name, string $data_value ): self; - /** - * @param string $data_name - * - * @return $this - */ - public function unset_data( $data_name ); + public function unset_data( string $data_name ): self; - /** - * @return mixed - */ + /** @return mixed */ public function get_possible_values(); - /** - * @return bool - */ - public function is_multiple(); + public function is_multiple(): bool; - /** - * @return Validator - */ - public function get_validator(); + public function get_validator(): Validator; - /** - * @return Sanitizer - */ - public function get_sanitizer(); + public function get_sanitizer(): Sanitizer; - /** @return Serializer */ - public function get_serializer(); + public function get_serializer(): Serializer; - /** @return int */ - public function get_priority(); + public function get_priority(): int; } diff --git a/src/Field/BasicField.php b/src/Field/BasicField.php index 54ef3cc1622524a8645ccd1f5ad49bbe467a552b..ee7f144cf2cf4518cedc9522a2e71bb44d2f0577 100644 --- a/src/Field/BasicField.php +++ b/src/Field/BasicField.php @@ -4,9 +4,11 @@ namespace WPDesk\Forms\Field; use WPDesk\Forms\Field; use WPDesk\Forms\Form\FormWithFields; +use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\NoSanitize; use WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer\NoSerialize; +use WPDesk\Forms\Validator; use WPDesk\Forms\Validator\ChainValidator; use WPDesk\Forms\Validator\RequiredValidator; @@ -19,210 +21,165 @@ use WPDesk\Forms\Validator\RequiredValidator; abstract class BasicField implements Field { use Field\Traits\HtmlAttributes; - /** @var array[] */ + const DEFAULT_PRIORITY = 10; + + /** @var array<string,int,bool> */ protected $meta; + /** @var string */ protected $default_value; public function __construct() { - $this->meta['class'] = []; - $this->meta['priority'] = 10; + $this->meta['class'] = []; + $this->meta['priority'] = self::DEFAULT_PRIORITY; } - public function get_label() { + public function get_label(): string { return $this->meta['label']; } - /** - * @param string $value - * - * @return $this - */ - public function set_label( $value ) { + public function set_label( string $value ): self { $this->meta['label'] = $value; return $this; } - public function get_description_tip() { + public function get_description_tip(): string { return $this->meta['description_tip']; } - public function has_description_tip() { + public function has_description_tip(): bool { return isset( $this->meta['description_tip'] ); } - public function should_override_form_template() { - return isset( $this->attributes['overrite_template'] ) ? $this->attributes['overrite_template'] : false; + public function should_override_form_template(): bool { + return $this->attributes['overrite_template'] ?? false; } - public function get_description() { + public function get_description(): string { return $this->meta['description']; } - public function has_label() { + public function has_label(): bool { return isset( $this->meta['label'] ); } - public function has_description() { + public function has_description(): bool { return isset( $this->meta['description'] ); } - public function set_description( $value ) { + public function set_description( string $value ): self { $this->meta['description'] = $value; return $this; } - public function set_description_tip( $value ) { + public function set_description_tip( string $value ): self { $this->meta['description_tip'] = $value; return $this; } - /** - * @return array - * - * @deprecated not sure if needed. TODO: Check later. - */ - public function get_type() { + public function get_type(): string { return $this->attributes['type']; } - /** - * @param string $value - * - * @return $this - */ - public function set_placeholder( $value ) { + public function set_placeholder( string $value ): self { $this->meta['placeholder'] = $value; return $this; } - public function has_placeholder() { + public function has_placeholder(): bool { return isset( $this->meta['placeholder'] ); } - public function get_placeholder() { + public function get_placeholder(): string { return $this->meta['placeholder']; } - /** - * @param string $name - * - * @return $this - */ - public function set_name( $name ) { + public function set_name( string $name ): self { $this->attributes['name'] = $name; return $this; } - public function get_meta_value( $name ) { + public function get_meta_value( string $name ): string { return $this->meta[ $name ]; } - public function get_classes() { + public function get_classes(): string { return implode( ' ', $this->meta['class'] ); } - public function has_classes() { + public function has_classes(): bool { return ! empty( $this->meta['class'] ); } - public function has_data() { + public function has_data(): bool { return ! empty( $this->meta['data'] ); } - /** - * @return array - */ - public function get_data() { - return empty( $this->meta['data'] ) ? [] : $this->meta['data']; + public function get_data(): array { + return $this->meta['data'] ?: []; } public function get_possible_values() { return isset( $this->meta['possible_values'] ) ? $this->meta['possible_values'] : []; } - public function get_id() { - return isset( $this->attributes['id'] ) ? $this->attributes['id'] : sanitize_title( $this->get_name() ); + public function get_id(): string { + return $this->attributes['id'] ?? sanitize_title( $this->get_name() ); } - public function get_name() { + public function get_name(): string { return $this->attributes['name']; } - public function is_multiple() { - return isset( $this->attributes['multiple'] ) ? $this->attributes['multiple'] : false; + public function is_multiple(): bool { + return $this->attributes['multiple'] ?? false; } - /** - * @return $this - */ - public function set_disabled() { + public function set_disabled(): self { $this->attributes['disabled'] = true; return $this; } - public function is_disabled() { - return isset( $this->attributes['disabled'] ) ? $this->attributes['disabled'] : false; + public function is_disabled(): bool { + return $this->attributes['disabled'] ?? false; } - /** - * @return $this - */ - public function set_readonly() { + public function set_readonly(): self { $this->attributes['readonly'] = true; return $this; } - public function is_readonly() { - return isset( $this->attributes['readonly'] ) ? $this->attributes['readonly'] : false; + public function is_readonly(): bool { + return $this->attributes['readonly'] ?? false; } - /** - * @return $this - */ - public function set_required() { + public function set_required(): self { $this->meta['required'] = true; return $this; } - /** - * @param string $class_name - * - * @return $this - */ - public function add_class( $class_name ) { + public function add_class( string $class_name ): self { $this->meta['class'][ $class_name ] = $class_name; return $this; } - /** - * @param string $class_name - * - * @return $this - */ - public function unset_class( $class_name ) { + public function unset_class( string $class_name ): self { unset( $this->meta['class'][ $class_name ] ); return $this; } - /** - * @param string $data_name - * @param string $data_value - * - * @return $this - */ - public function add_data( $data_name, $data_value ) { - if ( !isset( $this->meta['data'] ) ) { + public function add_data( string $data_name, string $data_value ): self { + if ( ! isset( $this->meta['data'] ) ) { $this->meta['data'] = []; } $this->meta['data'][ $data_name ] = $data_value; @@ -230,44 +187,31 @@ abstract class BasicField implements Field { return $this; } - /** - * @param string $data_name - * - * @return $this - */ - public function unset_data( $data_name ) { + public function unset_data( string $data_name ): self { unset( $this->meta['data'][ $data_name ] ); return $this; } - public function is_meta_value_set( $name ) { + public function is_meta_value_set( string $name ): bool { return isset( $this->meta[ $name ] ); } - public function is_class_set( $name ) { + public function is_class_set( string $name ): bool { return isset( $this->meta['class'][ $name ] ); } - public function get_default_value() { + public function get_default_value(): string { return $this->default_value; } - /** - * @param string $value - * - * @return $this - */ - public function set_default_value( $value ) { + public function set_default_value( string $value ): self { $this->default_value = $value; return $this; } - /** - * @return ChainValidator - */ - public function get_validator() { + public function get_validator(): Validator { $chain = new ChainValidator(); if ( $this->is_required() ) { $chain->attach( new RequiredValidator() ); @@ -276,18 +220,15 @@ abstract class BasicField implements Field { return $chain; } - public function is_required() { - return isset( $this->meta['required'] ) ? $this->meta['required'] : false; + public function is_required(): bool { + return $this->meta['required'] ?? false; } - public function get_sanitizer() { + public function get_sanitizer(): Sanitizer { return new NoSanitize(); } - /** - * @return Serializer - */ - public function get_serializer() { + public function get_serializer(): Serializer { if ( isset( $this->meta['serializer'] ) && $this->meta['serializer'] instanceof Serializer ) { return $this->meta['serializer']; } @@ -295,7 +236,7 @@ abstract class BasicField implements Field { return new NoSerialize(); } - public function set_serializer( Serializer $serializer ) { + public function set_serializer( Serializer $serializer ): self { $this->meta['serializer'] = $serializer; return $this; diff --git a/src/Field/ButtonField.php b/src/Field/ButtonField.php index 86312d03a1bab763609441410786f901be1290c7..f0a40238eef5c05f978f31ec899a005903785dac 100644 --- a/src/Field/ButtonField.php +++ b/src/Field/ButtonField.php @@ -2,14 +2,12 @@ namespace WPDesk\Forms\Field; -class ButtonField extends NoValueField -{ - public function get_template_name() - { - return 'button'; - } - public function get_type() - { - return 'button'; - } +class ButtonField extends NoValueField { + + public function get_template_name(): string { + return 'button'; + } + public function get_type(): string { + return 'button'; + } } diff --git a/src/Field/CheckboxField.php b/src/Field/CheckboxField.php index 705c4fe2f97e7f895ef1c58b4534c1465357a2ee..e8cc29fde23d0f66a4e21f391c7ed2552c75652e 100644 --- a/src/Field/CheckboxField.php +++ b/src/Field/CheckboxField.php @@ -2,9 +2,8 @@ namespace WPDesk\Forms\Field; - class CheckboxField extends BasicField { - const VALUE_TRUE = 'yes'; + const VALUE_TRUE = 'yes'; const VALUE_FALSE = 'no'; public function __construct() { @@ -12,21 +11,21 @@ class CheckboxField extends BasicField { $this->set_attribute( 'type', 'checkbox' ); } - public function get_template_name() { + public function get_template_name(): string { return 'input-checkbox'; } - public function get_sublabel() { + public function get_sublabel(): string { return $this->meta['sublabel']; } - public function set_sublabel( $value ) { + public function set_sublabel( string $value ): self { $this->meta['sublabel'] = $value; return $this; } - public function has_sublabel() { + public function has_sublabel(): bool { return isset( $this->meta['sublabel'] ); } } diff --git a/src/Field/DatePickerField.php b/src/Field/DatePickerField.php index 8b50df8927a706972fa740cb61f19ce7af7eb0dd..7060d9097e1805654f84345133cd5c091622c2ba 100644 --- a/src/Field/DatePickerField.php +++ b/src/Field/DatePickerField.php @@ -2,22 +2,23 @@ namespace WPDesk\Forms\Field; +use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class DatePickerField extends BasicField { public function __construct() { parent::__construct(); $this->set_default_value( '' ); - $this->add_class('date-picker'); - $this->set_placeholder('YYYY-MM-DD'); + $this->add_class( 'date-picker' ); + $this->set_placeholder( 'YYYY-MM-DD' ); $this->set_attribute( 'type', 'text' ); } - public function get_sanitizer() { + public function get_sanitizer(): Sanitizer { return new TextFieldSanitizer(); } - public function get_template_name() { + public function get_template_name(): string { return 'input-date-picker'; } } diff --git a/src/Field/Header.php b/src/Field/Header.php index 491d99c9e86981ff619da27f21786cd2830ee11e..fa86700d47cc77952409685507eb31609ec291a6 100644 --- a/src/Field/Header.php +++ b/src/Field/Header.php @@ -8,15 +8,15 @@ class Header extends NoValueField { $this->meta['header_size'] = ''; } - public function get_template_name() { + public function get_template_name(): string { return 'header'; } - public function should_override_form_template() { + public function should_override_form_template(): bool { return true; } - public function set_header_size( $value ) { + public function set_header_size( int $value ): self { $this->meta['header_size'] = $value; return $this; diff --git a/src/Field/HiddenField.php b/src/Field/HiddenField.php index 51b78080d7e9533b28f8ea54c362158971633223..9c3856ce13f415c8f0f5adffb728714373da98c3 100644 --- a/src/Field/HiddenField.php +++ b/src/Field/HiddenField.php @@ -2,6 +2,7 @@ namespace WPDesk\Forms\Field; +use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class HiddenField extends BasicField { @@ -11,11 +12,11 @@ class HiddenField extends BasicField { $this->set_attribute( 'type', 'hidden' ); } - public function get_sanitizer() { + public function get_sanitizer(): Sanitizer { return new TextFieldSanitizer(); } - public function get_template_name() { + public function get_template_name(): string { return 'input-hidden'; } } diff --git a/src/Field/ImageInputField.php b/src/Field/ImageInputField.php index 95d4bb68f3492b3139f2ebb56a3d4c379ad4d5c0..74c84101b7ff0c8994ab19e4caa203496377d74b 100644 --- a/src/Field/ImageInputField.php +++ b/src/Field/ImageInputField.php @@ -10,10 +10,7 @@ class ImageInputField extends BasicField { $this->set_attribute( 'type', 'text' ); } - /** - * @return string - */ - public function get_template_name() { + public function get_template_name(): string { return 'input-image'; } } diff --git a/src/Field/InputNumberField.php b/src/Field/InputNumberField.php index a563cd29fe80b418c0e1aee195883d512300b773..9ee74059c414fa025f85495277c11b1421e03623 100644 --- a/src/Field/InputNumberField.php +++ b/src/Field/InputNumberField.php @@ -2,6 +2,7 @@ namespace WPDesk\Forms\Field; +use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class InputNumberField extends BasicField { @@ -11,11 +12,11 @@ class InputNumberField extends BasicField { $this->set_attribute( 'type', 'number' ); } - public function get_sanitizer() { + public function get_sanitizer(): Sanitizer { return new TextFieldSanitizer(); } - public function get_template_name() { + public function get_template_name(): string { return 'input-number'; } } diff --git a/src/Field/InputTextField.php b/src/Field/InputTextField.php index 2070412379ea091eeb87a4ca9b3994bbe7acaf9b..7ca6a96b43d66d75167ea382a560a37404bb8979 100644 --- a/src/Field/InputTextField.php +++ b/src/Field/InputTextField.php @@ -2,6 +2,7 @@ namespace WPDesk\Forms\Field; +use WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer\TextFieldSanitizer; class InputTextField extends BasicField { @@ -11,11 +12,11 @@ class InputTextField extends BasicField { $this->set_attribute( 'type', 'text' ); } - public function get_sanitizer() { + public function get_sanitizer(): Sanitizer { return new TextFieldSanitizer(); } - public function get_template_name() { + public function get_template_name(): string { return 'input-text'; } } diff --git a/src/Field/MultipleInputTextField.php b/src/Field/MultipleInputTextField.php index fab83989cda40645ab355f631dc7ec1665c61185..cf976fe79f6f1309984fd24886a4187bd76b5cf1 100644 --- a/src/Field/MultipleInputTextField.php +++ b/src/Field/MultipleInputTextField.php @@ -4,10 +4,7 @@ namespace WPDesk\Forms\Field; class MultipleInputTextField extends InputTextField { - /** - * @return string - */ - public function get_template_name() { + public function get_template_name(): string { return 'input-text-multiple'; } } diff --git a/src/Field/NoOnceField.php b/src/Field/NoOnceField.php index 0b6eeafd10f30358cfd7f29e619a1b9d6fa28d7f..6419a3796a481839cdd0abf3c4f455ef3ebc346f 100644 --- a/src/Field/NoOnceField.php +++ b/src/Field/NoOnceField.php @@ -2,6 +2,7 @@ namespace WPDesk\Forms\Field; +use WPDesk\Forms\Validator; use WPDesk\Forms\Validator\NonceValidator; class NoOnceField extends BasicField { @@ -11,11 +12,11 @@ class NoOnceField extends BasicField { $this->meta['action'] = $action_name; } - public function get_validator() { + public function get_validator(): Validator { return new NonceValidator( $this->get_meta_value( 'action' ) ); } - public function get_template_name() { + public function get_template_name(): string { return 'noonce'; } } diff --git a/src/Field/NoValueField.php b/src/Field/NoValueField.php index 830f47ef4fda51d95d0260afe23e3f232d0ef4e2..90bbecb1d4c622e128c3b95c8d7a934ea2e6598d 100644 --- a/src/Field/NoValueField.php +++ b/src/Field/NoValueField.php @@ -8,7 +8,7 @@ namespace WPDesk\Forms\Field; * @package WPDesk\Forms */ abstract class NoValueField extends BasicField { - public function get_name() { + public function get_name(): string { return ''; } } diff --git a/src/Field/Paragraph.php b/src/Field/Paragraph.php index c62cb6b844d1846636e8aeaff8d2e1b69798ac03..6e74de31020521deeaaf0e6063a11a9b8b9b64c3 100644 --- a/src/Field/Paragraph.php +++ b/src/Field/Paragraph.php @@ -3,11 +3,11 @@ namespace WPDesk\Forms\Field; class Paragraph extends NoValueField { - public function get_template_name() { + public function get_template_name(): string { return 'paragraph'; } - public function should_override_form_template() { + public function should_override_form_template(): bool { return true; } } diff --git a/src/Field/ProductSelect.php b/src/Field/ProductSelect.php index 19b14aebc1223e6db73bf34a1a1811ce7560aa3c..896030424bde4622d2ff35b29e9a9d5f2a198b74 100644 --- a/src/Field/ProductSelect.php +++ b/src/Field/ProductSelect.php @@ -8,7 +8,7 @@ class ProductSelect extends SelectField { $this->set_multiple(); } - public function get_template_name() { + public function get_template_name(): string { return 'product-select'; } } diff --git a/src/Field/RadioField.php b/src/Field/RadioField.php index 69630bf430c7ba352948f44dedb8b340b5df67e4..3e20d1b01cd8d07c64d9b96c88a1cc7daf76cccf 100644 --- a/src/Field/RadioField.php +++ b/src/Field/RadioField.php @@ -3,7 +3,7 @@ namespace WPDesk\Forms\Field; class RadioField extends BasicField { - public function get_template_name() { + public function get_template_name(): string { return 'input-radio'; } } diff --git a/src/Field/SelectField.php b/src/Field/SelectField.php index 0578379dc7d68c781025955f815eee3018db3d47..bead3bb95ac07917388e0dfff4a598b09614d9ad 100644 --- a/src/Field/SelectField.php +++ b/src/Field/SelectField.php @@ -4,17 +4,18 @@ namespace WPDesk\Forms\Field; class SelectField extends BasicField { - public function get_template_name() { + public function get_template_name(): string { return 'select'; } - public function set_options( $options ) { + /** @param string[] $options */ + public function set_options( array $options ): self { $this->meta['possible_values'] = $options; return $this; } - public function set_multiple() { + public function set_multiple(): self { $this->attributes['multiple'] = true; return $this; diff --git a/src/Field/SubmitField.php b/src/Field/SubmitField.php index c8e1f799d895ae49a10712439043a2b0e6a223e5..bcb00f782883d2218e3060bb03a21d40ab9b0c82 100644 --- a/src/Field/SubmitField.php +++ b/src/Field/SubmitField.php @@ -3,15 +3,15 @@ namespace WPDesk\Forms\Field; class SubmitField extends NoValueField { - public function get_template_name() { + public function get_template_name(): string { return 'input-submit'; } - public function get_type() { + public function get_type(): string { return 'submit'; } - public function should_override_form_template() { + public function should_override_form_template(): bool { return true; } } diff --git a/src/Field/TextAreaField.php b/src/Field/TextAreaField.php index 94873b3f0b07dfa0f1a2ea033da60c69b92607b2..0f83ffe1dc6836ef947c1c0204a01ad0d03ff87b 100644 --- a/src/Field/TextAreaField.php +++ b/src/Field/TextAreaField.php @@ -8,7 +8,7 @@ class TextAreaField extends BasicField { $this->set_default_value( '' ); } - public function get_template_name() { + public function get_template_name(): string { return 'textarea'; } } diff --git a/src/Field/TimepickerField.php b/src/Field/TimepickerField.php index 55b1fb12c4d75af64163605303092e0819759beb..84682545bd7be8f4dfcb00cb3c78f5eed86762e0 100644 --- a/src/Field/TimepickerField.php +++ b/src/Field/TimepickerField.php @@ -3,10 +3,7 @@ namespace WPDesk\Forms\Field; class TimepickerField extends BasicField { - /** - * @inheritDoc - */ - public function get_template_name() { + public function get_template_name(): string { return 'timepicker'; } } diff --git a/src/Field/Traits/HtmlAttributes.php b/src/Field/Traits/HtmlAttributes.php index 8116ee9f0218dc9568b9adffb28a331d07f48728..4e54c858b8e41f4c2d632dbad5f6fe4e849cd70f 100644 --- a/src/Field/Traits/HtmlAttributes.php +++ b/src/Field/Traits/HtmlAttributes.php @@ -19,51 +19,39 @@ trait HtmlAttributes { * * @return string[] */ - public function get_attributes( $except = [ 'name', 'type' ] ) { - return array_filter( $this->attributes, function ( $value, $key ) use ( $except ) { - return ! in_array( $key, $except, true ); - }, ARRAY_FILTER_USE_BOTH ); + public function get_attributes( array $except = [ 'name', 'type' ] ): array { + return array_filter( + $this->attributes, + static function ( $value, $key ) use ( $except ) { + return ! in_array( $key, $except, true ); + }, + ARRAY_FILTER_USE_BOTH + ); } - /** - * @param string $name - * @param string $value - * - * @return $this - */ - public function set_attribute( $name, $value ) { + public function set_attribute( string $name, string $value ): self { $this->attributes[ $name ] = $value; return $this; } - /** - * @param string $name - * - * @return $this - */ - public function unset_attribute( $name ) { + public function unset_attribute( string $name ): self { unset( $this->attributes[ $name ] ); return $this; } - /** - * @param string $name - * - * @return bool - */ - public function is_attribute_set( $name ) { + public function is_attribute_set( string $name ): bool { return isset( $this->attributes[ $name ] ); } /** - * @param string $name - * @param mixed $default + * @param string $name + * @param ?string $default * * @return string */ - public function get_attribute( $name, $default = null ) { + public function get_attribute( string $name, $default = null ): string { return $this->attributes[ $name ] ?? $default; } } diff --git a/src/Field/WooSelect.php b/src/Field/WooSelect.php index b8d953808ca327e7ca4ef1ac0b305cd7410070bb..bc59f9c29fa60251876665ebd60163ac7934129d 100644 --- a/src/Field/WooSelect.php +++ b/src/Field/WooSelect.php @@ -9,7 +9,7 @@ class WooSelect extends SelectField { $this->add_class( 'wc-enhanced-select' ); } - public function get_template_name() { + public function get_template_name(): string { return 'woo-select'; } } diff --git a/src/Field/WyswigField.php b/src/Field/WyswigField.php index 846c91995b607526eec4c0b60e8c8cbd945186e6..06db980a61c1c85597c3f60f319dc912351431f4 100644 --- a/src/Field/WyswigField.php +++ b/src/Field/WyswigField.php @@ -8,11 +8,11 @@ class WyswigField extends BasicField { $this->set_default_value( '' ); } - public function get_template_name() { + public function get_template_name(): string { return 'wyswig'; } - public function should_override_form_template() { + public function should_override_form_template(): bool { return true; } } diff --git a/src/FieldProvider.php b/src/FieldProvider.php index f7f38419e4f0973c6e63f3e72fd67f40e91fb2e8..e693d005d2399f7b87ff6f02e486147bf7758d89 100644 --- a/src/FieldProvider.php +++ b/src/FieldProvider.php @@ -11,5 +11,5 @@ interface FieldProvider { * * @return Field[] */ - public function get_fields(); + public function get_fields(): array; } diff --git a/src/FieldRenderer.php b/src/FieldRenderer.php index 37a356d6cfeb065577a6efd647769a954af7edf2..f60c1b01f0b651a8d17efdca0d1be4e002367604 100644 --- a/src/FieldRenderer.php +++ b/src/FieldRenderer.php @@ -2,16 +2,7 @@ namespace WPDesk\Forms; -/** - * @class FieldRenderer - */ interface FieldRenderer { - /** - * @param FieldProvider $provider - * @param array $fields_data - * @param string $name_prefix - * - * @return string|array String or normalized array - */ - public function render_fields( FieldProvider $provider, array $fields_data, $name_prefix = '' ); + /** @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 9e2bdc9222ff58ff73be192494cbd9f60d96bb4b..86a158301f7c43dbafe4de4ed876efd5c8f6c179 100644 --- a/src/FieldsDataReceiver.php +++ b/src/FieldsDataReceiver.php @@ -14,8 +14,6 @@ interface FieldsDataReceiver { /** * Set values corresponding to fields. * - * @param ContainerInterface $data - * * @return void */ public function update_fields_data( ContainerInterface $data ); diff --git a/src/Form.php b/src/Form.php index 7450b2b1203750d62862f8af3b9980f3cf6b47e2..8ea2d785b46d16c64032504bbbf7de542707a158 100644 --- a/src/Form.php +++ b/src/Form.php @@ -12,55 +12,51 @@ use WPDesk\View\Renderer\Renderer; interface Form { /** * For some reason you may want to disable a form. Returns false when disabled. - * - * @return bool */ - public function is_active(); + public function is_active(): bool; /** * Whether form handle_request method was successfully executed. - * - * @return bool */ - public function is_submitted(); + public function is_submitted(): bool; /** * After handle_request or set_data the data in form can be invalid according to field validators. - * Returns false when onle of them says the data is invalid. - * - * @return bool + * Returns false when one of them says the data is invalid. */ - public function is_valid(); + public function is_valid(): bool; /** * Add array to update data. * * @param array $request New data to update. + * + * @return void */ - public function handle_request( $request = array() ); + public function handle_request( array $request = [] ); /** * Data could be saved in some place. Use this method to transmit them to form. * * @param array $data Data for form. + * + * @return void */ - public function set_data( $data ); + public function set_data( array $data ); /** * Use to render the form to string. * * @param Renderer $renderer Renderer to render form fields and form-templates. - * - * @return string */ - public function render_form( Renderer $renderer ); + public function render_form( Renderer $renderer ): string; /** * Get data from form. Use after handle_request or set_data. * - * @return array + * @return array<int,string> */ - public function get_data(); + public function get_data(): array; /** * Get data from form. Use after handle_request or set_data. @@ -68,13 +64,11 @@ interface Form { * * @return array */ - public function get_normalized_data(); + public function get_normalized_data(): array; /** * Form if you ever need to have more than one form at once. - * - * @return string */ - public function get_form_id(); + public function get_form_id(): int; } diff --git a/src/Form/AbstractForm.php b/src/Form/AbstractForm.php deleted file mode 100644 index b51a0e3661082a8018727991e80fd57ad7ba70ad..0000000000000000000000000000000000000000 --- a/src/Form/AbstractForm.php +++ /dev/null @@ -1,94 +0,0 @@ -<?php - -namespace WPDesk\Forms\Form; - - -/** - * Old abstraction layer for forms. - * - * @package WPDesk\Forms - * - * @deprecated Use ony for backward compatibility with Forms 1.x - */ -abstract class AbstractForm { - - /** - * Unique form_id. - * - * @var string - */ - protected $form_id = 'form'; - - /** - * Updated data. - * - * @var array - */ - protected $updated_data = array(); - - - /** - * Checks if form should be active. - * - * @return bool - */ - public function is_active() { - return true; - } - - /** - * Create form data and return an associative array. - * - * @return array - */ - abstract protected function create_form_data(); - - /** - * Add array to update data. - * - * @param array $new_data new data to update. - */ - public function update_form_data( array $new_data = array() ) { - $this->updated_data = $new_data; - } - - /** - * Merge created and updated data and return associative array. Add to all keys form prefix. - * - * @return array - */ - public function get_form_data() { - return array_merge( - $this->create_form_data(), - $this->updated_data - ); - } - - /** - * Get prefixed array returns array with prefixed form_id - * - * @return array - */ - public function get_prefixed_form_data() { - $array = $this->get_form_data(); - $form_id = $this->get_form_id(); - - return array_combine( - array_map( function ( $k ) use ( $form_id ) { - return $form_id . '_' . $k; - }, array_keys( $array ) ), - $array - ); - } - - /** - * return form Id - * - * @return string - */ - public function get_form_id() { - return $this->form_id; - } - -} - diff --git a/src/Form/FormWithFields.php b/src/Form/FormWithFields.php index a5a494cbee9cac26ea3121655ac3a540b77c6bb6..45dc5150049a438e26c04ea181fb24ca683f00c4 100644 --- a/src/Form/FormWithFields.php +++ b/src/Form/FormWithFields.php @@ -15,91 +15,58 @@ use WPDesk\View\Renderer\Renderer; class FormWithFields implements Form, ContainerForm, FieldProvider { use Field\Traits\HtmlAttributes; - /** - * Unique form_id. - * - * @var string - */ + /** @var string Unique form_id. */ protected $form_id = 'form'; - /** - * Updated data. - * - * @var array - */ + + /** @var array Updated data. */ private $updated_data; - /** - * Form fields. - * - * @var Field[] - */ + + /** @var Field[] Form fields. */ private $fields; /** * FormWithFields constructor. * - * @param array $fields Form fields. - * @param string $form_id Unique form id. + * @param Field[] $fields + * @param string $form_id */ - public function __construct( array $fields, $form_id = 'form' ) { - $this->fields = $fields; - $this->form_id = $form_id; - $this->updated_data = null; + public function __construct( array $fields, string $form_id = 'form' ) { + $this->fields = $fields; + $this->form_id = $form_id; } - /** - * Set Form action attribute. - * - * @param string $action - */ - public function set_action( $action ) { + /** Set Form action attribute. */ + public function set_action( string $action ): self { $this->attributes['action'] = $action; return $this; } - /** - * Set Form method attribute ie. GET/POST. - * - * @param string $method - */ - public function set_method( $method ) { + /** Set Form method attribute ie. GET/POST. */ + public function set_method( string $method ): self { $this->attributes['method'] = $method; return $this; } - /** - * @return string - */ - public function get_method() { - return isset( $this->attributes['method'] ) ? $this->attributes['method'] : 'POST'; + public function get_method(): string { + return $this->attributes['method'] ?? 'POST'; } - /** - * @return string - */ - public function get_action() { - return isset( $this->attributes['action'] ) ? $this->attributes['action'] : ''; + public function get_action(): string { + return $this->attributes['action'] ?? ''; } - /** - * @inheritDoc - */ - public function is_submitted() { + public function is_submitted(): bool { return null !== $this->updated_data; } - /** - * @inheritDoc - */ + /** @return void */ public function add_field( Field $field ) { $this->fields[] = $field; } - /** - * @inheritDoc - */ - public function is_active() { + public function is_active(): bool { return true; } @@ -107,17 +74,16 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { * Add more fields to form. * * @param Field[] $fields Field to add to form. + * + * @return void */ public function add_fields( array $fields ) { array_map( [ $this, 'add_field' ], $fields ); } - /** - * @inheritDoc - */ - public function is_valid() { + public function is_valid(): bool { foreach ( $this->fields as $field ) { - $field_value = isset( $this->updated_data[ $field->get_name() ] ) ? $this->updated_data[ $field->get_name() ] : $field->get_default_value(); + $field_value = $this->updated_data[ $field->get_name() ] ?? $field->get_default_value(); $field_validator = $field->get_validator(); if ( ! $field_validator->is_valid( $field_value ) ) { return false; @@ -132,7 +98,7 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { * * @param array|ContainerInterface $request new data to update. */ - public function handle_request( $request = array() ) { + public function handle_request( array $request = [] ) { if ( $this->updated_data === null ) { $this->updated_data = []; } @@ -165,48 +131,41 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { } } - /** - * Renders only fields without form. - * - * @param Renderer $renderer - * - * @return string - */ - public function render_fields( Renderer $renderer ) { + /** Renders only fields without form. */ + public function render_fields( Renderer $renderer ): string { $content = ''; $fields_data = $this->get_data(); foreach ( $this->get_fields() as $field ) { - $content .= $renderer->render( $field->should_override_form_template() ? $field->get_template_name() : 'form-field', + $content .= $renderer->render( + $field->should_override_form_template() ? $field->get_template_name() : 'form-field', [ 'field' => $field, 'renderer' => $renderer, 'name_prefix' => $this->get_form_id(), - 'value' => isset( $fields_data[ $field->get_name() ] ) ? $fields_data[ $field->get_name() ] : $field->get_default_value(), + 'value' => $fields_data[ $field->get_name() ] ?? $field->get_default_value(), 'template_name' => $field->get_template_name(), - ] ); + ] + ); } return $content; } - /** - * @inheritDoc - */ - public function render_form( Renderer $renderer ) { - $content = $renderer->render( 'form-start', [ - 'form' => $this, - 'method' => $this->get_method(), // backward compat - 'action' => $this->get_action(), // backward compat - ] ); + public function render_form( Renderer $renderer ): string { + $content = $renderer->render( + 'form-start', + [ + 'form' => $this, + 'method' => $this->get_method(), // backward compat. + 'action' => $this->get_action(), // backward compat. + ] + ); $content .= $this->render_fields( $renderer ); $content .= $renderer->render( 'form-end' ); return $content; } - /** - * @inheritDoc - */ public function put_data( PersistentContainer $container ) { foreach ( $this->get_fields() as $field ) { $data_key = $field->get_name(); @@ -223,10 +182,7 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { } } - /** - * @inheritDoc - */ - public function get_data() { + public function get_data(): array { $data = $this->updated_data; foreach ( $this->get_fields() as $field ) { @@ -239,10 +195,7 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { return $data; } - /** - * @inheritDoc - */ - public function get_fields() { + public function get_fields(): array { $fields = $this->fields; usort( @@ -255,17 +208,11 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { return $fields; } - /** - * @inheritDoc - */ - public function get_form_id() { + public function get_form_id(): int { return $this->form_id; } - /** - * @inheritDoc - */ - public function get_normalized_data() { + public function get_normalized_data(): array { return $this->get_data(); } } diff --git a/src/Form/FormsCollection.php b/src/Form/FormsCollection.php deleted file mode 100644 index f2603a31e043719383c1f59a3385aa97b77ee272..0000000000000000000000000000000000000000 --- a/src/Form/FormsCollection.php +++ /dev/null @@ -1,115 +0,0 @@ -<?php - -namespace WPDesk\Forms\Form; - -use WPDesk\Forms\Form; - -/** - * FormsCollection class store AbstractForm instances and merges forms data from all collections - * - * @deprecated Use ony for backward compatibility with Forms 1.x - * - * @package WPDesk\Forms - */ -class FormsCollection { - /** - * AbstractForm array collection. - * - * @var Form[] - */ - protected $forms = array(); - - /** - * Unique form_id. - * - * @var string - */ - protected $form_id = 'form'; - - /** - * Updated data. - * - * @var array - */ - protected $updated_data = array(); - - /** - * Add forms. All keys in this array must be unique, otherwise add_form will throw exception. - * - * @param Form[] $forms - */ - public function add_forms( array $forms = array() ) { - foreach ( $forms as $form_object ) { - $this->add_form( $form_object ); - } - } - - /** - * Add form. If key is not unique throw exception. - * - * @param Form $form - * - * @throws \OutOfBoundsException - */ - public function add_form( AbstractForm $form ) { - if ( ! $this->is_form_exists( $form->get_form_id() ) ) { - $this->forms[ $form->get_form_id() ] = $form; - } else { - throw new \OutOfBoundsException( 'Form with this key already exists' ); - } - } - - /** - * Is form exists. Checks if key exists in the array of forms and return bool. - * - * @param string $form_id - * - * @return bool - */ - public function is_form_exists( $form_id ) { - return isset( $this->forms[ (string) $form_id ] ); - } - - /** - * Get form. - * - * @param string $form_id - * - * @return Form - * @throws \OutOfRangeException - */ - public function get_form( $form_id ) { - if ( $this->is_form_exists( $form_id ) ) { - return $this->forms[ (string) $form_id ]; - } - - throw new \OutOfRangeException( 'Form with this key not exists' ); - } - - /** - * Get forms data. This method merge all arrays from forms and return associative array for woocommerce form_fields. - * - * @param bool $prefixed if true add form_id as prefix to form keys - * - * @return array - */ - public function get_forms_data( $prefixed = false ) { - - $forms_data = array(); - - foreach ( $this->forms as $form ) { - if ( ! $form->is_active() ) { - continue; - } - - if ( $prefixed ) { - $forms_data = array_merge( $forms_data, $form->get_prefixed_form_data() ); - } else { - $forms_data = array_merge( $forms_data, $form->get_form_data() ); - } - } - - return $forms_data; - } - -} diff --git a/src/Persistence/FieldPersistenceStrategy.php b/src/Persistence/FieldPersistenceStrategy.php index 092a01a61643fc5c0ce762a64a38c264f6637b90..02f2d4ffd3a051c7abd34ca5c01d179afa7ad907 100644 --- a/src/Persistence/FieldPersistenceStrategy.php +++ b/src/Persistence/FieldPersistenceStrategy.php @@ -12,6 +12,7 @@ use WPDesk\Persistence\PersistentContainer; * @package WPDesk\Forms */ class FieldPersistenceStrategy { + /** @var PersistentContainer */ private $persistence; @@ -21,9 +22,6 @@ class FieldPersistenceStrategy { /** * Save fields data. - * - * @param FieldProvider $fields_provider - * @param array $data */ public function persist_fields( FieldProvider $fields_provider, array $data ) { foreach ( $fields_provider->get_fields() as $field ) { @@ -34,10 +32,8 @@ class FieldPersistenceStrategy { /** * Load fields data. - * - * @return array */ - public function load_fields( FieldProvider $fields_provider ) { + public function load_fields( FieldProvider $fields_provider ): array { $data = []; foreach ( $fields_provider->get_fields() as $field ) { $field_key = $field->get_name(); @@ -45,7 +41,7 @@ class FieldPersistenceStrategy { $data[ $field_key ] = $field->get_serializer()->unserialize( $this->persistence->get( $field_key ) ); } catch ( NotFoundExceptionInterface $not_found ) { // TODO: Logger -// LoggerFactory::get_logger()->info( "FieldPersistenceStrategy:: Field {$field_key} not found" ); + // LoggerFactory::get_logger()->info( "FieldPersistenceStrategy:: Field {$field_key} not found" ); } } diff --git a/src/Renderer/JsonNormalizedRenderer.php b/src/Renderer/JsonNormalizedRenderer.php index 4d078ac531786a23d18b6e24293c13149540e0b9..371490c244f3a3b32bcbb924b6f016abcf4eb380 100644 --- a/src/Renderer/JsonNormalizedRenderer.php +++ b/src/Renderer/JsonNormalizedRenderer.php @@ -18,7 +18,7 @@ class JsonNormalizedRenderer implements FieldRenderer { * * @return array Normalized fields with data. */ - public function render_fields( FieldProvider $provider, array $fields_data, $name_prefix = '' ) { + public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ): array { $rendered_fields = []; foreach ( $provider->get_fields() as $field ) { $rendered = [ @@ -29,7 +29,7 @@ class JsonNormalizedRenderer implements FieldRenderer { 'readonly' => $field->is_readonly(), 'required' => $field->is_required(), 'prefix' => $name_prefix, - 'value ' => isset( $fields_data[ $field->get_name() ] ) ? $fields_data[ $field->get_name() ] : $field->get_default_value() + 'value ' => isset( $fields_data[ $field->get_name() ] ) ? $fields_data[ $field->get_name() ] : $field->get_default_value(), ]; if ( $field->has_classes() ) { @@ -52,7 +52,7 @@ class JsonNormalizedRenderer implements FieldRenderer { $rendered['options'] = $options; } if ( $field->has_data() ) { - $data = $field->get_data(); + $data = $field->get_data(); $rendered['data'] = []; foreach ( $data as $data_name => $data_value ) { $rendered['data'][] = [ @@ -61,7 +61,7 @@ class JsonNormalizedRenderer implements FieldRenderer { ]; } } - if (json_encode($rendered) !== false) { + if ( json_encode( $rendered ) !== false ) { $rendered_fields[] = $rendered; } } diff --git a/src/Resolver/DefaultFormFieldResolver.php b/src/Resolver/DefaultFormFieldResolver.php index 3609a6973a3137ce3e99a227c42f85c6650a9212..b5c167502452dc45e910619ab753b69d5b710a80 100644 --- a/src/Resolver/DefaultFormFieldResolver.php +++ b/src/Resolver/DefaultFormFieldResolver.php @@ -12,6 +12,7 @@ use WPDesk\View\Resolver\Resolver; * @package WPDesk\Forms\Resolver */ class DefaultFormFieldResolver implements Resolver { + /** @var Resolver */ private $dir_resolver; @@ -19,8 +20,8 @@ class DefaultFormFieldResolver implements Resolver { $this->dir_resolver = new DirResolver( __DIR__ . '/../../templates' ); } - public function resolve( $name, Renderer $renderer = null ) { + public function resolve( $name, Renderer $renderer = null ): string { return $this->dir_resolver->resolve( $name, $renderer ); } -} \ No newline at end of file +} diff --git a/src/Sanitizer.php b/src/Sanitizer.php index a7883dcf74f7927763d47285764dea3471be9dad..bc8f2070aca82873f5025230032554b485629eb0 100644 --- a/src/Sanitizer.php +++ b/src/Sanitizer.php @@ -3,10 +3,6 @@ namespace WPDesk\Forms; interface Sanitizer { - /** - * @param mixed $value - * - * @return string - */ - public function sanitize( $value ); + /** @param mixed $value */ + public function sanitize( $value ): string; } diff --git a/src/Sanitizer/CallableSanitizer.php b/src/Sanitizer/CallableSanitizer.php index c0a28b9756d8d18a12be39d645e643d6d828fcf9..da916abbb4971b884fb1f37368d8174299f35593 100644 --- a/src/Sanitizer/CallableSanitizer.php +++ b/src/Sanitizer/CallableSanitizer.php @@ -5,13 +5,15 @@ namespace WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer; class CallableSanitizer implements Sanitizer { + + /** @var callable */ private $callable; - public function __construct( $callable ) { + public function __construct( callable $callable ) { $this->callable = $callable; } - public function sanitize( $value ) { + public function sanitize( $value ): string { return call_user_func( $this->callable, $value ); } diff --git a/src/Sanitizer/NoSanitize.php b/src/Sanitizer/NoSanitize.php index a3fe4a61160f130cae2821f2bf9faa8352d761c7..21bce01b271ef5333a77fd9c3d56aa7f831fa162 100644 --- a/src/Sanitizer/NoSanitize.php +++ b/src/Sanitizer/NoSanitize.php @@ -5,7 +5,7 @@ namespace WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer; class NoSanitize implements Sanitizer { - public function sanitize( $value ) { + public function sanitize( $value ): string { return $value; } diff --git a/src/Sanitizer/TextFieldSanitizer.php b/src/Sanitizer/TextFieldSanitizer.php index 0a1bbcbca0f7f12580bfa0fb09ca78c3da9f82e4..325c6524e1d252a85908e44bf0909a9953f1123b 100644 --- a/src/Sanitizer/TextFieldSanitizer.php +++ b/src/Sanitizer/TextFieldSanitizer.php @@ -5,7 +5,7 @@ namespace WPDesk\Forms\Sanitizer; use WPDesk\Forms\Sanitizer; class TextFieldSanitizer implements Sanitizer { - public function sanitize( $value ) { + public function sanitize( $value ): string { return sanitize_text_field( $value ); } diff --git a/src/Serializer.php b/src/Serializer.php index 91054006581db530155966b9482962d72384761e..8302325f3414a1a570cc906656b9df54e3406aec 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -3,7 +3,17 @@ namespace WPDesk\Forms; interface Serializer { + /** + * @param mixed $value + * + * @return mixed + */ public function serialize( $value ); + /** + * @param mixed $value + * + * @return mixed + */ public function unserialize( $value ); } diff --git a/src/Serializer/JsonSerializer.php b/src/Serializer/JsonSerializer.php index d7e5ca038aedd247e7024785efed0cad27d0dc7d..bc8c0e0baa328261c53eb0b0f4f57b8b4fb4bcc4 100644 --- a/src/Serializer/JsonSerializer.php +++ b/src/Serializer/JsonSerializer.php @@ -5,8 +5,8 @@ namespace WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer; class JsonSerializer implements Serializer { - public function serialize( $value ) { - return json_encode( $value ); + public function serialize( $value ): string { + return (string) json_encode( $value ); } public function unserialize( $value ) { diff --git a/src/Serializer/NoSerialize.php b/src/Serializer/NoSerialize.php index b29e88b9e23420a2c67b5bd2f10136d8f509edfa..31a09e14ae627e60e0546c3f0a88a69438f58428 100644 --- a/src/Serializer/NoSerialize.php +++ b/src/Serializer/NoSerialize.php @@ -5,7 +5,7 @@ namespace WPDesk\Forms\Serializer; use WPDesk\Forms\Serializer; class NoSerialize implements Serializer { - public function serialize( $value ) { + public function serialize( $value ): string { return $value; } diff --git a/src/Serializer/ProductSelectSerializer.php b/src/Serializer/ProductSelectSerializer.php index 2adf327996bc3d2ad6f383a72a26642e9161cb92..e801d4d776b9aa8d5557378497252ac844adaccc 100644 --- a/src/Serializer/ProductSelectSerializer.php +++ b/src/Serializer/ProductSelectSerializer.php @@ -10,9 +10,6 @@ use WPDesk\Forms\Serializer; * @package WPDesk\Forms\Serializer */ class ProductSelectSerializer implements Serializer { - /** - * @param $value - */ public function serialize( $value ) { $products_with_names = []; if ( is_array( $value ) ) { diff --git a/src/Validator.php b/src/Validator.php index d953702d23861c2af778c70b4027d8cfb211f793..228182004c03280489b076b126a22dc7542e5de1 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -3,15 +3,9 @@ namespace WPDesk\Forms; interface Validator { - /** - * @param mixed $value - * - * @return bool - */ - public function is_valid( $value ); + /** @param mixed $value */ + public function is_valid( $value ): bool; - /** - * @return string[] - */ - public function get_messages(); + /** @return string[] */ + public function get_messages(): array; } diff --git a/src/Validator/ChainValidator.php b/src/Validator/ChainValidator.php index d3549d566ed861b8ccd7caef78734a1e2dc321cb..341abeec541d8fa18757c808aa69e1635a629d05 100644 --- a/src/Validator/ChainValidator.php +++ b/src/Validator/ChainValidator.php @@ -5,9 +5,11 @@ namespace WPDesk\Forms\Validator; use WPDesk\Forms\Validator; class ChainValidator implements Validator { + /** @var Validator[] */ private $validators; + /** @var array */ private $messages; public function __construct() { @@ -20,13 +22,13 @@ class ChainValidator implements Validator { * * @return $this */ - public function attach( Validator $validator ) { + public function attach( Validator $validator ): self { $this->validators[] = $validator; return $this; } - public function is_valid( $value ) { + public function is_valid( $value ): bool { $result = true; $messages = [ [] ]; foreach ( $this->validators as $validator ) { @@ -40,7 +42,7 @@ class ChainValidator implements Validator { return $result; } - public function get_messages() { + public function get_messages(): array { return $this->messages; } diff --git a/src/Validator/NoValidateValidator.php b/src/Validator/NoValidateValidator.php index dbc996646da8d0fb7d0128404abcb90669d61202..04cf59cdaad70077d60e484bcaefa5f09c952916 100644 --- a/src/Validator/NoValidateValidator.php +++ b/src/Validator/NoValidateValidator.php @@ -5,11 +5,11 @@ namespace WPDesk\Forms\Validator; use WPDesk\Forms\Validator; class NoValidateValidator implements Validator { - public function is_valid( $value ) { + public function is_valid( $value ): bool { return true; } - public function get_messages() { + public function get_messages(): array { return []; } diff --git a/src/Validator/NonceValidator.php b/src/Validator/NonceValidator.php index 87682257b08d4c14525a80a4f30f3082d0ba6014..8709af6125e7d31f44cd12d39bd7deac7f09b6b6 100644 --- a/src/Validator/NonceValidator.php +++ b/src/Validator/NonceValidator.php @@ -5,19 +5,18 @@ namespace WPDesk\Forms\Validator; use WPDesk\Forms\Validator; class NonceValidator implements Validator { + private $action; public function __construct( $action ) { $this->action = $action; } - public function is_valid( $value ) { - $valid = wp_verify_nonce( $value, $this->action ); - - return $valid; + public function is_valid( $value ): bool { + return wp_verify_nonce( $value, $this->action ); } - public function get_messages() { + public function get_messages(): array { return []; } diff --git a/src/Validator/RequiredValidator.php b/src/Validator/RequiredValidator.php index 016d803d86a95cf3c4c4dd3b6bf9a189c1edffa3..4e78faf0c4cf67dab01646b8381080370aa97c81 100644 --- a/src/Validator/RequiredValidator.php +++ b/src/Validator/RequiredValidator.php @@ -5,11 +5,11 @@ namespace WPDesk\Forms\Validator; use WPDesk\Forms\Validator; class RequiredValidator implements Validator { - public function is_valid( $value ) { + public function is_valid( $value ): bool { return $value !== null; } - public function get_messages() { + public function get_messages(): array { return []; } diff --git a/templates/button.php b/templates/button.php index 43c21190ad4326181dd29fd4a45f930642a7cc26..c558c6e50982f56cf4e1f62f011f442778abcef2 100644 --- a/templates/button.php +++ b/templates/button.php @@ -6,26 +6,31 @@ * @var string $value * * @var string $template_name Real field template. - * */ + ?> <button -<?php if ($field->has_classes()): ?> - class="<?php echo \esc_attr($field->get_classes()); ?>" +<?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 $key.'="'.\esc_attr($val).'"'; ?> +<?php foreach ( $field->get_attributes( [] ) as $key => $val ) : ?> + <?php echo $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); ?>" + 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_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_disabled() ) : + ?> + disabled="disabled"<?php endif; ?> + <?php + if ( $field->is_readonly() ) : + ?> + readonly="readonly"<?php endif; ?> -><?php echo \esc_html($field->get_label()); ?></button> +><?php echo \esc_html( $field->get_label() ); ?></button> diff --git a/templates/form-field.php b/templates/form-field.php index 971e53ba0447e37f1f3fcee0255f793797a29478..50f56784d782cc266542aa55d2824d2aee94aed5 100644 --- a/templates/form-field.php +++ b/templates/form-field.php @@ -6,24 +6,28 @@ * @var string $value * * @var string $template_name Real field template. - * */ ?> <tr valign="top"> - <?php if ( $field->has_label() ): ?> + <?php if ( $field->has_label() ) : ?> <?php echo $renderer->render( 'form-label', [ 'field' => $field ] ); ?> <?php endif; ?> <td class="forminp"> - <?php echo $renderer->render( $template_name, [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, - ] ); ?> + <?php + echo $renderer->render( + $template_name, + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] + ); + ?> - <?php if ( $field->has_description() ): ?> + <?php if ( $field->has_description() ) : ?> <p class="description"><?php echo wp_kses_post( $field->get_description() ); ?></p> <?php endif; ?> </td> diff --git a/templates/form-label.php b/templates/form-label.php index 703ed78e6909b409488e2697cb997c5dcb7ebe37..f6268a5da0b84a8fc2f4b5f879809d6b60bba295 100644 --- a/templates/form-label.php +++ b/templates/form-label.php @@ -7,8 +7,8 @@ ?> <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 wc_help_tip($field->get_description_tip()); ?> + <?php if ( $field->has_description_tip() ) : ?> + <?php echo wc_help_tip( $field->get_description_tip() ); ?> <?php endif ?> </label> </th> diff --git a/templates/form-start.php b/templates/form-start.php index 10d7686b33c36da7e75ad8976b1363a368904eac..e3d4bbeb7578b818643f6f14214f950ef37f07c0 100644 --- a/templates/form-start.php +++ b/templates/form-start.php @@ -3,7 +3,7 @@ * @var \WPDesk\Forms\Form\FormWithFields $form */ ?> -<form class="wrap woocommerce" method="<?php echo esc_attr($form->get_method()); ?>" action="<?php echo esc_attr($form->get_action()); ?>"> +<form class="wrap woocommerce" method="<?php echo esc_attr( $form->get_method() ); ?>" action="<?php echo esc_attr( $form->get_action() ); ?>"> <h2 style="display:none;"></h2><?php // All admin notices will be moved here by WP js ?> <table class="form-table"> diff --git a/templates/header.php b/templates/header.php index db07896cde8c2c90da788afbc6fc7d63bc302ffb..07a9a6e0bc64c90c184e926b8652b7f3659244cd 100644 --- a/templates/header.php +++ b/templates/header.php @@ -9,10 +9,10 @@ $header_size = (int) $field->get_meta_value( 'header_size' ) ?: 2; $classes = $field->has_classes() ? 'class="' . esc_attr( $field->get_classes() ) . '"' : ''; ?> -<?php if ( $field->has_label() ): ?> +<?php if ( $field->has_label() ) : ?> <h<?php echo $header_size; ?> <?php echo $classes; ?>><?php echo esc_html( $field->get_label() ); ?></h<?php echo $header_size; ?>> <?php endif; ?> -<?php if ( $field->has_description() ): ?> +<?php if ( $field->has_description() ) : ?> <p <?php echo $classes; ?>><?php echo wp_kses_post( $field->get_description() ); ?></p> <?php endif; ?> diff --git a/templates/input-checkbox.php b/templates/input-checkbox.php index 61dc39f5a6c06fa680a3969e7a53084c3e1bf0b1..d69ececf5a0696240b6cc46c0c269f547720a73c 100644 --- a/templates/input-checkbox.php +++ b/templates/input-checkbox.php @@ -6,13 +6,17 @@ * @var string $value * * @var string $template_name Real field template. - * */ + ?> -<?php echo $renderer->render('input', [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, -]); ?> +<?php +echo $renderer->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 7dea70268a228dd4633706c03d931b613e93a9f1..0c5b70c46fc2351ff598382349866fc8936eca50 100644 --- a/templates/input-date-picker.php +++ b/templates/input-date-picker.php @@ -7,6 +7,13 @@ * @var string $value * * @var string $template_name Real field template. - * */ -echo $renderer->render('input', ['field' => $field, 'renderer' => $renderer, 'name_prefix' => $name_prefix, 'value' => $value]); +echo $renderer->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 3effbd29c61a8eb271e5b199110318710978c54e..300944e64354f2a9e0193fa7906b71208c5a46a4 100644 --- a/templates/input-hidden.php +++ b/templates/input-hidden.php @@ -6,12 +6,15 @@ * @var string $value * * @var string $template_name Real field template. - * */ ?> -<?php echo $renderer->render('input', [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, -]); ?> +<?php +echo $renderer->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 34d13882e2fc34d6c461dd95be4d8b8cce6379b3..af9f792bb1274d93cd93ffe494dd277582929ba2 100644 --- a/templates/input-image.php +++ b/templates/input-image.php @@ -8,10 +8,10 @@ $media_container_id = 'media_' . sanitize_key( $field->get_id() ); ?> <div class="media-input-wrapper" id="<?php echo $media_container_id; ?>"> - <input type="hidden" class="image-field-value" value="<?php echo \esc_html( $value ); ?>" - name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>" - id="<?php echo \esc_attr( $field->get_id() ); ?>"/> - <div class="custom-img-container"> + <input type="hidden" class="image-field-value" value="<?php echo \esc_html( $value ); ?>" + name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>" + id="<?php echo \esc_attr( $field->get_id() ); ?>"/> + <div class="custom-img-container"> <?php if ( $value ) : ?> <img src="<?php echo \esc_html( $value ) ?>" alt="" width="100"/> <?php endif; ?> diff --git a/templates/input-number.php b/templates/input-number.php index 7dea70268a228dd4633706c03d931b613e93a9f1..0c5b70c46fc2351ff598382349866fc8936eca50 100644 --- a/templates/input-number.php +++ b/templates/input-number.php @@ -7,6 +7,13 @@ * @var string $value * * @var string $template_name Real field template. - * */ -echo $renderer->render('input', ['field' => $field, 'renderer' => $renderer, 'name_prefix' => $name_prefix, 'value' => $value]); +echo $renderer->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 3effbd29c61a8eb271e5b199110318710978c54e..300944e64354f2a9e0193fa7906b71208c5a46a4 100644 --- a/templates/input-radio.php +++ b/templates/input-radio.php @@ -6,12 +6,15 @@ * @var string $value * * @var string $template_name Real field template. - * */ ?> -<?php echo $renderer->render('input', [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, -]); ?> +<?php +echo $renderer->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 5f676450462383025628b7c61a65355585efcb1f..5c11fc86dcd3096fc22a075383e741340fc518a1 100644 --- a/templates/input-submit.php +++ b/templates/input-submit.php @@ -6,7 +6,6 @@ * @var string $value * * @var string $template_name Real field template. - * */ ?> @@ -14,17 +13,29 @@ <td style="padding-left:0;"> <p class="submit"> <input - <?php if ( $field->has_classes() ): ?>class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?> - <?php foreach ( $field->get_attributes( [] ) as $key => $value ): ?> - <?php echo $key ?>="<?php echo esc_attr( $value ); ?>" + <?php + if ( $field->has_classes() ) : + ?> + class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?> + <?php foreach ( $field->get_attributes( [] ) as $key => $value ) : ?> + <?php echo $key; ?>="<?php echo esc_attr( $value ); ?>" <?php endforeach; ?> type="<?php echo esc_attr( $field->get_type() ); ?>" 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; ?> + <?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 7d3fff829b5f52988afb67e097dac6aaed342495..f2787298f3f9fd5a2347ebf046978e95a3a24a2f 100644 --- a/templates/input-text-multiple.php +++ b/templates/input-text-multiple.php @@ -6,49 +6,62 @@ * @var string $value * * @var string $template_name Real field template. - * */ ?> <?php -if( empty( $value ) || is_string( $value ) ) { +if ( empty( $value ) || is_string( $value ) ) { $input_values[] = ''; } else { $input_values = $value; } ?> <div class="clone-element-container"> -<?php foreach( $input_values as $text_value ): ?> -<?php if (!\in_array($field->get_type(), ['number', 'text', 'hidden'])): ?> - <input type="hidden" name="<?php echo $name_prefix.'['.$field->get_name().']'; ?>" value="no"/> +<?php foreach ( $input_values as $text_value ) : ?> + <?php if ( ! \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ] ) ) : ?> + <input type="hidden" name="<?php echo $name_prefix . '[' . $field->get_name() . ']'; ?>" value="no"/> <?php endif; ?> -<?php if ($field->get_type() === 'checkbox' && $field->has_sublabel()): ?><label><?php endif; ?> + <?php + if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) : + ?> + <label><?php endif; ?> <div class="clone-wrapper"> <input - 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()); ?>" + 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() ); ?>" - <?php if ($field->has_classes()): ?> - class="<?php echo \esc_attr($field->get_classes()); ?>" + <?php if ( $field->has_classes() ) : ?> + 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 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 $key.'="'.\esc_attr($atr_val).'"'; ?> + <?php + foreach ( $field->get_attributes() as $key => $atr_val ) : + echo $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'])): ?> - value="<?php echo \esc_html($text_value); ?>" - <?php else: ?> + <?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' ] ) ) : ?> + value="<?php echo \esc_html( $text_value ); ?>" + <?php else : ?> value="yes" - <?php if ($value === 'yes'): ?> + <?php if ( $value === 'yes' ) : ?> checked="checked" <?php endif; ?> <?php endif; ?> @@ -57,25 +70,25 @@ if( empty( $value ) || is_string( $value ) ) { <span class="remove-field hidden"><span class="dashicons dashicons-remove"></span></span> </div> - <?php if ($field->get_type() === 'checkbox' && $field->has_sublabel()): ?> - <?php echo \esc_html($field->get_sublabel()); ?></label> + <?php if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) : ?> + <?php echo \esc_html( $field->get_sublabel() ); ?></label> <?php endif; ?> <?php endforeach; ?> </div> <style> - .clone-element-container .clone-wrapper .add-field { - display: none; - } - .clone-element-container .clone-wrapper:first-child .add-field { - display: inline-block; - } + .clone-element-container .clone-wrapper .add-field { + display: none; + } + .clone-element-container .clone-wrapper:first-child .add-field { + display: inline-block; + } - .clone-element-container .clone-wrapper .remove-field { - display: inline-block; - } - .clone-element-container .clone-wrapper:first-child .remove-field { - display: none; - } + .clone-element-container .clone-wrapper .remove-field { + display: inline-block; + } + .clone-element-container .clone-wrapper:first-child .remove-field { + display: none; + } </style> <script> jQuery( function ( $ ) { diff --git a/templates/input-text.php b/templates/input-text.php index 3effbd29c61a8eb271e5b199110318710978c54e..300944e64354f2a9e0193fa7906b71208c5a46a4 100644 --- a/templates/input-text.php +++ b/templates/input-text.php @@ -6,12 +6,15 @@ * @var string $value * * @var string $template_name Real field template. - * */ ?> -<?php echo $renderer->render('input', [ - 'field' => $field, - 'renderer' => $renderer, - 'name_prefix' => $name_prefix, - 'value' => $value, -]); ?> +<?php +echo $renderer->render( + 'input', + [ + 'field' => $field, + 'renderer' => $renderer, + 'name_prefix' => $name_prefix, + 'value' => $value, + ] +); diff --git a/templates/input.php b/templates/input.php index b02e5eaccb625c0a3b0d0c1302e97e3eb8064da4..83448ead5472bd38b2051d6573268026ed057af4 100644 --- a/templates/input.php +++ b/templates/input.php @@ -7,42 +7,56 @@ */ ?> -<?php if (!\in_array($field->get_type(), ['number', 'text', 'hidden'])): ?> - <input type="hidden" name="<?php echo $name_prefix.'['.$field->get_name().']'; ?>" value="no"/> +<?php if ( ! \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ] ) ) : ?> + <input type="hidden" name="<?php echo $name_prefix . '[' . $field->get_name() . ']'; ?>" value="no"/> <?php endif; ?> -<?php if ($field->get_type() === 'checkbox' && $field->has_sublabel()): ?><label><?php endif; ?> +<?php +if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) : + ?> + <label><?php endif; ?> <input - 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()); ?>" + 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() ); ?>" - <?php if ($field->has_classes()): ?> - class="<?php echo \esc_attr($field->get_classes()); ?>" + <?php if ( $field->has_classes() ) : ?> + 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 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 $key.'="'.\esc_attr($atr_val).'"'; ?> + <?php + foreach ( $field->get_attributes() as $key => $atr_val ) : + echo $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'])): ?> - value="<?php echo \esc_html($value); ?>" - <?php else: ?> + <?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' ] ) ) : ?> + value="<?php echo \esc_html( $value ); ?>" + <?php else : ?> value="yes" - <?php if ($value === 'yes'): ?> + <?php if ( $value === 'yes' ) : ?> checked="checked" <?php endif; ?> <?php endif; ?> /> -<?php if ($field->get_type() === 'checkbox' && $field->has_sublabel()): ?> - <?php echo \esc_html($field->get_sublabel()); ?></label> +<?php if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) : ?> + <?php echo \esc_html( $field->get_sublabel() ); ?></label> <?php endif; ?> diff --git a/templates/noonce.php b/templates/noonce.php index 0cd58a561d5024e462e5c04cd9c471d7bc4794b7..edc9522d19a9f5d42e8b2b252d898fddecb93f3b 100644 --- a/templates/noonce.php +++ b/templates/noonce.php @@ -1,8 +1,8 @@ <?php - /** * @var \WPDesk\Forms\Field $field * @var string $name_prefix * @var string $value */ -\wp_nonce_field($field->get_meta_value('action'), $name_prefix .'['. $field->get_name().']'); + +\wp_nonce_field( $field->get_meta_value( 'action' ), $name_prefix . '[' . $field->get_name() . ']' ); diff --git a/templates/paragraph.php b/templates/paragraph.php index 184e3943c5975b7f9d37ae199df61c3778c34a5f..9d2901582ab230015779c4e700d3a10d085fc724 100644 --- a/templates/paragraph.php +++ b/templates/paragraph.php @@ -6,10 +6,14 @@ */ ?> -<?php if ( $field->has_description() ): ?> +<?php if ( $field->has_description() ) : ?> <tr> <td style="padding-left:0;" colspan="2"> - <p <?php if ( $field->has_classes() ): ?>class="<?php echo $field->get_classes(); ?>"<?php endif; ?>><?php echo wp_kses_post( $field->get_description() ); ?></p> + <p + <?php + if ( $field->has_classes() ) : + ?> + class="<?php echo $field->get_classes(); ?>"<?php endif; ?>><?php echo wp_kses_post( $field->get_description() ); ?></p> </td> </tr> <?php endif; ?> diff --git a/templates/product-select.php b/templates/product-select.php index 3efbbafe5f2a675bbd37171d63a1bdc2576e38ad..bf545ba7098728eecc8f75952dd5d1d9014de463 100644 --- a/templates/product-select.php +++ b/templates/product-select.php @@ -1,23 +1,26 @@ <?php - /** * @var \WPDesk\Forms\Field $field * @var string $name_prefix * @var string[] $value */ + ?> <select class="wc-product-search" multiple="multiple" style="width: 50%;" - id="<?php echo esc_attr( $field->get_id() ); ?>" - name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>][]" - data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" - data-action="woocommerce_json_search_products_and_variations"> + id="<?php echo esc_attr( $field->get_id() ); ?>" + name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>][]" + data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" + data-action="woocommerce_json_search_products_and_variations"> <?php foreach ( (array) $value as $product_id ) { $product = wc_get_product( $product_id ); if ( is_object( $product ) ) { - echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, - false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>'; + echo '<option value="' . esc_attr( $product_id ) . '"' . selected( + true, + true, + false + ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>'; } } ?> diff --git a/templates/select.php b/templates/select.php index 0c533967388ea7e2dd27e3ccc7bf44bd748a8856..e437efca35dcba5daf5ebf702a15f32b2b029337 100644 --- a/templates/select.php +++ b/templates/select.php @@ -7,22 +7,43 @@ ?> <select id="<?php echo esc_attr( $field->get_id() ); ?>" - <?php if ($field->has_classes()): ?>class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?> - name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>]<?php echo $field->is_multiple()? '[]' : ''; ?>" - <?php foreach ($field->get_attributes() as $key => $attr_val): ?> - <?php echo esc_attr($key); ?>="<?php echo esc_attr($attr_val); ?>" + <?php + if ( $field->has_classes() ) : + ?> + class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?> + name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>]<?php echo $field->is_multiple() ? '[]' : ''; ?>" + <?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->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() ): ?><option value=""><?php echo esc_html( $field->get_placeholder() ); ?></option><?php endif; ?> + <?php + if ( $field->has_placeholder() ) : + ?> + <option value=""><?php echo esc_html( $field->get_placeholder() ); ?></option><?php endif; ?> - <?php foreach ( $field->get_possible_values() as $possible_value => $label ): ?> + <?php foreach ( $field->get_possible_values() as $possible_value => $label ) : ?> <option - <?php if ( $possible_value === $value || (is_array($value) && in_array($possible_value, $value)) || (is_numeric($possible_value) && is_numeric($value) && (int) $possible_value === (int) $value )): ?>selected="selected"<?php endif; ?> + <?php + if ( $possible_value === $value || ( is_array( $value ) && in_array( $possible_value, $value ) ) || ( is_numeric( $possible_value ) && is_numeric( $value ) && (int) $possible_value === (int) $value ) ) : + ?> + selected="selected"<?php endif; ?> value="<?php echo esc_attr( $possible_value ); ?>" ><?php echo esc_html( $label ); ?></option> <?php endforeach; ?> diff --git a/templates/textarea.php b/templates/textarea.php index cf2f2a7006edeb1d2aa59d8b7c59a547b1a320e6..4d140fa98473029bb9dc2c263c955aba2568df1e 100644 --- a/templates/textarea.php +++ b/templates/textarea.php @@ -7,17 +7,35 @@ ?> <textarea - id="<?php echo esc_attr( $field->get_id() ); ?>" - <?php if ( $field->has_classes() ): ?>class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?> + id="<?php echo esc_attr( $field->get_id() ); ?>" + <?php + if ( $field->has_classes() ) : + ?> + class="<?php echo esc_attr( $field->get_classes() ); ?>"<?php endif; ?> name="<?php echo esc_attr( $name_prefix ); ?>[<?php echo esc_attr( $field->get_name() ); ?>]" - <?php foreach ( $field->get_attributes() as $key => $attr_val ): ?> - <?php echo esc_attr( $key ); ?>="<?php echo esc_attr( $attr_val ); ?>" - <?php endforeach; ?> + <?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->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() ): ?>placeholder="<?php echo esc_html( $field->get_placeholder() ); ?>"<?php endif; ?> + <?php + if ( $field->has_placeholder() ) : + ?> + placeholder="<?php echo esc_html( $field->get_placeholder() ); ?>"<?php endif; ?> ><?php echo esc_html( $value ); ?></textarea> diff --git a/templates/wyswig.php b/templates/wyswig.php index fd1ea9daa103a72921b0fee100309329b59c21d8..6bf06d3ac70791372ca16ec6c942a8bc32a18498 100644 --- a/templates/wyswig.php +++ b/templates/wyswig.php @@ -13,14 +13,14 @@ <?php $id = uniqid( 'wyswig_' ); -$editor_settings = array( - 'textarea_name' => esc_attr( $name_prefix ) . '[' . esc_attr( $field->get_name() ) . ']' -); +$editor_settings = [ + 'textarea_name' => esc_attr( $name_prefix ) . '[' . esc_attr( $field->get_name() ) . ']', +]; wp_editor( wp_kses_post( $value ), $id, $editor_settings ); ?> <script type="text/javascript"> (function () { - ShopMagic.wyswig.init('<?php echo $id; ?>'); + ShopMagic.wyswig.init('<?php echo esc_attr( $id ); ?>'); }()); </script> diff --git a/tests/unit/TestForm.php b/tests/unit/TestForm.php deleted file mode 100644 index a0702cfbc8f04ad8e35bf54a51077f28c3876e71..0000000000000000000000000000000000000000 --- a/tests/unit/TestForm.php +++ /dev/null @@ -1,58 +0,0 @@ -<?php - -namespace Tests; - -use WPDesk\Forms\Form\AbstractForm; - -class TestForm extends \PHPUnit\Framework\TestCase -{ - - const FORM1_ID = 'test_form'; - const FORM1_FORM_DATA = [ 'test' => true ]; - const FORM1_UPDATED_FORM_DATA = [ 'test666' => true ]; - - private $form; - - protected function setUp(){ - // Create a new instance from the Abstract Class - $this->form = $this->getMockBuilder( AbstractForm::class ) - ->enableOriginalConstructor() - ->setMethods(['get_form_id']) - ->getMockForAbstractClass(); - $this->form->method( 'get_form_id' )->willReturn( self::FORM1_ID ); - $this->form->method( 'create_form_data' )->willReturn( self::FORM1_FORM_DATA ); - } - - protected function getForm(){ - return clone $this->form; - } - - /** - * Test getting form id. - */ - public function testFormId() - { - $form = $this->getForm(); - $this->assertEquals(self::FORM1_ID, $form->get_form_id()); - } - - /** - * Test getting form data. - */ - public function testFormData() - { - $form = $this->getForm(); - $this->assertSame( self::FORM1_FORM_DATA, $form->get_form_data()); - } - - /** - * Test updated form data. - */ - public function testUpdatedFormData() - { - $form = $this->getForm(); - - $form->update_form_data( self::FORM1_UPDATED_FORM_DATA ); - $this->assertSame( array_merge( self::FORM1_FORM_DATA, self::FORM1_UPDATED_FORM_DATA ), $form->get_form_data()); - } -} \ No newline at end of file diff --git a/tests/unit/TestFormCollection.php b/tests/unit/TestFormCollection.php deleted file mode 100644 index 67bd0ae7b5273b7e7c43b8633cbe988a64a72355..0000000000000000000000000000000000000000 --- a/tests/unit/TestFormCollection.php +++ /dev/null @@ -1,141 +0,0 @@ -<?php - -namespace Tests; - -use WPDesk\Forms\Form\AbstractForm; -use WPDesk\Forms\Form\FormsCollection; - -class TestFormCollection extends \PHPUnit\Framework\TestCase { - - const FORM1_ID = 'test_form'; - const FORM2_ID = 'test_form2'; - - const FORM1_FORM_DATA = [ 'test' => true ]; - const FORM2_FORM_DATA = [ 'test2' => 'potato' ]; - - const FORM1_PREFIXED_FORM_DATA = [ 'test_form_test' => true ]; - const FORM1_UPDATED_FORM_DATA = [ 'test666' => true ]; - - - private $formConditionalTrue; - private $formConditionalFalse; - - protected function setUp() { - $this->formConditionalTrue = $this->getMockBuilder( AbstractForm::class ) - ->enableOriginalConstructor() - ->setMethods( [ 'get_form_id', 'is_active' ] ) - ->getMockForAbstractClass(); - $this->formConditionalTrue->method( 'get_form_id' )->willReturn( self::FORM1_ID ); - $this->formConditionalTrue->method( 'is_active' )->willReturn( true ); - $this->formConditionalTrue->method( 'create_form_data' )->willReturn( self::FORM1_FORM_DATA ); - - $this->formConditionalFalse = $this->getMockBuilder( AbstractForm::class ) - ->enableOriginalConstructor() - ->setMethods( [ 'get_form_id', 'is_active' ] ) - ->getMockForAbstractClass(); - $this->formConditionalFalse->method( 'get_form_id' )->willReturn( self::FORM2_ID ); - $this->formConditionalFalse->method( 'is_active' )->willReturn( false ); - $this->formConditionalFalse->method( 'create_form_data' )->willReturn( self::FORM2_FORM_DATA ); - } - - protected function getFormConditionalTrue() { - return clone $this->formConditionalTrue; - } - - protected function getFormConditionalFalse() { - return clone $this->formConditionalFalse; - } - - /** - * Test adding and checking single form. - */ - public function testIfFormExists() { - $collection = new FormsCollection(); - $collection->add_form( $this->getFormConditionalTrue() ); - - $this->assertTrue( $collection->is_form_exists( self::FORM1_ID ) ); - } - - /** - * Test adding and checking multiple forms. - */ - public function testIfFormsExists() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $this->assertTrue( $collection->is_form_exists( self::FORM1_ID ) ); - $this->assertTrue( $collection->is_form_exists( self::FORM2_ID ) ); - } - - /** - * Test getting single form. AbstractForm object is expected - */ - public function testGettingExistingForm() { - $collection = new FormsCollection(); - $collection->add_form( $this->getFormConditionalTrue() ); - - $this->assertInstanceOf( - AbstractForm::class, - $collection->get_form( self::FORM1_ID ) - ); - } - - /** - * Test getting not existing single form. - */ - public function testGettingNotExistingForm() { - $collection = new FormsCollection(); - $collection->add_form( $this->getFormConditionalTrue() ); - - $this->expectException( \OutOfRangeException::class ); - $collection->get_form( '123456' ); - } - - /** - * Test returned data. - */ - public function testReturnedFormsData() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $this->assertSame( self::FORM1_FORM_DATA, $collection->get_forms_data() ); - } - - /** - * Test returned prefixed data. - */ - public function testReturnedPrefixedFormsData() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $this->assertSame( self::FORM1_PREFIXED_FORM_DATA, $collection->get_forms_data( true ) ); - - - } - - /** - * Test returned updated data. - */ - public function testReturnedUpdatedFormsData() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $form = $collection->get_form( self::FORM1_ID ); - $form->update_form_data( self::FORM1_UPDATED_FORM_DATA ); - $this->assertSame( array_merge( self::FORM1_FORM_DATA, self::FORM1_UPDATED_FORM_DATA ), $collection->get_forms_data() ); - - } - -} \ No newline at end of file