woocommerce-paypal-payments/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php

115 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2020-07-02 12:48:40 +03:00
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
use Inpsyde\PayPalCommerce\Onboarding\State;
use Psr\Container\ContainerInterface;
class SettingsRenderer
{
private $settings;
private $state;
private $fields;
private $dccApplies;
public function __construct(
2020-07-02 12:48:40 +03:00
ContainerInterface $settings,
State $state,
array $fields,
DccApplies $dccApplies
) {
2020-07-02 12:48:40 +03:00
$this->settings = $settings;
$this->state = $state;
$this->fields = $fields;
$this->dccApplies = $dccApplies;
}
2020-07-02 12:48:40 +03:00
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
public function renderMultiSelect($field, $key, $config, $value): string
{
if ($config['type'] !== 'ppcp-multiselect') {
return $field;
}
$options = [];
foreach ($config['options'] as $optionKey => $optionValue) {
2020-07-02 12:48:40 +03:00
$selected = (in_array($optionKey, $value, true)) ? 'selected="selected"' : '';
2020-07-02 12:48:40 +03:00
$options[] = '<option value="' . esc_attr($optionKey) . '" ' . $selected . '>' .
esc_html($optionValue) .
'</option>';
}
$html = sprintf(
'<select
multiple
class="%s"
name="%s"
>%s</select>',
implode(' ', $config['class']),
2020-07-03 10:21:06 +03:00
$key . '[]',
implode('', $options)
);
return $html;
}
2020-07-02 12:48:40 +03:00
//phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
2020-07-02 12:48:40 +03:00
public function render()
{
$nonce = wp_create_nonce(SettingsListener::NONCE);
?>
<input type="hidden" name="ppcp-nonce" value="<?php echo esc_attr($nonce); ?>">
<?php
foreach ($this->fields as $field => $config) :
if (! in_array($this->state->currentState(), $config['screens'], true)) {
continue;
}
if (in_array('dcc', $config['requirements'], true) && ! $this->dccApplies->forCountryCurrency()) {
continue;
}
$value = $this->settings->has($field) ? $this->settings->get($field) : null;
$id = 'ppcp[' . $field . ']';
?>
<tr valign="top">
<th>
<label
for="<?php echo esc_attr($id); ?>"
><?php echo esc_html($config['title']); ?></label>
2020-07-02 10:03:06 +03:00
<?php if (isset($config['desc_tip']) && $config['desc_tip']) : ?>
2020-07-02 12:48:40 +03:00
<span
class="woocommerce-help-tip"
data-tip="<?php echo esc_attr($config['description']); ?>"
></span>
<?php unset($config['description']);
endif; ?>
</th>
<td><?php
2020-07-02 12:48:40 +03:00
$config['type'] === 'ppcp-text' ?
$this->renderText($config)
: woocommerce_form_field($id, $config, $value); ?></td>
</tr>
<?php endforeach;
}
2020-07-02 12:48:40 +03:00
private function renderText(array $config)
{
echo wp_kses_post($config['text']);
if (isset($config['hidden'])) {
$value = $this->settings->has($config['hidden']) ?
(string) $this->settings->get($config['hidden'])
: '';
echo '<input
type="hidden"
name="ppcp[' . esc_attr($config['hidden']) . ']"
value="' . esc_attr($value) . '"
>';
}
}
}