* @version 1.1.0
*
* @package WordPress
* @subpackage Administration
*/
function wp_custom_navigation_get_menu_items( $menu_objects, $key = 'ID' ) {
$menu_items = array();
if ( !empty( $menu_objects ) && !empty( $key ) ) {
$args = array( 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish' );
if ( count( $menu_objects ) > 1 )
$args['include'] = implode( ',', $menu_objects );
else
$args['include'] = $menu_objects[0];
$posts = get_posts( $args );
if ( ! empty( $posts ) ) {
foreach ( $posts as $post ) {
$menu_items[ $post->$key ] = $post;
}
}
unset( $posts );
ksort( $menu_items );
}
return $menu_items;
}
function wp_custom_navigation_setup($override = false) {
$nav_version = '1.1.0';
//Custom Navigation Menu Setup
//Check for Upgrades
if (get_option('wp_settings_custom_nav_version') <> '') {
$nav_version_in_db = get_option('wp_settings_custom_nav_version');
}
else {
$nav_version_in_db = '0';
}
//Override for menu descriptions
update_option('wp_settings_custom_nav_advanced_options','yes');
if(($nav_version_in_db <> $nav_version) || ($override))
update_option('wp_settings_custom_nav_version',$nav_version);
$custom_menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
if ( !empty( $custom_menus ) ) {
foreach( $custom_menus as $menu ) {
$menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
if ( !empty( $menu_objects ) ) {
foreach( $menu_objects as $item )
wp_delete_post( $item );
}
wp_delete_term( $menu->term_id, 'nav_menu' );
}
}
}
/*-----------------------------------------------------------------------------------*/
/* Custom Navigation Functions */
/* wp_custom_navigation_output() displays the menu in the back/frontend
/* wp_custom_navigation_sub_items() is a recursive sub menu item function
/* wp_custom_nav_get_pages()
/* wp_custom_nav_get_categories()
/* wp_custom_navigation_default_sub_items() is a recursive sub menu item function
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Main Output Function
/* args list
/* type - frontend or backend
/* name - name of your menu
/* id - id of menu in db
/* desc - 1 = show descriptions, 2 = dont show descriptions
/* before_title - html before title is outputted in tag
/* after_title - html after title is outputted in tag
/*-----------------------------------------------------------------------------------*/
function wp_custom_navigation_output($args = array()) {
//DEFAULT ARGS
$type = 'frontend';
$name = 'Menu 1';
$id = 0;
$desc = 2;
$before_title = '';
$after_title = '';
if (isset($args)) {
if ( !is_array($args) )
parse_str( $args, $args );
extract($args);
}
$menu_objects = get_objects_in_term( $id, 'nav_menu' );
$menu_items = wp_custom_navigation_get_menu_items( $menu_objects, 'menu_order' );
//Override for menu descriptions
$advanced_option_descriptions = get_option('wp_settings_custom_nav_advanced_options');
if ( $advanced_option_descriptions == 'no' )
$desc = 2;
$queried_id = 0;
global $wp_query;
if ( is_page() )
$queried_id = $wp_query->get_queried_object_id();
elseif ( is_category() )
$queried_id = $wp_query->get_queried_object_id();
$parent_stack = array();
// Display Loop
foreach ( $menu_items as $menu_item ) {
$menu_type = get_post_meta($menu_item->ID, 'menu_type', true);
$object_id = get_post_meta($menu_item->ID, 'object_id', true);
switch ( $menu_type ) {
// Page Menu Item
case 'page':
if ( $menu_item->guid == '' )
$link = get_permalink( $object_id );
else
$link = $menu_item->guid;
if ( $menu_item->post_title == '' )
$title = htmlentities( get_the_title( $object_id ) );
else
$title = htmlentities( $menu_item->post_title );
if ( $menu_item->post_content == '' )
$description = htmlentities( get_post_meta( $menu_item->ID, 'page-description', true ) );
else
$description = htmlentities( $menu_item->post_content );
$target = '';
break;
// Category Menu Item
case 'category':
if ( $menu_item->guid == '' )
$link = get_category_link( $object_id );
else
$link = $menu_item->guid;
if ( $menu_item->post_title == '' ) {
$title_raw = get_categories( array('include' => $object_id) );
$title = htmlentities($title_raw[0]->cat_name);
} else {
$title = htmlentities( $menu_item->post_title );
}
if ( $menu_item->post_content == '' )
$description = htmlentities( strip_tags( category_description( $object_id ) ) );
else
$description = htmlentities( $menu_item->post_content );
$target = '';
break;
default:
// Custom Menu Item
$link = $menu_item->guid;
$title = htmlentities( $menu_item->post_title );
$description = htmlentities( $menu_item->post_content );
$target = 'target="_blank"';
break;
}
$li_class = '';
/* @todo: update to use tax/post data
//SET anchor title
if (isset($wp_custom_nav_menu_items->custom_anchor_title)) {
$anchor_title = htmlentities($wp_custom_nav_menu_items->custom_anchor_title);
}
else {
$anchor_title = $title;
}
if ($queried_id == $wp_custom_nav_menu_items->post_id) {
$li_class = 'class="current_page_item"';
}
if (isset($wp_custom_nav_menu_items->new_window)) {
if ($wp_custom_nav_menu_items->new_window > 0) {
$target = 'target="_blank"';
}
else {
$target = '';
}
}
*/
// Indent children
if ( empty( $parent_stack ) ) {
array_unshift( $parent_stack, $menu_item->ID );
} elseif ( $menu_item->post_parent > 0 ) {
if ( $menu_item->post_parent == $parent_stack[0] ) { ?>
ID );
} elseif ( count( $parent_stack ) > 1 && $menu_item->post_parent == $parent_stack[1] ) { ?>
ID;
} elseif ( in_array( $menu_item->post_parent, $parent_stack ) ) {
while ( !empty( $parent_stack ) && $menu_item->post_parent != $parent_stack[0] ) { ?>
array_shift( $parent_stack );
}
}
} else { ?>
ID;
}
// List Items
?>
array_shift( $parent_stack );
}
if ( !empty( $menu_items ) ) { ?>
get_results("SELECT id,post_id,parent_id,position,custom_title,custom_link,custom_description,menu_icon,link_type,custom_anchor_title,new_window FROM ".$table_name." WHERE parent_id = '".$post_id."' AND menu_id='".$menu_id."' ORDER BY position ASC");
if (empty($wp_custom_nav_menu))
{
}
else
{
?>
post->ID;
}
elseif (is_category()) {
$queried_id = $wp_query->query_vars['cat'];
}
else {
}
//DISPLAY Loop
foreach ($wp_custom_nav_menu as $sub_item)
{
//Figure out where the menu item sits
$counter=$sub_item->position;
//Prepare Menu Data
//Category Menu Item
if ($sub_item->link_type == 'category')
{
$parent_id = $sub_item->parent_id;
$post_id = $sub_item->post_id;
if ($sub_item->custom_link == '') {
$link = get_category_link($sub_item->post_id);
}
else {
$link = $sub_item->custom_link;
}
if ($sub_item->custom_title == '') {
$title_raw = get_categories('include='.$sub_item->post_id);
$title = htmlentities($title_raw[0]->cat_name);
}
else {
$title = htmlentities($sub_item->custom_title);
}
if ($sub_item->custom_description == '') {
$description = strip_tags(category_description($sub_item->post_id));
}
else {
$description = $sub_item->custom_description;
}
$target = '';
}
//Page Menu Item
elseif ($sub_item->link_type == 'page')
{
$parent_id = $sub_item->parent_id;
$post_id = $sub_item->post_id;
if ($sub_item->custom_link == '') {
$link = get_permalink($sub_item->post_id);
}
else {
$link = $sub_item->custom_link;
}
if ($sub_item->custom_title == '') {
$title = htmlentities(get_the_title($sub_item->post_id));
}
else {
$title = htmlentities($sub_item->custom_title);
}
if ($sub_item->custom_description == '') {
$description = get_post_meta($sub_item->post_id, 'page-description', true);
}
else {
$description = $sub_item->custom_description;
}
$target = '';
}
//Custom Menu Item
else
{
$link = $sub_item->custom_link;
$title = htmlentities($sub_item->custom_title);
$parent_id = $sub_item->parent_id;
$post_id = $sub_item->post_id;
$description = $sub_item->custom_description;
$target = 'target="_blank"';
}
if ($queried_id == $sub_item->post_id) {
$li_class = 'class="current_page_item"';
}
else {
$li_class = '';
}
//SET anchor title
if (isset($sub_item->custom_anchor_title)) {
$anchor_title = htmlentities($sub_item->custom_anchor_title);
}
else {
$anchor_title = $title;
}
if (isset($sub_item->new_window)) {
if ($sub_item->new_window > 0) {
$target = 'target="_blank"';
}
}
//List Items
?>
0,
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0 );
//GET all pages
$pages_array = get_pages($pages_args);
$intCounter = $counter;
$parentli = $intCounter;
if ($pages_array)
{
//DISPLAY Loop
foreach ($pages_array as $post)
{
if ($post->post_parent == 0)
{
//Custom Menu
if ($type == 'menu')
{
$description = get_post_meta($post->ID, 'page-description', true);
?>
-
post_title);
$post_url = get_permalink($post->ID);
$post_id = $post->ID;
$post_parent_id = $post->post_parent;
$description = htmlentities(get_post_meta($post_id, 'page-description', true));
?>
post_title; ?>
ID; ?>
'post',
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'include_last_update_time' => false,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'pad_counts' => false );
$intCounter = $counter;
//GET all categories
$categories_array = get_categories($category_args);
if ($categories_array)
{
//DISPLAY Loop
foreach ($categories_array as $cat_item)
{
if ($cat_item->parent == 0)
{
//Custom Menu
if ($type == 'menu')
{
?>
-
cat_name);
$post_url = get_category_link($cat_item->cat_ID);
$post_id = $cat_item->cat_ID;
$post_parent_id = $cat_item->parent;
$description = htmlentities(strip_tags($cat_item->description));
?>
cat_name; ?>
cat_ID; ?>
cat_ID, $intCounter, $parentli, 'categories','default');
?>
$childof,
'hide_empty' => false,
'parent' => $childof);
}
//Sidebar Menu
elseif ($output_type == 'default')
{
$sub_args = array(
'child_of' => $childof,
'hide_empty' => false,
'parent' => $childof);
}
else
{
}
//Get Sub Category Items
if ($type == 'categories')
{
$sub_array = get_categories($sub_args);
}
//Get Sub Page Items
elseif ($type == 'pages')
{
$sub_array = get_pages($sub_args);
}
if ($sub_array)
{
?>
cat_ID);
$title = htmlentities($sub_item->cat_name);
$parent_id = $sub_item->cat_ID;
$itemid = $sub_item->cat_ID;
$linktype = 'category';
$appendtype = 'Category';
$description = htmlentities(strip_tags($sub_item->description));
}
//Page Menu Item
elseif ($type == 'pages')
{
$link = get_permalink($sub_item->ID);
$title = htmlentities($sub_item->post_title);
$parent_id = $sub_item->ID;
$linktype = 'page';
$itemid = $sub_item->ID;
$appendtype = 'Page';
$description = htmlentities(get_post_meta($itemid, 'page-description', true));
}
//Custom Menu Item
else
{
$title = '';
$linktype = 'custom';
$appendtype= 'Custom';
}
//Custom Menu
if ($output_type == 'menu')
{
?>
-
-
$childof,
'hide_empty' => false,
'parent' => $childof);
$sub_array = get_categories($sub_args);
}
//Get Sub Page Items
elseif ($type == 'pages')
{
$sub_args = array(
'child_of' => $childof,
'parent' => $childof);
$sub_array = get_pages($sub_args);
}
else {
}
if ($sub_array)
{
//DISPLAY Loop
foreach ($sub_array as $sub_item)
{
if (isset($sub_item->parent)) {
$sub_item_parent = $sub_item->parent;
}
elseif (isset($sub_item->post_parent)) {
$sub_item_parent = $sub_item->post_parent;
}
else {
}
//Is child
if ($sub_item_parent == $childof)
{
//Prepare Menu Data
//Category Menu Item
if ($type == 'categories')
{
$link = get_category_link($sub_item->cat_ID);
$title = htmlentities($sub_item->cat_name);
$parent_id = $sub_item->category_parent;
$itemid = $sub_item->cat_ID;
$linktype = 'category';
$appendtype= 'Category';
}
//Page Menu Item
elseif ($type == 'pages')
{
$link = get_permalink($sub_item->ID);
$title = htmlentities($sub_item->post_title);
$parent_id = $sub_item->post_parent;
$linktype = 'page';
$itemid = $sub_item->ID;
$appendtype= 'Page';
}
//Custom Menu Item
else
{
$title = '';
$linktype = 'custom';
$appendtype= 'Custom';
}
//CHECK for existing parent records
//echo $parent_id;
$wp_result = $wpdb->get_results("SELECT id FROM ".$table_name." WHERE post_id='".$parent_id."' AND link_type='".$linktype."' AND menu_id='".$menu_id."'");
if ($wp_result > 0 && isset($wp_result[0]->id)) {
$parent_id = $wp_result[0]->id;
}
else {
//$parent_id = 0;
}
//INSERT item
$insert = "INSERT INTO ".$table_name." (position,post_id,parent_id,custom_title,custom_link,custom_description,menu_icon,link_type,menu_id,custom_anchor_title) "."VALUES ('".$counter."','".$itemid."','".$parent_id."','".$title."','".$link."','','','".$linktype."','".$menu_id."','".$title."')";
$results = $wpdb->query( $insert );
$counter++;
$counter = get_children_menu_elements($itemid, $counter, $parent_id, $type, $menu_id, $table_name);
}
//Do nothing
else {
}
}
}
return $counter;
}
?>