$max ) { return $default; } return $val; } /** * Sanitize text (strip tags, keep basic punctuation). * * @param mixed $val * @return string */ function bongoto_woocommerce_sanitize_text( $val ) { return sanitize_text_field( (string) $val ); } /** * Sanitize URL. * * @param mixed $val * @return string */ function bongoto_woocommerce_sanitize_url( $val ) { return esc_url_raw( (string) $val ); } /** * Sanitize array of strings. * * @param mixed $val * @return array */ function bongoto_woocommerce_sanitize_text_array( $val ) { $val = (array) $val; return array_map( 'sanitize_text_field', wp_unslash( $val ) ); } /** * ------------------------------------------------------------------------- * Allowed HTML contexts for wp_kses() * ------------------------------------------------------------------------- */ /** * Return allowed HTML tags/attrs for a given context. * * @param string $context 'basic'|'form'|'button'|'inline' * @return array */ function bongoto_woocommerce_allowed_html( $context = 'basic' ) { switch ( $context ) { case 'form': $allowed = array( 'form' => array( 'action' => true, 'method' => true, 'class' => true, 'id' => true, ), 'input' => array( 'type' => true, 'name' => true, 'value' => true, 'class' => true, 'id' => true, 'placeholder' => true, 'checked' => true, 'selected' => true, ), 'button' => array( 'type' => true, 'class' => true, 'id' => true, ), 'label' => array( 'for' => true, 'class' => true, ), 'select' => array( 'name' => true, 'class' => true, 'id' => true, ), 'option' => array( 'value' => true, 'selected' => true, ), 'small' => array( 'class' => true ), 'span' => array( 'class' => true ), ); break; case 'button': $allowed = array( 'a' => array( 'href' => true, 'class' => true, 'id' => true, 'aria-label' => true, 'rel' => true, 'target' => true, ), 'button' => array( 'type' => true, 'class' => true, 'id' => true, ), 'span' => array( 'class' => true ), 'svg' => array( 'class' => true, 'width' => true, 'height' => true, 'viewBox' => true, 'aria-hidden' => true, 'focusable' => true, 'role' => true, ), 'path' => array( 'd' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin'=> true, ), 'circle' => array( 'cx' => true, 'cy' => true, 'r' => true, 'fill' => true, ), ); break; case 'inline': $allowed = array( 'span' => array( 'class' => true ), 'strong' => array(), 'em' => array(), 'code' => array(), 'br' => array(), ); break; case 'basic': default: $allowed = array( 'a' => array( 'href' => true, 'title' => true, 'rel' => true, 'target' => true, ), 'br' => array(), 'em' => array(), 'strong' => array(), 'p' => array( 'class' => true ), 'ul' => array( 'class' => true ), 'ol' => array( 'class' => true ), 'li' => array( 'class' => true ), ); break; } return $allowed; } /** * ------------------------------------------------------------------------- * Escape shortcuts (for echoing) * ------------------------------------------------------------------------- */ /** * Echo safe HTML (limited by context). * * @param string $html * @param string $context * @return void */ function bongoto_woocommerce_echo_kses( $html, $context = 'basic' ) { echo wp_kses( $html, bongoto_woocommerce_allowed_html( $context ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Echo safe attribute value. * * @param string $text * @return void */ function bongoto_woocommerce_attr( $text ) { echo esc_attr( $text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * ------------------------------------------------------------------------- * Misc small helpers * ------------------------------------------------------------------------- */ /** * Loose truthy detection for form values. * * @param mixed $val * @return bool */ function bongoto_woocommerce_is_truthy( $val ) { if ( is_bool( $val ) ) { return $val; } $val = is_string( $val ) ? strtolower( trim( $val ) ) : $val; return in_array( $val, array( 1, '1', 'true', 'yes', 'on', 'checked' ), true ); } /** * Safe remote request wrapper (GET/POST) with sane defaults. * * @param string $url * @param array $args * @param string $method 'GET'|'POST' * @return array|WP_Error */ function bongoto_woocommerce_remote_request( $url, $args = array(), $method = 'GET' ) { $defaults = array( 'timeout' => 15, 'sslverify' => true, 'headers' => array( 'Accept' => 'application/json' ), ); $args = wp_parse_args( $args, $defaults ); if ( 'POST' === strtoupper( $method ) ) { return wp_remote_post( esc_url_raw( $url ), $args ); } return wp_remote_get( esc_url_raw( $url ), $args ); }