/kc-essentials-inc/widgets/menu.php

https://github.com/kucrut/kc-essentials · PHP · 266 lines · 232 code · 27 blank · 7 comment · 15 complexity · f91b1bae4d61e348297a07fc2ffda6ec MD5 · raw file

  1. <?php
  2. /**
  3. * Configurable menu widget module
  4. * @package KC_Essentials
  5. */
  6. Class kc_widget_menu extends WP_Widget {
  7. function __construct() {
  8. $widget_ops = array( 'classname' => 'kcw_menu', 'description' => __('Configurable custom menu widget', 'kc-essentials') );
  9. $control_ops = array( 'width' => 200, 'height' => 350 );
  10. parent::__construct( 'kcw_menu', 'KC Custom Menu', $widget_ops, $control_ops );
  11. }
  12. function widget( $args, $instance ) {
  13. # Get menu
  14. $_menu = ! empty( $instance['menu'] ) ? wp_get_nav_menu_object( $instance['menu'] ) : false;
  15. if ( !$_menu )
  16. return;
  17. if ( !empty($instance['walker']) ) {
  18. if ( class_exists($instance['walker']) )
  19. $instance['walker'] = new $instance['walker'];
  20. else
  21. return;
  22. }
  23. $instance['echo'] = false;
  24. $instance['fallback_cb'] = '';
  25. if ( isset($instance['container']) && !$instance['container'] )
  26. $instance['container'] = false;
  27. if ( isset($instance['mode']) && $instance['mode'] == 'select' ) {
  28. if ( !function_exists('kc_dropdown_menu') )
  29. require_once kcEssentials::get_data('paths', 'inc') . '/menu_dropdown.php';
  30. $menu = kc_dropdown_menu( $_menu->term_id, $instance );
  31. }
  32. else {
  33. $menu = wp_nav_menu( $instance );
  34. }
  35. if ( !$menu )
  36. return;
  37. $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  38. if ( $instance['action_id'] ) {
  39. $args['before_widget'] = apply_filters( "kc_widget-{$instance['action_id']}", $args['before_widget'], 'before_widget', 'widget_menu' );
  40. $args['after_widget'] = apply_filters( "kc_widget-{$instance['action_id']}", $args['after_widget'], 'after_widget', 'widget_menu' );
  41. }
  42. unset( $instance['action_id'] );
  43. echo $args['before_widget'];
  44. if ( !empty($instance['title']) )
  45. echo $args['before_title'] . $instance['title'] . $args['after_title'];
  46. unset( $instance['title'] );
  47. echo $menu;
  48. echo $args['after_widget'];
  49. }
  50. function update( $new, $old ) {
  51. $new['title'] = strip_tags( stripslashes($new['title']) );
  52. $new['menu'] = (int) $new['menu'];
  53. $new['depth'] = absint($new['depth']);
  54. $new['container_class'] = kc_essentials_sanitize_html_classes( $new['container_class'] );
  55. $new['container_id'] = sanitize_html_class( $new['container_id'] );
  56. $new['menu_class'] = kc_essentials_sanitize_html_classes( $new['menu_class'] );
  57. $new['menu_id'] = sanitize_html_class( $new['menu_id'] );
  58. $new['walker'] = sanitize_html_class( $new['walker'] );
  59. $new['action_id'] = sanitize_html_class( $new['action_id'] );
  60. return $new;
  61. }
  62. function form( $instance ) {
  63. # Get menus
  64. $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
  65. # If no menus exists, direct the user to go and create some.
  66. if ( !$menus ) {
  67. echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.', 'kc-essentials'), admin_url('nav-menus.php') ) .'</p>';
  68. return;
  69. }
  70. $nav_menus = array();
  71. foreach ( $menus as $m )
  72. $nav_menus[] = array( 'value' => $m->term_id, 'label' => $m->name );
  73. $options_main = array(
  74. 'title' => array(
  75. 'label' => __('Title:'),
  76. 'type' => 'text'
  77. ),
  78. 'menu' => array(
  79. 'label' => __('Menu', 'kc-essentials'),
  80. 'type' => 'select',
  81. 'options' => $nav_menus,
  82. 'none' => false
  83. ),
  84. 'depth' => array(
  85. 'label' => __('Depth', 'kc-essentials'),
  86. 'type' => 'text',
  87. 'current' => isset( $instance['depth'] ) ? $instance['depth'] : '0'
  88. ),
  89. 'menu_class' => array(
  90. 'label' => __('Menu class', 'kc-essentials'),
  91. 'type' => 'text',
  92. 'current' => isset( $instance['menu_class'] ) ? $instance['menu_class'] : 'menu'
  93. ),
  94. 'menu_id' => array(
  95. 'label' => __('Menu ID', 'kc-essentials'),
  96. 'type' => 'text'
  97. ),
  98. 'mode' => array(
  99. 'label' => __('Mode', 'kc-essentials'),
  100. 'type' => 'select',
  101. 'options' => array(
  102. 'list' => __('List', 'kc-essentials'),
  103. 'select' => __('Dropdown select', 'kc-essentials')
  104. ),
  105. 'none' => false,
  106. 'current' => isset( $instance['mode'] ) ? $instance['mode'] : 'list',
  107. 'attr' => array(
  108. 'class' => 'hasdep',
  109. 'data-child' => '.' .$this->get_field_id('mode') .'-child'
  110. )
  111. )
  112. );
  113. $options_list = array(
  114. 'container' => array(
  115. 'label' => __('Container tag', 'kc-essentials'),
  116. 'type' => 'select',
  117. 'options' => array(
  118. 'div' => '&lt;div /&gt;',
  119. 'nav' => '&lt;nav /&gt;',
  120. '0' => __('None', 'kc-essentials')
  121. ),
  122. 'none' => false,
  123. 'attr' => array(
  124. 'class' => 'hasdep',
  125. 'data-child' => '.' .$this->get_field_id('container'). '-child'
  126. )
  127. ),
  128. 'container_class' => array(
  129. 'label' => __('Container class', 'kc-essentials'),
  130. 'type' => 'text',
  131. 'wrap_attr' => array(
  132. 'class' => $this->get_field_id('container') . '-child',
  133. 'data-dep' => json_encode( array('div', 'nav') )
  134. )
  135. ),
  136. 'container_id' => array(
  137. 'label' => __('Container ID', 'kc-essentials'),
  138. 'type' => 'text',
  139. 'wrap_attr' => array(
  140. 'class' => $this->get_field_id('container') . '-child',
  141. 'data-dep' => json_encode( array('div', 'nav') )
  142. )
  143. ),
  144. 'before' => array(
  145. 'label' => __('Before link text', 'kc-essentials'),
  146. 'type' => 'text'
  147. ),
  148. 'after' => array(
  149. 'label' => __('After link text', 'kc-essentials'),
  150. 'type' => 'text'
  151. ),
  152. 'link_before' => array(
  153. 'label' => __('Before link', 'kc-essentials'),
  154. 'type' => 'text'
  155. ),
  156. 'link_after' => array(
  157. 'label' => __('After link', 'kc-essentials'),
  158. 'type' => 'text'
  159. ),
  160. 'items_wrap' => array(
  161. 'label' => __('Items wrap', 'kc-essentials'),
  162. 'type' => 'text',
  163. 'current' => isset( $instance['items_wrap'] ) ? $instance['items_wrap'] : '<ul id="%1$s" class="%2$s">%3$s</ul>'
  164. ),
  165. 'walker' => array(
  166. 'label' => __('Walker function', 'kc-essentials'),
  167. 'type' => 'text'
  168. ),
  169. 'action_id' => array(
  170. 'label' => __('Action ID', 'kc-essentials'),
  171. 'type' => 'text'
  172. )
  173. );
  174. $options_select = array(
  175. 'submit_text' => array(
  176. 'label' => __('Submit text', 'kc-essentials'),
  177. 'type' => 'text',
  178. 'current' => ( isset( $instance['submit_text'] ) && $instance['submit_text'] ) ? $instance['submit_text'] : __('Go', 'kc-essentials')
  179. ),
  180. 'select_text' => array(
  181. 'label' => __('Select text', 'kc-essentials'),
  182. 'type' => 'text',
  183. 'current' => isset( $instance['select_text'] ) ? $instance['select_text'] : '&mdash;&nbsp;'.__('Navigate', 'kc-essentials').'&nbsp;&mdash;'
  184. ),
  185. 'pad' => array(
  186. 'label' => __('Pad', 'kc-essentials'),
  187. 'type' => 'text',
  188. 'current' => isset( $instance['pad'] ) ? $instance['pad'] : '&mdash;'
  189. ),
  190. 'js' => array(
  191. 'label' => __('Enable Javascript?', 'kc-essentials'),
  192. 'type' => 'select',
  193. 'options' => kcSettings_options::$yesno,
  194. 'none' => false,
  195. 'current' => isset( $instance['js'] ) ? $instance['js'] : '1'
  196. )
  197. );
  198. echo kcEssentials_widgets::form( $this, $options_main, $instance );
  199. echo kcEssentials_widgets::form( $this, $options_list, $instance, array(
  200. 'class' => $this->get_field_id('mode') .'-child kcw-control-normal',
  201. 'data-dep' => 'list'
  202. ) );
  203. echo kcEssentials_widgets::form( $this, $options_select, $instance, array(
  204. 'class' => $this->get_field_id('mode') .'-child kcw-control-normal',
  205. 'data-dep' => 'select'
  206. ) );
  207. }
  208. public static function kcml_fields( $widgets ) {
  209. $widgets['widget_kcw_menu'] = array(
  210. array(
  211. 'id' => 'title',
  212. 'type' => 'text',
  213. 'label' => __('Title')
  214. ),
  215. array(
  216. 'id' => 'before',
  217. 'type' => 'text',
  218. 'label' => __('Before link text', 'kc-essentials')
  219. ),
  220. array(
  221. 'id' => 'after',
  222. 'type' => 'text',
  223. 'label' => __('After link text', 'kc-essentials')
  224. ),
  225. array(
  226. 'id' => 'link_before',
  227. 'type' => 'text',
  228. 'label' => __('Before link', 'kc-essentials')
  229. ),
  230. array(
  231. 'id' => 'link_after',
  232. 'type' => 'text',
  233. 'label' => __('After link', 'kc-essentials')
  234. )
  235. );
  236. return $widgets;
  237. }
  238. }
  239. add_filter( 'kcml_widget_fields', array('kc_widget_menu', 'kcml_fields') );