<?php

// Prevent Direct Access
if( !defined('ABSPATH') ){
	exit;
}

// ------- Function to get option value from url or customizer

function maxo_get_option($option, $default = false, $custom_option = false, $custom_default = false){

	// Define a variable to save output
	$output = [];

	// if there is a url parameter for the option get the value from url
	if( isset($_GET[$option]) ){
		$output['value'] = sanitize_text_field($_GET[$option]);
		$output['type'] = 'url';
	}

	// else get the value from customizer
	else{
		$output['value'] = get_theme_mod($option, $default);
		$output['type'] = 'customizer';
	}

	// if $custom_option is present and the value of output is "custom" means we want to get the value of the custom option that is related to the main option
	if( $custom_option && $output['value'] === 'custom' ){

		// If the type of output is "url", means the "custom" value was from url parameter and therefore we should also get the custom option from url
		if( $output['type'] == 'url' ){

			// if the custom option is set in the url
			if( isset($_GET[$custom_option]) ){

				// Get value of $custom_option from url
				$output['value'] = sanitize_text_field($_GET[$custom_option]);
			}

			// if the custom option is not set in the url
			else{

				// make the value equal to $option value which is 'custom'
				$output['value'] = 'custom';
			}
		}

		// If the type of output is not "url, means it's "customizer" and the "custom" value was from customizer and therefore we should also get the custom option from customizer
		else{

			// Get value of $custom_option from Customizer
			$output['value'] = get_theme_mod($custom_option, $custom_default);
		}
	}

	// Return the output
	return $output['value'];
}
