Skip to content

Commit

Permalink
Improve Settings System.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 16, 2023
1 parent 70a5d7b commit 88732ac
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 46 deletions.
1 change: 0 additions & 1 deletion assets/css/general.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#hcaptcha-options table tbody {
background: #fff;
}
Expand Down
17 changes: 15 additions & 2 deletions assets/css/settings-base.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
.wrap h1.hcaptcha-settings-header {
font-size: 34px;
font-weight: bold;
display: flex;
align-items: center;
}

.hcaptcha-logo {
width: 235px;
margin: 10px 0;
height: 64px;
margin: 10px 5px 10px 0;
}

.hcaptcha-settings-tabs {
Expand Down Expand Up @@ -37,6 +44,12 @@
margin: 1.5em 0 1em;
}

#hcaptcha-options table tbody tr td {
padding: 0;
margin: 0;
position: relative;
}

#hcaptcha-options table tr td input[type="checkbox"] {
display: inline;
border: none;
Expand Down
80 changes: 60 additions & 20 deletions src/php/Settings/Abstracts/SettingsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,11 @@ public function add_settings_page() {
* Invoke relevant settings_page() basing on tabs.
*/
public function settings_base_page() {
echo '<div class="wrap">';

$this->get_active_tab()->settings_page();

echo '</div>';
}

/**
Expand Down Expand Up @@ -727,11 +731,15 @@ private function print_checkbox_field( array $arguments ) {
$options_markup = '';
$iterator = 0;

if ( is_bool( $arguments['disabled'] ) ) {
$arguments['disabled'] = $arguments['disabled'] ? $arguments['options'] : [];
}

foreach ( $arguments['options'] as $key => $label ) {
$iterator ++;
$options_markup .= sprintf(
'<label for="%2$s_%7$s">' .
'<input id="%2$s_%7$s" name="%1$s[%2$s][]" type="%3$s" value="%4$s" %5$s />' .
'<input id="%2$s_%7$s" name="%1$s[%2$s][]" type="%3$s" value="%4$s" %5$s %8$s />' .
' %6$s' .
'</label>' .
'<br/>',
Expand All @@ -741,25 +749,29 @@ private function print_checkbox_field( array $arguments ) {
$key,
checked( in_array( $key, $value, true ), true, false ),
$label,
$iterator
$iterator,
disabled( in_array( $label, $arguments['disabled'], true ), true, false )
);
}

$element_disabled = empty( array_diff( $arguments['options'], $arguments['disabled'] ) );

printf(
'<fieldset %1$s>%2$s</fieldset>',
disabled( $arguments['disabled'], true, false ),
disabled( $element_disabled, true, false ),
wp_kses(
$options_markup,
[
'label' => [
'for' => [],
],
'input' => [
'id' => [],
'name' => [],
'type' => [],
'value' => [],
'checked' => [],
'id' => [],
'name' => [],
'type' => [],
'value' => [],
'checked' => [],
'disabled' => [],
],
'br' => [],
]
Expand All @@ -785,11 +797,15 @@ private function print_radio_field( array $arguments ) {
$options_markup = '';
$iterator = 0;

if ( is_bool( $arguments['disabled'] ) ) {
$arguments['disabled'] = $arguments['disabled'] ? $arguments['options'] : [];
}

foreach ( $arguments['options'] as $key => $label ) {
$iterator ++;
$options_markup .= sprintf(
'<label for="%2$s_%7$s">' .
'<input id="%2$s_%7$s" name="%1$s[%2$s]" type="%3$s" value="%4$s" %5$s />' .
'<input id="%2$s_%7$s" name="%1$s[%2$s]" type="%3$s" value="%4$s" %5$s %8$s />' .
' %6$s' .
'</label>' .
'<br/>',
Expand All @@ -799,25 +815,29 @@ private function print_radio_field( array $arguments ) {
$key,
checked( $value, $key, false ),
$label,
$iterator
$iterator,
disabled( in_array( $label, $arguments['disabled'], true ), true, false )
);
}

$element_disabled = empty( array_diff( $arguments['options'], $arguments['disabled'] ) );

printf(
'<fieldset %1$s>%2$s</fieldset>',
disabled( $arguments['disabled'], true, false ),
disabled( $element_disabled, true, false ),
wp_kses(
$options_markup,
[
'label' => [
'for' => [],
],
'input' => [
'id' => [],
'name' => [],
'type' => [],
'value' => [],
'checked' => [],
'id' => [],
'name' => [],
'type' => [],
'value' => [],
'checked' => [],
'disabled' => [],
],
'br' => [],
]
Expand All @@ -841,18 +861,26 @@ private function print_select_field( array $arguments ) {
}

$options_markup = '';

if ( is_bool( $arguments['disabled'] ) ) {
$arguments['disabled'] = $arguments['disabled'] ? $arguments['options'] : [];
}

foreach ( $arguments['options'] as $key => $label ) {
$options_markup .= sprintf(
'<option value="%s" %s>%s</option>',
'<option value="%s" %s %s>%s</option>',
$key,
selected( $value, $key, false ),
disabled( in_array( $label, $arguments['disabled'], true ), true, false ),
$label
);
}

$element_disabled = empty( array_diff( $arguments['options'], $arguments['disabled'] ) );

printf(
'<select %1$s name="%2$s[%3$s]">%4$s</select>',
disabled( $arguments['disabled'], true, false ),
disabled( $element_disabled, true, false ),
esc_html( $this->option_name() ),
esc_html( $arguments['field_id'] ),
wp_kses(
Expand All @@ -861,6 +889,7 @@ private function print_select_field( array $arguments ) {
'option' => [
'value' => [],
'selected' => [],
'disabled' => [],
],
]
)
Expand All @@ -883,22 +912,32 @@ private function print_multiple_select_field( array $arguments ) {
}

$options_markup = '';

if ( is_bool( $arguments['disabled'] ) ) {
$arguments['disabled'] = $arguments['disabled'] ? $arguments['options'] : [];
}

foreach ( $arguments['options'] as $key => $label ) {
$selected = '';

if ( is_array( $value ) && in_array( $key, $value, true ) ) {
$selected = selected( $key, $key, false );
}

$options_markup .= sprintf(
'<option value="%s" %s>%s</option>',
'<option value="%s" %s %s>%s</option>',
$key,
$selected,
disabled( in_array( $label, $arguments['disabled'], true ), true, false ),
$label
);
}

$element_disabled = empty( array_diff( $arguments['options'], $arguments['disabled'] ) );

printf(
'<select %1$s multiple="multiple" name="%2$s[%3$s][]">%4$s</select>',
disabled( $arguments['disabled'], true, false ),
disabled( $element_disabled, true, false ),
esc_html( $this->option_name() ),
esc_html( $arguments['field_id'] ),
wp_kses(
Expand All @@ -907,6 +946,7 @@ private function print_multiple_select_field( array $arguments ) {
'option' => [
'value' => [],
'selected' => [],
'disabled' => [],
],
]
)
Expand Down
43 changes: 20 additions & 23 deletions src/php/Settings/PluginSettingsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,26 @@ public function setup_fields() {
*/
public function settings_page() {
?>
<div class="wrap">
<img
<img
src="<?php echo esc_url( HCAPTCHA_URL . '/assets/images/hcaptcha-logo.svg' ); ?>"
alt="hCaptcha Logo"
class="hcaptcha-logo"
/>

<form
id="hcaptcha-options"
class="hcaptcha-<?php echo esc_attr( $this->section_title() ); ?>"
action="<?php echo esc_url( admin_url( 'options.php' ) ); ?>"
method="post">
<?php
do_settings_sections( $this->option_page() ); // Sections with options.
settings_fields( $this->option_group() ); // Hidden protection fields.

if ( ! empty( $this->form_fields ) ) {
submit_button();
}
?>
</form>
</div>
/>

<form
id="hcaptcha-options"
class="hcaptcha-<?php echo esc_attr( $this->section_title() ); ?>"
action="<?php echo esc_url( admin_url( 'options.php' ) ); ?>"
method="post">
<?php
do_settings_sections( $this->option_page() ); // Sections with options.
settings_fields( $this->option_group() ); // Hidden protection fields.

if ( ! empty( $this->form_fields ) ) {
submit_button();
}
?>
</form>
<?php
}

Expand All @@ -180,7 +178,6 @@ class="hcaptcha-<?php echo esc_attr( $this->section_title() ); ?>"
* @return string
*/
public function admin_footer_text( $text ) {

if ( ! $this->is_options_screen() ) {
return $text;
}
Expand All @@ -189,7 +186,7 @@ public function admin_footer_text( $text ) {

return wp_kses(
sprintf(
/* translators: 1: hCaptcha plugin name, 2: wp.org review link with stars, 3: wp.org review link with text. */
/* translators: 1: plugin name, 2: wp.org review link with stars, 3: wp.org review link with text. */
__( 'Please rate %1$s %2$s on %3$s</a>. Thank you!', 'hcaptcha-for-forms-and-more' ),
'<strong>hCaptcha for WordPress</strong>',
sprintf(
Expand All @@ -212,7 +209,7 @@ public function admin_footer_text( $text ) {
}

/**
* Show hCaptcha version in the update footer.
* Show plugin version in the update footer.
*
* @param string $content The content that will be printed.
*
Expand All @@ -224,7 +221,7 @@ public function update_footer( $content ) {
}

return sprintf(
/* translators: 1: hCaptcha version. */
/* translators: 1: plugin version. */
__( 'Version %s', 'hcaptcha-for-forms-and-more' ),
HCAPTCHA_VERSION
);
Expand Down

0 comments on commit 88732ac

Please sign in to comment.