<?php
namespace FinchCart;

/**
 * Get the value of a settings field
 *
 * @param string $option Settings field name.
 * @param string $section The section name this field belongs to.
 * @param string $default_value Default text if it's not found.
 *
 * @return mixed
 */
function fc_get_option( $option, $section, $default_value = '' ) {
	$options = get_option( $section );

	if ( isset( $options[ $option ] ) ) {
		return $options[ $option ];
	}

	return $default_value;
}

/**
 * Data attribute array to data types
 *
 * @param array $atts Data attributes array.
 * @return void
 */
function fc_data_attributes( $atts ) {
	if ( ! empty( $atts ) ) {
		foreach ( $atts as $key => $value ) {
			if ( ! is_array( $value ) ) {
				echo 'data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
			} elseif ( isset( $value['data'] ) && false === $value['data'] ) {
				echo esc_attr( $key ) . '="' . esc_attr( $value['value'] ) . '" ';
			} else {
				echo 'data-' . esc_attr( $key ) . '="' . esc_attr( $value['value'] ) . '" ';
			}
		}
	}
}

/**
 * Escaping SVG with KSES
 *
 * @return array
 */
function fc_kses_allowed_svg() {
	$kses_defaults = wp_kses_allowed_html( 'post' );

	$svg_args = array(
		'svg'      => array(
			'class'           => true,
			'aria-hidden'     => true,
			'aria-labelledby' => true,
			'role'            => true,
			'xmlns'           => true,
			'width'           => true,
			'height'          => true,
			'viewbox'         => true,
			'fill'            => true,
			'stroke'          => true,
			'stroke-width'    => true,
			'stroke-linecap'  => true,
			'stroke-linejoin' => true,

		),
		'g'        => array(
			'fill'      => true,
			'clip-path' => true,
		),
		'title'    => array( 'title' => true ),
		'polyline' => array( 'points' => true ),
		'path'     => array(
			'd'               => true,
			'fill'            => true,
			'stroke'          => true,
			'stroke-width'    => true,
			'stroke-linejoin' => true,
			'stroke-linecap'  => true,
			'fill-rule'       => true,
			'clip-rule'       => true,
		),
		'rect'     => array(
			'x'      => true,
			'y'      => true,
			'width'  => true,
			'height' => true,
			'fill'   => true,
			'rx'     => true,
			'ry'     => true,
		),
		'circle'   => array(
			'cx'              => true,
			'cy'              => true,
			'r'               => true,
			'fill'            => true,
			'stroke'          => true,
			'stroke-width'    => true,
			'stroke-linejoin' => true,
			'stroke-linecap'  => true,
			'fill-rule'       => true,
			'clip-rule'       => true,
			'transform'       => true,
		),
		'defs'     => true,
		'clipPath' => array(
			'id' => true,
		),
	);

	$allowed_tags = array_merge( $kses_defaults, $svg_args );

	return $allowed_tags;
}

/**
 * Clean variables using sanitize_text_field and wp_kses_post. Arrays are cleaned recursively.
 * Non-scalar values are ignored.
 *
 * @param string|array $data Data to sanitize.
 * @return string|array
 */
function finchcart_clean( $data ) {
	if ( is_array( $data ) ) {
		return array_map( 'FinchCart\finchcart_clean', $data );
	} elseif ( wp_strip_all_tags( $data ) !== $data ) {
		return $data;
	} else {
		return is_scalar( $data ) ? sanitize_text_field( $data ) : $data;
	}
}
