<?php
/**
 * Utils functions.
 *
 * Enqueue util scripts, CSS util functions.
 *
 * @package    Cherry_Framework
 * @subpackage Functions
 * @author     Cherry Team <support@cherryframework.com>
 * @copyright  Copyright (c) 2012 - 2015, Cherry Team
 * @link       http://www.cherryframework.com/
 * @license    http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

// Load Cherry Framework scripts.
add_action( 'wp_enqueue_scripts', 'cherry_enqueue_utility_scripts' );

/**
 * Enqueue utility scripts.
 * @since 4.0.0
 */
function cherry_enqueue_utility_scripts() {
	global $is_chrome;

	$cherry_uri = trailingslashit( CHERRY_URI );

	if ( 'false' != cherry_get_option( 'general-smoothscroll' ) ) {
		wp_register_script(
			'jquery-easing',
			esc_url( $cherry_uri . 'assets/js/jquery.easing.1.3.min.js' ),
			array( 'jquery' ),
			'1.3.0',
			true
		);
		wp_register_script(
			'jquery-smoothscroll',
			esc_url( $cherry_uri . 'assets/js/jquery.smoothscroll.js' ),
			array( 'jquery', 'jquery-easing' ),
			'3.0.6',
			true
		);

		if ( ! wp_is_mobile() && $is_chrome ) {
			wp_enqueue_script( 'jquery-smoothscroll' );
		}
	}

	if ( 'false' != cherry_get_option( 'header-sticky' ) ) {
		wp_enqueue_script(
			'cherry-stick-up',
			esc_url( $cherry_uri . 'assets/js/jquery.cherry.stickup.min.js' ),
			array( 'jquery' ),
			'1.0.0',
			true
		);
	}

	if ( 'false' != cherry_get_option( 'cookie-banner-visibility' )
		&& !( isset( $_COOKIE['cherry_cookie_banner'] ) && '1' == $_COOKIE['cherry_cookie_banner'] )
		) {
		wp_enqueue_script(
			'cherry-cookie-banner',
			esc_url( $cherry_uri . 'assets/js/jquery.cherry.cookie.banner.min.js' ),
			array( 'jquery' ),
			'1.0.0',
			true
		);
		wp_localize_script( 'cherry-cookie-banner', 'cookie_banner_args', array(
				'name'    => 'cherry_cookie_banner',
				'value'   => '1',
				'options' => array(
					'expires' => YEAR_IN_SECONDS,
					'path'    => ( defined( 'COOKIEPATH' ) ? COOKIEPATH : '/' ),
					'domain'  => ( defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : '' ),
				),
			)
		);
	}
}

/**
 * Returns an array of the core framework's available styles for use in themes.
 *
 * @since  4.0.0
 * @return array $styles All the available framework styles.
 */
function cherry_get_styles() {
	// Get the theme prefix.
	$prefix = cherry_get_prefix();

	// Get the active theme stylesheet version.
	$version = wp_get_theme()->get( 'Version' );

	// Prepare set of stylesheets.
	$styles = array();

	// Get the theme-supported stylesheets.
	$supports = get_theme_support( 'cherry-styles' );

	// If the theme support for any styles.
	if ( ! empty( $supports[0] ) && is_array( $supports[0] ) ) :

		$drop_downs = array(
			'handle'  => get_template() . '-drop-downs',
			'src'     => trailingslashit( CHERRY_URI ) . 'assets/css/drop-downs.css',
			'version' => '1.0.0',
		);

		// Is responsive site?
		$responsive = cherry_get_option( 'grid-responsive' );

		$grid_responsive = ( 'true' == $responsive ) ?
			array(
				'handle'  => $prefix . 'grid-responsive',
				'src'     => cherry_file_uri( 'assets/css/grid-responsive.css' ),
				'version' => $version,
			) : false;

		$main_responsive = ( 'true' == $responsive ) ?
			array(
				'handle'  => $prefix . 'main-responsive',
				'src'     => cherry_file_uri( 'assets/css/main-responsive.css' ),
				'version' => $version,
			) : false;

		/**
		 * Filter a set of default framework styles.
		 *
		 * @since 4.0.0
		 * @param array $defaults Default styles.
		 */
		$defaults = apply_filters( 'cherry_get_styles_defaults', array(
			'grid-base' => array(
				'handle'  => $prefix . 'grid-base',
				'src'     => cherry_file_uri( 'assets/css/grid-base.css' ),
				'version' => $version,
			),
			'grid-responsive' => $grid_responsive,
			'drop-downs'      => $drop_downs,
			'magnific-popup'  => array(
				'handle'  => 'magnific-popup',
				'src'     => trailingslashit( CHERRY_URI ) . 'assets/css/magnific-popup.css',
				'version' => '1.0.0',
			),
			'slick' => array(
				'handle'  => 'slick',
				'src'     => trailingslashit( CHERRY_URI ) . 'assets/css/slick.css',
				'version' => '1.5.0',
			),
			'main' => array(
				'handle'  => $prefix . 'main',
				'src'     => cherry_file_uri( 'assets/css/main.css' ),
				'version' => $version,
			),
			'main-responsive' => $main_responsive,
			'add-ons' => array(
				'handle'  => get_template() . '-add-ons',
				'src'     => trailingslashit( CHERRY_URI ) . 'assets/css/add-ons.css',
				'version' => CHERRY_VERSION,
			),
		) );

		foreach ( $supports[0] as $s ) {

			if ( empty( $defaults[ $s ] ) ) {
				continue;
			}

			if ( ! is_array( $defaults[ $s ] ) ) {
				continue;
			}

			$styles[ $s ] = $defaults[ $s ];
		}

	endif;

	// Add the main stylesheet (this must be included).
	$styles['style'] = array(
		'handle'  => $prefix . 'style',
		'src'     => get_stylesheet_uri(),
		'version' => $version,
	);

	/**
	 * Filters the array of styles.
	 *
	 * @since 4.0.0
	 * @param array $styles Array of styles.
	 */
	return apply_filters( 'cherry_get_styles', $styles );
}

