' ); $args = shortcode_parse_atts( $tag ); $return = array(); foreach ( $args as $key => $value ) { if ( is_int( $key ) ) { $return[ $value ] = 'true'; continue; } $return[ $key ] = $value; } return $return; } /** * Check if an element type is a void elements. * * @param string $element The element to check. * * @return bool */ public static function is_void_element( $element ) { $void_elements = array( 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr', ); return in_array( strtolower( $element ), $void_elements, true ); } /** * Build an HTML tag. * * @param string $element The element to build. * @param array $attributes The attributes for the tags. * @param string $content The element content. * * @return string */ public static function build_tag( $element, $attributes = array(), $content = '' ) { $parts = array( '<' . $element, ); if ( ! empty( $attributes ) ) { $parts[] = self::build_attributes( $attributes ); } $suffix = null; if ( self::is_void_element( $element ) ) { $parts[] = '/>'; } else { $parts[] = '>'; $suffix = $content . ''; } return implode( ' ', $parts ) . $suffix; } /** * Builds and sanitizes attributes for an HTML tag. * * @param array $attributes Array of key value attributes to build. * * @return string */ public static function build_attributes( $attributes ) { $parts = array(); foreach ( $attributes as $attribute => $value ) { if ( is_array( $value ) ) { if ( count( $value ) !== count( $value, COUNT_RECURSIVE ) ) { $value = wp_json_encode( $value ); } else { $value = implode( ' ', $value ); } } $parts[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"'; } return implode( ' ', $parts ); } /** * Generate a new ID for a group. * * @return string */ public static function generate_id() { return uniqid( 'nd' ); } /** * Get a sanitized input text field. * * Reads directly from the superglobals rather than filter_input(), which has a * long-standing PHP/SAPI bug where INPUT_POST/INPUT_GET can return NULL even * though the superglobal has the value. * * @param int $type The type to get. * @param string $var The value to get. * * @return mixed */ public static function get_sanitized_text( $type, $var ) { switch ( $type ) { case INPUT_POST: $value = isset( $_POST[ $var ] ) ? wp_unslash( $_POST[ $var ] ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing break; case INPUT_GET: $value = isset( $_GET[ $var ] ) ? wp_unslash( $_GET[ $var ] ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended break; case INPUT_SERVER: $value = isset( $_SERVER[ $var ] ) ? wp_unslash( $_SERVER[ $var ] ) : null; break; default: $value = null; } return null === $value ? null : sanitize_text_field( $value ); } }