<?php
//####################################################
//#### Inheritance system (for internal use only) #### 
//####################################################

// Add item to the inheritance settings
if ( !function_exists( 'seohub_add_theme_inheritance' ) ) {
	function seohub_add_theme_inheritance($options, $append=true) {
		global $SEOHUB_GLOBALS;
		if (!isset($SEOHUB_GLOBALS["inheritance"])) $SEOHUB_GLOBALS["inheritance"] = array();
		$SEOHUB_GLOBALS['inheritance'] = $append 
			? seohub_array_merge($SEOHUB_GLOBALS['inheritance'], $options) 
			: seohub_array_merge($options, $SEOHUB_GLOBALS['inheritance']);
	}
}



// Return inheritance settings
if ( !function_exists( 'seohub_get_theme_inheritance' ) ) {
	function seohub_get_theme_inheritance($key = '') {
		global $SEOHUB_GLOBALS;
		return $key ? $SEOHUB_GLOBALS['inheritance'][$key] : $SEOHUB_GLOBALS['inheritance'];
	}
}



// Detect inheritance key for the current mode
if ( !function_exists( 'seohub_detect_inheritance_key' ) ) {
	function seohub_detect_inheritance_key() {
		static $inheritance_key = '';
		if (!empty($inheritance_key)) return $inheritance_key;
		$key = apply_filters('seohub_filter_detect_inheritance_key', '');
		global $SEOHUB_GLOBALS;
		if (empty($SEOHUB_GLOBALS['pre_query'])) $inheritance_key = $key;
		return $key;
	}
}


// Return key for override parameter
if ( !function_exists( 'seohub_get_override_key' ) ) {
	function seohub_get_override_key($value, $by) {
		$key = '';
		$inheritance = seohub_get_theme_inheritance();
		if (!empty($inheritance) && is_array($inheritance)) {
			foreach ($inheritance as $k=>$v) {
				if (!empty($v[$by]) && in_array($value, $v[$by])) {
					$key = $by=='taxonomy' 
						? $value
						: (!empty($v['override']) ? $v['override'] : $k);
					break;
				}
			}
		}
		return $key;
	}
}


// Return taxonomy (for categories) by post_type from inheritance array
if ( !function_exists( 'seohub_get_taxonomy_categories_by_post_type' ) ) {
	function seohub_get_taxonomy_categories_by_post_type($value) {
		$key = '';
		$inheritance = seohub_get_theme_inheritance();
		if (!empty($inheritance) && is_array($inheritance)) {
			foreach ($inheritance as $k=>$v) {
				if (!empty($v['post_type']) && in_array($value, $v['post_type'])) {
					$key = !empty($v['taxonomy']) ? $v['taxonomy'][0] : '';
					break;
				}
			}
		}
		return $key;
	}
}


// Return taxonomy (for tags) by post_type from inheritance array
if ( !function_exists( 'seohub_get_taxonomy_tags_by_post_type' ) ) {
	function seohub_get_taxonomy_tags_by_post_type($value) {
		$key = '';
		$inheritance = seohub_get_theme_inheritance();
		if (!empty($inheritance) && is_array($inheritance)) {
			foreach($inheritance as $k=>$v) {
				if (!empty($v['post_type']) && in_array($value, $v['post_type'])) {
					$key = !empty($v['taxonomy_tags']) ? $v['taxonomy_tags'][0] : '';
					break;
				}
			}
		}
		return $key;
	}
}
?>