Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding multiple checkbox setting type #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Voce Settings API
Contributors: prettyboymp, kevinlangleyjr, banderon, voceplatforms
Tags: settings, api
Requires at least: 3.3
Tested up to: 4.2.2
Stable tag: 0.5.3
Tested up to: 4.7.1
Stable tag: 0.5.5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -96,10 +96,12 @@ Using the ```get_settting()``` method of the ```Voce_Settings_API```, you can re
* ```vs_display_dropdown```
* ```vs_display_textarea```
* ```vs_display_checkbox```
* ```vs_display_multiple_checkboxes```

### Sanitization Methods
* ```vs_sanitize_text``` *Default*
* ```vs_sanitize_checkbox```
* ```vs_sanitize_multiple_checkboxes```
* ```vs_sanitize_url```
* ```vs_sanitize_email```
* ```vs_sanitize_dropdown```
Expand Down
25 changes: 25 additions & 0 deletions display-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,28 @@ function vs_display_checkbox($value, $setting, $args) {
<br/><span class="description"><?php echo wp_kses_post( $args['description'] ); ?></span>
<?php endif;
}

function vs_display_multiple_checkboxes($value, $setting, $args) {
if( empty( $args['options'] ) ){
printf( '<p class="error">An options argument is required in the $args array to use %s</p>', __FUNCTION__ );
return;
}

if( !is_array( $value ) ){
$value = array( $value );
}

foreach( $args['options'] as $option_value => $option_text ){
printf( '<p><label><input type="checkbox" id="%s" name="%s" value="%s" %s /> %s</label></p>',
esc_attr( $setting->get_field_id() ),
esc_attr( $setting->get_field_name() . '[]' ),
esc_attr( $option_value ),
checked( in_array( $option_value, $value ), true, false ),
esc_attr( $option_text )
);
}

if(!empty($args['description'])){
printf( '<br/><span class="description">%s</span>', wp_kses_post( $args['description'] ) );
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "voce-settings-api",
"description": "Voce Settings API",
"version": "0.5.3",
"version": "0.5.5",
"repository": {
"type": "git",
"url": "[email protected]:voceconnect/voce-settings-api.git"
Expand Down
26 changes: 26 additions & 0 deletions sanitize-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ function vs_sanitize_int( $value ) {
return intval( $value );
}

/**
*
* @param variable $value
* @param Voce_Setting $setting
* @param array $args
* @return variable
*/
function vs_sanitize_multiple_checkboxes($values, $setting, $args) {
if( !is_array( $values ) || empty( $values ) ){
return false;
}

$sanitized_values = [];

foreach( $values as $value ){
$value = sanitize_text_field( $value );
if( ! in_array( $value, array_keys( $args['options'] ) ) ){
continue;
}

$sanitized_values[] = $value;
}

return $sanitized_values;
}

/* Deprecated Functions */
function vs_santize_checkbox($value, $setting, $args) {
_deprecated_function( __FUNCTION__, '0.2', 'vs_sanitize_checkbox()' );
Expand Down
4 changes: 2 additions & 2 deletions voce-settings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* A simplification of the settings API
* @author Michael Pretty (prettyboymp)
* @version 0.5.3
* @version 0.5.5
*/

if(!class_exists('Voce_Settings_API')) {
Expand All @@ -14,7 +14,7 @@ class Voce_Settings_API {

private $settings_pages;

CONST VERSION = '0.5.3';
CONST VERSION = '0.5.4';

/**
* Returns singleton instance of api
Expand Down