HEX
Server: LiteSpeed
System: Linux pulsar191.sitesanctuary.org 5.14.0-284.30.1.el9.tuxcare.els9.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jan 10 17:34:05 UTC 2025 x86_64
User: lgooir (1604)
PHP: 8.1.32
Disabled: exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/lgooir/.trash/rising-bamboo/inc/helper/class-term.php
<?php
/**
 * RisingBamBooCore
 *
 * @package RisingBamBooCore
 */

namespace RisingBambooCore\Helper;

use RisingBambooCore\Core\Singleton;

/**
 *  Term Helper.
 */
class Term extends Singleton {

	/**
	 * Sort terms.
	 *
	 * @param array $cats Category.
	 * @param array $into Sorted.
	 * @param int   $parent_id Parent ID.
	 * @return void
	 */
	public static function sort_terms_hierarchically( array &$cats, array &$into, int $parent_id = 0 ): void {
		foreach ( $cats as $i => $cat ) {
			if ( $cat->parent === $parent_id ) {
				$into[ $cat->term_id ] = $cat;
				unset($cats[ $i ]);
			}
		}

		foreach ( $into as $top_cat ) {
			$top_cat->children = [];
			self::sort_terms_hierarchically($cats, $top_cat->children, $top_cat->term_id);
		}
	}

	/**
	 * Get Term hierarchy flat.
	 *
	 * @param array  $result Result.
	 * @param string $taxonomy Taxonomy.
	 * @param array  $args Args.
	 * @param int    $parent_id Parent ID.
	 * @param string $prefix Prefix.
	 * @param int    $depth Depth.
	 * @return void
	 */
	public static function get_terms_hierarchically_flat( array &$result, string $taxonomy = 'category', array $args = [], int $parent_id = 0, string $prefix = '-', int $depth = 1 ): void {
		$args  = wp_parse_args(
			[
				'taxonomy'     => $taxonomy,
				'hierarchical' => false,
				'parent'       => $parent_id,
				'depth'        => 1,
				'hide_empty'   => false,
			],
			$args
		);
		$terms = get_terms($args);
		foreach ( $terms as $term ) {
			if ( 0 === $term->parent ) {
				$name  = $term->name;
				$depth = 1;
			} else {
				$name = $prefix . $term->name;
				++$depth;
			}
			$result[ $term->term_id ] = $name;

			self::get_terms_hierarchically_flat($result, $taxonomy, [], $term->term_id, str_pad($prefix, $depth, $prefix), $depth);
		}
	}
}