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

/of-taxonomy-walker.php

https://bitbucket.org/purenetgit/search-filter
PHP | 319 lines | 220 code | 59 blank | 40 comment | 76 complexity | 784577b46c20de7dda78bdecc6290386 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. class Taxonomy_Walker extends Walker_Category {
  3. private $type = '';
  4. private $defaults = array();
  5. private $multidepth = 0; //manually calculate depth on multiselects
  6. private $multilastid = 0; //manually calculate depth on multiselects
  7. private $multilastdepthchange = 0; //manually calculate depth on multiselects
  8. function __construct($type = 'checkbox', $defaults = array()) {
  9. // fetch the list of term ids for the given post
  10. //$this->term_ids = wp_get_post_terms( $post_id, $taxonomy, 'fields=ids' );
  11. $this->type = $type;
  12. $this->defaults = $defaults;
  13. }
  14. function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
  15. /*$display = false;
  16. $id = $element->term_id;
  17. $display = true;
  18. if ( isset( $children_elements[ $id ] ) ) {
  19. // the current term has children
  20. foreach ( $children_elements[ $id ] as $child ) {
  21. if ( in_array( $child->term_id, $this->term_ids ) ) {
  22. // one of the term's children is in the list
  23. $display = true;
  24. // can stop searching now
  25. break;
  26. }
  27. }
  28. }
  29. if ( $display )*/
  30. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  31. }
  32. function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 )
  33. {
  34. if($this->type=="list")
  35. {
  36. extract($args);
  37. $cat_name = esc_attr( $sf_name );
  38. $cat_name = apply_filters( 'list_cats', $cat_name, $category );
  39. $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
  40. if ( $use_desc_for_title == 0 || empty($category->description) )
  41. $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
  42. else
  43. $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
  44. $link .= '>';
  45. $link .= $cat_name . '</a>';
  46. if ( !empty($feed_image) || !empty($feed) ) {
  47. $link .= ' ';
  48. if ( empty($feed_image) )
  49. $link .= '(';
  50. $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
  51. if ( empty($feed) ) {
  52. $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
  53. } else {
  54. $title = ' title="' . $feed . '"';
  55. $alt = ' alt="' . $feed . '"';
  56. $name = $feed;
  57. $link .= $title;
  58. }
  59. $link .= '>';
  60. if ( empty($feed_image) )
  61. $link .= $name;
  62. else
  63. $link .= "<img src='$feed_image'$alt$title" . ' />';
  64. $link .= '</a>';
  65. if ( empty($feed_image) )
  66. $link .= ')';
  67. }
  68. if ( !empty($show_count) )
  69. $link .= ' (' . intval($category->count) . ')';
  70. if ( 'list' == $args['style'] ) {
  71. $output .= "\t<li";
  72. $class = 'cat-item cat-item-' . $category->term_id;
  73. if ( !empty($current_category) ) {
  74. $_current_category = get_term( $current_category, $category->taxonomy );
  75. if ( $category->term_id == $current_category )
  76. $class .= ' current-cat';
  77. elseif ( $category->term_id == $_current_category->parent )
  78. $class .= ' current-cat-parent';
  79. }
  80. $output .= ' class="' . $class . '"';
  81. $output .= ">$link\n";
  82. } else {
  83. $output .= "\t$link<br />\n";
  84. }
  85. }
  86. else if(($this->type=="checkbox")||($this->type=="radio"))
  87. {
  88. extract($args);
  89. $cat_name = esc_attr( $category->name );
  90. $cat_id = esc_attr( $category->term_id );
  91. $cat_name = apply_filters( 'list_cats', $cat_name, $category );
  92. //check a default has been set
  93. $checked = "";
  94. if($defaults)
  95. {
  96. $noselected = count($this->defaults);
  97. if(($noselected>0)&&(is_array($defaults)))
  98. {
  99. foreach($defaults as $defaultid)
  100. {
  101. if($defaultid==$cat_id)
  102. {
  103. $checked = ' checked="checked"';
  104. }
  105. }
  106. }
  107. }
  108. $link = "<label><input type='".$this->type."' name='".$sf_name."[]' value='".$cat_id."'".$checked." /> ".$cat_name;
  109. if ( !empty($show_count) )
  110. $link .= ' (' . intval($category->count) . ')';
  111. $link .= "</label>";
  112. if ( 'list' == $args['style'] ) {
  113. $output .= "\t<li";
  114. $class = 'cat-item cat-item-' . $category->term_id;
  115. if ( !empty($current_category) ) {
  116. $_current_category = get_term( $current_category, $category->taxonomy );
  117. if ( $category->term_id == $current_category )
  118. $class .= ' current-cat';
  119. elseif ( $category->term_id == $_current_category->parent )
  120. $class .= ' current-cat-parent';
  121. }
  122. $output .= ' class="' . $class . '"';
  123. $output .= ">$link\n";
  124. } else {
  125. $output .= "\t$link<br />\n";
  126. }
  127. }
  128. else if($this->type=="multiselect")
  129. {
  130. extract($args);
  131. $cat_name = esc_attr( $category->name );
  132. $cat_id = esc_attr( $category->term_id );
  133. $cat_name = apply_filters( 'list_cats', $cat_name, $category );
  134. //check a default has been set
  135. $checked = "";
  136. if($defaults)
  137. {
  138. $noselected = count($this->defaults);
  139. if(($noselected>0)&&(is_array($defaults)))
  140. {
  141. foreach($defaults as $defaultid)
  142. {
  143. if($defaultid==$cat_id)
  144. {
  145. $checked = ' selected="selected"';
  146. }
  147. }
  148. }
  149. }
  150. /* Custom depth calculations! :/ */
  151. if($category->parent == 0)
  152. {//then this has no parent so reset depth
  153. $this->multidepth = 0;
  154. }
  155. else if($category->parent == $this->multilastid)
  156. {
  157. $this->multidepth++;
  158. $this->multilastdepthchange = $this->multilastid;
  159. }
  160. else if($category->parent == $this->multilastdepthchange)
  161. {//then this is also a child with the same parent so don't change depth
  162. }
  163. else
  164. {//then this has a different parent so must be lower depth
  165. if($this->multidepth>0)
  166. {
  167. $this->multidepth--;
  168. }
  169. }
  170. $pad = str_repeat('&nbsp;', $this->multidepth * 3);
  171. $link = "<option class=\"level-".$this->multidepth."\" value='".$cat_id."'$checked />".$pad.$cat_name;
  172. if ( !empty($show_count) )
  173. $link .= '&nbsp;&nbsp;(' . intval($category->count) . ')';
  174. $link .= "</option>";
  175. $output .= "\t$link\n";
  176. $this->multilastid = $cat_id;
  177. /*
  178. $pad = str_repeat('&nbsp;', $depth * 3);
  179. $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
  180. $cat_name = apply_filters('list_cats', $category->sf_name, $category);
  181. if ( $category->term_id == $args['selected'] )
  182. $output .= ' selected="selected"';
  183. $output .= '>';
  184. $output .= $pad.$cat_name;
  185. if ( $args['show_count'] )
  186. $output .= '&nbsp;&nbsp;('. $category->count .')';
  187. $output .= "</option>\n";*/
  188. }
  189. }
  190. function end_el( &$output, $page, $depth = 0, $args = array() )
  191. {
  192. if($this->type=="list")
  193. {
  194. if ( 'list' != $args['style'] )
  195. return;
  196. $output .= "</li>\n";
  197. }
  198. else if(($this->type=="checkbox")||($this->type=="radio"))
  199. {
  200. if ( 'list' != $args['style'] )
  201. return;
  202. $output .= "</li>\n";
  203. }
  204. else if($this->type=="multiselect")
  205. {
  206. if ( 'list' != $args['style'] )
  207. return;
  208. $output .= "</option>\n";
  209. }
  210. }
  211. function start_lvl( &$output, $depth = 0, $args = array() )
  212. {
  213. if($this->type=="list")
  214. {
  215. if ( 'list' != $args['style'] )
  216. return;
  217. $indent = str_repeat("\t", $depth);
  218. $output .= "$indent<ul class='children'>\n";
  219. }
  220. else if(($this->type=="checkbox")||($this->type=="radio"))
  221. {
  222. if ( 'list' != $args['style'] )
  223. return;
  224. $indent = str_repeat("\t", $depth);
  225. $output .= "$indent<ul class='children'>\n";
  226. }
  227. else if($this->type=="multiselect")
  228. {
  229. /*if ( 'list' != $args['style'] )
  230. return;
  231. $indent = str_repeat("\t", $depth);
  232. $output .= "$indent<ul class='children'>\n";*/
  233. }
  234. }
  235. function end_lvl( &$output, $depth = 0, $args = array() ) {
  236. if($this->type=="list")
  237. {
  238. if ( 'list' != $args['style'] )
  239. return;
  240. $indent = str_repeat("\t", $depth);
  241. $output .= "$indent</ul>\n";
  242. }
  243. else if(($this->type=="checkbox")||($this->type=="radio"))
  244. {
  245. if ( 'list' != $args['style'] )
  246. return;
  247. $indent = str_repeat("\t", $depth);
  248. $output .= "$indent</ul>\n";
  249. }
  250. else if($this->type=="multiselect")
  251. {
  252. }
  253. }
  254. }
  255. ?>