PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/req-foundation.php

https://github.com/tomtom10/required-foundation
PHP | 286 lines | 157 code | 51 blank | 78 comment | 38 complexity | 8fbf1e899d37f432f130ea53eb08c77c MD5 | raw file
  1. <?php
  2. /**
  3. * Make the ZURB foundation work with WordPress
  4. *
  5. * @package required+ Foundation
  6. * @since required+ Foundation 0.1.0
  7. */
  8. /**
  9. * Display the list for paginated links according to ZURB Foundation
  10. * Code from: http://themefortress.com/reverie/
  11. *
  12. * @since required+ Foundation 0.3.1
  13. *
  14. * @return string
  15. */
  16. function required_pagination() {
  17. global $wp_query;
  18. $big = 999999999; // This needs to be an unlikely integer
  19. // For more options and info view the docs for paginate_links()
  20. // http://codex.wordpress.org/Function_Reference/paginate_links
  21. $paginate_links = paginate_links( array(
  22. 'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
  23. 'current' => max( 1, get_query_var('paged') ),
  24. 'total' => $wp_query->max_num_pages,
  25. 'mid_size' => 5,
  26. 'prev_next' => True,
  27. 'prev_text' => __( '&laquo;', 'requiredfoundation' ),
  28. 'next_text' => __( '&raquo;', 'requiredfoundation' ),
  29. 'type' => 'list'
  30. ) );
  31. // Display the pagination if more than one page is found
  32. if ( $paginate_links ) {
  33. echo '<div class="required-pagination">';
  34. echo $paginate_links;
  35. echo '</div><!--// end .pagination -->';
  36. }
  37. }
  38. /**
  39. * A fallback when no navigation is selected by default, otherwise it throws some nasty errors in your face.
  40. *
  41. * @since required+ Foundation 0.2.0
  42. *
  43. * @return string
  44. */
  45. function required_menu_fallback() {
  46. echo '<div class="alert-box secondary">';
  47. // Translators 1: Link to Menus, 2: Link to Customize
  48. printf( __( 'Please assign a menu to the primary menu location under %1$s or %2$s the design.', 'requiredfoundation' ),
  49. sprintf( __( '<a href="%s">Menus</a>', 'requiredfoundation' ),
  50. get_admin_url( get_current_blog_id(), 'nav-menus.php' )
  51. ),
  52. sprintf( __( '<a href="%s">Customize</a>', 'requiredfoundation' ),
  53. get_admin_url( get_current_blog_id(), 'customize.php' )
  54. )
  55. );
  56. echo '</div>';
  57. }
  58. /**
  59. * Use the active class of the ZURB Foundation for the current menu item.
  60. * From: https://github.com/milohuang/reverie/blob/master/functions.php
  61. *
  62. * @since required+ Foundation 0.3.0
  63. *
  64. * @param array $classes
  65. * @param obj $item
  66. * @return array
  67. */
  68. function required_active_nav_class( $classes, $item ) {
  69. if ( $item->current == 1 || $item->current_item_ancestor == true ) {
  70. $classes[] = 'active';
  71. }
  72. return $classes;
  73. }
  74. add_filter( 'nav_menu_css_class', 'required_active_nav_class', 10, 2 );
  75. /**
  76. * Use the active class of ZURB Foundation on wp_list_pages output.
  77. * @param string $input
  78. * @return string
  79. *
  80. * @since required+ Foundation 0.5.0
  81. */
  82. function required_active_list_pages_class( $input ) {
  83. $pattern = '/current_page_item/';
  84. $replace = 'current_page_item active';
  85. $output = preg_replace( $pattern, $replace, $input );
  86. return $output;
  87. }
  88. add_filter( 'wp_list_pages', 'required_active_list_pages_class', 10, 2 );
  89. /**
  90. * class required_walker
  91. *
  92. * Custom output to enable the the ZURB Navigation style.
  93. * Courtesy of Kriesi.at. http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output
  94. *
  95. * @since required+ Foundation 0.1.0
  96. *
  97. * @return string the code of the full navigation menu
  98. */
  99. class REQ_Foundation_Walker extends Walker_Nav_Menu {
  100. /**
  101. * Specify the item type to allow different walkers
  102. * @var array
  103. */
  104. var $nav_bar = '';
  105. function __construct( $nav_args = '' ) {
  106. $defaults = array(
  107. 'item_type' => 'li',
  108. 'in_top_bar' => false,
  109. );
  110. $this->nav_bar = apply_filters( 'req_nav_args', wp_parse_args( $nav_args, $defaults ) );
  111. }
  112. function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
  113. $id_field = $this->db_fields['id'];
  114. if ( is_object( $args[0] ) ) {
  115. $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
  116. }
  117. return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  118. }
  119. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  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. // Check for flyout
  126. $flyout_toggle = '';
  127. if ( $args->has_children && $this->nav_bar['item_type'] == 'li' ) {
  128. if ( $depth == 0 && $this->nav_bar['in_top_bar'] == false ) {
  129. $classes[] = 'has-flyout';
  130. $flyout_toggle = '<a href="#" class="flyout-toggle"><span></span></a>';
  131. } else if ( $this->nav_bar['in_top_bar'] == true ) {
  132. $classes[] = 'has-dropdown';
  133. $flyout_toggle = '';
  134. }
  135. }
  136. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  137. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  138. if ( $depth > 0 ) {
  139. $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  140. } else {
  141. $output .= $indent . ( $this->nav_bar['in_top_bar'] == true ? '<li class="divider"></li>' : '' ) . '<' . $this->nav_bar['item_type'] . ' id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  142. }
  143. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  144. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  145. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  146. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  147. $item_output = $args->before;
  148. $item_output .= '<a '. $attributes .'>';
  149. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  150. $item_output .= '</a>';
  151. $item_output .= $flyout_toggle; // Add possible flyout toggle
  152. $item_output .= $args->after;
  153. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  154. }
  155. function end_el( &$output, $item, $depth = 0, $args = array() ) {
  156. if ( $depth > 0 ) {
  157. $output .= "</li>\n";
  158. } else {
  159. $output .= "</" . $this->nav_bar['item_type'] . ">\n";
  160. }
  161. }
  162. function start_lvl( &$output, $depth = 0, $args = array() ) {
  163. if ( $depth == 0 && $this->nav_bar['item_type'] == 'li' ) {
  164. $indent = str_repeat("\t", 1);
  165. $output .= $this->nav_bar['in_top_bar'] == true ? "\n$indent<ul class=\"dropdown\">\n" : "\n$indent<ul class=\"flyout\">\n";
  166. } else {
  167. $indent = str_repeat("\t", $depth);
  168. $output .= $this->nav_bar['in_top_bar'] == true ? "\n$indent<ul class=\"dropdown\">\n" : "\n$indent<ul class=\"level-$depth\">\n";
  169. }
  170. }
  171. }
  172. // Yes you can overwrite the whole function
  173. if ( ! function_exists( 'required_side_nav' ) ) :
  174. /**
  175. * Displays a simple subnav with child pages of the current
  176. * or page above. See usage in page-templates/left-nav-page.php
  177. *
  178. * @param integer $depth Levels of child pages to show, default is 1
  179. * @param string $before List to start the nav, you could use something like <ul class="nav-bar vertical">
  180. * @param string $after Closing </ul>
  181. * @param bool $show_home Show a home link? Default: false
  182. * @param string $item_type Usually an li, if not we use dd for you buddy!
  183. * @return string Echo out the whole navigation
  184. *
  185. * @since required+ Foundation 0.5.0
  186. */
  187. function required_side_nav( $nav_args = '' ) {
  188. global $post;
  189. $defaults = array(
  190. 'show_home' => false,
  191. 'depth' => 1,
  192. 'before' => '<ul class="side-nav">',
  193. 'after' => '</ul>',
  194. 'item_type' => 'li',
  195. );
  196. $nav_args = apply_filters( 'req_side_nav_args', wp_parse_args( $nav_args, $defaults ) );
  197. $args = array(
  198. 'title_li' => '',
  199. 'depth' => $nav_args['depth'],
  200. 'sort_column' => 'menu_order',
  201. 'echo' => 0,
  202. );
  203. // Make sure the dl only shows 1 level
  204. if ( $nav_args['item_type'] != 'li' ) {
  205. $args['depth'] = 0;
  206. }
  207. if ( $post->post_parent ) {
  208. // So we have a post parent
  209. $args['child_of'] = $post->post_parent;
  210. } else {
  211. // So we don't have a post parent, so you are!
  212. $args['child_of'] = $post->ID;
  213. }
  214. // Filter the $args if you want to do something different!
  215. $children = wp_list_pages( $args );
  216. // Point as back home or not?
  217. if ( $nav_args['show_home'] == true ) {
  218. $nav_args['before'] .= '<li><a href="' . get_home_url() . '">' . __( '&larr; Home', 'requiredfoundation' ) . '</a></li><li class="divider"></li>';
  219. }
  220. // Do we have children?
  221. if ( $children ) {
  222. $output = $nav_args['before'] . $children . $nav_args['after'];
  223. // Replace the output if we are on a definition list
  224. if ( $nav_args['item_type'] != 'li' ) {
  225. $pattern_start = '/<li/';
  226. $pattern_end = '/<\/li>/';
  227. $replace_start = '<dd';
  228. $replace_end = '</dd>';
  229. $output = preg_replace($pattern_start, $replace_start, $output);
  230. $output = preg_replace($pattern_end, $replace_end, $output);
  231. }
  232. echo $output;
  233. }
  234. }
  235. endif;