/**
 * Make passed color darken.
 *
 * @since  4.0.0
 * @param  string  $color  HEX or RGB(A) color value.
 * @param  float   $darken Darken percent (0-100).
 * @return string          Processed color.
 */
function cherry_colors_darken( $color, $darken = 0 ) {

	if ( ! $color ) {
		return false;
	}

	$prepared_data = cherry_prepare_color_mod( $color, $darken );

	if ( ! $prepared_data || ! is_array( $prepared_data ) ) {
		return false;
	}

	$r       = $prepared_data['r'];
	$g       = $prepared_data['g'];
	$b       = $prepared_data['b'];
	$a       = $prepared_data['a'];
	$percent = $prepared_data['percent'];

	// Calc darken vals.
	$r = round( $r - 255*$percent, 0 );
	$g = round( $g - 255*$percent, 0 );
	$b = round( $b - 255*$percent, 0 );

	$r = $r < 0 ? 0 : $r;
	$g = $g < 0 ? 0 : $g;
	$b = $b < 0 ? 0 : $b;

	if ( false !== $a ) {
		return sprintf( 'rgba(%s,%s,%s,%s)', $r, $g, $b, $a );
	} else {
		return sprintf( 'rgb(%s,%s,%s)', $r, $g, $b );
	}
}

/**
 * Make passed color lighten.
 *
 * @since  4.0.0
 * @param  string  $color   HEX or RGB(A) color value.
 * @param  float   $lighten Lighten percent (0-100).
 * @return string           Processed color.
 */
function cherry_colors_lighten( $color, $lighten = 0 ) {

	if ( ! $color ) {
		return false;
	}

	$prepared_data = cherry_prepare_color_mod( $color, $lighten );

	if ( ! $prepared_data || ! is_array( $prepared_data ) ) {
		return false;
	}

	$r       = $prepared_data['r'];
	$g       = $prepared_data['g'];
	$b       = $prepared_data['b'];
	$a       = $prepared_data['a'];
	$percent = $prepared_data['percent'];

	// Calc lighten vals
	$r = round( $r + 255*$percent, 0 );
	$g = round( $g + 255*$percent, 0 );
	$b = round( $b + 255*$percent, 0 );

	$r = $r > 255 ? 255 : $r;
	$g = $g > 255 ? 255 : $g;
	$b = $b > 255 ? 255 : $b;

	if ( false !== $a ) {
		return sprintf( 'rgba(%s,%s,%s,%s)', $r, $g, $b, $a );
	} else {
		return sprintf( 'rgb(%s,%s,%s)', $r, $g, $b );
	}
}

