Skip to content

Instantly share code, notes, and snippets.

@JiveDig
Created July 21, 2017 14:05
Show Gist options
  • Save JiveDig/54c4039cdcd52b51d4462b5cf829ee41 to your computer and use it in GitHub Desktop.
Save JiveDig/54c4039cdcd52b51d4462b5cf829ee41 to your computer and use it in GitHub Desktop.
Get the specified metadata value for the term or from one of it's parent terms.
<?php
/**
* Get the specified metadata value for the term or from
* one of it's parent terms.
*
* @param WP_Term $term Term object
* @param string $key The meta key to retrieve.
* @param bool $check_enabled Whether to check if custom archive settings are enabled.
*
* @return mixed|null
*/
function mai_get_hierarchichal_term_meta( WP_Term $term, $key, $check_enabled ) {
if ( ! is_taxonomy_hierarchical( $term->taxonomy ) ) {
return;
}
if ( ! mai_has_parent_term( $term ) ) {
return;
}
return mai_get_term_meta_recursively( $term, $key, $check_enabled );
}
/**
* Recursively get the term metadata by the specified meta key.
*
* This function walks up the term hierarchical tree, searching for
* a valid metadata value for the given meta key.
*
* The recursive action stops when:
* 1. The current term level has the metadata value.
* 2. The current term level does not have a parent term.
*
* @param WP_Term $term Term object
* @param string $key The meta key to retrieve.
* @param bool $check_enabled Whether to check if custom archive settings are enabled.
* @param mixed|null $meta
*
* @return mixed|null
*/
function mai_get_term_meta_recursively( WP_Term $term, $key, $check_enabled, $meta = null ) {
// Setup level.
static $level = 1;
/**
* If we're over the 4th level, we've gone too far.
* If we allow too many levels things could get slow.
*/
if ( $level > 4 ) {
// Reset.
$level = 1;
// Return.
return $meta;
}
// Increment the level.
$level++;
// We need to checking whether archive settings are enabled.
if ( $check_enabled ) {
// Enabled.
if ( $enabled = get_term_meta( $term->term_id, 'enable_content_archive_settings', true ) ) {
// Reset.
$level = 1;
// Return this level's meta.
return get_term_meta( $term->term_id, $key, true );
}
// Not enabled.
else {
// Try the parent(s)
$meta = mai_get_parent_term_meta_recursively( $term, $key, $check_enabled, $meta );
}
}
// Don't check if archive settings are enabled.
else {
$meta = get_term_meta( $term->term_id, $key, true );
}
if ( ! $meta ) {
// Try the parent(s)
return mai_get_parent_term_meta_recursively( $term, $key, $check_enabled, $meta );
}
// Reset.
$level = 1;
// Return.
return $meta;
}
/**
* Continue the recursive term meta function, on a parent.
*
* @param WP_Term $term Term object
* @param string $key The meta key to retrieve.
* @param bool $check_enabled Whether to check if custom archive settings are enabled.
* @param mixed|null $meta
*
* @return mixed|null
*/
function mai_get_parent_term_meta_recursively( $term, $key, $check_enabled, $meta ) {
// If no parent, use what we have.
if ( ! mai_has_parent_term( $term ) ) {
// Reset.
$level = 1;
// Return.
return $meta;
}
// Get the parent term.
$parent_term = get_term_by( 'id', $term->parent, $term->taxonomy );
if ( false === $parent_term ) {
// Reset.
$level = 1;
// Return.
return $meta;
}
// Try again
return mai_get_term_meta_recursively( $parent_term, $key, $check_enabled, $meta );
}
/**
* Checks if the term has a parent.
*
* @param WP_Term $term Term object.
*
* @return bool
*/
function mai_has_parent_term( WP_Term $term ) {
return ( $term->parent > 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment