widget.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // disable direct access
  4. }
  5. if ( ! class_exists('Mega_Menu_Widget') ) :
  6. /**
  7. * Outputs a registered menu location using wp_nav_menu
  8. */
  9. class Mega_Menu_Widget extends WP_Widget {
  10. /**
  11. * Register widget with WordPress.
  12. */
  13. public function __construct() {
  14. parent::__construct(
  15. 'maxmegamenu', // Base ID
  16. 'Max Mega Menu', // Name
  17. array( 'description' => __( 'Outputs a menu for a selected theme location.', 'megamenu' ) ) // Args
  18. );
  19. }
  20. /**
  21. * Front-end display of widget.
  22. *
  23. * @since 1.7.4
  24. * @see WP_Widget::widget()
  25. * @param array $args Widget arguments.
  26. * @param array $instance Saved values from database.
  27. */
  28. public function widget( $args, $instance ) {
  29. extract( $args );
  30. if ( isset( $instance['location'] ) ) {
  31. $location = $instance['location'];
  32. $title = apply_filters( 'widget_title', $instance['title'] );
  33. echo $before_widget;
  34. if ( ! empty( $title ) ) {
  35. echo $before_title . $title . $after_title;
  36. }
  37. if ( has_nav_menu( $location ) ) {
  38. wp_nav_menu( array( 'theme_location' => $location ) );
  39. }
  40. echo $after_widget;
  41. }
  42. }
  43. /**
  44. * Sanitize widget form values as they are saved.
  45. *
  46. * @since 1.7.4
  47. * @see WP_Widget::update()
  48. * @param array $new_instance Values just sent to be saved.
  49. * @param array $old_instance Previously saved values from database.
  50. * @return array Updated safe values to be saved.
  51. */
  52. public function update( $new_instance, $old_instance ) {
  53. $instance = array();
  54. $instance['location'] = strip_tags( $new_instance['location'] );
  55. $instance['title'] = strip_tags( $new_instance['title'] );
  56. return $instance;
  57. }
  58. /**
  59. * Back-end widget form.
  60. *
  61. * @since 1.7.4
  62. * @see WP_Widget::form()
  63. * @param array $instance Previously saved values from database.
  64. */
  65. public function form( $instance ) {
  66. $selected_location = 0;
  67. $title = "";
  68. $locations = get_registered_nav_menus();
  69. if ( isset( $instance['location'] ) ) {
  70. $selected_location = $instance['location'];
  71. }
  72. if ( isset( $instance['title'] ) ) {
  73. $title = $instance['title'];
  74. }
  75. ?>
  76. <p>
  77. <?php if ( $locations ) { ?>
  78. <p>
  79. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'megamenu' ); ?></label>
  80. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  81. </p>
  82. <label for="<?php echo $this->get_field_id( 'location' ); ?>"><?php _e( 'Menu Location:', 'megamenu' ); ?></label>
  83. <select id="<?php echo $this->get_field_id( 'location' ); ?>" name="<?php echo $this->get_field_name( 'location' ); ?>">
  84. <?php
  85. if ( $selected_location === 0 ) {
  86. echo "<option selected='true' disabled='disabled'>" . __("Select location", "megamenu") ."</option>";
  87. }
  88. ?>
  89. <?php
  90. $enabled_locations = array();
  91. $disabled_locations = array();
  92. foreach ( $locations as $location => $description ) {
  93. if ( max_mega_menu_is_enabled( $location ) ) {
  94. $enabled_locations[$location] = $description;
  95. } else {
  96. $disabled_locations[$location] = $description;
  97. }
  98. }
  99. if ( count( $enabled_locations ) ) {
  100. echo "<optgroup label='&#10003; " . __("Active locations", "megamenu") ."'>";
  101. foreach ( $enabled_locations as $location => $description ) {
  102. echo "<option value='{$location}'" . selected( $location, $selected_location ) . ">{$description}</option>";
  103. }
  104. echo "</optgroup>";
  105. }
  106. if ( count( $disabled_locations ) ) {
  107. echo "<optgroup label='&#x2718; " . __("Inactive locations", "megamenu") ."'>";
  108. foreach ( $disabled_locations as $location => $description ) {
  109. echo "<option value='{$location}'" . selected( $location, $selected_location ) . ">{$description}</option>";
  110. }
  111. echo "</optgroup>";
  112. }
  113. ?>
  114. </select>
  115. <?php } else {
  116. _e( 'No menu locations found', 'megamenu' );
  117. } ?>
  118. </p>
  119. <?php
  120. }
  121. }
  122. endif;