/**
 * Select contrast color for passed from 2 proposed.
 *
 * 1st proposed color must be light - it will selected if passed color is dark,
 * 2nd selected if passed is light, so it must be darken.
 *
 * @since  4.0.0
 * @param  string $color     Color to get contrast for.
 * @param  string $if_dark   Return this if we had dark color.
 * @param  string $if_light  Return this if we had light color.
 * @return string            Color.
 */
function cherry_contrast_color( $color, $if_dark = '#ffffff', $if_light = '#000000' ) {

	if ( ! $color ) {
		return false;
	}

	$prepared_data = cherry_prepare_color_mod( $color, 100 );

	if ( ! $prepared_data || ! is_array( $prepared_data ) ) {
		return false;
	}

	$r = $prepared_data['r'];
	$g = $prepared_data['g'];
	$b = $prepared_data['b'];

	$luminance = 0.299 * $r + 0.587 * $g + 0.114 * $b;

	if ( $luminance >= 127 ) {
		return $if_light;
	} else {
		return $if_dark;
	}
}

/**
 * Prepare color to modify.
 *
 * Bring passed color and change percent to array
 * with R, G, B color values, opacity (if provided)
 * and change percentage.
 *
 * @since  4.0.0
 * @param  string $color   HEX or RGB(A) color value.
 * @param  float  $percent Modify percent (0-100).
 * @return array           Prepared color and modify percent.
 */
function cherry_prepare_color_mod( $color, $percent = 0 ) {
	$is_rgba = ( false !== strpos( $color, 'rgba' ) ) ? true : false;
	$is_rgb  = ( false !== strpos( $color, 'rgb' ) && false === $is_rgba ) ? true : false;
	$is_hex  = ( false === $is_rgba && false === $is_rgb ) ? true : false;

	$percent = round( (double)$percent / 100, 4 );

	if ( $is_hex && '#' == $color[0] ) {
		$color = substr( $color, 1 );
	}

	// Prepare hex color.
	if ( $is_hex && strlen( $color ) == 6 ) {
		list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
	} elseif ( $is_hex && strlen( $color ) == 3 ) {
		list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
	} elseif ( $is_hex ) {
		return false;
	}

	if ( $is_hex ) {
		$r = hexdec( $r );
		$g = hexdec( $g );
		$b = hexdec( $b );
	}

	$color = str_replace( ' ', '', $color );

	// Prepare RGBA.
	if ( $is_rgba ) {
		preg_match( '/rgba\((.*)\)/', $color, $matches );
		if ( ! is_array( $matches ) || empty( $matches[1] ) ) {
			return false;
		}
		list( $r, $g, $b, $a ) = explode( ',', $matches[1] );
	}

	// Prepare RGB.
	if ( $is_rgb ) {
		preg_match( '/rgb\((.*)\)/', $color, $matches );
		if ( ! is_array( $matches ) || empty( $matches[1] ) ) {
			return false;
		}
		list( $r, $g, $b ) = explode( ',', $matches[1] );
	}

	$result = array(
		'r'       => $r,
		'g'       => $g,
		'b'       => $b,
		'a'       => isset( $a ) ? $a : false,
		'percent' => $percent,
	);

	return $result;
}

/**
 * Get background CSS by bg data from options and selector.
 * If passed multiplie images - returns retina ready CSS.
 *
 * @since  4.0.0
 * @param  string $selector CSS selector to apply bg for.
 * @param  array  $data     data-array from options.
 * @return string
 */
function cherry_get_background_css( $selector, $data ) {

	if ( ! $selector ) {
		return;
	}

	if ( ! is_array( $data ) ) {
		return;
	}

	if ( empty( $data['image'] ) && empty( $data['color'] ) ) {
		return;
	}

	$standard_bg = cherry_prepare_background( $data );

	if ( empty( $data['image'] ) ) {
		$standard_bg .= 'background-image:none;';
		return $selector . '{' . $standard_bg . '}';
	}

	$images = explode( ',', $data['image'] );

	$property_format = "%s {background-image: url(%s);%s}";

	if ( 1 == count( $images ) && wp_attachment_is_image( $images[0] ) ) {

		$img    = wp_get_attachment_image_src( $images[0], 'full' );
		$result = sprintf( $property_format, $selector, $img[0], $standard_bg );

		return $result;
	}

	$img1x    = null;
	$img2x    = null;
	$width1x  = 0;
	$count    = 2;

	for ( $i = 0; $i < $count; $i++ ) {

		if ( ( !isset($images[$i]) ) || ( ! wp_attachment_is_image( $images[$i] ) ) ) {
			continue;
		}

		$img = wp_get_attachment_image_src( $images[$i], 'full' );

		if ( ! is_array( $img ) ) {
			continue;
		}

		$img_url    = $img[0];
		$img_width  = intval( $img[1] );

		if ( null == $img1x ) {
			$img1x   = $img_url;
			$img2x   = $img_url;
			$width1x = $img_width;
		} elseif ( $img_width > $width1x ) {
			$img2x = $img_url;
		} else {
			$img1x = $img_url;
		}

	}

	$bg1 = sprintf( $property_format, $selector, $img1x, $standard_bg );
	$bg2 = sprintf( $property_format, $selector, $img2x, '' );
	$result = $bg1 . ' @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {' . $bg2 . '}';

	return $result;
}

