GC_Admin_Bar::get_nodes()

最后更新于:2021-11-26 09:08:39

GC_Admin_Bar::get_nodes()

响应

(array|void)

源文件

文件: gc-includes/class-gc-admin-bar.php

	final public function get_nodes() {
		$nodes = $this->_get_nodes();
		if ( ! $nodes ) {
			return;
		}

		foreach ( $nodes as &$node ) {
			$node = clone $node;
		}
		return $nodes;
	}
/**
 * Prefix top-level toolbar items with a span container.
 *
 * @param GC_Admin_Bar $gc_admin_bar Toolbar instance.
 */
function gcdocs_all_toolbar_nodes( $gc_admin_bar ) {

	$all_toolbar_nodes = $gc_admin_bar->get_nodes();

	foreach ( $all_toolbar_nodes as $node ) {

		// use the same node's properties
		$args = $node;

		// put a span before the title
		$args->title = '<span class="my-class"></span>' . $node->title;

		// update the Toolbar node
		$gc_admin_bar->add_node( $args );
	}

}
add_action( 'admin_bar_menu', 'gcdocs_all_toolbar_nodes', 999 );
// use 'gc_before_admin_bar_render' hook to also get nodes produced by plugins.
add_action( 'gc_before_admin_bar_render', 'add_all_node_ids_to_toolbar' );

function add_all_node_ids_to_toolbar() {

	global $gc_admin_bar;
	$all_toolbar_nodes = $gc_admin_bar->get_nodes();

	if ( $all_toolbar_nodes ) {

		// add a top-level Toolbar item called "Node Id's" to the Toolbar
		$args = array(
			'id'    => 'node_ids',
			'title' => 'Node ID's'
		);
		$gc_admin_bar->add_node( $args );

		// add all current parent node id's to the top-level node.
		foreach ( $all_toolbar_nodes as $node  ) {
			if ( isset($node->parent) && $node->parent ) {

				$args = array(
					'id'     => 'node_id_'.$node->id, // prefix id with "node_id_" to make it a unique id
					'title'  => $node->id,
					'parent' => 'node_ids'
					// 'href' => $node->href,
				);
				// add parent node to node "node_ids"
				$gc_admin_bar->add_node($args);
			}
		}

		// add all current Toolbar items to their parent node or to the top-level node
		foreach ( $all_toolbar_nodes as $node ) {

			$args = array(
				'id'      => 'node_id_'.$node->id, // prefix id with "node_id_" to make it a unique id
				'title'   => $node->id,
				// 'href' => $node->href,
			);

			if ( isset($node->parent) && $node->parent ) {
				$args['parent'] = 'node_id_'.$node->parent;
			} else {
				$args['parent'] = 'node_ids';
			}

			$gc_admin_bar->add_node($args);
		}
	}
}