PageRenderTime 72ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/modernize/include/plugin/dropdown-menus.php

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