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

332 lines
8.1 KiB
PHP
Raw Normal View History

<?php
2020-08-28 08:13:45 +03:00
/**
* Renders the settings of the Gateways.
*
* @package Inpsyde\PayPalCommerce\WcGateway\Settings
*/
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;
2020-08-28 08:13:45 +03:00
/**
* Class SettingsRenderer
*/
2020-08-27 11:08:36 +03:00
class SettingsRenderer {
2020-08-28 08:13:45 +03:00
/**
* The settings.
*
* @var ContainerInterface
*/
2020-08-27 11:08:36 +03:00
private $settings;
2020-08-28 08:13:45 +03:00
/**
* The current onboarding state.
*
* @var State
*/
2020-08-27 11:08:36 +03:00
private $state;
2020-08-28 08:13:45 +03:00
/**
* The setting fields.
*
* @var array
*/
2020-08-27 11:08:36 +03:00
private $fields;
2020-08-28 08:13:45 +03:00
/**
* Helper to see if DCC gateway can be shown.
*
* @var DccApplies
*/
private $dcc_applies;
/**
* Helper to see if messages are supposed to show up.
*
* @var MessagesApply
*/
private $messages_apply;
/**
* SettingsRenderer constructor.
*
* @param ContainerInterface $settings The Settings.
* @param State $state The current state.
* @param array $fields The setting fields.
* @param DccApplies $dcc_applies Whether DCC gateway can be shown.
* @param MessagesApply $messages_apply Whether messages can be shown.
*/
2020-08-27 11:08:36 +03:00
public function __construct(
ContainerInterface $settings,
State $state,
array $fields,
2020-08-28 08:13:45 +03:00
DccApplies $dcc_applies,
MessagesApply $messages_apply
2020-08-27 11:08:36 +03:00
) {
2020-08-28 08:13:45 +03:00
$this->settings = $settings;
$this->state = $state;
$this->fields = $fields;
$this->dcc_applies = $dcc_applies;
$this->messages_apply = $messages_apply;
2020-08-27 11:08:36 +03:00
}
2020-08-28 08:13:45 +03:00
/**
* Renders the multiselect field.
*
* @param string $field The current field HTML.
* @param string $key The current key.
* @param array $config The configuration array.
* @param string $value The current value.
*
* @return string
*/
public function render_multiselect( $field, $key, $config, $value ): string {
2020-07-02 12:48:40 +03:00
2020-08-28 08:13:45 +03:00
if ( 'ppcp-multiselect' !== $config['type'] ) {
2020-08-27 11:08:36 +03:00
return $field;
}
2020-08-27 11:08:36 +03:00
$options = array();
2020-08-28 08:13:45 +03:00
foreach ( $config['options'] as $option_key => $option_value ) {
$selected = ( in_array( $option_key, $value, true ) ) ? 'selected="selected"' : '';
2020-08-28 08:13:45 +03:00
$options[] = '<option value="' . esc_attr( $option_key ) . '" ' . $selected . '>' .
esc_html( $option_value ) .
2020-08-27 11:08:36 +03:00
'</option>';
}
2020-08-27 11:08:36 +03:00
$html = sprintf(
'<select
multiple
class="%s"
name="%s"
>%s</select>',
2020-08-27 11:08:36 +03:00
esc_attr( implode( ' ', $config['class'] ) ),
esc_attr( $key ) . '[]',
implode( '', $options )
);
2020-08-27 11:08:36 +03:00
return $html;
}
2020-08-28 08:13:45 +03:00
/**
* Renders the password input field.
*
* @param string $field The current field HTML.
* @param string $key The current key.
* @param array $config The configuration array.
* @param string $value The current value.
*
* @return string
*/
public function render_password( $field, $key, $config, $value ): string {
2020-08-28 08:13:45 +03:00
if ( 'ppcp-password' !== $config['type'] ) {
2020-08-27 11:08:36 +03:00
return $field;
}
2020-08-27 11:08:36 +03:00
$html = sprintf(
'<input
type="password"
autocomplete="new-password"
class="%s"
name="%s"
value="%s"
>',
2020-08-27 11:08:36 +03:00
esc_attr( implode( ' ', $config['class'] ) ),
esc_attr( $key ),
esc_attr( $value )
);
2020-08-27 11:08:36 +03:00
return $html;
}
2020-08-28 08:13:45 +03:00
/**
* Renders the text input field.
*
* @param string $field The current field HTML.
* @param string $key The current key.
* @param array $config The configuration array.
* @param string $value The current value.
*
* @return string
*/
public function render_text_input( $field, $key, $config, $value ): string {
if ( 'ppcp-text-input' !== $config['type'] ) {
2020-08-27 11:08:36 +03:00
return $field;
}
2020-08-27 11:08:36 +03:00
$html = sprintf(
'<input
type="text"
autocomplete="off"
class="%s"
name="%s"
value="%s"
>',
2020-08-27 11:08:36 +03:00
esc_attr( implode( ' ', $config['class'] ) ),
esc_attr( $key ),
esc_attr( $value )
);
2020-08-27 11:08:36 +03:00
return $html;
}
2020-08-14 10:09:11 +03:00
2020-08-28 08:13:45 +03:00
/**
* Renders the heading field.
*
* @param string $field The current field HTML.
* @param string $key The current key.
* @param array $config The configuration array.
* @param string $value The current value.
*
* @return string
*/
public function render_heading( $field, $key, $config, $value ): string {
2020-08-14 10:09:11 +03:00
2020-08-28 08:13:45 +03:00
if ( 'ppcp-heading' !== $config['type'] ) {
2020-08-27 11:08:36 +03:00
return $field;
}
2020-08-14 10:09:11 +03:00
2020-08-27 11:08:36 +03:00
$html = sprintf(
'<h3 class="%s">%s</h3>',
esc_attr( implode( ' ', $config['class'] ) ),
esc_html( $config['heading'] )
);
2020-08-14 10:09:11 +03:00
2020-08-27 11:08:36 +03:00
return $html;
}
2020-08-28 08:13:45 +03:00
/**
* Renders the settings.
*
* @param bool $is_dcc Whether it is the DCC gateway or not.
*/
public function render( bool $is_dcc ) {
2020-08-27 11:08:36 +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-28 08:13:45 +03:00
if ( $is_dcc && ! in_array( $config['gateway'], array( 'all', 'dcc' ), true ) ) {
2020-08-27 11:08:36 +03:00
continue;
}
2020-08-28 08:13:45 +03:00
if ( ! $is_dcc && ! in_array( $config['gateway'], array( 'all', 'paypal' ), true ) ) {
2020-08-27 11:08:36 +03:00
continue;
}
if (
in_array( 'dcc', $config['requirements'], true )
2020-08-28 08:13:45 +03:00
&& ! $this->dcc_applies->forCountryCurrency()
2020-08-27 11:08:36 +03:00
) {
continue;
}
if (
in_array( 'messages', $config['requirements'], true )
2020-08-28 08:13:45 +03:00
&& ! $this->messages_apply->forCountry()
2020-08-27 11:08:36 +03:00
) {
continue;
}
$value = $this->settings->has( $field ) ? $this->settings->get( $field ) : null;
$key = 'ppcp[' . $field . ']';
$id = 'ppcp-' . $field;
$config['id'] = $id;
2020-08-28 08:13:45 +03:00
$th_td = 'ppcp-heading' !== $config['type'] ? 'td' : 'th';
$colspan = 'ppcp-heading' !== $config['type'] ? 1 : 2;
2020-08-27 11:08:36 +03:00
?>
<tr valign="top" id="<?php echo esc_attr( 'field-' . $field ); ?>">
2020-08-28 08:13:45 +03:00
<?php if ( 'ppcp-heading' !== $config['type'] ) : ?>
2020-08-27 11:08:36 +03:00
<th>
<label
for="<?php echo esc_attr( $id ); ?>"
><?php echo esc_html( $config['title'] ); ?></label>
<?php if ( isset( $config['desc_tip'] ) && $config['desc_tip'] ) : ?>
<span
class="woocommerce-help-tip"
data-tip="<?php echo esc_attr( $config['description'] ); ?>"
></span>
<?php
unset( $config['description'] );
endif;
?>
</th>
<?php endif; ?>
2020-08-28 08:13:45 +03:00
<<?php echo esc_attr( $th_td ); ?> colspan="<?php echo (int) $colspan; ?>">
2020-08-27 11:08:36 +03:00
<?php
2020-08-28 08:13:45 +03:00
'ppcp-text' === $config['type'] ?
$this->render_text( $config )
2020-08-27 11:08:36 +03:00
: woocommerce_form_field( $key, $config, $value );
?>
2020-08-28 08:13:45 +03:00
</<?php echo esc_attr( $th_td ); ?>>
2020-08-27 11:08:36 +03:00
</tr>
<?php
endforeach;
2020-08-28 08:13:45 +03:00
if ( $is_dcc ) :
2020-08-27 11:08:36 +03:00
?>
<tr>
<th><?php esc_html_e( '3D Secure', 'woocommerce-paypal-commerce-gateway' ); ?></th>
<td>
<p>
<?php
2020-08-28 08:13:45 +03:00
/**
* We still need to provide a docs link.
*
* @todo: Provide link to documentation.
*/
2020-08-27 11:08:36 +03:00
echo wp_kses_post(
sprintf(
2020-08-28 08:13:45 +03:00
// translators: %1$s and %2$s is a link tag.
2020-08-27 11:08:36 +03:00
__(
'3D Secure benefits cardholders and merchants by providing
2020-08-21 09:53:32 +03:00
an additional layer of verification using Verified by Visa,
MasterCard SecureCode and American Express SafeKey.
%1$sLearn more about 3D Secure.%2$s',
2020-08-28 08:13:45 +03:00
'woocommerce-paypal-commerce-gateway'
2020-08-27 11:08:36 +03:00
),
'<a href = "#">',
'</a>'
)
);
?>
</p>
</td>
</tr>
<?php
endif;
}
2020-07-02 12:48:40 +03:00
2020-08-28 08:13:45 +03:00
/**
* Renders the ppcp-text field given a configuration.
*
* @param array $config The configuration array.
*/
private function render_text( array $config ) {
2020-08-27 11:08:36 +03:00
echo wp_kses_post( $config['text'] );
if ( isset( $config['hidden'] ) ) {
$value = $this->settings->has( $config['hidden'] ) ?
(string) $this->settings->get( $config['hidden'] )
: '';
echo ' <input
2020-08-21 09:41:12 +03:00
type = "hidden"
2020-08-27 11:08:36 +03:00
name = "ppcp[' . esc_attr( $config['hidden'] ) . ']"
value = "' . esc_attr( $value ) . '"
2020-08-21 09:41:12 +03:00
> ';
2020-08-27 11:08:36 +03:00
}
}
}