/**
 * Sanitizes a hex color.
 *
 * Always adds hash to color use `sanitize_hex_color` if exist.
 *
 * @since  4.0.0
 * @param  string  $color  Maybe HEX color.
 * @return string|null     Sanitized color.
 */
function cherry_sanitize_hex_color( $color ) {
	$color = ltrim( $color, '#' );
	$color = '#' . $color;

	if ( '' === $color ) {
		return '';
	}

	if ( function_exists( 'sanitize_hex_color' ) ) {
		return sanitize_hex_color( $color );
	}

	// 3 or 6 hex digits, or the empty string.
	if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
		return $color;
	}

	return null;
}

/**
 * Implode background properties array into CSS string.
 *
 * @since  4.0.0
 * @param  array  $data Background data-array.
 * @return string       Set of background rules.
 */
function cherry_prepare_background( $data ) {

	if ( ! is_array( $data ) ) {
		return;
	}

	unset( $data['image'] );

	$result = '';
	$format = 'background-%s:%s;';

	foreach ( $data as $prop => $value ) {

		if ( ! $value ) {
			continue;
		}

		switch ( $prop ) {
			case 'color':
				$value = cherry_sanitize_hex_color( $value );
				break;

			case 'position':
				$value = str_replace( '-', ' ', $value );
				break;
		}

		$result .= sprintf( $format, $prop, $value );
	}

	return $result;
}

/**
 * Implode typography data array from options into CSS string.
 *
 * @since  4.0.0
 * @param  array  $data Typography parameters array from options.
 * @param  array  $mod  Optional parameter - pass function name and arg to modify values inside typography array.
 * @return string       Font, letter-spacing, text-align, color CSS properties string.
 */
function cherry_get_typography_css( $data, $mod = array() ) {

	if ( ! is_array( $data ) || empty( $data ) ) {
		return;
	}

	$defaults = array(
		'family'        => '',
		'style'         => '',
		'color'         => '',
		'size'          => '',
		'lineheight'    => '',
		'letterspacing' => '',
		'align'         => '',
	);

	$data = wp_parse_args( $data, $defaults );

	$result = array();

	if ( '' !== $data['letterspacing'] ) {
		$units = '0' != $data['letterspacing'] ? 'px' : '';
		$result[] = 'letter-spacing:' . $data['letterspacing'] . $units;
	}

	if ( 'notdefined' != $data['align'] ) {
		$result[] = 'text-align:' . $data['align'];
	}

	if ( '' != $data['color'] ) {
		$color = cherry_sanitize_hex_color( $data['color'] );

		if ( 1 < count( $mod ) && ( in_array( $mod[0], array( 'cherry_colors_lighten', 'cherry_colors_darken' ) ) ) ) {
			$color = $mod[0]( $color, $mod[1] );
		}

		$result[] = 'color:' . $color;
	}

	$family   = stripcslashes( $data['family'] );
	$family   = explode( ',', $family );

	array_walk( $family, 'cherry_typography_prepare_family' );

	$family[] = ! empty( $data['category'] ) ? $data['category'] : 'sans-serif';
	$family   = array_unique( $family );

	$font_style  = false;
	$font_weight = false;
	$font_size   = $data['size'] . 'px';
	$line_height = $data['lineheight'] . 'px';

	$font_family = implode( ', ', $family );

	preg_match( '/^(\d*)(\w*)/i', $data['style'], $matches );

	if ( is_array( $matches ) ) {
		$font_style  = ( 'regular' != $matches[2] ) ? $matches[2] : false;
		$font_weight = $matches[1];
	}

	$font = array(
		$font_style,
		$font_weight,
		$font_size . '/' . $line_height,
		$font_family,
	);

	$font = implode( ' ', array_filter( $font ) );

	$result[] = 'font:' . ltrim( $font );

	$result = implode( ';', $result ) . ';';

	return $result;
}

