Advertisement
GregL71

Help with admin_head-nav-menus.php

Mar 28th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.28 KB | None | 0 0
  1. // This code is supposed to build two sets of items which are inserted into the nav menu meta box.  I am trying to make this code accept an array, and then build menu items from said arrays.  The problem is that while the menu items are generated, and all URLs in place, only the first set of items will be functional.  I cannot seen to get both arrays of menu_items to work.
  2.  
  3. eg. :
  4. only one of the following sets of data will function as clickable links, despite them both being registered and properly inserting the urls info.  
  5. $items['level_1'] = ms_level_1_menu_items();
  6. $items['level_2'] = ms_level_2_menu_items();
  7.  
  8. Two arrays being accepted:
  9.  
  10.     public static $level_1_components =
  11.         array(
  12.             'messages' => array(
  13.                 'slug' => 'messages',
  14.                 'page_title' => 'Messages',
  15.                 'page_header' => 'My Messages',
  16.                 'link_title' => 'Mine Messages',
  17.                 'link_alt' => 'Private Messages'
  18.             ),
  19.             'friends' => array(
  20.                 'slug' => 'friends',
  21.                 'page_title' => 'Friends Title',
  22.                 'page_header' => 'Friends Header',
  23.                 'link_title' => 'Friends',
  24.                 'link_alt' => 'My Friends List'        
  25.             ),
  26.             'follow' => array(
  27.                 'slug' => 'follow',
  28.                 'page_title' => 'Following & Followers Title',
  29.                 'page_header' => 'Following & Followers Header',
  30.                 'link_title' => 'Following & Followers',
  31.                 'link_alt' => 'View Your Followers and See Who Your Following'         
  32.             ),         
  33.             'login' => array(
  34.                 'slug' => 'login',
  35.                 'page_title' => 'Login',
  36.                 'page_header' => null,
  37.                 'link_title' => 'Login',
  38.                 'link_alt' => null         
  39.             ),
  40.             'logout' => array(
  41.                 'slug' => 'logout',
  42.                 'page_title' => 'Logout',
  43.                 'page_header' => null,
  44.                 'link_title' => 'Logout',
  45.                 'link_alt' => null
  46.             ),     
  47.         );
  48.  
  49.     public static $level_2_components = array(
  50.             'messages' => array(
  51.                 'inbox' => 'Inbox',
  52.                 'sent' => 'Sent Messages',
  53.                 'new' => 'New Message',
  54.                 ),
  55.             'follow' => array(
  56.                 'following' => 'Who Im Following',
  57.                 'followers' => 'My Followrs',
  58.             ),
  59.         );
  60.  
  61.  
  62. ----****
  63. ----****  Functions to generate url
  64. ----****
  65.  
  66. function ms_generate_tiered_slug($component, $child ) {
  67.     if (!empty($child))
  68.         return generate_level_2_slug($component);
  69.     else
  70.         return generate_level_1_slug($component);
  71. }
  72.  
  73.  
  74. function generate_level_2_slug($component) {
  75.     $profile_url = MS_Users::user_profile_url();
  76.     $level_2 = MemberShare::$level_2_components;
  77.  
  78.     foreach ( $level_2 as $key => $value ) {
  79.         if (array_key_exists($component, $value )) 
  80.             return $profile_url.$key.'/'.$component;
  81.     }
  82. return null;
  83. }
  84.  
  85.  
  86. function generate_level_1_slug($component) {
  87.     $profile_url = MS_Users::user_profile_url();
  88.     $level_1 = MemberShare::$level_1_components;
  89.     foreach ($level_1 as $slug ) {
  90.         if (array_key_exists($component, $slug )) {        
  91.             $component_url = trailingslashit($profile_url).$component;
  92.             return trailingslashit($component_url);
  93.         }
  94.     }
  95. }
  96.  
  97. ----****
  98. ----****  END
  99. ----****
  100. function ms_admin_add_wp_nav_meta_box() {
  101.    
  102.     add_meta_box( 'ms-menu-nav', __( 'MemberShare', 'membershare' ), 'ms_admin_wp_nav_menu_meta_box', 'nav-menus', 'side', 'default' );
  103. }
  104.  add_action( 'admin_head-nav-menus.php', 'ms_admin_add_wp_nav_meta_box' );
  105.  //add_action( 'load-nav-menus.php', 'ms_admin_add_wp_nav_meta_box' );
  106.  
  107.  
  108.  
  109. function ms_level_1_menu_items(  ) {
  110.     $loaded_components = MemberShare::$level_1_components;
  111.  
  112.     $my_items = array();
  113.     foreach ( $loaded_components as $component ) {
  114.  
  115.         $my_items[ $component['slug'] ] = (object) array(
  116.             'ID'            => -1,
  117.             'db_id'         => 0,
  118.             'menu_item_parent'  => 0,
  119.             'object_id'     => 0,
  120.             'post_parent'       => 0,
  121.             'parent'        => 0,
  122.             'type'          => null,
  123.             'object'        => 0,
  124.             'type_label'        => null,
  125.             'title'         => $component['link_title'],
  126.             'url'           => null,
  127.             'target'        => null,
  128.             'attr_title'        => $component['link_alt'],
  129.             'description'       => null,
  130.             'classes'       => array( 'ms-'.$component['slug'] ),
  131.             'xfn'           => ''
  132.         );
  133.     }
  134.     return $my_items;
  135. }
  136.  
  137. function ms_level_2_menu_items(  ) {
  138.     $loaded_children = MemberShare::$level_2_components;
  139.  
  140.     $my_items = array();
  141.     foreach ( $loaded_children as $key => $value ) {
  142.         foreach ( $value as $slug => $name ) {
  143.  
  144.             $my_items[ $slug ] = (object) array(
  145.                 'ID'            => -1,
  146.                 'db_id'         => 0,
  147.                 'menu_item_parent'  => 0,
  148.                 'object_id'     => 0,
  149.                 'post_parent'       => 0,
  150.                 'parent'        => 0,
  151.                 'type'          => null,
  152.                 'object'        => 0,
  153.                 'type_label'        => null,
  154.                 'title'         => $name,
  155.                 'url'           => null,
  156.                 'target'        => null,
  157.                 'attr_title'        => $name,
  158.                 'description'       => null,
  159.                 'classes'       => array( 'ms-'.$slug.'-child' ),
  160.                 'xfn'           => ''
  161.             );
  162.         }
  163.     }
  164.     return $my_items;
  165. }
  166.  
  167.  
  168.  
  169. function set_url_for_meta_box_items($menu_item) {
  170.     if ( is_admin() ) {
  171.     return $menu_item;
  172.     }
  173.    
  174.  
  175.     $check = preg_match( '/\ms-(.*)?-(.*)/', implode( ' ', $menu_item->classes), $matches );
  176.  
  177.     if ( empty( $matches[1] ) ) {
  178.         return $menu_item;
  179.     }
  180.    
  181.     $menu_item->type_label = __('Testing');
  182.  
  183.     $child = (isset($matches[2])) ? (bool)true : (bool)false;
  184.    
  185.         switch ( $matches[1] ) {
  186.             case 'login' :
  187.                 if ( ! is_user_logged_in() ) {
  188.                     $menu_item->url = wp_login_url(ms_user_profile_url());
  189.                 } else {
  190.                     $menu_item->_invalid = true;
  191.                 }
  192.             break; 
  193.  
  194.             case 'logout' :
  195.                 if ( ! is_user_logged_in() ) {
  196.                     $menu_item->_invalid = true;
  197.                 } else {
  198.                     $menu_item->url = wp_logout_url();
  199.                 }
  200.             break; 
  201.  
  202.             case 'profile' :
  203.                 if ( ! is_user_logged_in() ) {
  204.                     $menu_item->_invalid = true;
  205.                 } else {
  206.                     $menu_item->url = ms_user_profile_url();
  207.                 }
  208.             break; 
  209.  
  210.             case 'register' :
  211.                 if ( is_user_logged_in() ) {
  212.                     $menu_item->_invalid = true;
  213.                 }
  214.             break;
  215.  
  216.             default:
  217.                 if ( is_user_logged_in() ) {
  218.                     $menu_item->url = ms_generate_tiered_slug($matches[1], $child );
  219.                     $menu_item->guid = ms_generate_tiered_slug($matches[1], $child );
  220.                     $menu_item->type_label = __('MemberShare');
  221.                    
  222.                 } else {
  223.                     $menu_item->_invalid = true;
  224.                 }
  225.             break;
  226.         }
  227.    
  228.     // Using This to Watch What Is Being Output  
  229.     //error_log(print_r($menu_item, true));
  230.     return $menu_item; 
  231. }
  232. add_filter( 'wp_setup_nav_menu_item', 'set_url_for_meta_box_items', 10, 1 );
  233.  
  234.  
  235.  
  236.  
  237. function ms_admin_wp_nav_menu_meta_box($object, $args) {
  238.     global $nav_menu_selected_id;
  239.     $items = array();
  240.     $items['level_1'] = ms_level_1_menu_items();
  241.     $items['level_2'] = ms_level_2_menu_items();
  242.  
  243.     $merged_array = array_merge($items['level_1'], $items['level_2']);
  244.    
  245.     $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'ID' );
  246.  
  247.     $walker = new Walker_Nav_Menu_Checklist( false );
  248.     ?>
  249.     <div id="my-plugin-div" class="posttypediv">
  250.         <h4><?php _e( 'MemberShare Pages', 'membershare' ) ?></h4>
  251.         <div id="tabs-panel-my-plugin-all" class="tabs-panel tabs-panel-active">
  252.         <ul id="my-plugin-checklist-pop" class="categorychecklist form-no-clear" >
  253.             <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $merged_array ), 0, (object) array( 'walker' => $walker ) ); ?>
  254.         </ul>
  255.         </div>
  256.  
  257.         <p class="button-controls">
  258.  
  259.         <span class="add-to-menu">
  260.             <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-my-plugin-menu-item" id="submit-my-plugin-div" />
  261.         <span class="spinner"></span>
  262.         </span>
  263.         </p>
  264.     </div>
  265.     <?php
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement