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

Optionally set checkbox values as 'true' or 'false' #682

Open
wants to merge 3 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
23 changes: 22 additions & 1 deletion inc/class-shortcode-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public function handle_ajax_bulk_do_shortcode() {
}

/**
* Decode any encoded attributes.
* Decode any encoded attributes, and convert true/false strings to their boolean equivalents.
*
* @param array $out The output array of shortcode attributes.
* @param array $pairs The supported attributes and their defaults.
Expand All @@ -452,13 +452,34 @@ public function filter_shortcode_atts_decode_encoded( $out, $pairs, $atts ) {
$default = isset( $fields[ $attr['type'] ]['encode'] ) ? $fields[ $attr['type'] ]['encode'] : false;
$encoded = isset( $attr['encode'] ) ? $attr['encode'] : $default;

$explicitbool = isset( $attr['explicitbool'] ) ? $attr['explicitbool'] : false;

if ( $encoded && isset( $out[ $attr['attr'] ] ) ) {
$out[ $attr['attr'] ] = rawurldecode( $out[ $attr['attr'] ] );
}

if ( $explicitbool && in_array( $out[ $attr['attr'] ], array( 'true', 'false' ), true ) ) {
$out[ $attr['attr'] ] = $this->explicitbool( $out[ $attr['attr'] ] );
}
}

return $out;

}

/**
* Convert 'true' or 'false' strings to their boolean equivalent.
*
* @param string Expected to be either 'true' or 'false'
* @return bool
*/
private function explicitbool( $bool_string ) {
if ( 'true' === $bool_string ) {
return true;
}
if ( 'false' === $bool_string ) {
return false;
}
return null;
}
}
7 changes: 6 additions & 1 deletion js-tests/build/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,12 @@ Shortcode = Backbone.Model.extend({

this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
// Send 'true' and 'false' as checkbox values if 'explicitbool' option is on
if ( attr.get( 'explicitbool' ) && 'boolean' === typeof attr.get( 'value' ) ) {
attr.set( 'value', attr.get( 'value' ) ? 'true' : 'false' );
}

// Otherwise, skip empty or false attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
return;
}
Expand Down
7 changes: 6 additions & 1 deletion js/build/shortcode-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ Shortcode = Backbone.Model.extend({

this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
// Send 'true' and 'false' as checkbox values if 'explicitbool' option is on
if ( attr.get( 'explicitbool' ) && 'boolean' === typeof attr.get( 'value' ) ) {
attr.set( 'value', attr.get( 'value' ) ? 'true' : 'false' );
}

// Otherwise, skip empty or false attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
return;
}
Expand Down
7 changes: 6 additions & 1 deletion js/src/models/shortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ Shortcode = Backbone.Model.extend({

this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
// Send 'true' and 'false' as checkbox values if 'explicitbool' option is on
if ( attr.get( 'explicitbool' ) && 'boolean' === typeof attr.get( 'value' ) ) {
attr.set( 'value', attr.get( 'value' ) ? 'true' : 'false' );
}

// Otherwise, skip empty or false attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
return;
}
Expand Down