/**
 * Prepare font family for passing into typography function.
 *
 * @since  4.0.5
 * @param  string &$item Array item.
 * @param  int    $index Array item index.
 * @return void
 */
function cherry_typography_prepare_family( &$item, $index ) {
	$item = trim( $item );

	if ( strpos( $item, ' ' ) ) {
		$item = '"' . $item . '"';
	}
}

/**
 * Get box model CSS from layout editor option.
 *
 * @since  4.0.0
 * @param  array  $data Layout parameters array from options.
 * @param  array  $mod  Optional parameter - pass function name and arg to modify values inside layout array.
 * @return string       Indents, border etc.
 */
function cherry_get_box_model_css( $data, $mod = array() ) {

	if ( ! is_array( $data ) || empty( $data ) ) {
		return;
	}

	$defaults = array(
		'position'  => array(),
		'margin'    => array(),
		'border'    => array(),
		'padding'   => array(),
		'container' => array(),
	);

	$box_defaults = array(
		'top'    => '',
		'right'  => '',
		'bottom' => '',
		'left'   => '',
	);

	$data = wp_parse_args( $data, $defaults );

	$result = '';

	// Prepare postion
	$data['position'] = array_filter( $data['position'] );
	if ( ! empty( $data['position'] ) ) {

		$data['position'] = array_intersect_key( $data['position'], $box_defaults );

		$parser_data = array(
			'prefix'  => '',
			'allowed' => $box_defaults
		);

		array_walk( $data['position'], 'cherry_prepare_box_item', $parser_data );

		$result .= implode( ';', array_filter( $data['position'] ) ) . ';';
	}

	// Prepare indents.
	$result .= cherry_prepare_css_indents( $data['margin'], 'margin' );
	$result .= cherry_prepare_css_indents( $data['padding'], 'padding' );

	// Prepare borders
	if ( ! empty( $data['border'] ) ) {

		$border_style  = ! empty( $data['border']['style'] ) ? $data['border']['style'] : '';
		$border_color  = ! empty( $data['border']['color'] ) ? $data['border']['color'] : '';
		$border_radius = ! empty( $data['border']['radius'] ) ? $data['border']['radius'] : '';

		if ( '' != $border_radius ) {
			$result .= 'border-radius:' . $border_radius . ';';
		}

		$border_format = 'border-%1$s:%2$s %3$s %4$s;';

		foreach ( $data['border'] as $property => $value ) {

			if ( ! array_key_exists( $property, $box_defaults ) ) {
				continue;
			}

			if ( empty( $value ) ) {
				continue;
			}

			$result .= sprintf(
				$border_format,
				$property, $value, $border_style, $border_color
			);
		}
	}

	// Prepare dimensions.
	if ( ! empty( $data['container']['width'] ) ) {
		$result .= 'width:' . $data['container']['width'] . ';';
	}

	if ( ! empty( $data['container']['height'] ) ) {
		$result .= 'height:' . $data['container']['height'] . ';';
	}

	return $result;
}

/**
 * Service function to grab CSS indents from data array into string.
 *
 * @since  4.0.0
 * @param  array  $data     data-array.
 * @param  string $property CSS property.
 * @return string
 */
function cherry_prepare_css_indents( $data, $property ) {

	if ( empty( $data ) ) {
		return;
	}

	$box_defaults = array(
		'top'    => '',
		'right'  => '',
		'bottom' => '',
		'left'   => '',
	);

	$data = array_intersect_key( $data, $box_defaults );
	$data = array_filter( $data );

	if ( 4 == count( $data ) ) {
		$result = $property . ':' . implode( ' ', $data ) . ';';
		return $result;
	}

	$parser_data = array(
		'prefix'  => $property,
		'allowed' => $box_defaults,
	);

	array_walk( $data, 'cherry_prepare_box_item', $parser_data );

	$result = implode( ';', array_filter( $data ) ) . ';';

	return $result;
}

/**
 * Service callback function for.
 *
 * @since  4.0.0
 * @param  string $item Position value.
 * @param  string $key  Position key.
 * @param  array  $data Array of allowed positions and property prefix.
 * @return void
 */
