Skip to content

Commit

Permalink
Allow multicheckbox field to have individual descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
brettsmason committed Nov 25, 2022
1 parent 4bea73c commit 92a7f0f
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions includes/classes/SettingsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ class SettingsApi {
*/
private $page_title;


/**
* Menu title for the settings page.
*
* @var string
*/
private $menu_title;


/**
* Capability for the settings page.
*
Expand All @@ -42,6 +40,13 @@ class SettingsApi {
*/
private $slug;

/**
* Menu position for the settings page.
*
* @var int
*/
private $position;

/**
* Sections array.
*
Expand All @@ -63,14 +68,16 @@ class SettingsApi {
* @param string $menu_title Menu title for the settings page.
* @param string $capability Capability for the settings page.
* @param string $slug Slug for the settings page.
* @param int $position Menu position for the settings page.
*/
public function __construct( $page_title, $menu_title, $capability, $slug ) {
public function __construct( $page_title, $menu_title, $capability, $slug, $position ) {

// Set variables.
$this->page_title = $page_title;
$this->menu_title = $menu_title;
$this->capability = $capability;
$this->slug = $slug;
$this->position = $position;

// Enqueue the admin scripts.
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
Expand Down Expand Up @@ -485,9 +492,9 @@ public function callback_checkbox( $args ) {
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );

$html = '<fieldset>';
$html .= sprintf( '<label for="plugin-name-%1$s[%2$s]">', $args['section'], $args['id'] );
$html .= sprintf( '<label for="' . $this->slug . '%1$s[%2$s]">', $args['section'], $args['id'] );
$html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
$html .= sprintf( '<input type="checkbox" class="checkbox" id="plugin-name-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked( $value, 'on', false ) );
$html .= sprintf( '<input type="checkbox" class="checkbox" id="' . $this->slug . '-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked( $value, 'on', false ) );
$html .= sprintf( '%1$s</label>', $args['desc'] );
$html .= '</fieldset>';

Expand All @@ -504,11 +511,14 @@ public function callback_multicheck( $args ) {
$value = $this->get_option( $args['id'], $args['section'], $args['std'] );

$html = '<fieldset>';
foreach ( $args['options'] as $key => $label ) {
foreach ( $args['options'] as $key => $value ) {
$label = is_array( $value ) ? $value['label'] : $value;
$checked = isset( $value[ $key ] ) ? $value[ $key ] : '0';
$html .= sprintf( '<label for="plugin-name-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
$html .= sprintf( '<input type="checkbox" class="checkbox" id="plugin-name-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $checked, $key, false ) );

$html .= sprintf( '<label for="' . $this->slug . '-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
$html .= sprintf( '<input type="checkbox" class="checkbox" id="' . $this->slug . '-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $checked, $key, false ) );
$html .= sprintf( '%1$s</label><br>', $label );
$html .= $this->get_field_description( $value );
}
$html .= $this->get_field_description( $args );
$html .= '</fieldset>';
Expand All @@ -527,8 +537,8 @@ public function callback_radio( $args ) {

$html = '<fieldset>';
foreach ( $args['options'] as $key => $label ) {
$html .= sprintf( '<label for="plugin-name-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
$html .= sprintf( '<input type="radio" class="radio" id="plugin-name-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $value, $key, false ) );
$html .= sprintf( '<label for="' . $this->slug . '-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key );
$html .= sprintf( '<input type="radio" class="radio" id="' . $this->slug . '-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked( $value, $key, false ) );
$html .= sprintf( '%1$s</label><br>', $label );
}
$html .= $this->get_field_description( $args );
Expand Down Expand Up @@ -728,7 +738,8 @@ public function admin_menu() {
$this->menu_title,
$this->capability,
$this->slug,
[ $this, 'plugin_page' ]
[ $this, 'plugin_page' ],
$this->position,
);
}

Expand Down Expand Up @@ -930,9 +941,17 @@ public function script() {
left: 0;
width: 99%;
}

.group .form-table input.color-picker {
max-width: 100px;
}

/**
* Space for multi checkbox and multi radio fields with their own descriptions.
*/
.group .form-table .description + label {
margin-top: 20px !important;
}
</style>
<?php
}
Expand Down

0 comments on commit 92a7f0f

Please sign in to comment.