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

410 lines
10 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;
2020-09-02 12:21:37 +03:00
use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
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.
*/
2020-09-02 09:56:04 +03:00
public function render() {
2020-08-27 11:08:36 +03:00
2020-09-02 10:56:26 +03:00
//phpcs:ignore WordPress.Security.NonceVerification.Recommended
2020-09-02 12:21:37 +03:00
$is_dcc = isset( $_GET[ SectionsRenderer::KEY ] ) && CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) );
2020-09-02 10:56:26 +03:00
$nonce = wp_create_nonce( SettingsListener::NONCE );
2020-08-27 11:08:36 +03:00
?>
<input type="hidden" name="ppcp-nonce" value="<?php echo esc_attr( $nonce ); ?>">
<?php
foreach ( $this->fields as $field => $config ) :
2020-08-31 09:42:39 +03:00
if ( ! in_array( $this->state->current_state(), $config['screens'], 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', '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-09-01 09:00:45 +03:00
&& ! $this->dcc_applies->for_country_currency()
2020-08-27 11:08:36 +03:00
) {
continue;
}
if (
in_array( 'messages', $config['requirements'], true )
2020-08-31 11:12:46 +03:00
&& ! $this->messages_apply->for_country()
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;
if ( $is_dcc ) {
if ( $this->dcc_applies->for_country_currency() ) {
if ( State::STATE_ONBOARDED > $this->state->current_state() ) {
$this->render_dcc_onboarding_info();
}
if ( State::STATE_ONBOARDED === $this->state->current_state() ) {
$this->render_3d_secure_info();
}
} else {
$this->render_dcc_does_not_apply_info();
}
}
2020-08-27 11:08:36 +03:00
}
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
}
}
/**
* Renders the 3d secure info text.
*/
private function render_3d_secure_info() {
?>
<tr>
<th><?php esc_html_e( '3D Secure', 'paypal-payments-for-woocommerce' ); ?></th>
<td>
<p>
<?php
/**
* We still need to provide a docs link.
*
* @todo: Provide link to documentation.
*/
echo wp_kses_post(
sprintf(
// translators: %1$s and %2$s is a link tag.
__(
'3D Secure benefits cardholders and merchants by providing
an additional layer of verification using Verified by Visa,
MasterCard SecureCode and American Express SafeKey.
%1$sLearn more about 3D Secure.%2$s',
'paypal-payments-for-woocommerce'
),
'<a href = "#">',
'</a>'
)
);
?>
</p>
</td>
</tr>
<?php
}
/**
* Renders the DCC onboarding info.
*/
private function render_dcc_onboarding_info() {
?>
<tr>
<th><?php esc_html_e( 'Onboarding', 'paypal-payments-for-woocommerce' ); ?></th>
<td class="notice notice-error">
<p>
<?php
esc_html_e(
'You need to complete your onboarding, before you can use the PayPal Card Processing option.',
'paypal-payments-for-woocommerce'
);
?>
<a
href="<?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' ) ); ?>"
>
<?php esc_html_e( 'Click here to complete your onboarding.', 'paypal-payments-for-woocommerce' ); ?>
</a>
</p>
</td>
</tr>
<?php
}
/**
* Renders the info, that DCC is not available in the merchant's country.
*/
private function render_dcc_does_not_apply_info() {
?>
<tr>
<th><?php esc_html_e( 'Card Processing not available', 'paypal-payments-for-woocommerce' ); ?></th>
<td class="notice notice-error">
<p>
<?php
esc_html_e(
'Unfortunatly, the card processing option is not yet available in your country.',
'paypal-payments-for-woocommerce'
);
?>
<a
href="https://developer.paypal.com/docs/platforms/checkout/reference/country-availability-advanced-cards/"
target="_blank"
rel="noreferrer noopener"
>
<?php
esc_html_e(
'Click here to see, in which countries this option is currently available.',
'paypal-payments-for-woocommerce'
);
?>
</a>
</p>
</td>
</tr>
<?php
}
2020-08-27 11:08:36 +03:00
}