Last active
November 17, 2023 09:28
-
-
Save ihslimn/b583e390d5d8a0343feff694f330e145 to your computer and use it in GitHub Desktop.
Nth_Level_Terms_Macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'jet-engine/register-macros', function(){ | |
class Nth_Level_Terms_Macro extends \Jet_Engine_Base_Macros { | |
public function macros_tag() { | |
return 'nth_level_terms'; | |
} | |
public function macros_name() { | |
return 'Get Nth level terms'; | |
} | |
public function macros_args() { | |
return array( | |
'nth_mode' => array( | |
'label' => 'Mode', | |
'type' => 'select', | |
'options' => array( | |
'term' => 'Nth level children of current term', | |
'post' => 'Post terms of Nth level', | |
), | |
'default' => 'term', | |
), | |
'nth_tax' => array( | |
'label' => 'Taxonomy', | |
'type' => 'select', | |
'options' => function() { | |
return jet_engine()->listings->get_taxonomies_for_options(); | |
}, | |
), | |
'nth_level' => array( | |
'label' => 'Level', | |
'type' => 'text', | |
'default' => '1', | |
), | |
'nth_compare' => array( | |
'label' => 'Level comparison', | |
'type' => 'select', | |
'options' => array( | |
'exact' => 'Exact level', | |
'up_to' => 'Up to the chosen level', | |
'from' => 'Down from the chosen level' | |
), | |
'default' => 'exact', | |
), | |
'nth_include' => array( | |
'label' => 'Include chosen', | |
'type' => 'select', | |
'options' => array( | |
'yes' => 'Yes', | |
'no' => 'No', | |
), | |
'default' => 'yes', | |
'condition' => array( 'nth_compare!' => array( 'exact' ) ), | |
'description' => 'Include terms of the chosen level' | |
), | |
); | |
} | |
public function macros_callback( $args = array() ) { | |
$mode = $args[ 'nth_mode' ] ?? 'term'; | |
$taxonomy = $args[ 'nth_tax' ] ?? false; | |
$level = $args[ 'nth_level' ] ?? false; | |
$level = absint( $level ); | |
$level = $level - 1; | |
$compare = $args[ 'nth_compare' ] ?? 'exact'; | |
$include = $args[ 'nth_include' ] ?? 'yes'; | |
if ( ! $taxonomy || false === $level || $level < 0 ) { | |
return ''; | |
} | |
if ( 'term' === $mode ) { | |
$macro = '%queried_term%'; | |
} else { | |
$macro = "%current_terms|{$taxonomy}%"; | |
} | |
$term_ids = jet_engine()->listings->macros->do_macros( $macro ); | |
if ( empty( $term_ids ) ) { | |
return ''; | |
} | |
if ( 'term' === $mode ) { | |
$term_ids = get_term_children( $term_ids, $taxonomy ); | |
} else { | |
$term_ids = explode( ',', $term_ids ); | |
} | |
$output = array(); | |
foreach ( $term_ids as $term_id ) { | |
$ancestors = get_ancestors( $term_id, $taxonomy ); | |
$count = count( $ancestors ); | |
switch ( $compare ) { | |
case 'exact': | |
$pass = $count === $level ? true : false; | |
break; | |
case 'up_to': | |
$pass = $count <= $level ? true : false; | |
break; | |
case 'from': | |
$pass = $count >= $level ? true : false; | |
break; | |
} | |
$pass = ( 'yes' === $include || 'exact' === $compare ) ? $pass : ( $count === $level ? false : $pass ); | |
if ( $pass ) { | |
$output[] = $term_id; | |
} | |
} | |
if ( empty( $output ) ) { | |
return ''; | |
} | |
return implode( ',', $output ); | |
} | |
} | |
new Nth_Level_Terms_Macro(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment