Skip to content

Commit

Permalink
Refactor: split gateways settings in tabs (#6923)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloiankoski authored Sep 8, 2023
1 parent fee6e54 commit 9ab426a
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 167 deletions.
18 changes: 13 additions & 5 deletions assets/src/css/admin/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,25 @@ div.give-field-description {
//--------------------------------------------------------------
// Payment Gateways
//--------------------------------------------------------------
.give-payment-gateways-settings {
&.give-settings-section-content {
.give-settings-section-group-menu {
width: 20rem;

ul li a {
padding: 1.25rem 1rem;
}
}
}
}

.gateway-enabled-wrap {
background-color: #f7f7f7;
border: 1px solid #e5e5e5;

.gateway-enabled-settings-title {
display: grid;
grid-template-columns: 0.5fr 2.5fr 5fr 1fr 1fr;
grid-gap: 20px;
border-bottom: 1px solid #e5e5e5;
padding: 1rem;
padding: 1rem 0;
font-weight: 600;
}

Expand All @@ -315,7 +323,7 @@ div.give-field-description {

.give-payment-gatways-list {
margin: 0;
padding: 1rem;
padding: 1rem 0;
}

.give-payment-gatways-list li {
Expand Down
234 changes: 155 additions & 79 deletions includes/admin/settings/class-settings-gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@ public function get_settings() {
],
],
[
'name' => __('Enabled Gateways', 'give'),
'name' => __('Enabled Gateways', 'give') . ' - v2',
'desc' => __('Enable your payment gateway. Can be ordered by dragging.', 'give'),
'id' => 'gateways',
'type' => 'enabled_gateways',
],
[
'name' => __('Enabled Gateways', 'give') . ' - v3',
'desc' => __('Enable your payment gateway. Can be ordered by dragging.', 'give'),
'id' => 'gateways_v3',
'type' => 'enabled_gateways_hidden',
],

/**
* "Enabled Gateways" setting field contains gateways label setting but when you save gateway settings then label will not save
Expand All @@ -135,11 +141,17 @@ public function get_settings() {
* This setting will not render on admin setting screen but help internal code to recognize "gateways_label" setting and add them to give setting when save.
*/
[
'name' => __('Gateways Label', 'give'),
'name' => __('Gateways Label', 'give') . ' - v2',
'desc' => '',
'id' => 'gateways_label',
'type' => 'gateways_label_hidden',
],
[
'name' => __('Gateways Label', 'give') . ' - v3',
'desc' => '',
'id' => 'gateways_label_v3',
'type' => 'gateways_label_hidden',
],

/**
* "Enabled Gateways" setting field contains default gateway setting but when you save gateway settings then this setting will not save
Expand All @@ -148,11 +160,17 @@ public function get_settings() {
* This setting will not render on admin setting screen but help internal code to recognize "default_gateway" setting and add them to give setting when save.
*/
[
'name' => __('Default Gateway', 'give'),
'name' => __('Default Gateway', 'give') . ' - v2',
'desc' => __('The gateway that will be selected by default.', 'give'),
'id' => 'default_gateway',
'type' => 'default_gateway_hidden',
],
[
'name' => __('Default Gateway', 'give') . ' - v3',
'desc' => __('The gateway that will be selected by default.', 'give'),
'id' => 'default_gateway_v3',
'type' => 'default_gateway_hidden',
],

[
'name' => __('Gateways Docs Link', 'give'),
Expand Down Expand Up @@ -315,94 +333,152 @@ public function render_gateway_notice($field, $settings)
* @param $field
* @param $settings
*/
public function render_enabled_gateways( $field, $settings ) {
$id = $field['id'];
$gateways = give_get_ordered_payment_gateways( give_get_payment_gateways() );
$gateways_label = give_get_option( 'gateways_label', [] );
$default_gateway = give_get_option( 'default_gateway', current( array_keys( $gateways ) ) );

ob_start();

echo '<div class="gateway-enabled-wrap">';

echo '<div class="gateway-enabled-settings-title">';
printf(
'
<span></span>
<span>%1$s</span>
<span>%2$s</span>
<span>%3$s</span>
<span>%4$s</span>
',
__( 'Gateway', 'give' ),
__( 'Label', 'give' ),
__( 'Default', 'give' ),
__( 'Enabled', 'give' )
);
echo '</div>';

echo '<ul class="give-checklist-fields give-payment-gatways-list">';
foreach ( $gateways as $key => $option ) :
$enabled = null;
if (is_array($settings) && array_key_exists($key, $settings)) {
$enabled = '1';
}

echo '<li>';
printf('<span class="give-drag-handle"><span class="dashicons dashicons-menu"></span></span>');
printf(
'<span class="admin-label">%1$s %2$s</span>',
esc_html($option['admin_label']),
!empty($option['admin_tooltip']) ? Give()->tooltips->render_help(
esc_attr($option['admin_tooltip'])
) : ''
);
public function render_enabled_gateways($field, $settings)
{
$id = $field['id'];
$gateways = give_get_payment_gateways();

$label = '';
if (!empty($gateways_label[$key])) {
$label = $gateways_label[$key];
}
$current_page = give_get_current_setting_page();
$current_tab = give_get_current_setting_tab();
$current_section = give_get_current_setting_section();

printf(
'<input class="checkout-label" type="text" id="%1$s[%2$s]" name="%1$s[%2$s]" value="%3$s" placeholder="%4$s"/>',
'gateways_label',
esc_attr($key),
esc_html($label),
esc_html($option['checkout_label'])
$v2Gateways = give_get_ordered_payment_gateways(
array_intersect_key($gateways, give()->gateways->getPaymentGateways(2)),
2
);
$v3Gateways = give_get_ordered_payment_gateways(
array_intersect_key($gateways, give()->gateways->getPaymentGateways(3)),
3
);

$groups = [
'v2' => [
'label' => __('Option-Based Form Editor', 'give'),
'gateways' => $v2Gateways,
'settings' => $settings,
'gatewaysLabel' => give_get_option('gateways_label', []),
'defaultGateway' => give_get_option('default_gateway', current(array_keys($v2Gateways))),
],
'v3' => [
'label' => __('Visual Form Builder', 'give'),
'gateways' => $v3Gateways,
'settings' => give_get_option('gateways_v3', []),
'gatewaysLabel' => give_get_option('gateways_label_v3', []),
'defaultGateway' => give_get_option('default_gateway_v3', current(array_keys($v3Gateways))),
],
];
$defaultGroup = current(array_keys($groups));

ob_start();

echo '<h4>' . __('Enabled Gateways', 'give') . '</h4>';
echo '<div class="give-settings-section-content give-payment-gateways-settings">';
echo '<div class="give-settings-section-group-menu">';
echo '<ul>';
foreach ($groups as $slug => $group) {
$current_group = !empty($_GET['group']) ? give_clean($_GET['group']) : $defaultGroup;
$active_class = ($slug === $current_group) ? 'active' : '';

echo sprintf(
'<li><a class="%1$s" href="%2$s" data-group="%3$s">%4$s</a></li>',
esc_html($active_class),
esc_url(
admin_url(
"edit.php?post_type=give_forms&page={$current_page}&tab={$current_tab}&section={$current_section}&group={$slug}"
)
),
esc_html($slug),
esc_html($group['label'])
);
}
echo '</ul>';
echo '</div>';

echo '<div class="give-settings-section-group-content">';
foreach ($groups as $slug => $group) :
$current_group = !empty($_GET['group']) ? give_clean($_GET['group']) : $defaultGroup;
$hide_class = $slug !== $current_group ? 'give-hidden' : '';
$suffix = $slug === 'v3' ? '_v3' : '';

printf(
'<input class="gateways-radio" type="radio" name="%1$s" value="%2$s" %3$s %4$s>',
'default_gateway',
$key,
checked($key, $default_gateway, false),
disabled(null, $enabled, false)
'<div id="give-settings-section-group-%1$s" class="give-settings-section-group %2$s">',
esc_attr($slug),
esc_html($hide_class)
);

echo '<div class="gateway-enabled-wrap">';
echo '<div class="gateway-enabled-settings-title">';
printf(
'<input class="gateways-checkbox" name="%1$s[%2$s]" id="%1$s[%2$s]" type="checkbox" value="1" %3$s data-payment-gateway="%4$s"/>',
esc_attr($id),
esc_attr($key),
checked('1', $enabled, false),
esc_html($option['admin_label'])
'
<span></span>
<span>%1$s</span>
<span>%2$s</span>
<span style="text-align: center;">%3$s</span>
<span style="text-align: center;">%4$s</span>
',
__('Gateway', 'give'),
__('Label', 'give'),
__('Default', 'give'),
__('Enabled', 'give')
);
echo '</li>';
echo '</div>';

echo '<ul class="give-checklist-fields give-payment-gatways-list">';
foreach ($group['gateways'] as $key => $option) :
$enabled = null;
if (is_array($group['settings']) && array_key_exists($key, $group['settings'])) {
$enabled = '1';
}

echo '<li>';
printf('<span class="give-drag-handle"><span class="dashicons dashicons-menu"></span></span>');
printf(
'<span class="admin-label">%1$s %2$s</span>',
esc_html($option['admin_label']),
!empty($option['admin_tooltip']) ? Give()->tooltips->render_help(
esc_attr($option['admin_tooltip'])
) : ''
);

$label = '';
if (!empty($group['gatewaysLabel'][$key])) {
$label = $group['gatewaysLabel'][$key];
}

printf(
'<input class="checkout-label" type="text" id="%1$s[%2$s]" name="%1$s[%2$s]" value="%3$s" placeholder="%4$s"/>',
'gateways_label' . $suffix,
esc_attr($key),
esc_html($label),
esc_html($option['checkout_label'])
);

printf(
'<input class="gateways-radio" type="radio" name="%1$s" value="%2$s" %3$s %4$s>',
'default_gateway' . $suffix,
$key,
checked($key, $group['defaultGateway'], false),
disabled(null, $enabled, false)
);

printf(
'<input class="gateways-checkbox" name="%1$s[%2$s]" id="%1$s[%2$s]" type="checkbox" value="1" %3$s data-payment-gateway="%4$s"/>',
esc_attr($id) . $suffix,
esc_attr($key),
checked('1', $enabled, false),
esc_html($option['admin_label'])
);
echo '</li>';
endforeach;
echo '</ul>';

echo '</div>'; // end gateway-enabled-wrap.
echo '</div>'; // end give-settings-section-group-content.
endforeach;
echo '</ul>';

echo '</div>'; // end gateway-enabled-wrap.
echo '</div>'; // end give-settings-section-content.

if (defined('GIVE_VERSION') && version_compare(GIVE_VERSION, '3.0.0', '<')) {
echo '<div style="padding-top: 1rem;"><p><sup>*</sup>(v2) GiveWP 3.0 is coming! In preparation for that, gateways that only work on forms created with GiveWP version 2.x.x are distinguished with a (v2) label.</p></div>';
} else {
echo '<div style="padding-top: 1rem;"><p><sup>*</sup>(v2) Gateways that only work on forms created with GiveWP version 2.x.x are distinguished with a (v2) label.</p></div>';
}

printf(
'<tr><th>%1$s</th><td>%2$s</td></tr>',
$field['title'],
ob_get_clean()
);
printf('<tr><td colspan="2" style="padding: 0">%s</td></tr>', ob_get_clean());
}
}

Expand Down
Loading

0 comments on commit 9ab426a

Please sign in to comment.