function cherry_prepare_box_item( &$item, $key, $data ) {

	if ( ! array_key_exists( $key, $data['allowed'] ) ) {
		$item = false;
		return;
	}

	if ( empty( $item ) ) {
		$item = false;
		return;
	}

	$prefix = '';

	if ( ! empty( $data['prefix'] ) ) {
		$prefix = $data['prefix'] . '-';
	}

	$item = $prefix . $key . ':' . $item;
}

/**
 * Make float size.
 *
 * @since  4.0.0
 * @param  double $size
 * @param  string $operation Arithmetic operator (multiple, addition).
 * @param  string $func      Function name (floor, ceil, round, abs).
 * @param  double $percent
 * @return double            Size.
 */
function cherry_typography_size( $size, $operation = ' ', $func = 'round', $percent ) {

	if ( ! $size ) {
		return false;
	}

	switch( $operation ) {
		case 'multiple':
			$size = (double)$size * (double)$percent;
			break;
		case 'addition':
			$size = (double)$size + (double)$percent;
			break;
	}

	switch( $func ) {
		case 'floor':
			$size = floor( $size );
			break;
		case 'ceil':
			$size = ceil( $size );
			break;
		case 'round':
			$size = round( $size );
			break;
		case 'abs':
			$size = abs( $size );
			break;
	}

	return $size;
}

/**
 * Build a CSS-rule.
 *
 * @since  4.0.0
 * @param  string|int $value CSS-proterty value.
 * @param  string     $rule  CSS-proterty name.
 * @return string            CSS-rule.
 */
function cherry_empty_value( $value, $rule ) {

	if ( '' == $value || 'notdefined' == $value ) {
		return;
	}

	echo $rule . ": " . $value;

	if ( is_numeric( $value ) ) {
		echo "px; ";
	} else {
		echo"; ";
	}
}

/**
 * Set element emphasis.
 *
 * @since 4.0.0
 * @param string $parent   Parent selector.
 * @param string $color    Color.
 * @param string $property To define.
 */
function cherry_element_emphasis( $parent, $color, $property ) {
	$result  = $parent . ' {' . $property . ':' . $color . ';}';
	$result .= $parent . ':hover {' . $property . ':' . cherry_colors_darken( $color, 10 ) . ';}';

	return $result;
}

/**
 * Return width value for container.
 *
 * @since  4.0.0
 * @param  int   $container_width A container width value.
 * @param  int   $element_width   Some-block (parent-block for container) width value.
 * @return int                    Width value.
 */
function cherry_container_width_compare( $container_width, $element_width ) {
	return ( $container_width > $element_width ) ? $element_width : $container_width;
}

/**
 * Retrieve CSS-rule only when site non-responsive.
 *
 * @since  4.0.0
 * @param  string $style CSS-rule.
 * @return string        Responsive CSS-rule.
 */
function cherry_non_responsive_style( $style ) {
	return ( 'false' == cherry_get_option( 'grid-responsive' ) ) ? $style : '';
}

/**
 * Open `@media` rule.
 *
 * @since  4.0.0
 * @param  string $function Media function.
 * @return string           Opened `@media` rule (if it needed).
 */
function cherry_media_queries_open( $function ) {
	return ( 'true' == cherry_get_option( 'grid-responsive' ) ) ? '@media ( ' . $function . ' ) {' : '';
}

/**
 * Close `@media` rule.
 *
 * @since  4.0.0
 * @return string Closed `@media` rule (if it needed).
 */
function cherry_media_queries_close() {
	return ( 'true' == cherry_get_option( 'grid-responsive' ) ) ? '}' : '';
}

/**
 * Sum of $a and $b.
 *
 * @since  4.0.0
 * @param  int $a Operand 1.
 * @param  int $b Operand 2.
 * @return int    Addition.
 */
function cherry_simple_sum( $a, $b ) {
	return intval( $a ) + intval( $b );
}

/**
 * Difference of $a and $b.
 *
 * @since  4.0.0
 * @param  int $a Operand 1.
 * @param  int $b Operand 2.
 * @return int    Subtraction.
 */
function cherry_simple_diff( $a, $b ) {
	return intval( $a ) - intval( $b );
}

/**
 * Retrieve a width to swith on mobile menu from.
 *
 * @since  4.0.0
 * @return int Width value.
 */
function cherry_menu_toogle_endpoint() {
	/**
	 * Filters a value when mobile menu switched.
	 *
	 * @since 4.0.0
	 * @param int $value Width value.
	 */
	return apply_filters( 'cherry_menu_toogle_endpoint', 600 );
}
