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

203 lines
6.2 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;
2020-08-19 09:35:48 +03:00
use Inpsyde\PayPalCommerce\Button\Helper\MessagesApply;
use Inpsyde\PayPalCommerce\Onboarding\State;
use Psr\Container\ContainerInterface;
class SettingsRenderer
{
private $settings;
private $state;
private $fields;
private $dccApplies;
2020-08-19 09:35:48 +03:00
private $messagesApply;
public function __construct(
2020-07-02 12:48:40 +03:00
ContainerInterface $settings,
State $state,
array $fields,
2020-08-19 09:35:48 +03:00
DccApplies $dccApplies,
MessagesApply $messagesApply
) {
2020-07-02 12:48:40 +03:00
$this->settings = $settings;
$this->state = $state;
$this->fields = $fields;
$this->dccApplies = $dccApplies;
2020-08-19 09:35:48 +03:00
$this->messagesApply = $messagesApply;
}
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>',
esc_attr(implode(' ', $config['class'])),
esc_attr($key) . '[]',
implode('', $options)
);
return $html;
}
public function renderPassword($field, $key, $config, $value): string
{
if ($config['type'] !== 'ppcp-password') {
return $field;
}
$html = sprintf(
'<input
type="password"
autocomplete="new-password"
class="%s"
name="%s"
value="%s"
>',
esc_attr(implode(' ', $config['class'])),
esc_attr($key),
esc_attr($value)
);
return $html;
}
public function renderTextInput($field, $key, $config, $value): string
{
if ($config['type'] !== 'ppcp-text-input') {
return $field;
}
$html = sprintf(
'<input
type="text"
autocomplete="off"
class="%s"
name="%s"
value="%s"
>',
esc_attr(implode(' ', $config['class'])),
esc_attr($key),
esc_attr($value)
);
return $html;
}
2020-08-14 10:09:11 +03:00
public function renderHeading($field, $key, $config, $value): string
{
if ($config['type'] !== 'ppcp-heading') {
return $field;
}
$html = sprintf(
2020-08-14 13:38:20 +03:00
'<h3 class="%s">%s</h3>',
2020-08-14 10:09:11 +03:00
esc_attr(implode(' ', $config['class'])),
esc_html($config['heading'])
);
return $html;
}
2020-07-02 12:48:40 +03:00
//phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
2020-08-14 10:41:53 +03:00
//phpcs:disable Inpsyde.CodeQuality.NestingLevel.High
2020-08-18 09:04:58 +03:00
public function render(bool $isDcc)
2020-07-02 12:48:40 +03:00
{
$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;
}
2020-08-19 05:39:45 +03:00
if ($isDcc && ! in_array($config['gateway'], ['all', 'dcc'], true)) {
2020-08-18 09:04:58 +03:00
continue;
}
2020-08-19 05:39:45 +03:00
if (! $isDcc && ! in_array($config['gateway'], ['all', 'paypal'], true)) {
2020-08-18 09:04:58 +03:00
continue;
}
2020-08-19 09:35:48 +03:00
if (
in_array('dcc', $config['requirements'], true)
&& ! $this->dccApplies->forCountryCurrency()
) {
continue;
}
if (
in_array('messages', $config['requirements'], true)
&& ! $this->messagesApply->forCountry()
) {
continue;
}
$value = $this->settings->has($field) ? $this->settings->get($field) : null;
$id = 'ppcp[' . $field . ']';
2020-08-14 13:38:20 +03:00
$thTd = $config['type'] !== 'ppcp-heading' ? 'td' : 'th';
2020-08-14 10:09:11 +03:00
$colspan = $config['type'] !== 'ppcp-heading' ? 1 : 2;
?>
<tr valign="top" id="<?php echo esc_attr('field-' . $field); ?>">
2020-08-14 10:09:11 +03:00
<?php if ($config['type'] !== 'ppcp-heading') : ?>
<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>
2020-08-14 10:09:11 +03:00
<?php endif; ?>
2020-08-19 05:39:45 +03:00
<<?php echo esc_attr($thTd); ?> colspan="<?php echo (int) $colspan; ?>"><?php
2020-07-02 12:48:40 +03:00
$config['type'] === 'ppcp-text' ?
$this->renderText($config)
2020-08-19 05:39:45 +03:00
: woocommerce_form_field($id, $config, $value); ?></<?php echo esc_attr($thTd); ?>>
</tr>
<?php endforeach;
}
2020-08-14 10:41:53 +03:00
//phpcs:enable Inpsyde.CodeQuality.NestingLevel.High
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) . '"
>';
}
}
}