| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Settings; |
| 4 |
|
| 5 |
use MultiSafepay\WooCommerce\Utils\EscapeUtil; |
| 6 |
|
| 7 |
/** |
| 8 |
* The display fields settings fields |
| 9 |
* |
| 10 |
* Contains all the functions needed to display each setting field |
| 11 |
*/ |
| 12 |
class SettingsFieldsDisplay { |
| 13 |
|
| 14 |
/** |
| 15 |
* The field |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
private $field; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor the class |
| 23 |
* |
| 24 |
* @param array $field |
| 25 |
*/ |
| 26 |
public function __construct( array $field ) { |
| 27 |
$this->field = $field; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the value by setting field |
| 32 |
* |
| 33 |
* @see https://developer.wordpress.org/reference/functions/get_option/ |
| 34 |
* @param array $field |
| 35 |
* @return mixed |
| 36 |
*/ |
| 37 |
private function get_option_by_field( array $field ) { |
| 38 |
$value = get_option( $field['id'], false ); |
| 39 |
if ( ! $value ) { |
| 40 |
return $field['default']; |
| 41 |
} |
| 42 |
return esc_html( $value ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Render the html for a text type input |
| 47 |
* |
| 48 |
* @param array $field |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
private function render_text_field( array $field ): string { |
| 52 |
$value = $this->get_option_by_field( $field ); |
| 53 |
$field_id = esc_attr( $field['id'] ); |
| 54 |
$placeholder = esc_attr( $field['placeholder'] ); |
| 55 |
$html = '<input id="' . $field_id . '" type="' . $field['type'] . '" name="' . $field_id . '" placeholder="' . $placeholder . '" value="' . $value . '"/>'; |
| 56 |
if ( ! empty( $field['description'] ) ) { |
| 57 |
$html .= '<p class="description">' . $field['description'] . '</p>'; |
| 58 |
} |
| 59 |
return $html; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Render the html for a select type input |
| 64 |
* |
| 65 |
* @param array $field |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
private function render_select_field( array $field ): string { |
| 69 |
$value = $this->get_option_by_field( $field ); |
| 70 |
$field_id = esc_attr( $field['id'] ); |
| 71 |
$html = '<select name="' . $field_id . '" id="' . $field_id . '">'; |
| 72 |
foreach ( $field['options'] as $option_value => $option_name ) { |
| 73 |
if ( $option_value === $value ) { |
| 74 |
$html .= '<option value="' . esc_attr( $option_value ) . '" selected>' . $option_name . '</option>'; |
| 75 |
} |
| 76 |
if ( $option_value !== $value ) { |
| 77 |
$html .= '<option value="' . esc_attr( $option_value ) . '">' . $option_name . '</option>'; |
| 78 |
} |
| 79 |
} |
| 80 |
$html .= '</select> '; |
| 81 |
if ( ! empty( $field['description'] ) ) { |
| 82 |
$html .= '<p class="description">' . $field['description'] . '</p>'; |
| 83 |
} |
| 84 |
return $html; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Render the html for a checkbox type input |
| 89 |
* |
| 90 |
* @param array $field |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
private function render_checkbox_field( array $field ): string { |
| 94 |
$checked = ( $this->get_option_by_field( $field ) ) ? 'checked="checked"' : ''; |
| 95 |
$field_id = esc_attr( $field['id'] ); |
| 96 |
$placeholder = esc_attr( $field['placeholder'] ); |
| 97 |
$html = '<input id="' . $field_id . '" type="' . $field['type'] . '" name="' . $field_id . '" placeholder="' . $placeholder . '" value="1" ' . $checked . ' />'; |
| 98 |
if ( ! empty( $field['description'] ) ) { |
| 99 |
$html .= '<p class="description">' . $field['description'] . '</p>'; |
| 100 |
} |
| 101 |
return $html; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Render the html for a password type input |
| 106 |
* |
| 107 |
* @param array $field |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
private function render_password_field( array $field ): string { |
| 111 |
$value = $this->get_option_by_field( $field ); |
| 112 |
$field_id = esc_attr( $field['id'] ); |
| 113 |
$placeholder = esc_attr( $field['placeholder'] ); |
| 114 |
$html = '<input id="' . $field_id . '" type="' . $field['type'] . '" name="' . $field_id . '" placeholder="' . $placeholder . '" value="' . $value . '"/>'; |
| 115 |
if ( ! empty( $field['description'] ) ) { |
| 116 |
$html .= '<p class="description">' . $field['description'] . '</p>'; |
| 117 |
} |
| 118 |
return $html; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Render the html for each type of the registered setting field |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function display(): void { |
| 127 |
$html = ''; |
| 128 |
switch ( $this->field['type'] ) { |
| 129 |
case 'text': |
| 130 |
$html .= $this->render_text_field( $this->field ); |
| 131 |
break; |
| 132 |
case 'select': |
| 133 |
$html .= $this->render_select_field( $this->field ); |
| 134 |
break; |
| 135 |
case 'checkbox': |
| 136 |
$html .= $this->render_checkbox_field( $this->field ); |
| 137 |
break; |
| 138 |
case 'password': |
| 139 |
$html .= $this->render_password_field( $this->field ); |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
echo wp_kses( $html, EscapeUtil::get_allowed_html_tags() ); |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|