Skip to content

Instantly share code, notes, and snippets.

@morganestes
Last active February 9, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganestes/015a4c30f90e6cf10350 to your computer and use it in GitHub Desktop.
Save morganestes/015a4c30f90e6cf10350 to your computer and use it in GitHub Desktop.
WP ARIA checked helpers
<?php
/**
* Outputs the html aria-checked attribute.
*
* Compares the first two arguments and if identical marks as checked
*
* @since 2016-02-08
* @see checked()
*
* @param mixed $checked One of the values to compare.
* @param mixed $current Optional. The other value to compare if not just true. Default true.
* @param bool $echo Optional. Whether to echo or just return the string. Default true.
* @return string HTML ARIA attribute with appropriate value.
*/
function aria_checked( $checked, $current = true, $echo = true ) {
return __aria_checked_selected_helper( $checked, $current, $echo, 'checked' );
}
/**
* Outputs the html aria-selected attribute.
*
* Compares the first two arguments and if identical marks as checked
*
* @since 2016-02-08
* @see selected()
*
* @param mixed $checked One of the values to compare.
* @param mixed $current Optional. The other value to compare if not just true. Default true.
* @param bool $echo Optional. Whether to echo or just return the string. Default true.
* @return string HTML ARIA attribute with appropriate value.
*/
function aria_selected( $checked, $current = true, $echo = true ) {
return __aria_checked_selected_helper( $checked, $current, $echo, 'selected' );
}
/**
* Outputs the html aria-disabled attribute.
*
* Compares the first two arguments and if identical marks as checked
*
* @since 2016-02-08
* @see disabled()
*
* @param mixed $checked One of the values to compare.
* @param mixed $current Optional. The other value to compare if not just true. Default true.
* @param bool $echo Optional. Whether to echo or just return the string. Default true.
* @return string HTML ARIA attribute with appropriate value.
*/
function aria_disabled( $checked, $current = true, $echo = true ) {
return __aria_checked_selected_helper( $checked, $current, $echo, 'disabled' );
}
/**
* Private helper function for checked, selected, and disabled.
*
* Compares the first two arguments and if identical marks as 'true'.
* This provides basic support for checked/selected true/false, but not
* 'undefined' or 'mixed'.
*
* @since 2016-02-08
* @access private
* @see __checked_selected_helper()
*
* @param mixed $helper One of the values to compare.
* @param mixed $current The other value to compare if not just true. Default true.
* @param bool $echo Whether to echo or just return the string.
* @param string $type The type of checked|selected|disabled we are doing.
* @return string HTML ARIA attribute with appropriate value, or empty string if invalid type.
*/
function __aria_checked_selected_helper( $helper, $current, $echo, $type ) {
//Whitelist the types to check.
$types = array( 'checked', 'selected', 'disabled' );
/**
* Filter the types of ARIA attributes that allow true/false values.
*
* @since 2016-02-08
*
* @param array $types A list of aria-$type attributes.
*/
$aria_bool_attributes = apply_filters( 'aria_bool_attributes', $types );
if ( ! in_array( $type, $aria_bool_attributes, true ) ) {
return '';
}
$val = ( (string) $helper === (string) $current ) ? 'true' : 'false';
$result = sprintf( ' aria-%1$s="%2$s"', $type, $val );
if ( $echo ) {
echo $result; //xss okay (use late-escaping)
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment