PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/icarostore/functions/nav/dropdown-menus.php

https://gitlab.com/hop23typhu/list-theme
PHP | 260 lines | 127 code | 44 blank | 89 comment | 21 complexity | c4572e5cd0705c866208356b949ccba5 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Dropdown Menus
  4. Plugin URI: http://interconnectit.com/?p=2190
  5. Description: Outputs WordPress Menus as a dropdown. Use the widget or the function <code>dropdown_menu();</code> with the same arguments as <code>wp_nav_menu();</code>.
  6. Author: Robert O'Rourke @ interconnect/it
  7. Version: 0.7
  8. Author URI: http://interconnectit.com
  9. */
  10. /*
  11. Changelog:
  12. 0.7:
  13. added a filter for the dropdown menu class name as it very generic and can cause conflicts
  14. 0.6:
  15. fixed the echo argument, thanks to squingynaut for the tip
  16. fixed the ability to call the menu via the theme location name
  17. 0.5:
  18. improved backwards compat with getElementsByClassName. Works back to IE 5.5. Thanks to Rob Nyman http://code.google.com/p/getelementsbyclassname/
  19. 0.4:
  20. added the use of the menu name as the blank item text
  21. fixed it for when the menu object wasn't present if called via theme_location
  22. changed white space to reflect coding guidelines
  23. 0.3:
  24. added an argument to alter the blanking text, empty to not have it all together, and an improved filter that passes $args
  25. changed widget class name
  26. */
  27. // useless without this
  28. if ( ! function_exists( 'wp_nav_menu' ) )
  29. return false;
  30. /**
  31. * Tack on the blank option for urls not in the menu
  32. */
  33. add_filter( 'wp_nav_menu_items', 'dropdown_add_blank_item', 10, 2 );
  34. function dropdown_add_blank_item( $items, $args ) {
  35. if ( isset( $args->walker ) && is_object( $args->walker ) && method_exists( $args->walker, 'is_dropdown' ) ) {
  36. if ( ( ! isset( $args->menu ) || empty( $args->menu ) ) && isset( $args->theme_location ) ) {
  37. $theme_locations = get_nav_menu_locations();
  38. $args->menu = wp_get_nav_menu_object( $theme_locations[ $args->theme_location ] );
  39. }
  40. $title = isset( $args->dropdown_title ) ? wptexturize( $args->dropdown_title ) : '&mdash; ' . $args->menu->name . ' &mdash;';
  41. if ( ! empty( $title ) )
  42. $items = '<option value="" class="blank">' . apply_filters( 'dropdown_blank_item_text', $title, $args ) . '</option>' . $items;
  43. }
  44. return $items;
  45. }
  46. /**
  47. * Remove empty options created in the sub levels output
  48. */
  49. add_filter( 'wp_nav_menu_items', 'dropdown_remove_empty_items', 10, 2 );
  50. function dropdown_remove_empty_items( $items, $args ) {
  51. if ( isset( $args->walker ) && is_object( $args->walker ) && method_exists( $args->walker, 'is_dropdown' ) )
  52. $items = str_replace( "<option></option>", "", $items );
  53. return $items;
  54. }
  55. /**
  56. * Script to make it go (no jquery! (for once))
  57. */
  58. add_action( 'wp_footer', 'dropdown_javascript' );
  59. function dropdown_javascript() {
  60. if ( is_admin() ) return; ?>
  61. <script>
  62. var getElementsByClassName=function(a,b,c){if(document.getElementsByClassName){getElementsByClassName=function(a,b,c){c=c||document;var d=c.getElementsByClassName(a),e=b?new RegExp("\\b"+b+"\\b","i"):null,f=[],g;for(var h=0,i=d.length;h<i;h+=1){g=d[h];if(!e||e.test(g.nodeName)){f.push(g)}}return f}}else if(document.evaluate){getElementsByClassName=function(a,b,c){b=b||"*";c=c||document;var d=a.split(" "),e="",f="http://www.w3.org/1999/xhtml",g=document.documentElement.namespaceURI===f?f:null,h=[],i,j;for(var k=0,l=d.length;k<l;k+=1){e+="[contains(concat(' ', @class, ' '), ' "+d[k]+" ')]"}try{i=document.evaluate(".//"+b+e,c,g,0,null)}catch(m){i=document.evaluate(".//"+b+e,c,null,0,null)}while(j=i.iterateNext()){h.push(j)}return h}}else{getElementsByClassName=function(a,b,c){b=b||"*";c=c||document;var d=a.split(" "),e=[],f=b==="*"&&c.all?c.all:c.getElementsByTagName(b),g,h=[],i;for(var j=0,k=d.length;j<k;j+=1){e.push(new RegExp("(^|\\s)"+d[j]+"(\\s|$)"))}for(var l=0,m=f.length;l<m;l+=1){g=f[l];i=false;for(var n=0,o=e.length;n<o;n+=1){i=e[n].test(g.className);if(!i){break}}if(i){h.push(g)}}return h}}return getElementsByClassName(a,b,c)},
  63. dropdowns = getElementsByClassName( '<?php echo apply_filters( 'dropdown_menus_class', 'dropdown-menu' ); ?>' );
  64. for ( i=0; i<dropdowns.length; i++ )
  65. dropdowns[i].onchange = function(){ if ( this.value != '' ) window.location.href = this.value; }
  66. </script>
  67. <?php
  68. }
  69. /**
  70. * Overrides the walker argument and container argument then calls wp_nav_menu
  71. */
  72. function dropdown_menu( $args ) {
  73. // if non array supplied use as theme location
  74. if ( ! is_array( $args ) )
  75. $args = array( 'menu' => $args );
  76. // enforce these arguments so it actually works
  77. $args[ 'walker' ] = new DropDown_Nav_Menu();
  78. $args[ 'items_wrap' ] = '<select id="%1$s" class="%2$s ' . apply_filters( 'dropdown_menus_class', 'dropdown-menu' ) . '">%3$s</select>';
  79. // custom args for controlling indentation of sub menu items
  80. $args[ 'indent_string' ] = isset( $args[ 'indent_string' ] ) ? $args[ 'indent_string' ] : '&ndash;&nbsp;';
  81. $args[ 'indent_after' ] = isset( $args[ 'indent_after' ] ) ? $args[ 'indent_after' ] : '';
  82. return wp_nav_menu( $args );
  83. }
  84. class DropDown_Nav_Menu extends Walker_Nav_Menu {
  85. // easy way to check it's this walker we're using to mod the output
  86. function is_dropdown() {
  87. return true;
  88. }
  89. /**
  90. * @see Walker::start_lvl()
  91. * @since 3.0.0
  92. *
  93. * @param string $output Passed by reference. Used to append additional content.
  94. * @param int $depth Depth of page. Used for padding.
  95. */
  96. function start_lvl( &$output, $depth ) {
  97. $output .= "</option>";
  98. }
  99. /**
  100. * @see Walker::end_lvl()
  101. * @since 3.0.0
  102. *
  103. * @param string $output Passed by reference. Used to append additional content.
  104. * @param int $depth Depth of page. Used for padding.
  105. */
  106. function end_lvl( &$output, $depth ) {
  107. $output .= "<option>";
  108. }
  109. /**
  110. * @see Walker::start_el()
  111. * @since 3.0.0
  112. *
  113. * @param string $output Passed by reference. Used to append additional content.
  114. * @param object $item Menu item data object.
  115. * @param int $depth Depth of menu item. Used for padding.
  116. * @param int $current_page Menu item ID.
  117. * @param object $args
  118. */
  119. function start_el( &$output, $item, $depth, $args ) {
  120. global $wp_query;
  121. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  122. $class_names = $value = '';
  123. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  124. $classes[] = 'menu-item-' . $item->ID;
  125. $classes[] = 'menu-item-depth-' . $depth;
  126. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_unique( array_filter( $classes ) ), $item, $args ) );
  127. $class_names = ' class="' . esc_attr( $class_names ) . '"';
  128. // select current item
  129. $selected = in_array( 'current-menu-item', $classes ) ? ' selected="selected"' : '';
  130. $output .= $indent . '<option' . $class_names .' value="'. $item->url .'"'. $selected .'>';
  131. // push sub-menu items in as we can't nest optgroups
  132. $indent_string = str_repeat( apply_filters( 'dropdown_menus_indent_string', $args->indent_string, $item, $depth, $args ), ( $depth ) ? $depth : 0 );
  133. $indent_string .= !empty( $indent_string ) ? apply_filters( 'dropdown_menus_indent_after', $args->indent_after, $item, $depth, $args ) : '';
  134. $item_output = $args->before . $indent_string;
  135. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  136. $item_output .= $args->after;
  137. $output .= apply_filters( 'walker_nav_menu_dropdown_start_el', $item_output, $item, $depth, $args );
  138. }
  139. /**
  140. * @see Walker::end_el()
  141. * @since 3.0.0
  142. *
  143. * @param string $output Passed by reference. Used to append additional content.
  144. * @param object $item Page data object. Not used.
  145. * @param int $depth Depth of page. Not Used.
  146. */
  147. function end_el( &$output, $item, $depth ) {
  148. $output .= apply_filters( 'walker_nav_menu_dropdown_end_el', "</option>\n", $item, $depth);
  149. }
  150. }
  151. /**
  152. * Navigation DropDown Menu widget class
  153. */
  154. class DropDown_Menu_Widget extends WP_Widget {
  155. function __construct() {
  156. $widget_ops = array( 'classname' => 'dropdown-menu-widget', 'description' => __( 'Use this widget to add one of your custom menus as a dropdown.', 'tfbasedetails' ) );
  157. parent::__construct( 'dropdown_menu', __('Dropdown Menu', 'tfbasedetails'), $widget_ops );
  158. }
  159. function widget( $args, $instance ) {
  160. // Get menu
  161. $nav_menu = wp_get_nav_menu_object( $instance[ 'nav_menu' ] );
  162. if ( ! $nav_menu )
  163. return;
  164. $instance[ 'title' ] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
  165. echo $args[ 'before_widget' ];
  166. if ( ! empty( $instance[ 'title' ] ) )
  167. echo $args[ 'before_title' ] . $instance[ 'title' ] . $args[ 'after_title' ];
  168. dropdown_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );
  169. echo $args[ 'after_widget' ];
  170. }
  171. function update( $new_instance, $old_instance ) {
  172. $instance[ 'title' ] = strip_tags( stripslashes( $new_instance[ 'title' ] ) );
  173. $instance[ 'nav_menu' ] = (int) $new_instance[ 'nav_menu' ];
  174. return $instance;
  175. }
  176. function form( $instance ) {
  177. $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : '';
  178. $nav_menu = isset( $instance[ 'nav_menu' ] ) ? $instance[ 'nav_menu' ] : '';
  179. // Get menus
  180. $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
  181. // If no menus exists, direct the user to go and create some.
  182. if ( ! $menus ) {
  183. echo '<p>'. sprintf( __( 'No menus have been created yet. <a href="%s">Create some</a>.', 'tfbasedetails' ), admin_url( 'nav-menus.php' ) ) .'</p>';
  184. return;
  185. }
  186. ?>
  187. <p>
  188. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'tfbasedetails' ) ?></label>
  189. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" />
  190. </p>
  191. <p>
  192. <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:', 'tfbasedetails' ); ?></label>
  193. <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name( 'nav_menu' ); ?>">
  194. <?php
  195. foreach ( $menus as $menu ) {
  196. $selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
  197. echo '<option'. $selected .' value="'. $menu->term_id .'">'. $menu->name .'</option>';
  198. }
  199. ?>
  200. </select>
  201. </p>
  202. <?php
  203. }
  204. function init() {
  205. register_widget( __CLASS__ );
  206. }
  207. }
  208. // add widget
  209. add_action( 'widgets_init', array( 'DropDown_Menu_Widget', 'init' ) );
  210. ?>