2020-07-02 09:37:07 +03:00
|
|
|
<?php
|
2020-07-02 12:48:40 +03:00
|
|
|
|
2020-07-02 09:37:07 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Inpsyde\PayPalCommerce\WcGateway\Settings;
|
|
|
|
|
2020-07-23 11:10:37 +03:00
|
|
|
use Inpsyde\PayPalCommerce\ApiClient\Helper\DccApplies;
|
2020-07-02 09:37:07 +03:00
|
|
|
use Inpsyde\PayPalCommerce\Onboarding\State;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
|
|
|
class SettingsRenderer
|
|
|
|
{
|
|
|
|
|
|
|
|
private $settings;
|
|
|
|
private $state;
|
|
|
|
private $fields;
|
2020-07-23 11:10:37 +03:00
|
|
|
private $dccApplies;
|
2020-07-02 09:37:07 +03:00
|
|
|
public function __construct(
|
2020-07-02 12:48:40 +03:00
|
|
|
ContainerInterface $settings,
|
|
|
|
State $state,
|
2020-07-23 11:10:37 +03:00
|
|
|
array $fields,
|
|
|
|
DccApplies $dccApplies
|
2020-07-02 09:37:07 +03:00
|
|
|
) {
|
2020-07-02 12:48:40 +03:00
|
|
|
|
2020-07-02 09:37:07 +03:00
|
|
|
$this->settings = $settings;
|
|
|
|
$this->state = $state;
|
|
|
|
$this->fields = $fields;
|
2020-07-23 11:10:37 +03:00
|
|
|
$this->dccApplies = $dccApplies;
|
2020-07-02 09:37:07 +03:00
|
|
|
}
|
|
|
|
|
2020-07-02 12:48:40 +03:00
|
|
|
//phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
|
|
|
|
public function renderMultiSelect($field, $key, $config, $value): string
|
|
|
|
{
|
|
|
|
|
2020-07-02 09:37:07 +03:00
|
|
|
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 09:37:07 +03:00
|
|
|
|
2020-07-02 12:48:40 +03:00
|
|
|
$options[] = '<option value="' . esc_attr($optionKey) . '" ' . $selected . '>' .
|
|
|
|
esc_html($optionValue) .
|
|
|
|
'</option>';
|
2020-07-02 09:37:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$html = sprintf(
|
|
|
|
'<select
|
|
|
|
multiple
|
|
|
|
class="%s"
|
|
|
|
name="%s"
|
|
|
|
>%s</select>',
|
|
|
|
implode(' ', $config['class']),
|
2020-07-03 10:21:06 +03:00
|
|
|
$key . '[]',
|
2020-07-02 09:37:07 +03:00
|
|
|
implode('', $options)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
2020-07-02 12:48:40 +03:00
|
|
|
//phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
|
2020-07-02 09:37:07 +03:00
|
|
|
|
2020-07-02 12:48:40 +03:00
|
|
|
public function render()
|
|
|
|
{
|
2020-07-02 09:37:07 +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-07-23 11:10:37 +03:00
|
|
|
if (in_array('dcc', $config['requirements'], true) && ! $this->dccApplies->forCountryCurrency()) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-02 09:37:07 +03:00
|
|
|
$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; ?>
|
2020-07-02 09:37:07 +03:00
|
|
|
</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>
|
2020-07-02 09:37:07 +03:00
|
|
|
</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) . '"
|
|
|
|
>';
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 09:37:07 +03:00
|
|
|
}
|