<?php
/**
 * The FANCIFY Utilities is a set of tools to ease building applications.
 *
 * Since these functions are used throughout the SplendorStudio Framework and are therefore required, they are
 * loaded automatically when the SplendorStudio Framework is included.
 *
 * @package SplendorStudio\Framework\API\Utilities
 */

/**
 * Calls function given by the first parameter and passes the remaining parameters as arguments.
 *
 * @param Callable $callback The callback to be called.
 * @param mixed    $args,... Optional. Additional parameters to be passed to the callback.
 *
 * @return string The callback content.
 */
function fancify_lite_render_function( $callback ) {
	if ( ! is_callable( $callback ) ) {
		return;
	}

	$args = func_get_args();

	ob_start();

	call_user_func_array( $callback, array_slice( $args, 1 ) );

	return ob_get_clean();
}

/**
 * Get value from $_GET or defined $haystack.
 *
 * @param string $needle   Name of the searched key.
 * @param mixed  $haystack Optional. The target to search. If false, $_GET is set to be the $haystack.
 * @param mixed  $default  Optional. Value to return if the needle isn't found.
 *
 * @return string Returns the value if found; else $default is returned.
 */
function fancify_lite_get( $needle, $haystack = false, $default = null ) {
	if ( false === $haystack ) {
		$haystack = $_GET; // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification -- The nonce verification check should be at the form processing level.
	}

	$haystack = (array) $haystack;

	if ( isset( $haystack[ $needle ] ) ) {
		return $haystack[ $needle ];
	}

	return $default;
}

/**
 * Get value from $_POST.
 *
 * @param string $needle  Name of the searched key.
 * @param mixed  $default Optional. Value to return if the needle isn't found.
 *
 * @return string Returns the value if found; else $default is returned.
 */
function fancify_lite_post( $needle, $default = null ) {
	return fancify_lite_get( $needle, $_POST, $default ); // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification -- The nonce verification check should be at the form processing level.
}

/**
 * Get value from $_GET or $_POST superglobals.
 *
 * @param string $needle  Name of the searched key.
 * @param mixed  $default Optional. Value to return if the needle isn't found.
 *
 * @return string Returns the value if found; else $default is returned.
 */
function fancify_lite_get_or_post( $needle, $default = null ) {
	$get = fancify_lite_get( $needle );

	if ( $get ) {
		return $get;
	}

	$post = fancify_lite_post( $needle );

	if ( $post ) {
		return $post;
	}

	return $default;
}


/**
 * Search content for shortcodes and filter shortcodes through their hooks.
 *
 * @param string $content  Content containing the shortcode(s) delimited with curly brackets (e.g. {key}).
 *
 * @param array  $haystack The associative array used to replace shortcode(s).
 *
 * @return string Content with shortcodes filtered out.
 */
function fancify_lite_array_shortcodes( $content, $haystack ) {
	if ( preg_match_all( '#{(.*?)}#', $content, $matches ) ) {

		foreach ( $matches[1] as $needle ) {
			$sub_keys = explode( '.', $needle );
			$value    = false;

			foreach ( $sub_keys as $sub_key ) {
				$search = $value ? $value : $haystack;
				$value  = fancify_lite_get( $sub_key, $search );
			}

			if ( $value ) {
				$content = str_replace( '{' . $needle . '}', $value, $content );
			}
		}
	}

	return $content;
}

/**
 * Sanitize HTML attributes from array to string.
 *
 * @param array $attributes The array key defines the attribute name and the array value define the
 *                          attribute value.
 *
 * @return string The escaped attributes.
 */
function fancify_lite_esc_attributes( $attributes ) {
	$methods = apply_filters( 'fancify_lite_escape_attributes_methods', array(
		'href'     => 'esc_url',
		'src'      => 'esc_url',
		'itemtype' => 'esc_url',
		'onclick'  => 'esc_js',
	) );

	$string = '';

	foreach ( (array) $attributes as $attribute => $value ) {

		if ( null === $value ) {
			continue;
		}

		$method = fancify_lite_get( $attribute, $methods );

		if ( $method ) {
			$value = call_user_func( $method, $value );
		} else {
			$value = esc_attr( $value );
		}

		$string .= $attribute . '="' . $value . '" ';
	}

	return trim( $string );
}

/**
 * check if header is transparent
 * 
 * @return class
 */
function fancify_lite_header_main_class() {
	global $post;

	// Handle Page Side Bar
	if ( isset( $post->post_type ) && 'page' === $post->post_type ) {
		// Get specific page option
		$isTransparent = Fancify_Lite_ACF::get_field( 'page_header_transparent', 'theme_default', $post->ID );
		if ( 'theme_default' === $isTransparent ) {
			// Get general page option
			$isTransparent = get_theme_mod( 'transparent_header', false );
		} else {
			$isTransparent = filter_var($isTransparent, FILTER_VALIDATE_BOOLEAN );
		}
	} else {
		$isTransparent = get_theme_mod( 'transparent_header', false );
	}

	$mainClass = '';

	if( isset($isTransparent) && true == $isTransparent ) {
		$mainClass .= ' fancify-header-transparent';
	}

	return $mainClass;
}

/**
 * check if header is transparent
 * 
 * @return class
 */
function fancify_lite_is_coming_soon() {
	$coming_soon = get_theme_mod( 'coming_soon', false );
	if ( current_user_can( 'manage_options' ) === true ) {
		return false;
	}
	if ( $coming_soon === true ){
		return true;
	}

	return false;
}
