| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Menus Widget |
| 5 |
* |
| 6 |
* @package Essential_Widgets |
| 7 |
*/ |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Makes a custom Widget for Displaying About |
| 12 |
* |
| 13 |
* Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets |
| 14 |
* |
| 15 |
* @package Essential_Widgets |
| 16 |
*/ |
| 17 |
if ( ! class_exists( 'EW_Menus' ) ) : |
| 18 |
class EW_Menus extends WP_Widget { |
| 19 |
|
| 20 |
|
| 21 |
/** |
| 22 |
* Holds widget settings defaults, populated in constructor. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $defaults; |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
$this->defaults = array( |
| 30 |
'title' => esc_attr__( 'Navigation', 'essential-widgets' ), |
| 31 |
'menu' => '', |
| 32 |
'container' => 'div', |
| 33 |
'container_id' => '', |
| 34 |
'container_class' => '', |
| 35 |
'menu_id' => '', |
| 36 |
'menu_class' => 'nav-menu', |
| 37 |
'depth' => 0, |
| 38 |
'before' => '', |
| 39 |
'after' => '', |
| 40 |
'link_before' => '', |
| 41 |
'link_after' => '', |
| 42 |
'fallback_cb' => 'wp_page_menu', |
| 43 |
); |
| 44 |
|
| 45 |
$widget_ops = array( |
| 46 |
'classname' => 'essential-widgets ew-menus ewmenus widget_nav_menu', |
| 47 |
'description' => esc_html__( 'Displays a list of menus.', 'essential-widgets' ), |
| 48 |
); |
| 49 |
|
| 50 |
$control_ops = array( |
| 51 |
'id_base' => 'ew-menus', |
| 52 |
); |
| 53 |
|
| 54 |
parent::__construct( |
| 55 |
'ew-menus', // Base ID |
| 56 |
esc_html__( 'EW: Menus', 'essential-widgets' ), // Name |
| 57 |
$widget_ops, |
| 58 |
$control_ops |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Displays the widget control options in the Widgets admin screen. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* @access public |
| 67 |
* @param array $instance |
| 68 |
* @param void |
| 69 |
*/ |
| 70 |
public function form( $instance ) { |
| 71 |
|
| 72 |
// Merge the user-selected arguments with the defaults. |
| 73 |
$instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 74 |
|
| 75 |
$container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); ?> |
| 76 |
|
| 77 |
<p> |
| 78 |
<label> |
| 79 |
<?php esc_html_e( 'Title:', 'essential-widgets' ); ?> |
| 80 |
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" /> |
| 81 |
</label> |
| 82 |
</p> |
| 83 |
|
| 84 |
<p> |
| 85 |
<label> |
| 86 |
<?php esc_html_e( 'Menu:', 'essential-widgets' ); ?> |
| 87 |
|
| 88 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu' ) ); ?>"> |
| 89 |
<option value=""></option> |
| 90 |
|
| 91 |
<?php foreach ( wp_get_nav_menus() as $menu ) : ?> |
| 92 |
|
| 93 |
<option value="<?php echo esc_attr( $menu->term_id ); ?>" <?php selected( $instance['menu'], $menu->term_id ); ?>><?php echo esc_html( $menu->name ); ?></option> |
| 94 |
|
| 95 |
<?php endforeach; ?> |
| 96 |
|
| 97 |
</select> |
| 98 |
</label> |
| 99 |
</p> |
| 100 |
<p> |
| 101 |
<label> |
| 102 |
<?php esc_html_e( 'Container:', 'essential-widgets' ); ?> |
| 103 |
|
| 104 |
<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container' ) ); ?>"> |
| 105 |
|
| 106 |
<?php foreach ( $container as $option ) : ?> |
| 107 |
|
| 108 |
<option value="<?php echo esc_attr( $option ); ?>" <?php selected( $instance['container'], $option ); ?>><?php echo esc_html( $option ); ?></option> |
| 109 |
|
| 110 |
<?php endforeach; ?> |
| 111 |
|
| 112 |
</select> |
| 113 |
</label> |
| 114 |
</p> |
| 115 |
|
| 116 |
<p> |
| 117 |
<label> |
| 118 |
<?php esc_html_e( 'Container ID:', 'essential-widgets' ); ?> |
| 119 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container_id' ) ); ?>" value="<?php echo esc_attr( $instance['container_id'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 120 |
</label> |
| 121 |
</p> |
| 122 |
|
| 123 |
<p> |
| 124 |
<label> |
| 125 |
<?php esc_html_e( 'Container Class:', 'essential-widgets' ); ?> |
| 126 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'container_class' ) ); ?>" value="<?php echo esc_attr( $instance['container_class'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 127 |
</label> |
| 128 |
</p> |
| 129 |
|
| 130 |
<p> |
| 131 |
<label> |
| 132 |
<?php esc_html_e( 'Menu ID:', 'essential-widgets' ); ?> |
| 133 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu_id' ) ); ?>" value="<?php echo esc_attr( $instance['menu_id'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 134 |
</label> |
| 135 |
</p> |
| 136 |
|
| 137 |
<p> |
| 138 |
<label> |
| 139 |
<?php esc_html_e( 'Menu Class:', 'essential-widgets' ); ?> |
| 140 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'menu_class' ) ); ?>" value="<?php echo esc_attr( $instance['menu_class'] ); ?>" placeholder="<?php echo esc_html__( 'example' ); ?>" /> |
| 141 |
</label> |
| 142 |
</p> |
| 143 |
|
| 144 |
<p class="button-primary ect-toggle-btn more"> |
| 145 |
<span class="ect-more-text"><?php esc_html_e( 'More Options', 'essential-widgets-pro' ); ?><i class="dashicons dashicons-arrow-down"></i></span> |
| 146 |
<span class="ect-hide-text"><?php esc_html_e( 'Hide Options', 'essential-widgets-pro' ); ?><i class="dashicons dashicons-arrow-up"></i></span> |
| 147 |
|
| 148 |
<div class="advanced-section"> |
| 149 |
|
| 150 |
<p> |
| 151 |
<label> |
| 152 |
<?php esc_html_e( 'Depth:', 'essential-widgets' ); ?> |
| 153 |
<input type="number" class="widefat" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'depth' ) ); ?>" value="<?php echo esc_attr( $instance['depth'] ); ?>" placeholder="0" /> |
| 154 |
</label> |
| 155 |
</p> |
| 156 |
|
| 157 |
<p> |
| 158 |
<label> |
| 159 |
<?php esc_html_e( 'Before:', 'essential-widgets' ); ?> |
| 160 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'before' ) ); ?>" value="<?php echo esc_attr( $instance['before'] ); ?>" /> |
| 161 |
</label> |
| 162 |
</p> |
| 163 |
|
| 164 |
<p> |
| 165 |
<label> |
| 166 |
<?php esc_html_e( 'After:', 'essential-widgets' ); ?> |
| 167 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'after' ) ); ?>" value="<?php echo esc_attr( $instance['after'] ); ?>" /> |
| 168 |
</label> |
| 169 |
</p> |
| 170 |
|
| 171 |
<p> |
| 172 |
<label> |
| 173 |
<?php esc_html_e( 'Link Before:', 'essential-widgets' ); ?> |
| 174 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_before' ) ); ?>" value="<?php echo esc_attr( $instance['link_before'] ); ?>" /> |
| 175 |
</label> |
| 176 |
</p> |
| 177 |
|
| 178 |
<p> |
| 179 |
<label> |
| 180 |
<?php esc_html_e( 'Link After:', 'essential-widgets' ); ?> |
| 181 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_after' ) ); ?>" value="<?php echo esc_attr( $instance['link_after'] ); ?>" /> |
| 182 |
</label> |
| 183 |
</p> |
| 184 |
|
| 185 |
<p> |
| 186 |
<label> |
| 187 |
<?php esc_html_e( 'Fallback Callback Function:', 'essential-widgets' ); ?> |
| 188 |
<input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'fallback_cb' ) ); ?>" value="<?php echo esc_attr( $instance['fallback_cb'] ); ?>" placeholder="wp_page_menu" /> |
| 189 |
</label> |
| 190 |
</p> |
| 191 |
|
| 192 |
</div><!-- .advanced-section --> |
| 193 |
|
| 194 |
<div style="clear:both;"> </div> |
| 195 |
<?php |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* update the particular instant |
| 200 |
* |
| 201 |
* This function should check that $new_instance is set correctly. |
| 202 |
* The newly calculated value of $instance should be returned. |
| 203 |
* If "false" is returned, the instance won't be saved/updated. |
| 204 |
* |
| 205 |
* $new_instance New settings for this instance as input by the user via form() |
| 206 |
* $old_instance Old settings for this instance |
| 207 |
* Settings to save or bool false to cancel saving |
| 208 |
*/ |
| 209 |
public function update( $new_instance, $old_instance ) { |
| 210 |
$instance = $old_instance; |
| 211 |
|
| 212 |
// Sanitize title. |
| 213 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 214 |
|
| 215 |
// Strip tags. |
| 216 |
$instance['menu'] = strip_tags( $new_instance['menu'] ); |
| 217 |
|
| 218 |
// Whitelist options. |
| 219 |
$container = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); |
| 220 |
|
| 221 |
$instance['container'] = in_array( $new_instance['container'], $container ) ? $new_instance['container'] : 'div'; |
| 222 |
|
| 223 |
// Integers. |
| 224 |
$instance['depth'] = absint( $new_instance['depth'] ); |
| 225 |
|
| 226 |
// HTML class. |
| 227 |
$instance['container_id'] = sanitize_html_class( $new_instance['container_id'] ); |
| 228 |
$instance['container_class'] = sanitize_html_class( $new_instance['container_class'] ); |
| 229 |
$instance['menu_id'] = sanitize_html_class( $new_instance['menu_id'] ); |
| 230 |
$instance['menu_class'] = sanitize_html_class( $new_instance['menu_class'] ); |
| 231 |
|
| 232 |
// Text boxes. Make sure user can use 'unfiltered_html'. |
| 233 |
$instance['before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['before'] : wp_kses_post( $new_instance['before'] ); |
| 234 |
$instance['after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['after'] : wp_kses_post( $new_instance['after'] ); |
| 235 |
$instance['link_before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_before'] : wp_kses_post( $new_instance['link_before'] ); |
| 236 |
$instance['link_after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_after'] : wp_kses_post( $new_instance['link_after'] ); |
| 237 |
|
| 238 |
// Function name. |
| 239 |
$instance['fallback_cb'] = empty( $new_instance['fallback_cb'] ) || function_exists( $new_instance['fallback_cb'] ) ? $new_instance['fallback_cb'] : 'wp_page_menu'; |
| 240 |
|
| 241 |
// Return sanitized options. |
| 242 |
return $instance; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Displays the Widget in the front-end. |
| 247 |
* |
| 248 |
* $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 249 |
* $instance The settings for the particular instance of the widget |
| 250 |
*/ |
| 251 |
public function widget( $args, $instance ) { |
| 252 |
// Set the $args for wp_nav_menu() to the $instance array. |
| 253 |
$instance = wp_parse_args( $instance, $this->defaults ); |
| 254 |
|
| 255 |
// Output the sidebar's $before_widget wrapper. |
| 256 |
echo $args['before_widget']; |
| 257 |
|
| 258 |
// If a title was input by the user, display it. |
| 259 |
if ( ! empty( $instance['title'] ) ) { |
| 260 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; |
| 261 |
} |
| 262 |
|
| 263 |
echo $this->shortcode( $instance ); |
| 264 |
|
| 265 |
// Close the sidebar's widget wrapper. |
| 266 |
echo $args['after_widget']; |
| 267 |
} |
| 268 |
|
| 269 |
public function shortcode( $atts ) { |
| 270 |
// Overwrite the $echo argument and set it to false. |
| 271 |
$atts['echo'] = false; |
| 272 |
|
| 273 |
$ew_menu = ''; |
| 274 |
|
| 275 |
// Only show this title in block element and not on widget. |
| 276 |
if ( isset( $atts['is_block'] ) && true === $atts['is_block'] && !empty( $atts['title'] ) ) { |
| 277 |
$ew_menu .= '<h2 class="ew-menu-block-title">' . $atts['title'] . '</h2>'; |
| 278 |
} |
| 279 |
|
| 280 |
// Output the nav menu. |
| 281 |
$ew_menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_nav_menu( $atts ) ); |
| 282 |
|
| 283 |
return $ew_menu; |
| 284 |
} |
| 285 |
} |
| 286 |
endif; |
| 287 |
|
| 288 |
if ( ! function_exists( 'ew_menu_register' ) ) : |
| 289 |
/** |
| 290 |
* Intiate Menu_Widget Class. |
| 291 |
* |
| 292 |
* @since 1.0.0 |
| 293 |
*/ |
| 294 |
function ew_menu_register() { |
| 295 |
register_widget( 'EW_Menus' ); |
| 296 |
} |
| 297 |
add_action( 'widgets_init', 'ew_menu_register' ); |
| 298 |
endif; |
| 299 |
|