Skip to content

Commit

Permalink
Improve fields API and escaping.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Oct 5, 2023
1 parent ab7d9c4 commit 519532b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 96 deletions.
36 changes: 14 additions & 22 deletions src/Fields/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,21 @@
*/
class DateField extends Field {
/**
* Get HTML attributes.
*
* @return array<string, string>
* Get element.
*
* @return Element|null
*/
protected function get_html_attributes(): array {
$attributes = parent::get_html_attributes();

$attributes['type'] = 'date';
$attributes['id'] = $this->get_id();
$attributes['name'] = $this->get_id();

return $attributes;
}

/**
* Render field.
*
* @return string
*/
public function render(): string {
$element = new Element( 'input', $this->get_html_attributes() );

return $element->render();
protected function get_element() {
$element = new Element(
'input',
[
'type' => 'date',
'id' => $this->get_id(),
'name' => $this->get_id(),
]
);

return $element;
}

/**
Expand Down
29 changes: 21 additions & 8 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Pronamic\WordPress\Pay\Fields;

use JsonSerializable;
use Pronamic\WordPress\Html\Element;

/**
* Field class
Expand Down Expand Up @@ -109,12 +110,12 @@ public function is_required(): bool {
}

/**
* Get HTML attributes.
*
* @return array<string, string>
* Get element.
*
* @return Element|null
*/
protected function get_html_attributes() {
return [];
protected function get_element() {
return null;
}

/**
Expand All @@ -123,16 +124,28 @@ protected function get_html_attributes() {
* @return string
*/
public function render() {
return '';
$element = $this->get_element();

if ( null === $element ) {
return '';
}

return $element->render();
}

/**
* Print output.
* Output.
*
* @return int
*/
public function output() {
return print $this->render();
$element = $this->get_element();

if ( null === $element ) {
return 0;
}

return $element->output();
}

/**
Expand Down
14 changes: 0 additions & 14 deletions src/Fields/IDealIssuerSelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@ protected function setup() {
$this->set_label( \__( 'Bank', 'pronamic_ideal' ) );
}

/**
* Get HTML attributes.
*
* @return array<string, string>
*/
protected function get_html_attributes() {
$attributes = parent::get_html_attributes();

$attributes['id'] = $this->get_id();
$attributes['name'] = $this->get_id();

return $attributes;
}

/**
* Get options.
*
Expand Down
34 changes: 13 additions & 21 deletions src/Fields/SelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@
* @link https://github.com/WordPress/gutenberg/tree/trunk/packages/components/src/select-control
*/
class SelectField extends Field {
/**
* Get HTML attributes.
*
* @return array<string, string>
*/
protected function get_html_attributes() {
$attributes = parent::get_html_attributes();

$attributes['id'] = $this->get_id();
$attributes['name'] = $this->get_id();

return $attributes;
}

/**
* Options.
*
Expand Down Expand Up @@ -83,18 +69,24 @@ public function get_flat_options() {
}

/**
* Render field.
*
* @return string
* Get element.
*
* @return Element|null
*/
public function render() {
$element = new Element( 'select', $this->get_html_attributes() );
protected function get_element() {
$element = new Element(
'select',
[
'id' => $this->get_id(),
'name' => $this->get_id(),
]
);

foreach ( $this->get_options() as $child ) {
$element->children[] = $child->render();
$element->children[] = $child->get_element()->render();
}

return $element->render();
return $element;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Fields/SelectFieldOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public function __construct( string $value, string $label ) {
}

/**
* Render field.
* Get element.
*
* @return string
* @return Element
*/
public function render(): string {
public function get_element() {
$element = new Element(
'option',
[
Expand All @@ -60,7 +60,7 @@ public function render(): string {

$element->children[] = $this->label;

return $element->render();
return $element;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Fields/SelectFieldOptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function __construct( $label ) {
}

/**
* Render.
* Get element.
*
* @return string
* @return Element
*/
public function render() {
public function get_element() {
$element = new Element(
'optgroup',
[
Expand All @@ -53,9 +53,9 @@ public function render() {
);

foreach ( $this->options as $option ) {
$element->children[] = $option->render();
$element->children[] = $option->get_element()->render();
}

return $element->render();
return $element;
}
}
36 changes: 14 additions & 22 deletions src/Fields/TextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,21 @@
*/
class TextField extends Field {
/**
* Get HTML attributes.
*
* @return array<string, string>
* Get element.
*
* @return Element|null
*/
protected function get_html_attributes(): array {
$attributes = parent::get_html_attributes();

$attributes['type'] = 'text';
$attributes['id'] = $this->get_id();
$attributes['name'] = $this->get_id();

return $attributes;
}

/**
* Render field.
*
* @return string
*/
public function render(): string {
$element = new Element( 'input', $this->get_html_attributes() );

return $element->render();
protected function get_element() {
$element = new Element(
'input',
[
'type' => 'text',
'id' => $this->get_id(),
'name' => $this->get_id(),
]
);

return $element;
}

/**
Expand Down

0 comments on commit 519532b

Please sign in to comment.