<?php
/**
 * Data sanitization functions
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}

/**
 * Sanitization methods for theme settings
 */
class Multimagazine_Sanitizer {

	/**
	 * Sanitizes checkbox value
	 *
	 * @param boolean $checked Checkbox value.
	 */
	public static function checkbox( $checked ) {
		return isset( $checked ) && ( true === $checked || 1 === $checked );
	}

	/**
	 * Sanitizes integer value
	 *
	 * @param integer $number Form field value.
	 */
	public static function number_int( $number ) {
		$number = intval( $number );
		return $number;
	}

	/**
	 * Sanitizes size value
	 *
	 * @param string $number Form field value.
	 */
	public static function size( $number ) {
		if ( '' !== $number ) {
			$number = floatval( $number );
		}
		return $number;
	}

	/**
	 * Sanitizes image value
	 *
	 * @param string $image Image url.
	 * @param object $setting Setting object.
	 */
	public static function image( $image, $setting ) {
		$mimes = array(
			'jpg|jpeg|jpe' => 'image/jpeg',
			'gif'          => 'image/gif',
			'png'          => 'image/png',
			'ico'          => 'image/x-icon',
		);
		$file  = wp_check_filetype( $image, $mimes );
		return ( $file['ext'] ? $image : $setting->default );
	}

	/**
	 * Sanitizes media value
	 *
	 * @param string $image Image or video url.
	 * @param object $setting Setting object.
	 */
	public static function media( $image, $setting ) {
		$mimes = array(
			'jpg|jpeg|jpe' => 'image/jpeg',
			'gif'          => 'image/gif',
			'png'          => 'image/png',
			'ico'          => 'image/x-icon',
			'mp4'          => 'video/mp4',
		);
		$file  = wp_check_filetype( $image, $mimes );
		return ( $file['ext'] ? $image : $setting->default );
	}

	/**
	 * Sanitizes json value
	 *
	 * @param string $file File mime type.
	 * @param object $setting Setting object.
	 */
	public static function json( $file, $setting ) {
		$mimes = array(
			'json' => 'application/json',
		);
		$file  = wp_check_filetype( $file, $mimes );
		return ( $file['ext'] ? $file : $setting->default );
	}

	/* adsense() method moved to Multi_Pro_Sanitizer (Multi Pro plugin). */

	/**
	 * Sanitizes text with simple html formatting
	 *
	 * @param string $string Markup.
	 */
	public static function simple_html( $string ) {
		$string = wp_kses(
			$string,
			array(
				'a'      => array(
					'href'   => array(),
					'title'  => array(),
					'rel'    => array(),
					'target' => array(),
					'class'  => array(),
				),
				'b'      => array(),
				'i'      => array(),
				'strong' => array(),
				'em'     => array(),
			)
		);

		return $string;
	}
}

// Backward-compatible function aliases.

/**
 * Sanitizes checkbox value
 *
 * @param boolean $checked Checkbox value.
 */
function multimagazine_sanitize_checkbox( $checked ) {
	return Multimagazine_Sanitizer::checkbox( $checked );
}

/**
 * Sanitizes size value
 *
 * @param string $number Form field value.
 */
function multimagazine_sanitize_size( $number ) {
	return Multimagazine_Sanitizer::size( $number );
}

/**
 * Sanitizes image value
 *
 * @param string $image Image url.
 * @param object $setting Setting object.
 */
function multimagazine_sanitize_image( $image, $setting ) {
	return Multimagazine_Sanitizer::image( $image, $setting );
}

/**
 * Sanitizes media value
 *
 * @param string $image Image or video url.
 * @param object $setting Setting object.
 */
function multimagazine_sanitize_media( $image, $setting ) {
	return Multimagazine_Sanitizer::media( $image, $setting );
}

/**
 * Sanitizes json value
 *
 * @param string $file File mime type.
 * @param object $setting Setting object.
 */
function multimagazine_sanitize_json( $file, $setting ) {
	return Multimagazine_Sanitizer::json( $file, $setting );
}

/* multimagazine_sanitize_adsense() moved to Multi_Pro_Sanitizer (Multi Pro plugin). */

/**
 * Sanitizes text with simple html formatting
 *
 * @param string $string Markup.
 */
function multimagazine_sanitize_simple_html( $string ) {
	return Multimagazine_Sanitizer::simple_html( $string );
}
