123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- <?php
- /**
- * acf_filter_attrs
- *
- * Filters out empty attrs from the provided array.
- *
- * @date 11/6/19
- * @since 5.8.1
- *
- * @param array $attrs The array of attrs.
- * @return array
- */
- function acf_filter_attrs( $attrs ) {
- // Filter out empty attrs but allow "0" values.
- $filtered = array_filter( $attrs, 'acf_not_empty' );
- // Correct specific attributes (required="required").
- foreach ( array( 'required', 'readonly', 'disabled', 'multiple' ) as $key ) {
- unset( $filtered[ $key ] );
- if ( ! empty( $attrs[ $key ] ) ) {
- $filtered[ $key ] = $key;
- }
- }
- return $filtered;
- }
- /**
- * acf_esc_attrs
- *
- * Generated valid HTML from an array of attrs.
- *
- * @date 11/6/19
- * @since 5.8.1
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_esc_attrs( $attrs ) {
- $html = '';
- // Loop over attrs and validate data types.
- foreach ( $attrs as $k => $v ) {
- // String (but don't trim value).
- if ( is_string( $v ) && ( $k !== 'value' ) ) {
- $v = trim( $v );
- // Boolean
- } elseif ( is_bool( $v ) ) {
- $v = $v ? 1 : 0;
- // Object
- } elseif ( is_array( $v ) || is_object( $v ) ) {
- $v = json_encode( $v );
- }
- // Generate HTML.
- $html .= sprintf( ' %s="%s"', esc_attr( $k ), esc_attr( $v ) );
- }
- // Return trimmed.
- return trim( $html );
- }
- /**
- * Sanitizes text content and strips out disallowed HTML.
- *
- * This function emulates `wp_kses_post()` with a context of "acf" for extensibility.
- *
- * @date 16/4/21
- * @since 5.9.6
- *
- * @param string $string
- * @return string
- */
- function acf_esc_html( $string = '' ) {
- return wp_kses( (string) $string, 'acf' );
- }
- /**
- * Private callback for the "wp_kses_allowed_html" filter used to return allowed HTML for "acf" context.
- *
- * @date 16/4/21
- * @since 5.9.6
- *
- * @param array $tags An array of allowed tags.
- * @param string $context The context name.
- * @return array.
- */
- function _acf_kses_allowed_html( $tags, $context ) {
- global $allowedposttags;
- if ( $context === 'acf' ) {
- return $allowedposttags;
- }
- return $tags;
- }
- add_filter( 'wp_kses_allowed_html', '_acf_kses_allowed_html', 0, 2 );
- /**
- * acf_html_input
- *
- * Returns the HTML of an input.
- *
- * @date 13/6/19
- * @since 5.8.1
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- // function acf_html_input( $attrs = array() ) {
- // return sprintf( '<input %s/>', acf_esc_attrs($attrs) );
- // }
- /**
- * acf_hidden_input
- *
- * Renders the HTML of a hidden input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_hidden_input( $attrs = array() ) {
- echo acf_get_hidden_input( $attrs );
- }
- /**
- * acf_get_hidden_input
- *
- * Returns the HTML of a hidden input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_get_hidden_input( $attrs = array() ) {
- return sprintf( '<input type="hidden" %s/>', acf_esc_attrs( $attrs ) );
- }
- /**
- * acf_text_input
- *
- * Renders the HTML of a text input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_text_input( $attrs = array() ) {
- echo acf_get_text_input( $attrs );
- }
- /**
- * acf_get_text_input
- *
- * Returns the HTML of a text input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_get_text_input( $attrs = array() ) {
- $attrs = wp_parse_args(
- $attrs,
- array(
- 'type' => 'text',
- )
- );
- if ( isset( $attrs['value'] ) && is_string( $attrs['value'] ) ) {
- $attrs['value'] = htmlspecialchars( $attrs['value'] );
- }
- return sprintf( '<input %s/>', acf_esc_attrs( $attrs ) );
- }
- /**
- * acf_file_input
- *
- * Renders the HTML of a file input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_file_input( $attrs = array() ) {
- echo acf_get_file_input( $attrs );
- }
- /**
- * acf_get_file_input
- *
- * Returns the HTML of a file input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_get_file_input( $attrs = array() ) {
- $field_key = isset( $attrs['key'] ) && is_string( $attrs['key'] ) ? $attrs['key'] : '';
- $nonce_field = '';
- /**
- * If we don't have a field key (most likely because this was called by a third-party field),
- * we have to try to guess the field key based on the field name.
- */
- if ( '' === $field_key ) {
- $parts = explode( '[', $attrs['name'] );
- if ( is_array( $parts ) && ! empty( $parts[1] ) ) {
- // Remove the trailing `]`.
- $field_key = substr( end( $parts ), 0, -1 );
- }
- }
- /**
- * We only output the nonce if we have a field key, as it's possible to render
- * the file input without a real field. But, basic uploaders that don't have any
- * custom logic will likely fail to upload anyway if they don't have a field key.
- */
- if ( '' !== $field_key ) {
- $nonce_attrs = array(
- 'name' => 'acf[' . $field_key . '_file_nonce]',
- 'value' => wp_create_nonce( 'acf/file_uploader_nonce/' . $field_key ),
- );
- $nonce_field = sprintf(
- '<input type="hidden" %s />',
- acf_esc_attrs( $nonce_attrs )
- );
- }
- return sprintf(
- '<input type="file" %1$s />%2$s',
- acf_esc_attrs( $attrs ),
- $nonce_field
- );
- }
- /**
- * acf_textarea_input
- *
- * Renders the HTML of a textarea input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_textarea_input( $attrs = array() ) {
- echo acf_get_textarea_input( $attrs );
- }
- /**
- * acf_get_textarea_input
- *
- * Returns the HTML of a textarea input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_get_textarea_input( $attrs = array() ) {
- $value = '';
- if ( isset( $attrs['value'] ) ) {
- $value = $attrs['value'];
- unset( $attrs['value'] );
- }
- return sprintf( '<textarea %s>%s</textarea>', acf_esc_attrs( $attrs ), esc_textarea( $value ) );
- }
- /**
- * acf_checkbox_input
- *
- * Renders the HTML of a checkbox input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_checkbox_input( $attrs = array() ) {
- echo acf_get_checkbox_input( $attrs );
- }
- /**
- * acf_get_checkbox_input
- *
- * Returns the HTML of a checkbox input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_get_checkbox_input( $attrs = array() ) {
- // Allow radio or checkbox type.
- $attrs = wp_parse_args(
- $attrs,
- array(
- 'type' => 'checkbox',
- )
- );
- // Get label.
- $label = '';
- if ( isset( $attrs['label'] ) ) {
- $label = $attrs['label'];
- unset( $attrs['label'] );
- }
- // Render.
- $checked = isset( $attrs['checked'] );
- return '<label' . ( $checked ? ' class="selected"' : '' ) . '><input ' . acf_esc_attr( $attrs ) . '/> ' . acf_esc_html( $label ) . '</label>';
- }
- /**
- * acf_radio_input
- *
- * Renders the HTML of a radio input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_radio_input( $attrs = array() ) {
- echo acf_get_radio_input( $attrs );
- }
- /**
- * acf_get_radio_input
- *
- * Returns the HTML of a radio input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_get_radio_input( $attrs = array() ) {
- $attrs['type'] = 'radio';
- return acf_get_checkbox_input( $attrs );
- }
- /**
- * acf_select_input
- *
- * Renders the HTML of a select input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_select_input( $attrs = array() ) {
- echo acf_get_select_input( $attrs );
- }
- /**
- * acf_select_input
- *
- * Returns the HTML of a select input.
- *
- * @date 3/02/2014
- * @since 5.0.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_get_select_input( $attrs = array() ) {
- $value = (array) acf_extract_var( $attrs, 'value' );
- $choices = (array) acf_extract_var( $attrs, 'choices' );
- return sprintf(
- '<select %s>%s</select>',
- acf_esc_attrs( $attrs ),
- acf_walk_select_input( $choices, $value )
- );
- }
- /**
- * acf_walk_select_input
- *
- * Returns the HTML of a select input's choices.
- *
- * @date 27/6/17
- * @since 5.6.0
- *
- * @param array $choices The choices to walk through.
- * @param array $values The selected choices.
- * @param array $depth The current walk depth.
- * @return string
- */
- function acf_walk_select_input( $choices = array(), $values = array(), $depth = 0 ) {
- $html = '';
- // Sanitize values for 'selected' matching (only once).
- if ( $depth == 0 ) {
- $values = array_map( 'esc_attr', $values );
- }
- // Loop over choices and append to html.
- if ( $choices ) {
- foreach ( $choices as $value => $label ) {
- // Multiple (optgroup)
- if ( is_array( $label ) ) {
- $html .= sprintf(
- '<optgroup label="%s">%s</optgroup>',
- esc_attr( $value ),
- acf_walk_select_input( $label, $values, $depth + 1 )
- );
- // single (option)
- } else {
- $attrs = array(
- 'value' => $value,
- );
- // If is selected.
- $pos = array_search( esc_attr( $value ), $values );
- if ( $pos !== false ) {
- $attrs['selected'] = 'selected';
- $attrs['data-i'] = $pos;
- }
- $html .= sprintf( '<option %s>%s</option>', acf_esc_attr( $attrs ), esc_html( $label ) );
- }
- }
- }
- return $html;
- }
- /**
- * acf_clean_atts
- *
- * See acf_filter_attrs().
- *
- * @date 3/10/17
- * @since 5.6.3
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_clean_atts( $attrs ) {
- return acf_filter_attrs( $attrs );
- }
- /**
- * acf_esc_atts
- *
- * See acf_esc_attrs().
- *
- * @date 27/6/17
- * @since 5.6.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_esc_atts( $attrs ) {
- return acf_esc_attrs( $attrs );
- }
- /**
- * acf_esc_attr
- *
- * See acf_esc_attrs().
- *
- * @date 13/6/19
- * @since 5.8.1
- * @deprecated 5.6.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_esc_attr( $attrs ) {
- return acf_esc_attrs( $attrs );
- }
- /**
- * acf_esc_attr_e
- *
- * See acf_esc_attrs().
- *
- * @date 13/6/19
- * @since 5.8.1
- * @deprecated 5.6.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_esc_attr_e( $attrs ) {
- echo acf_esc_attrs( $attrs );
- }
- /**
- * acf_esc_atts_e
- *
- * See acf_esc_attrs().
- *
- * @date 13/6/19
- * @since 5.8.1
- * @deprecated 5.6.0
- *
- * @param array $attrs The array of attrs.
- * @return string
- */
- function acf_esc_atts_e( $attrs ) {
- echo acf_esc_attrs( $attrs );
- }
|